source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/temporalrelation/DefaultSequenceDetectionRule.java @ 439

Last change on this file since 439 was 439, checked in by pharms, 12 years ago

initial import after refactoring of module structure with Steffen

  • Property svn:executable set to *
File size: 5.4 KB
Line 
1//-------------------------------------------------------------------------------------------------
2// Module    : $RCSfile: DefaultSequenceDetectionRule.java,v $
3// Version   : $Revision: 0.0 $  $Author: patrick $  $Date: 18.03.2012 $
4// Project   : TaskTreeCreator
5// Creation  : 2012 by patrick
6// Copyright : Patrick Harms, 2012
7//-------------------------------------------------------------------------------------------------
8package de.ugoe.cs.quest.tasktrees.temporalrelation;
9
10import de.ugoe.cs.quest.tasktrees.treeifc.InteractionTask;
11import de.ugoe.cs.quest.tasktrees.treeifc.Sequence;
12import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeBuilder;
13import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode;
14import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNodeFactory;
15
16//-------------------------------------------------------------------------------------------------
17/**
18 * TODO comment
19 *
20 * @version $Revision: $ $Date: 18.03.2012$
21 * @author 2012, last modified by $Author: patrick$
22 */
23//-------------------------------------------------------------------------------------------------
24public class DefaultSequenceDetectionRule implements TemporalRelationshipRule
25{
26
27  //-----------------------------------------------------------------------------------------------
28  /* (non-Javadoc)
29   * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory)
30   */
31  //-----------------------------------------------------------------------------------------------
32  @Override
33  public RuleApplicationResult apply(TaskTreeNode        parent,
34                                     TaskTreeBuilder     builder,
35                                     TaskTreeNodeFactory nodeFactory,
36                                     boolean             finalize)
37  {
38    if (!(parent instanceof Sequence))
39    {
40      return null;
41    }
42   
43    RuleApplicationResult result = new RuleApplicationResult();
44    int sequenceStartingIndex = -1;
45   
46    int index = 0;
47    while (index < parent.getChildren().size())
48    {
49      TaskTreeNode child = parent.getChildren().get(index);
50     
51      if (child instanceof InteractionTask)
52      {
53        if (((InteractionTask) child).getInteraction().finishesLogicalSequence() &&
54            (sequenceStartingIndex > -1))
55        {
56          // There are several situations in which this implementation may cause infinite
57          // loops. This is because the rule manager will reapply rules until
58          // no rule is applied anymore. A sequence identified in a first iteration will
59          // be identified as a sequence also in a second iteration. As an example
60          // many sequences start with an interaction starting that sequence and end
61          // with an interaction ending that sequence. This will be reidentified as further
62          // subsequence. It must therefore be assured, that a sequence, that was once
63          // identified is not reidentified in a further application of the rule. For this,
64          // the implementation performs a kind of dry run. It creates a list of children
65          // that would belong to an identified sequence. Only if this list is not a
66          // reidentification then a new sequence is created and added to the parent. If it
67          // is a reidentification can be identified, if the list of children will contain
68          // all children of the parent, or if the list of children only consists of one
69          // sequence. Further, an identified sequence must at least have one child.
70          if (((sequenceStartingIndex != 0) || (index != (parent.getChildren().size() - 1))) &&
71              (((index - sequenceStartingIndex) > 0) ||
72                (((index - sequenceStartingIndex) == 0) &&
73                 (!((InteractionTask) child).getInteraction().startsLogicalSequence()))))
74          {
75            boolean allNewChildrenAreSequences = true;
76           
77            for (int j = sequenceStartingIndex; ((allNewChildrenAreSequences) && (j < index)); j++)
78            {
79              allNewChildrenAreSequences &= (parent.getChildren().get(j) instanceof Sequence);
80            }
81           
82            if (!allNewChildrenAreSequences)
83            {
84              Sequence sequence = nodeFactory.createNewSequence();
85           
86              for (int j = sequenceStartingIndex; j < index; j++)
87              {
88                builder.addChild(sequence, parent.getChildren().get(sequenceStartingIndex));
89                builder.removeChild((Sequence) parent, sequenceStartingIndex);
90              }
91
92              if (!((InteractionTask) child).getInteraction().startsLogicalSequence())
93              {
94                builder.addChild(sequence, parent.getChildren().get(sequenceStartingIndex));
95                builder.removeChild((Sequence) parent, sequenceStartingIndex);
96              }
97
98              builder.addChild((Sequence) parent, sequenceStartingIndex, sequence);
99
100              result.addNewlyCreatedParentNode(sequence);
101              result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
102              return result;
103            }
104          }
105        }
106       
107        if (((InteractionTask) child).getInteraction().startsLogicalSequence())
108        {
109          sequenceStartingIndex = index;
110        }
111      }
112     
113      index++;
114    }
115   
116    if (sequenceStartingIndex >= 0)
117    {
118      result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
119    }
120   
121    return result;
122  }
123
124}
Note: See TracBrowser for help on using the repository browser.