Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. package net.fm.geco.model.comparison.model;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import geco.model.util.Expression;
  7. import geco.model.util.ExpressionFactor;
  8. import geco.model.util.ExpressionTerm;
  9. import net.fm.geco.model.condition.tree.EBooleanType;
  10. import net.fm.geco.model.condition.tree.TreeNode;
  11. import net.fm.geco.model.value.Value;
  12.  
  13. public class OperationTree {
  14.  
  15. private OperationNode root;
  16. private String currentCollection;
  17. OperationNode left = null, right = null;
  18. OperationNode node = null;
  19.  
  20. // OperationNode node2 = null;
  21. public OperationTree(String currentCollection) {
  22.  
  23. this.root = null;
  24. this.currentCollection = currentCollection;
  25.  
  26. }
  27.  
  28. public void expression2Tree(Expression e) {
  29. System.out.println(e.terms.toString() + " " + e.operators.toString());
  30. expression2Tree(e, root, null);
  31.  
  32. }
  33.  
  34. // lastNode indica il nodo inserito in precedenza
  35. private void expression2Tree(Expression e, OperationNode actualNode, OperationNode parent) {
  36.  
  37. for (int i = e.terms.size() - 1; i >= 0; i--) {
  38. String op = e.operators.get(i);
  39.  
  40. if (op != "") {
  41. if (this.root == null) {
  42. this.root = new OperationNode(string2OperatorType(op), currentCollection);
  43. actualNode = this.root;
  44. } else
  45.  
  46. if (parent == null) {
  47. if (actualNode == null) {
  48. actualNode = new OperationNode(string2OperatorType(op), currentCollection);
  49.  
  50. } else if (actualNode != null) {
  51.  
  52. if (actualNode.getRight() != null && actualNode.getLeft() == null) {
  53. left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
  54.  
  55. actualNode = left;
  56. }
  57. }
  58. }
  59. /*
  60. * caso in cui abbiamo annidamento quindi ho parentesi ...così
  61. * non può funzionare perchè resto chiuso in un ramo dell'albero
  62. */
  63. else {
  64. if (actualNode == null) {
  65. actualNode = new OperationNode(string2OperatorType(op), currentCollection);
  66.  
  67. /*
  68. * aggancio i nuovi nodi al parent in base al ramo senza
  69. * nodi
  70. */
  71.  
  72. if (parent.getRight() == null)
  73. parent.right = actualNode;
  74. else
  75. parent.left = actualNode;
  76.  
  77. } else if (actualNode != null) {
  78. if (actualNode.getRight() != null && actualNode.getLeft() == null) {
  79. left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
  80. actualNode = left;
  81. } else
  82. actualNode = parent;
  83. }
  84. }
  85. }
  86.  
  87. ExpressionTerm term = e.terms.get(i);
  88.  
  89. evaluateTerm(term, actualNode, parent);
  90. }
  91. }
  92.  
  93. private void evaluateTerm(ExpressionTerm term, OperationNode actualNode, OperationNode parent) {
  94.  
  95. for (int i = term.factors.size() - 1; i >= 0; i--) {
  96. String op = term.operators.get(i); // il primo è nullo
  97. if (op != "") {
  98. if (this.root == null) {
  99. this.root = new OperationNode(string2OperatorType(op), currentCollection);
  100. actualNode = this.root;
  101.  
  102. } else
  103.  
  104. if (parent == null) {
  105. if (actualNode == null) {
  106. actualNode = new OperationNode(string2OperatorType(op), currentCollection);
  107. } else if (actualNode != null) {
  108.  
  109. if (actualNode.getRight() != null && actualNode.getLeft() == null) {
  110. left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
  111. actualNode = left;
  112. }
  113. }
  114. }
  115. /*
  116. * caso in cui abbiamo annidamento quindi ho parentesi ...così
  117. * non può funzionare perchè resto chiuso in un ramo dell'albero
  118. */
  119. else {
  120. if (actualNode == null) {
  121. actualNode = new OperationNode(string2OperatorType(op), currentCollection);
  122.  
  123. /*
  124. * aggancio i nuovi nodi al parent in base al ramo senza
  125. * nodi
  126. */
  127.  
  128. if (parent.getRight() == null)
  129. parent.right = actualNode;
  130. else
  131. parent.left = actualNode;
  132. } else if (actualNode != null) {
  133. if (actualNode.getRight() != null && actualNode.getLeft() == null) {
  134. left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
  135. actualNode = left;
  136. } else
  137. actualNode = parent;
  138. }
  139. }
  140. }
  141. ExpressionFactor factor = term.factors.get(i);
  142. evaluateFactor(factor, actualNode);
  143. }
  144. }
  145.  
  146. private void evaluateFactor(ExpressionFactor factor, OperationNode actualNode) {
  147. /* value */
  148. if (factor.type == 1) {
  149. if (actualNode.getRight() == null) {
  150. right = actualNode.addRight(new OperationNode(factor, currentCollection));
  151. } else if (actualNode.getLeft() == null && actualNode.getRight() != null) {
  152. left = actualNode.addLeft(new OperationNode(factor, currentCollection));
  153. }
  154. }
  155. /* fieldname */
  156. else if (factor.type == 5) {
  157. if (actualNode.getRight() == null) {
  158. right = actualNode.addRight(new OperationNode(factor, currentCollection));
  159. } else if (actualNode.getLeft() == null && actualNode.getRight() != null) {
  160. left = actualNode.addLeft(new OperationNode(factor, currentCollection));
  161. }
  162. }
  163. /* subcondition */
  164. else if (factor.type == 2) {
  165. if (actualNode.getRight() == null) {
  166. expression2Tree(factor.condition.predicate.expression1, actualNode.right, actualNode);
  167. } else if (actualNode.getLeft() == null && actualNode.getRight() != null) {
  168. expression2Tree(factor.condition.predicate.expression1, actualNode.left, actualNode);
  169. }
  170. }
  171.  
  172. }
  173.  
  174. /* metodo per castare da stringa a eval */
  175. private EOperationType string2OperatorType(String operator) {
  176. if (operator.equals("+"))
  177. return EOperationType.ADD;
  178. if (operator.equals("-"))
  179. return EOperationType.SUB;
  180. if (operator.equals("*"))
  181. return EOperationType.MUL;
  182. if (operator.equals("\\"))
  183. return EOperationType.DIV;
  184. return null;
  185. }
  186.  
  187. public void printTree() {
  188.  
  189. printTree(root, 0);
  190. }
  191.  
  192. private void printTree(OperationNode root, int level) {
  193.  
  194. if (root != null) {
  195. for (int i = 0; i < level; i++) {
  196. System.out.print("\t");
  197. }
  198. System.out.println(root.toString());
  199. printTree(root.getRight(), level + 1);
  200. printTree(root.getLeft(), level + 1);
  201.  
  202. }
  203.  
  204. }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement