Ignore:
Timestamp:
08/17/12 08:33:29 (12 years ago)
Author:
pharms
Message:
  • adapted task tree creation stuff to more general event handling
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/temporalrelation/TrackBarSelectionDetectionRule.java

    r451 r557  
    1 //------------------------------------------------------------------------------------------------- 
    21// Module    : $RCSfile: TrackBarSelectionDetectionRule.java,v $ 
    32// Version   : $Revision: 0.0 $  $Author: patrick $  $Date: 28.04.2012 $ 
     
    54// Creation  : 2012 by patrick 
    65// Copyright : Patrick Harms, 2012 
    7 //------------------------------------------------------------------------------------------------- 
     6 
    87package de.ugoe.cs.quest.tasktrees.temporalrelation; 
    98 
    10 import de.ugoe.cs.quest.eventcore.guimodel.TrackBar; 
    11 import de.ugoe.cs.quest.eventcore.userinteraction.ValueSelection; 
     9import de.ugoe.cs.quest.eventcore.gui.ValueSelection; 
     10import de.ugoe.cs.quest.eventcore.guimodel.ITrackBar; 
    1211import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality; 
    1312import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager; 
    14 import de.ugoe.cs.quest.tasktrees.treeifc.InteractionTask; 
    15 import de.ugoe.cs.quest.tasktrees.treeifc.Iteration; 
    16 import de.ugoe.cs.quest.tasktrees.treeifc.Selection; 
    17 import de.ugoe.cs.quest.tasktrees.treeifc.Sequence; 
    18 import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeBuilder; 
    19 import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode; 
    20 import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNodeFactory; 
     13import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; 
     14import de.ugoe.cs.quest.tasktrees.treeifc.IIteration; 
     15import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; 
     16import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; 
     17import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; 
     18import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; 
     19import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; 
    2120 
    22 //------------------------------------------------------------------------------------------------- 
    2321/** 
    2422 * TODO comment 
     
    2725 * @author 2012, last modified by $Author: patrick$ 
    2826 */ 
    29 //------------------------------------------------------------------------------------------------- 
    30 public class TrackBarSelectionDetectionRule implements TemporalRelationshipRule 
    31 { 
     27public class TrackBarSelectionDetectionRule implements TemporalRelationshipRule { 
    3228 
    33   /** */ 
    34   private NodeEqualityRuleManager mNodeEqualityRuleManager; 
     29    /** */ 
     30    private NodeEqualityRuleManager nodeEqualityRuleManager; 
    3531 
    36   //----------------------------------------------------------------------------------------------- 
    37   /** 
    38    * TODO: comment 
     32    /** 
     33     * TODO: comment 
     34     *  
     35     */ 
     36    TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager) { 
     37        super(); 
     38        this.nodeEqualityRuleManager = nodeEqualityRuleManager; 
     39    } 
     40 
     41    /* 
     42     * (non-Javadoc) 
     43     *  
     44     * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory) 
     45     */ 
     46    @Override 
     47    public RuleApplicationResult apply(ITaskTreeNode        parent, 
     48                                       ITaskTreeBuilder     builder, 
     49                                       ITaskTreeNodeFactory nodeFactory, 
     50                                       boolean              finalize) 
     51    { 
     52        if (!(parent instanceof ISequence)) { 
     53            return null; 
     54        } 
     55 
     56        RuleApplicationResult result = new RuleApplicationResult(); 
     57 
     58        int valueSelectionStartIndex = -1; 
     59 
     60        int index = 0; 
     61        while (index < parent.getChildren().size()) { 
     62            ITaskTreeNode child = parent.getChildren().get(index); 
     63 
     64            if ((child instanceof IEventTask) && 
     65                (((IEventTask) child).getEventTarget() instanceof ITrackBar) && 
     66                (((IEventTask) child).getEventType() instanceof ValueSelection)) 
     67            { 
     68                if (valueSelectionStartIndex < 0) { 
     69                    // let the show begin 
     70                    valueSelectionStartIndex = index; 
     71                } 
     72            } 
     73            else if (valueSelectionStartIndex >= 0) { 
     74                // current child is no more value selection. But the preceding tasks were. 
     75                // Therefore, 
     76                // create an iteration with the different selectable values as selection children 
     77                handleValueSelections(valueSelectionStartIndex, index - 1, parent, builder, 
     78                                      nodeFactory, result); 
     79 
     80                return result; 
     81            } 
     82 
     83            index++; 
     84        } 
     85 
     86        if (valueSelectionStartIndex >= 0) { 
     87            if (finalize) { 
     88                handleValueSelections(valueSelectionStartIndex, parent.getChildren().size() - 1, 
     89                                      parent, builder, nodeFactory, result); 
     90            } 
     91            else { 
     92                result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); 
     93            } 
     94        } 
     95 
     96        return result; 
     97    } 
     98 
     99    /** 
     100     * TODO: comment 
     101     *  
     102     * @param valueSelectionStartIndex 
     103     * @param i 
     104     */ 
     105    private void handleValueSelections(int                   startIndex, 
     106                                       int                   endIndex, 
     107                                       ITaskTreeNode         parent, 
     108                                       ITaskTreeBuilder      builder, 
     109                                       ITaskTreeNodeFactory  nodeFactory, 
     110                                       RuleApplicationResult result) 
     111    { 
     112        IIteration iteration = nodeFactory.createNewIteration(); 
     113        result.addNewlyCreatedParentNode(iteration); 
     114 
     115        ISelection selection = nodeFactory.createNewSelection(); 
     116        result.addNewlyCreatedParentNode(selection); 
     117        builder.setChild(iteration, selection); 
     118 
     119        for (int i = endIndex - startIndex; i >= 0; i--) { 
     120            addChildIfNecessary(selection, parent.getChildren().get(startIndex), builder, 
     121                                nodeFactory, result); 
     122            builder.removeChild((ISequence) parent, startIndex); 
     123        } 
     124 
     125        builder.addChild((ISequence) parent, startIndex, iteration); 
     126 
     127        result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); 
     128    } 
     129 
     130    /** 
    39131   * 
    40132   */ 
    41   //----------------------------------------------------------------------------------------------- 
    42   TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager) 
    43   { 
    44     super(); 
    45     mNodeEqualityRuleManager = nodeEqualityRuleManager; 
    46   } 
     133    private void addChildIfNecessary(ISelection            parentSelection, 
     134                                     ITaskTreeNode         node, 
     135                                     ITaskTreeBuilder      builder, 
     136                                     ITaskTreeNodeFactory  nodeFactory, 
     137                                     RuleApplicationResult result) 
     138    { 
     139        for (int i = 0; i < parentSelection.getChildren().size(); i++) { 
     140            ITaskTreeNode child = parentSelection.getChildren().get(i); 
    47141 
    48   //----------------------------------------------------------------------------------------------- 
    49   /* (non-Javadoc) 
    50    * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory) 
    51    */ 
    52   //----------------------------------------------------------------------------------------------- 
    53   @Override 
    54   public RuleApplicationResult apply(TaskTreeNode        parent, 
    55                                      TaskTreeBuilder     builder, 
    56                                      TaskTreeNodeFactory nodeFactory, 
    57                                      boolean             finalize) 
    58   { 
    59     if (!(parent instanceof Sequence)) 
    60     { 
    61       return null; 
    62     } 
    63      
    64     RuleApplicationResult result = new RuleApplicationResult(); 
    65      
    66     int valueSelectionStartIndex = -1; 
    67      
    68     int index = 0; 
    69     while (index < parent.getChildren().size()) 
    70     { 
    71       TaskTreeNode child = parent.getChildren().get(index); 
    72        
    73       if ((child instanceof InteractionTask) && 
    74           (((InteractionTask) child).getGUIElement() instanceof TrackBar) && 
    75           (((InteractionTask) child).getInteraction() instanceof ValueSelection)) 
    76       { 
    77         if (valueSelectionStartIndex < 0) 
    78         { 
    79           // let the show begin 
    80           valueSelectionStartIndex = index; 
    81         } 
    82       } 
    83       else if (valueSelectionStartIndex >= 0) 
    84       { 
    85         // current child is no more value selection. But the preceding tasks were. Therefore, 
    86         // create an iteration with the different selectable values as selection children 
    87         handleValueSelections 
    88           (valueSelectionStartIndex, index - 1, parent, builder, nodeFactory, result); 
    89          
    90         return result; 
    91       } 
    92        
    93       index++; 
    94     } 
    95      
    96     if (valueSelectionStartIndex >= 0) 
    97     { 
    98       if (finalize) 
    99       { 
    100         handleValueSelections(valueSelectionStartIndex, parent.getChildren().size() - 1, parent, 
    101                               builder, nodeFactory, result); 
    102       } 
    103       else 
    104       { 
    105         result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); 
    106       } 
    107     } 
    108      
    109     return result; 
    110   } 
     142            if (child instanceof IEventTask) { 
     143                // check, if the new node is a variant for the current event task 
     144                NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); 
     145                if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { 
     146                    // the node is a variant. If it not structurally equal, a new sub-selection for 
     147                    // the existing and the new node must be created. Otherwise, the new node does 
     148                    // not need to be added 
     149                    if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { 
     150                        ISelection selection = nodeFactory.createNewSelection(); 
     151                        result.addNewlyCreatedParentNode(selection); 
     152                        builder.addChild(parentSelection, selection); 
    111153 
    112   //----------------------------------------------------------------------------------------------- 
    113   /** 
    114    * TODO: comment 
    115    * 
    116    * @param valueSelectionStartIndex 
    117    * @param i 
    118    */ 
    119   //----------------------------------------------------------------------------------------------- 
    120   private void handleValueSelections(int                   startIndex, 
    121                                      int                   endIndex, 
    122                                      TaskTreeNode          parent, 
    123                                      TaskTreeBuilder       builder, 
    124                                      TaskTreeNodeFactory   nodeFactory, 
    125                                      RuleApplicationResult result) 
    126   { 
    127     Iteration iteration = nodeFactory.createNewIteration(); 
    128     result.addNewlyCreatedParentNode(iteration); 
    129      
    130     Selection selection = nodeFactory.createNewSelection(); 
    131     result.addNewlyCreatedParentNode(selection); 
    132     builder.setChild(iteration, selection); 
    133      
    134     for (int i = endIndex - startIndex; i >= 0 ; i--) 
    135     { 
    136       addChildIfNecessary 
    137         (selection, parent.getChildren().get(startIndex), builder, nodeFactory, result); 
    138       builder.removeChild((Sequence) parent, startIndex); 
    139     } 
    140      
    141     builder.addChild((Sequence) parent, startIndex, iteration); 
    142      
    143     result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); 
    144   } 
     154                        builder.addChild(selection, child); 
     155                        builder.addChild(selection, node); 
     156                        builder.removeChild(parentSelection, child); 
     157                    } 
    145158 
    146   //----------------------------------------------------------------------------------------------- 
    147   /** 
    148    * 
    149    */ 
    150   //----------------------------------------------------------------------------------------------- 
    151   private void addChildIfNecessary(Selection             parentSelection, 
    152                                    TaskTreeNode          node, 
    153                                    TaskTreeBuilder       builder, 
    154                                    TaskTreeNodeFactory   nodeFactory, 
    155                                    RuleApplicationResult result) 
    156   { 
    157     for (int i = 0; i < parentSelection.getChildren().size(); i++) 
    158     { 
    159       TaskTreeNode child = parentSelection.getChildren().get(i); 
     159                    return; 
     160                } 
     161            } 
     162            else if (child instanceof ISelection) { 
     163                // check, if the new node is a variant for the semantically equal children of the 
     164                // current 
     165                // selection 
     166                boolean addNode = true; 
     167                for (int j = 0; j < child.getChildren().size(); j++) { 
     168                    NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); 
     169                    if (!nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { 
     170                        // the new node is no semantical equivalent of the nodes in the current 
     171                        // selection - break up 
     172                        addNode = false; 
     173                        break; 
     174                    } 
     175                    else if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { 
     176                        addNode = false; 
     177                        break; 
     178                    } 
     179                } 
    160180 
    161       if (child instanceof InteractionTask) 
    162       { 
    163         // check, if the new node is a variant for the current interaction task 
    164         NodeEquality nodeEquality = mNodeEqualityRuleManager.applyRules(child, node); 
    165         if (nodeEquality.getSemanticalEquality()) 
    166         { 
    167           // the node is a variant. If it not structurally equal, a new sub-selection for the 
    168           // existing and the new node must be created. Otherwise, the new node does not need 
    169           // to be added 
    170           if (!nodeEquality.getStructuralEquality()) 
    171           { 
    172             Selection selection = nodeFactory.createNewSelection(); 
    173             result.addNewlyCreatedParentNode(selection); 
    174             builder.addChild(parentSelection, selection); 
    175  
    176             builder.addChild(selection, child); 
    177             builder.addChild(selection, node); 
    178             builder.removeChild(parentSelection, child); 
    179           } 
    180  
    181           return; 
    182         } 
    183       } 
    184       else if (child instanceof Selection) 
    185       { 
    186         // check, if the new node is a variant for the semantically equal children of the current 
    187         // selection 
    188         boolean addNode = true; 
    189         for (int j = 0; j < child.getChildren().size(); j++) 
    190         { 
    191           NodeEquality nodeEquality = mNodeEqualityRuleManager.applyRules(child, node); 
    192           if (!nodeEquality.getSemanticalEquality()) 
    193           { 
    194             // the new node is no semantical equivalent of the nodes in the current selection - 
    195             // break up 
    196             addNode = false; 
    197             break; 
    198           } 
    199           else if (nodeEquality.getStructuralEquality()) 
    200           { 
    201             addNode = false; 
    202             break; 
    203           } 
     181                if (addNode) { 
     182                    // the node is a semantical equivalent to all the nodes in the existing 
     183                    // sub-selection 
     184                    // but it is not structurally identical to either of them. Therefore add it. 
     185                    builder.addChild((ISelection) child, node); 
     186                    return; 
     187                } 
     188            } 
    204189        } 
    205190 
    206         if (addNode) 
    207         { 
    208           // the node is a semantical equivalent to all the nodes in the existing sub-selection 
    209           // but it is not structurally identical to either of them. Therefore add it. 
    210           builder.addChild((Selection) child, node); 
    211           return; 
    212         } 
    213       } 
     191        // if we did not return in the previous checks, then the node must be added 
     192        builder.addChild(parentSelection, node); 
    214193    } 
    215      
    216     // if we did not return in the previous checks, then the node must be added 
    217     builder.addChild(parentSelection, node); 
    218   } 
    219194 
    220195} 
Note: See TracChangeset for help on using the changeset viewer.