source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/SequenceComparisonRule.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.1 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 SequenceComparisonRule implements NodeComparisonRule
22{
23
24
25  /** */
26  private NodeEqualityRuleManager mRuleManager;
27
28  //-----------------------------------------------------------------------------------------------
29  /**
30   * TODO: comment
31   *
32   */
33  //-----------------------------------------------------------------------------------------------
34  SequenceComparisonRule(NodeEqualityRuleManager ruleManager)
35  {
36    super();
37    mRuleManager = ruleManager;
38  }
39
40  //-----------------------------------------------------------------------------------------------
41  /* (non-Javadoc)
42   * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
43   */
44  //-----------------------------------------------------------------------------------------------
45  @Override
46  public NodeEquality compare(TaskTreeNode node1, TaskTreeNode node2)
47  {
48    if ((!(node1 instanceof Sequence)) || (!(node2 instanceof Sequence)))
49    {
50      return null;
51    }
52   
53    // if both sequences do not have children, they are equal although this doesn't make sense
54    if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0))
55    {
56      return new NodesEqual();
57    }
58   
59    // Sequences are equal, if they have the same number and order of children. If all the
60    // children match structurally, then the sequences are structurally equal. Otherwise, if
61    // at least one pair of children matches only semantically, then the sequences match only
62    // semantically.
63    if (node1.getChildren().size() != node2.getChildren().size())
64    {
65      return null;
66    }
67   
68    boolean structurallyEqual = true;
69    for (int i = 0; i < node1.getChildren().size(); i++)
70    {
71      TaskTreeNode child1 = node1.getChildren().get(i);
72      TaskTreeNode child2 = node2.getChildren().get(i);
73     
74      NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
75     
76      structurallyEqual &= nodeEquality.getStructuralEquality();
77     
78      if (!structurallyEqual && !nodeEquality.getSemanticalEquality())
79      {
80        return null;
81      }
82    }
83   
84    if (structurallyEqual)
85    {
86      return new NodesEqual();
87    }
88    else
89    {
90      return new NodesSemanticallyEqual();
91    }
92  }
93
94}
Note: See TracBrowser for help on using the repository browser.