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