ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilBaseWorkflow.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 require_once './Services/WorkflowEngine/interfaces/ilExternalDetector.php';
7 require_once './Services/WorkflowEngine/interfaces/ilWorkflow.php';
9 require_once './Services/WorkflowEngine/classes/nodes/class.ilBasicNode.php';
10 
21 abstract class ilBaseWorkflow implements ilWorkflow
22 {
28  protected $nodes;
29 
35  protected $detectors;
36 
42  protected $start_node;
43 
49  protected $active;
50 
56  protected $db_id;
57 
68  protected $workflow_type;
69 
79  protected $workflow_content;
80 
85  protected $workflow_class;
86 
91  protected $workflow_location;
92 
101 
109 
118 
127 
133  protected $instance_vars = array();
134 
136  protected $data_inputs;
137 
139  protected $data_outputs;
140 
142  protected $require_data_persistence = false;
143 
149  public function __construct()
150  {
151  }
152 
156  public function startWorkflow()
157  {
158  // Write the workflow to the database, so detectors find a parent id to save with them.
159  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
160  $this->active = true;
161  ilWorkflowDbHelper::writeWorkflow($this);
162  $this->onStartWorkflow();
163 
164  // Figure out, if there is a start-node set - or nodes at all.
165  if ($this->start_node == null)
166  {
167  if (count($this->nodes) != 0)
168  {
169  $this->start_node = $this->nodes[0];
170  } else {
171  //ilWorkflowDbHelper::deleteWorkflow($this);
172  throw new Exception ('No start_node, no node, no start. Doh.');
173  }
174  }
175  $this->start_node->activate();
176  ilWorkflowDbHelper::writeWorkflow($this);
177  }
178 
182  public function stopWorkflow()
183  {
184  $this->active = false;
185  foreach ($this->nodes as $node)
186  {
187  $node->deactivate();
188  }
189  $this->onStopWorkflow();
190  }
191 
196  public function onStartWorkflow()
197  {
198  return;
199  }
200 
208  public function onStopWorkflow()
209  {
210  return;
211  }
212 
220  public function onWorkflowFinished()
221  {
222  return;
223  }
224 
230  public function isActive()
231  {
232  return (bool)$this->active;
233  }
234 
242  public function handleEvent($params)
243  {
244  $active_nodes_available = false;
245  // Hier nur an aktive Nodes dispatchen.
246  foreach ((array)$this->detectors as $detector)
247  {
248  $node = $detector->getContext();
249  if ($node->isActive())
250  {
251  $detector->trigger($params);
252  $node = $detector->getContext();
253  if ($node->isActive())
254  {
255  $active_nodes_available = true;
256  }
257  }
258  }
259 
260  if ($active_nodes_available == false)
261  {
262  $this->active = false;
263  $this->onWorkflowFinished();
264  }
265  }
266 
270  public function registerDetector(ilDetector $detector)
271  {
272  $reflection_class = new ReflectionClass($detector);
273  if (in_array('ilExternalDetector', $reflection_class->getInterfaceNames()))
274  {
275  $this->detectors[] = $detector;
276  }
277  }
278 
284  public function getWorkflowData()
285  {
286  return array('type' => $this->workflow_type, 'content' => $this->workflow_content);
287  }
288 
294  public function getWorkflowSubject()
295  {
296  return array('type' => $this->workflow_subject_type, 'identifier' => $this->workflow_subject_identifier);
297  }
298 
304  public function getWorkflowContext()
305  {
306  return array('type' => $this->workflow_context_type, 'identifier' => $this->workflow_context_identifier);
307  }
308 
314  public function setDbId($id)
315  {
316  $this->db_id = $id;
317  }
318 
325  public function getDbId()
326  {
327  if ($this->db_id != null)
328  {
329  return $this->db_id;
330  }
331  else
332  {
333  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
334  throw new ilWorkflowObjectStateException('No database ID set.');
335  }
336  }
337 
342  public function hasDbId()
343  {
344  if ($this->db_id == null)
345  {
346  return false;
347  }
348  return true;
349  }
350 
357  public function setStartNode(ilNode $node)
358  {
359  $this->start_node = $node;
360  }
361 
367  public function addNode(ilNode $node)
368  {
369  $this->nodes[] = $node;
370  }
371 
379  public function setWorkflowClass($class)
380  {
381  $this->workflow_class = $class;
382  }
383 
391  public function getWorkflowClass()
392  {
393  return $this->workflow_class;
394  }
395 
403  public function setWorkflowLocation($path)
404  {
405  $this->workflow_location = $path;
406  }
407 
415  public function getWorkflowLocation()
416  {
418  }
419 
425  public function getNodes()
426  {
427  return $this->nodes;
428  }
429 
436  public static function autoload($class_name)
437  {
438  switch(true)
439  {
440  case strtolower(substr($class_name, strlen($class_name) - 8, 8)) == 'activity':
441  $componentDirectory = 'activities';
442  break;
443 
444  case strtolower(substr($class_name, strlen($class_name) - 8, 8)) == 'detector':
445  $componentDirectory = 'detectors';
446  break;
447 
448  case strtolower(substr($class_name, strlen($class_name) - 7, 7)) == 'emitter':
449  $componentDirectory = 'emitters';
450  break;
451 
452  case strtolower(substr($class_name, strlen($class_name) - 4, 4)) == 'node':
453  $componentDirectory = 'node';
454  break;
455 
456  default:
457  return;
458  }
459 
460  $filename = './Services/WorkflowEngine/classes/' . $componentDirectory . '/class.'.$class_name.'.php';
461  if(file_exists($filename))
462  {
463  require_once $filename;
464  }
465  }
466 
470  public function isDataPersistenceRequired()
471  {
473  }
474 
479  {
480  $this->require_data_persistence = false;
481  }
482 
483  #region InstanceVars
484 
485  /*
486  * Instancevars work like this:
487  * array(
488  * 'id' => 'string',
489  * 'name' => 'string',
490  * 'value' => mixed
491  * );
492  *
493  */
494 
503  public function defineInstanceVar($id, $name, $reference = false, $reference_target = '',
504  $type = 'mixed', $role = 'undefined')
505  {
506  $this->instance_vars[] = array(
507  'id' => $id,
508  'name' => $name,
509  'value' => null,
510  'reference' => $reference,
511  'target' => $reference_target,
512  'type' => $type,
513  'role' => $role
514  );
515  }
516 
523  public function hasInstanceVarByName($name)
524  {
525  foreach($this->instance_vars as $instance_var)
526  {
527  if($instance_var['name'] == $name)
528  {
529  return true;
530  }
531  }
532  return false;
533  }
534 
542  public function hasInstanceVarById($id)
543  {
544  foreach($this->instance_vars as $instance_var)
545  {
546  if($instance_var['id'] == $id)
547  {
548  return true;
549  }
550  }
551  return false;
552  }
553 
560  public function getInstanceVarByName($name)
561  {
562  foreach($this->instance_vars as &$instance_var)
563  {
564  if($instance_var['name'] == $name)
565  {
566  if($instance_var['reference'] === true)
567  {
568  return $this->getInstanceVarByName($instance_var['target']);
569  }
570  else
571  {
572  return $instance_var['value'];
573  }
574  }
575  }
576  return false;
577  }
578 
585  public function getInstanceVarById($id)
586  {
587  foreach($this->instance_vars as $instance_var)
588  {
589  if($instance_var['id'] == $id)
590  {
591  if($instance_var['reference'] === true)
592  {
593  return $this->getInstanceVarById($instance_var['target']);
594  }
595  else
596  {
597  return $instance_var['value'];
598  }
599  }
600  }
601  return false;
602  }
603 
609  public function setInstanceVarByName($name, $value)
610  {
611  foreach($this->instance_vars as &$instance_var)
612  {
613  if($instance_var['name'] == $name)
614  {
615  if($instance_var['reference'] === true)
616  {
617  $this->setInstanceVarById($instance_var['target'], $value);
618  }
619  else
620  {
621  $instance_var['value'] = $value;
622  }
623  }
624  }
625  }
626 
633  public function setInstanceVarById($id, $value)
634  {
635  foreach($this->instance_vars as &$instance_var)
636  {
637  if($instance_var['id'] == $id)
638  {
639  if($instance_var['reference'] === true)
640  {
641  $this->setInstanceVarById($instance_var['target'], $value);
642  }
643  else
644  {
645  $instance_var['value'] = $value;
646  return;
647  }
648  }
649  }
650  }
651 
658  public function setInstanceVarByRole($role, $value)
659  {
660  foreach($this->instance_vars as &$instance_var)
661  {
662  if($instance_var['role'] == $role)
663  {
664  if($instance_var['reference'] === true)
665  {
666  $this->setInstanceVarById($instance_var['target'], $value);
667  }
668  else
669  {
670  $instance_var['value'] = $value;
671  return;
672  }
673  }
674  }
675  }
676 
682  public function getInstanceVars()
683  {
684  return (array) $this->instance_vars;
685  }
686 
690  public function flushInstanceVars()
691  {
692  $this->instance_vars = array();
693  }
694 
695  #endregion
696 
697  #region Data IO
698 
702  public function defineInputVar($name)
703  {
704  $this->data_inputs[$name] = null;
705  $this->require_data_persistence = true;
706  }
707 
711  public function defineOutputVar($name)
712  {
713  $this->data_outputs[$name] = null;
714  $this->require_data_persistence = true;
715  }
716 
720  public function readInputVar($name)
721  {
722  if($this->data_inputs[$name])
723  {
724  return $this->data_inputs[$name];
725  }
726  return null;
727  }
728 
732  public function hasInputVar($name)
733  {
734  return array_key_exists($name, (array)$this->data_inputs);
735  }
736 
740  public function hasOutputVar($name)
741  {
742  return array_key_exists($name, (array)$this->data_outputs);
743  }
744 
748  public function writeInputVar($name, $value)
749  {
750  $this->data_inputs[$name] = $value;
751  $this->require_data_persistence = true;
752  }
753 
757  public function readOutputVar($name)
758  {
759  if($this->data_outputs[$name])
760  {
761  return $this->data_outputs[$name];
762  }
763  return null;
764  }
765 
769  public function writeOutputVar($name, $value)
770  {
771  $this->data_outputs[$name] = $value;
772  $this->require_data_persistence = true;
773  }
774 
775  public function getInputVars()
776  {
777  return (array)$this->data_inputs;
778  }
779 
783  public function getOutputVars()
784  {
785  return (array)$this->data_outputs;
786  }
787 
792  public function registerInputVar($name, $definition)
793  {
794  $definition['name'] = $name;
795  $this->data_inputs[$name] = $definition;
796  }
797 
801  public function registerOutputVar($name)
802  {
803  $this->data_outputs[] = $name;
804  }
805 
806  #endregion
807 }
808 
809 spl_autoload_register(array('ilBaseWorkflow', 'autoload'));
startWorkflow()
Starts the workflow, activating the start_node.
defineInstanceVar($id, $name, $reference=false, $reference_target='', $type='mixed', $role='undefined')
getNodes()
Returns all nodes attached to the workflow.
hasInstanceVarById($id)
Returns if an instance variable of the given id is set.
$path
Definition: aliased.php:25
getInstanceVarByName($name)
Returns the given instance variables content.
getWorkflowClass()
Returns the currently set workflow class definition name.
setDbId($id)
Sets the database id of the detector.
onStopWorkflow()
Method called on stopping of the workflow, after deactivating all nodes.
setInstanceVarById($id, $value)
Sets the given instance var with the given content.
hasInstanceVarByName($name)
Returns if an instance variable of the given name is set.
setWorkflowClass($class)
Sets the classname of the workflow definition.
getWorkflowSubject()
Get the workflow subject set to the workflow.
PhpIncludeInspection
getDbId()
Returns the database id of the detector if set.
onWorkflowFinished()
Method called after workflow is finished, after detecting no more nodes are active.
$db_id
This holds the database id of the workflow.
addNode(ilNode $node)
This method adds a node to the workflow.
handleEvent($params)
Handles an event.
PhpIncludeInspection
Definition: ilNode.php:25
flushInstanceVars()
Empties the instance variables.
registerDetector(ilDetector $detector)
getWorkflowLocation()
Returns the currently set path to the workflow definition.
hasDbId()
Returns, if the detector has a database id.
Create styles array
The data for the language used.
__construct()
Default constructor.
$active
Holds the activation state of the workflow.
getInstanceVars()
Returns an array with all set instance variables.
stopWorkflow()
Stops the workflow, deactivating all nodes.
setWorkflowLocation($path)
Sets the location of the workflow definition file as relative path.
registerInputVar($name, $definition)
getWorkflowData()
Returns the workflow type and content currently set to the workflow.
PhpIncludeInspection
Definition: ilWorkflow.php:23
setStartNode(ilNode $node)
Sets the start node of the workflow.
setInstanceVarByRole($role, $value)
Sets the given instance var with the given content.
onStartWorkflow()
Method called on start of the workflow, prior to activating the first node.
writeInputVar($name, $value)
isActive()
Returns the activation status of the workflow.
ilDetector Interface is part of the petri net based workflow engine.
Definition: ilDetector.php:16
static autoload($class_name)
Autoloader function to dynamically include files for instantiation of objects during deserialization...
getWorkflowContext()
Get the event context set to the workflow.
writeOutputVar($name, $value)
$params
Definition: example_049.php:96
setInstanceVarByName($name, $value)
Sets the given instance var with the given content.
getInstanceVarById($id)
Returns the given instance variables content.