Task.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20  /*!
21  \file terralib/process/Task.h
22 
23  \brief Abstraction that represents a Task that can be executed
24  */
25 
26 #ifndef __TERRALIB_PROCESS_INTERNAL_TASK_H
27 #define __TERRALIB_PROCESS_INTERNAL_TASK_H
28 
29  // TerraLib
30 #include "Config.h"
31 
32 #include "../common/AbstractFactory.h"
33 
34 #include <map>
35 #include <memory>
36 #include <string>
37 #include <vector>
38 
39 namespace te
40 {
41  namespace process
42  {
43  class Connector;
44  class ParameterValue;
45  class TaskCapabilities;
46  class TaskParameters;
47 
48  /*!
49  \class Task
50 
51  \brief Abstraction that represents a Task that can be executed
52  */
54  {
55  public:
56 
57  enum class State
58  {
59  NOT_INITIALIZED, MISSING_PARAMETERS, READY_TO_RUN, RUNNING, FINISHED, FINISHED_WITH_ERRORS
60  };
61 
62  /*!
63  \brief It initializes the Task
64  */
65  Task(const std::string& taskType);
66 
67  /*! \brief Virtual destructor. */
68  virtual ~Task();
69 
70  //!< Gets the id of the task
71  const std::string& getId() const;
72 
73  //< Sets the id of the task. Generally used only of a deserialization
74  void setId(const std::string& id);
75 
76  //!< Gets the type of the task
77  const std::string& getType() const;
78 
79  //!< Gets the capabilities of the task
81 
82  //!< Gets the current execution state of the task
83  State getState() const;
84 
85  //!< Gets the index-th parameter
86  const te::process::ParameterValue* getParameter(std::size_t index) const;
87 
88  //!< Sets the index-th parameter. This object will optionally take the ownership of the value pointer
89  void setParameter(std::size_t index, te::process::ParameterValue* value, bool takeOwnership);
90 
91  //!< Gets the task paramters
93 
94  //!< Sets the task paramters. This class will take the ownership of the given pointer
95  void setTaskParameters(TaskParameters* taskParameters);
96 
97  //!< Adds a parameter connector to this class
98  void addConnector(Connector* connector);
99 
100  //!< Removes a parameter connector to this class
101  void removeConnector(Connector* connector);
102 
103  //!< Gets all the input connectors
104  std::vector<Connector*> getInputConnectors() const;
105 
106  //!< Gets all the output connectors
107  std::vector<Connector*> getOutputConnectors() const;
108 
109  //!< Checks if the task is valid, that is, if all the required input parameters have values OR connectors associated to them
110  bool isValid(std::string& errorMessage) const;
111 
112  //!< Runs the task
113  void run();
114 
115  protected:
116 
117  //!< Initializes the task. This function must be called by any class that extends this abstraction. The abstraction will take the ownership of the pointer
119 
120  //!< Checks if all the input connectors are ready. If at least one connector is not ready, it returns FALSE.
122 
123  //!< Pushes the parameters into the output connectors
125 
126  //!< This function is called whenever the parameters are changed and is responsible for checking the parameters are valid. If they are, change the task state to Ready to Run
128 
129  //!< Runs the task
130  virtual void runImpl() = 0;
131 
132  protected:
133  std::string m_taskType; //!< The type of the task. Used by the factory
134  std::string m_id; //!< The ID of the task
136  std::unique_ptr<TaskCapabilities> m_capabilities;
137  std::unique_ptr<TaskParameters> m_parameters;
138  std::vector<Connector*> m_connectors;
139  };
140 
141  //Typedef
142  class TEPROCESSEXPORT TaskPtr : public std::unique_ptr<Task>
143  {
144  public:
145  explicit TaskPtr(te::process::Task* task = nullptr);
147  };
148 
149  class TEPROCESSEXPORT TaskSharedPtr : public std::shared_ptr<Task>
150  {
151  public:
152  explicit TaskSharedPtr(te::process::Task* task = nullptr);
154  };
155 
156  /*!
157  \class TaskFactory
158 
159  \brief Abstract factory used to create Tasks.
160  */
161  class TEPROCESSEXPORT TaskFactory : public te::common::AbstractFactory < te::process::Task, std::string >
162  {
163  public:
164 
165  /*!
166  \brief Constructor.
167 
168  \param factoryKey The key that identifies the factory.
169  */
170  TaskFactory(const std::string& taskType, const std::string& category);
171 
172  //!< Destructor
173  virtual ~TaskFactory();
174 
175  //!< Gets all registered categories
176  static std::vector<std::string> getCategories();
177 
178  //!< Gets all tasks from the give category
179  static std::vector<std::string> getTasksFromCategory(const std::string& category);
180 
181  protected:
182 
183  static std::map<std::string, std::vector<std::string> > m_mapCategories;
184  };
185 
186  //!< Wrapper function used to create taks. Used to avoid C++ to create multiple instances of the factory singleton on Windows
187  TEPROCESSEXPORT Task* CreateTask(const std::string& taskType);
188 
189  } // end namespace process
190 } // end namespace te
191 
192 #endif // __TERRALIB_PROCESS_INTERNAL_TASK_H
193 
te::process::TaskPtr::TaskPtr
TaskPtr(te::process::Task *task=nullptr)
te::process::Task::getParameter
const te::process::ParameterValue * getParameter(std::size_t index) const
Sets the index-th parameter. This object will optionally take the ownership of the value pointer.
te::process::Task::m_connectors
std::vector< Connector * > m_connectors
Definition: Task.h:138
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::common::AbstractFactory
This class defines the interface of abstract factories without initializing parameters.
Definition: AbstractFactory.h:70
te::process::Task::State
State
Definition: Task.h:58
te::process::Task::run
void run()
te::process::Task::getInputConnectors
std::vector< Connector * > getInputConnectors() const
Gets all the output connectors.
te::process::ParameterValue
Definition: ParameterValue.h:47
te::process::Task::setId
void setId(const std::string &id)
Gets the type of the task.
te::process::TaskSharedPtr
Definition: Task.h:150
te::process::Task::~Task
virtual ~Task()
Virtual destructor.
te::process::TaskFactory
Wrapper function used to create taks. Used to avoid C++ to create multiple instances of the factory s...
Definition: Task.h:162
te::process::TaskPtr::~TaskPtr
~TaskPtr()
te::process::Task::addConnector
void addConnector(Connector *connector)
Removes a parameter connector to this class.
te::process::Task
This class represents the description of a parameter.
Definition: Task.h:54
te::process::Task::init
void init(TaskCapabilities *capabilities)
< Initializes the task. This function must be called by any class that extends this abstraction....
te::process::Task::setParameter
void setParameter(std::size_t index, te::process::ParameterValue *value, bool takeOwnership)
Gets the task paramters.
te::process::TaskFactory::~TaskFactory
virtual ~TaskFactory()
Gets all registered categories.
te::process::TaskCapabilities
Represents the capabilities of a task.
Definition: TaskCapabilities.h:47
te::process::Task::setTaskParameters
void setTaskParameters(TaskParameters *taskParameters)
Adds a parameter connector to this class.
te::process::Task::Task
Task(const std::string &taskType)
It initializes the Task.
te::process::CreateTask
TEPROCESSEXPORT Task * CreateTask(const std::string &taskType)
capabilities
te::da::DataSourceCapabilities capabilities
Definition: PostGISCapabilities.h:129
te::process::TaskPtr
Definition: Task.h:143
te::process::Task::m_id
std::string m_id
The ID of the task.
Definition: Task.h:134
te::process::TaskFactory::getTasksFromCategory
static std::vector< std::string > getTasksFromCategory(const std::string &category)
te::process::TaskFactory::getCategories
static std::vector< std::string > getCategories()
Gets all tasks from the give category.
te::process::Task::getType
const std::string & getType() const
Gets the capabilities of the task.
te::process::Task::removeConnector
void removeConnector(Connector *connector)
Gets all the input connectors.
te::process::TaskFactory::TaskFactory
TaskFactory(const std::string &taskType, const std::string &category)
Constructor.
te::process::Task::m_parameters
std::unique_ptr< TaskParameters > m_parameters
Definition: Task.h:137
te::process::Task::m_capabilities
std::unique_ptr< TaskCapabilities > m_capabilities
Definition: Task.h:136
te::process::TaskSharedPtr::TaskSharedPtr
TaskSharedPtr(te::process::Task *task=nullptr)
te::process::TaskParameters
Definition: TaskParameters.h:49
te::process::Task::getCapabilities
const TaskCapabilities * getCapabilities() const
Gets the current execution state of the task.
te::process::Task::getId
const std::string & getId() const
te::process::Task::getState
State getState() const
Gets the index-th parameter.
te::process::Task::pushParametersToConnectors
void pushParametersToConnectors()
This function is called whenever the parameters are changed and is responsible for checking the param...
TEPROCESSEXPORT
#define TEPROCESSEXPORT
Definition: Config.h:47
te::process::Task::m_state
State m_state
Definition: Task.h:135
te::process::Task::checkForReadyToRun
void checkForReadyToRun()
Runs the task.
te::process::Task::getOutputConnectors
std::vector< Connector * > getOutputConnectors() const
Checks if the task is valid, that is, if all the required input parameters have values OR connectors ...
te::process::Task::runImpl
virtual void runImpl()=0
te::process::Task::getTaskParameters
const TaskParameters * getTaskParameters() const
Sets the task paramters. This class will take the ownership of the given pointer.
te::process::Task::checkConnectorsAreReady
bool checkConnectorsAreReady()
Pushes the parameters into the output connectors.
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::process::Task::m_taskType
std::string m_taskType
The type of the task. Used by the factory.
Definition: Task.h:133
te::process::TaskSharedPtr::~TaskSharedPtr
~TaskSharedPtr()
te::process::Connector
Abstraction that represents a Connector between parameters of a Task.
Definition: Connector.h:48
te::process::TaskFactory::m_mapCategories
static std::map< std::string, std::vector< std::string > > m_mapCategories
Definition: Task.h:183
te::process::Task::isValid
bool isValid(std::string &errorMessage) const
Runs the task.