Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.fm.geco.model.comparison.model;
- import java.util.ArrayList;
- import java.util.List;
- import geco.model.util.Expression;
- import geco.model.util.ExpressionFactor;
- import geco.model.util.ExpressionTerm;
- import net.fm.geco.model.condition.tree.EBooleanType;
- import net.fm.geco.model.condition.tree.TreeNode;
- import net.fm.geco.model.value.Value;
- public class OperationTree {
- private OperationNode root;
- private String currentCollection;
- OperationNode left = null, right = null;
- OperationNode node = null;
- // OperationNode node2 = null;
- public OperationTree(String currentCollection) {
- this.root = null;
- this.currentCollection = currentCollection;
- }
- public void expression2Tree(Expression e) {
- System.out.println(e.terms.toString() + " " + e.operators.toString());
- expression2Tree(e, root, null);
- }
- // lastNode indica il nodo inserito in precedenza
- private void expression2Tree(Expression e, OperationNode actualNode, OperationNode parent) {
- for (int i = e.terms.size() - 1; i >= 0; i--) {
- String op = e.operators.get(i);
- if (op != "") {
- if (this.root == null) {
- this.root = new OperationNode(string2OperatorType(op), currentCollection);
- actualNode = this.root;
- } else
- if (parent == null) {
- if (actualNode == null) {
- actualNode = new OperationNode(string2OperatorType(op), currentCollection);
- } else if (actualNode != null) {
- if (actualNode.getRight() != null && actualNode.getLeft() == null) {
- left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
- actualNode = left;
- }
- }
- }
- /*
- * caso in cui abbiamo annidamento quindi ho parentesi ...così
- * non può funzionare perchè resto chiuso in un ramo dell'albero
- */
- else {
- if (actualNode == null) {
- actualNode = new OperationNode(string2OperatorType(op), currentCollection);
- /*
- * aggancio i nuovi nodi al parent in base al ramo senza
- * nodi
- */
- if (parent.getRight() == null)
- parent.right = actualNode;
- else
- parent.left = actualNode;
- } else if (actualNode != null) {
- if (actualNode.getRight() != null && actualNode.getLeft() == null) {
- left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
- actualNode = left;
- } else
- actualNode = parent;
- }
- }
- }
- ExpressionTerm term = e.terms.get(i);
- evaluateTerm(term, actualNode, parent);
- }
- }
- private void evaluateTerm(ExpressionTerm term, OperationNode actualNode, OperationNode parent) {
- for (int i = term.factors.size() - 1; i >= 0; i--) {
- String op = term.operators.get(i); // il primo è nullo
- if (op != "") {
- if (this.root == null) {
- this.root = new OperationNode(string2OperatorType(op), currentCollection);
- actualNode = this.root;
- } else
- if (parent == null) {
- if (actualNode == null) {
- actualNode = new OperationNode(string2OperatorType(op), currentCollection);
- } else if (actualNode != null) {
- if (actualNode.getRight() != null && actualNode.getLeft() == null) {
- left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
- actualNode = left;
- }
- }
- }
- /*
- * caso in cui abbiamo annidamento quindi ho parentesi ...così
- * non può funzionare perchè resto chiuso in un ramo dell'albero
- */
- else {
- if (actualNode == null) {
- actualNode = new OperationNode(string2OperatorType(op), currentCollection);
- /*
- * aggancio i nuovi nodi al parent in base al ramo senza
- * nodi
- */
- if (parent.getRight() == null)
- parent.right = actualNode;
- else
- parent.left = actualNode;
- } else if (actualNode != null) {
- if (actualNode.getRight() != null && actualNode.getLeft() == null) {
- left = actualNode.addLeft(new OperationNode(string2OperatorType(op), currentCollection));
- actualNode = left;
- } else
- actualNode = parent;
- }
- }
- }
- ExpressionFactor factor = term.factors.get(i);
- evaluateFactor(factor, actualNode);
- }
- }
- private void evaluateFactor(ExpressionFactor factor, OperationNode actualNode) {
- /* value */
- if (factor.type == 1) {
- if (actualNode.getRight() == null) {
- right = actualNode.addRight(new OperationNode(factor, currentCollection));
- } else if (actualNode.getLeft() == null && actualNode.getRight() != null) {
- left = actualNode.addLeft(new OperationNode(factor, currentCollection));
- }
- }
- /* fieldname */
- else if (factor.type == 5) {
- if (actualNode.getRight() == null) {
- right = actualNode.addRight(new OperationNode(factor, currentCollection));
- } else if (actualNode.getLeft() == null && actualNode.getRight() != null) {
- left = actualNode.addLeft(new OperationNode(factor, currentCollection));
- }
- }
- /* subcondition */
- else if (factor.type == 2) {
- if (actualNode.getRight() == null) {
- expression2Tree(factor.condition.predicate.expression1, actualNode.right, actualNode);
- } else if (actualNode.getLeft() == null && actualNode.getRight() != null) {
- expression2Tree(factor.condition.predicate.expression1, actualNode.left, actualNode);
- }
- }
- }
- /* metodo per castare da stringa a eval */
- private EOperationType string2OperatorType(String operator) {
- if (operator.equals("+"))
- return EOperationType.ADD;
- if (operator.equals("-"))
- return EOperationType.SUB;
- if (operator.equals("*"))
- return EOperationType.MUL;
- if (operator.equals("\\"))
- return EOperationType.DIV;
- return null;
- }
- public void printTree() {
- printTree(root, 0);
- }
- private void printTree(OperationNode root, int level) {
- if (root != null) {
- for (int i = 0; i < level; i++) {
- System.out.print("\t");
- }
- System.out.println(root.toString());
- printTree(root.getRight(), level + 1);
- printTree(root.getLeft(), level + 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement