source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/SelectionComparisonRule.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: 3.2 KB
Line 
1//-------------------------------------------------------------------------------------------------
2// Module    : $RCSfile: NodeIdentityRule.java,v $
3// Version   : $Revision: 0.0 $  $Author: patrick $  $Date: 19.02.2012 $
4// Project   : TaskTreeCreator
5// Creation  : 2012 by patrick
6// Copyright : Patrick Harms, 2012
7//-------------------------------------------------------------------------------------------------
8package de.ugoe.cs.quest.tasktrees.nodeequality;
9
10import de.ugoe.cs.quest.tasktrees.treeifc.Sequence;
11import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode;
12
13//-------------------------------------------------------------------------------------------------
14/**
15 * TODO comment
16 *
17 * @version $Revision: $ $Date: 19.02.2012$
18 * @author 2012, last modified by $Author: patrick$
19 */
20//-------------------------------------------------------------------------------------------------
21public class SelectionComparisonRule implements NodeComparisonRule
22{
23
24  /** */
25  private NodeEqualityRuleManager mRuleManager;
26
27  //-----------------------------------------------------------------------------------------------
28  /**
29   * TODO: comment
30   *
31   */
32  SelectionComparisonRule(NodeEqualityRuleManager ruleManager)
33  {
34    super();
35    mRuleManager = ruleManager;
36  }
37
38  //-----------------------------------------------------------------------------------------------
39  /* (non-Javadoc)
40   * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
41   */
42  //-----------------------------------------------------------------------------------------------
43  @Override
44  public NodeEquality compare(TaskTreeNode node1, TaskTreeNode node2)
45  {
46    if ((!(node1 instanceof Sequence)) || (!(node2 instanceof Sequence)))
47    {
48      return null;
49    }
50   
51    // if both sequences do not have children, they are equal although this doesn't make sense
52    if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0))
53    {
54      return new NodesEqual();
55    }
56   
57    // Selections are semantically equal, if they have children, which are all semantically equal.
58    // they are structurally equals, if they have the same number and order of structurally equal
59    // children
60    boolean structurallyEqual =
61      node1.getChildren().size() == node2.getChildren().size();
62   
63    for (int i = 0; i < node1.getChildren().size(); i++)
64    {
65      TaskTreeNode child1 = node1.getChildren().get(i);
66     
67      for (int j = 0; j < node2.getChildren().size(); j++)
68      {
69        TaskTreeNode child2 = node2.getChildren().get(j);
70     
71        NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
72       
73        if (!nodeEquality.getSemanticalEquality())
74        {
75          return null;
76        }
77        else if (structurallyEqual && (i == j))
78        {
79          // if we compare two children at the same position and if they are structurally equal
80          // then it can be further expected, that the selections are structurally equal
81          structurallyEqual &= nodeEquality.getStructuralEquality();
82        }
83      }
84    }
85   
86    if (structurallyEqual)
87    {
88      return new NodesEqual();
89    }
90    else
91    {
92      return new NodesSemanticallyEqual();
93    }
94  }
95
96}
Note: See TracBrowser for help on using the repository browser.