ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  if (count($this->nodes) != 0) {
167  $this->start_node = $this->nodes[0];
168  } else {
169  //ilWorkflowDbHelper::deleteWorkflow($this);
170  throw new Exception('No start_node, no node, no start. Doh.');
171  }
172  }
173  $this->start_node->activate();
174  ilWorkflowDbHelper::writeWorkflow($this);
175  }
176 
180  public function stopWorkflow()
181  {
182  $this->active = false;
183  foreach ($this->nodes as $node) {
184  $node->deactivate();
185  }
186  $this->onStopWorkflow();
187  }
188 
193  public function onStartWorkflow()
194  {
195  return;
196  }
197 
205  public function onStopWorkflow()
206  {
207  return;
208  }
209 
217  public function onWorkflowFinished()
218  {
219  return;
220  }
221 
227  public function isActive()
228  {
229  return (bool) $this->active;
230  }
231 
239  public function handleEvent($params)
240  {
241  $active_nodes_available = false;
242  // Hier nur an aktive Nodes dispatchen.
243  foreach ((array) $this->detectors as $detector) {
244  $node = $detector->getContext();
245  if ($node->isActive()) {
246  $detector->trigger($params);
247  $node = $detector->getContext();
248  if ($node->isActive()) {
249  $active_nodes_available = true;
250  }
251  }
252  }
253 
254  if ($active_nodes_available == false) {
255  $this->active = false;
256  $this->onWorkflowFinished();
257  }
258  }
259 
263  public function registerDetector(ilDetector $detector)
264  {
265  $reflection_class = new ReflectionClass($detector);
266  if (in_array('ilExternalDetector', $reflection_class->getInterfaceNames())) {
267  $this->detectors[] = $detector;
268  }
269  }
270 
276  public function getWorkflowData()
277  {
278  return array('type' => $this->workflow_type, 'content' => $this->workflow_content);
279  }
280 
286  public function getWorkflowSubject()
287  {
288  return array('type' => $this->workflow_subject_type, 'identifier' => $this->workflow_subject_identifier);
289  }
290 
296  public function getWorkflowContext()
297  {
298  return array('type' => $this->workflow_context_type, 'identifier' => $this->workflow_context_identifier);
299  }
300 
306  public function setDbId($id)
307  {
308  $this->db_id = $id;
309  }
310 
317  public function getDbId()
318  {
319  if ($this->db_id != null) {
320  return $this->db_id;
321  } else {
322  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
323  throw new ilWorkflowObjectStateException('No database ID set.');
324  }
325  }
326 
331  public function hasDbId()
332  {
333  if ($this->db_id == null) {
334  return false;
335  }
336  return true;
337  }
338 
345  public function setStartNode(ilNode $node)
346  {
347  $this->start_node = $node;
348  }
349 
355  public function addNode(ilNode $node)
356  {
357  $this->nodes[] = $node;
358  }
359 
367  public function setWorkflowClass($class)
368  {
369  $this->workflow_class = $class;
370  }
371 
379  public function getWorkflowClass()
380  {
381  return $this->workflow_class;
382  }
383 
391  public function setWorkflowLocation($path)
392  {
393  $this->workflow_location = $path;
394  }
395 
403  public function getWorkflowLocation()
404  {
406  }
407 
413  public function getNodes()
414  {
415  return $this->nodes;
416  }
417 
424  public static function autoload($class_name)
425  {
426  switch (true) {
427  case strtolower(substr($class_name, strlen($class_name) - 8, 8)) == 'activity':
428  $componentDirectory = 'activities';
429  break;
430 
431  case strtolower(substr($class_name, strlen($class_name) - 8, 8)) == 'detector':
432  $componentDirectory = 'detectors';
433  break;
434 
435  case strtolower(substr($class_name, strlen($class_name) - 7, 7)) == 'emitter':
436  $componentDirectory = 'emitters';
437  break;
438 
439  case strtolower(substr($class_name, strlen($class_name) - 4, 4)) == 'node':
440  $componentDirectory = 'node';
441  break;
442 
443  default:
444  return;
445  }
446 
447  $filename = './Services/WorkflowEngine/classes/' . $componentDirectory . '/class.' . $class_name . '.php';
448  if (file_exists($filename)) {
449  require_once $filename;
450  }
451  }
452 
456  public function isDataPersistenceRequired()
457  {
459  }
460 
465  {
466  $this->require_data_persistence = false;
467  }
468 
469  #region InstanceVars
470 
471  /*
472  * Instancevars work like this:
473  * array(
474  * 'id' => 'string',
475  * 'name' => 'string',
476  * 'value' => mixed
477  * );
478  *
479  */
480 
489  public function defineInstanceVar(
490  $id,
491  $name,
492  $reference = false,
493  $reference_target = '',
494  $type = 'mixed',
495  $role = 'undefined'
496  ) {
497  $this->instance_vars[] = array(
498  'id' => $id,
499  'name' => $name,
500  'value' => null,
501  'reference' => $reference,
502  'target' => $reference_target,
503  'type' => $type,
504  'role' => $role
505  );
506  }
507 
514  public function hasInstanceVarByName($name)
515  {
516  foreach ($this->instance_vars as $instance_var) {
517  if ($instance_var['name'] == $name) {
518  return true;
519  }
520  }
521  return false;
522  }
523 
531  public function hasInstanceVarById($id)
532  {
533  foreach ($this->instance_vars as $instance_var) {
534  if ($instance_var['id'] == $id) {
535  return true;
536  }
537  }
538  return false;
539  }
540 
547  public function getInstanceVarByName($name)
548  {
549  foreach ($this->instance_vars as &$instance_var) {
550  if ($instance_var['name'] == $name) {
551  if ($instance_var['reference'] === true) {
552  return $this->getInstanceVarByName($instance_var['target']);
553  } else {
554  return $instance_var['value'];
555  }
556  }
557  }
558  return false;
559  }
560 
567  public function getInstanceVarById($id)
568  {
569  foreach ($this->instance_vars as $instance_var) {
570  if ($instance_var['id'] == $id) {
571  if ($instance_var['reference'] === true) {
572  return $this->getInstanceVarById($instance_var['target']);
573  } else {
574  return $instance_var['value'];
575  }
576  }
577  }
578  return false;
579  }
580 
586  public function setInstanceVarByName($name, $value)
587  {
588  foreach ($this->instance_vars as &$instance_var) {
589  if ($instance_var['name'] == $name) {
590  if ($instance_var['reference'] === true) {
591  $this->setInstanceVarById($instance_var['target'], $value);
592  } else {
593  $instance_var['value'] = $value;
594  }
595  }
596  }
597  }
598 
605  public function setInstanceVarById($id, $value)
606  {
607  foreach ($this->instance_vars as &$instance_var) {
608  if ($instance_var['id'] == $id) {
609  if ($instance_var['reference'] === true) {
610  $this->setInstanceVarById($instance_var['target'], $value);
611  } else {
612  $instance_var['value'] = $value;
613  return;
614  }
615  }
616  }
617  }
618 
625  public function setInstanceVarByRole($role, $value)
626  {
627  foreach ($this->instance_vars as &$instance_var) {
628  if ($instance_var['role'] == $role) {
629  if ($instance_var['reference'] === true) {
630  $this->setInstanceVarById($instance_var['target'], $value);
631  } else {
632  $instance_var['value'] = $value;
633  return;
634  }
635  }
636  }
637  }
638 
644  public function getInstanceVars()
645  {
646  return (array) $this->instance_vars;
647  }
648 
652  public function flushInstanceVars()
653  {
654  $this->instance_vars = array();
655  }
656 
657  #endregion
658 
659  #region Data IO
660 
664  public function defineInputVar($name)
665  {
666  $this->data_inputs[$name] = null;
667  $this->require_data_persistence = true;
668  }
669 
673  public function defineOutputVar($name)
674  {
675  $this->data_outputs[$name] = null;
676  $this->require_data_persistence = true;
677  }
678 
682  public function readInputVar($name)
683  {
684  if ($this->data_inputs[$name]) {
685  return $this->data_inputs[$name];
686  }
687  return null;
688  }
689 
693  public function hasInputVar($name)
694  {
695  return array_key_exists($name, (array) $this->data_inputs);
696  }
697 
701  public function hasOutputVar($name)
702  {
703  return array_key_exists($name, (array) $this->data_outputs);
704  }
705 
709  public function writeInputVar($name, $value)
710  {
711  $this->data_inputs[$name] = $value;
712  $this->require_data_persistence = true;
713  }
714 
718  public function readOutputVar($name)
719  {
720  if ($this->data_outputs[$name]) {
721  return $this->data_outputs[$name];
722  }
723  return null;
724  }
725 
729  public function writeOutputVar($name, $value)
730  {
731  $this->data_outputs[$name] = $value;
732  $this->require_data_persistence = true;
733  }
734 
735  public function getInputVars()
736  {
737  return (array) $this->data_inputs;
738  }
739 
743  public function getOutputVars()
744  {
745  return (array) $this->data_outputs;
746  }
747 
752  public function registerInputVar($name, $definition)
753  {
754  $definition['name'] = $name;
755  $this->data_inputs[$name] = $definition;
756  }
757 
761  public function registerOutputVar($name)
762  {
763  $this->data_outputs[] = $name;
764  }
765 
766  #endregion
767 }
768 
769 spl_autoload_register(array('ilBaseWorkflow', 'autoload'));
startWorkflow()
Starts the workflow, activating the start_node.
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.
$type
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.
if(!array_key_exists('StateId', $_REQUEST)) $id
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.
defineInstanceVar( $id, $name, $reference=false, $reference_target='', $type='mixed', $role='undefined')
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.
__construct()
Default constructor.
$filename
Definition: buildRTE.php:89
$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)
setInstanceVarByName($name, $value)
Sets the given instance var with the given content.
getInstanceVarById($id)
Returns the given instance variables content.