1 define(['require', 'Collections'],
  2 // module definition function
  3 // dependencies (foo and bar) are mapped to function parameters
  4 function(require, Collection) {
  5 
  6 	/**
  7 	 * @description A Helper class to generate the Expression tree in the context of parsing and evaluation of the Expressions.
  8 	 * @name ExpressionTreeHelper
  9 	 * @class ExpressionTreeHelper
 10 	 * @example require('ExpressionTreeHelper')
 11 	 * @exports ExpressionTreeHelper
 12 	 * @version 1.0
 13 	 * @module ExpressionTreeHelper
 14 	 * @augments Collections
 15 	 **/
 16 	var ExpressionTreeHelper = {
 17 		/**
 18 		 * @name getProgramBodyParts
 19 		 * @memberOf ExpressionTreeHelper
 20 		 * @method 
 21 		 * @description It iterates through the program and partition the object type into different Lists.and Method returns a HashMap of factList, constrEqList, constrRelList
 22 		 * @param {programs} the program object parsed by the Jison parser
 23 		 * @return {HashMap} A Map of list of the different items like Operation Equal, Binary Relation and Facts   
 24 		 * @function
 25 		 */
 26 		getProgramBodyParts : function(program) {
 27 			var bodyItems = program.getBody();
 28 			var factList = Collection.ArrayList();
 29 			var negFactList = Collection.ArrayList();
 30 			var constrEqList = Collection.ArrayList();
 31 			var constrRelList = Collection.ArrayList();
 32 			for (var i = 0; bodyItems && i < bodyItems.length; i++) {
 33 				if (bodyItems[i].type() == ObjectType.FACT) {
 34 					/*if(bodyItems[i].isNegative())
 35 					 negFactList.add(bodyItems[i]);
 36 					 else*/
 37 					factList.add(bodyItems[i]);
 38 					// Fact is a Range Object with Type FACT.
 39 				} else if (bodyItems[i].type() == ObjectType.OPER_EQUAL)
 40 					constrEqList.add(bodyItems[i]);
 41 				else if (bodyItems[i].type() == ObjectType.BIN_REL)
 42 					constrRelList.add(bodyItems[i]);
 43 			}
 44 			var bodyParts = new Collection.HashMap();
 45 			bodyParts.put(ObjectType.FACT_LIST, factList);
 46 			bodyParts.put(ObjectType.NEG_FACT_LIST, negFactList);
 47 			bodyParts.put(ObjectType.CONSTR_EQ_LIST, constrEqList);
 48 			bodyParts.put(ObjectType.BIN_REL_LIST, constrRelList);
 49 			return bodyParts;
 50 		},
 51 
 52 		/**
 53 		 * @name getConstrOpVariables
 54 		 * @memberOf ExpressionTreeHelper
 55 		 * @method 
 56 		 * @description It evaluates the ConstrOp and adds the variables to the set provided.
 57 		 * @param {ConstrOp} the ConstrOp Object
 58 		 * @param {HashSet} constrOpVariables Set of Objects, It will keep add to the existing set.
 59 		 * @return {HashSet} A Set contains all the ConstrOp varibles.   
 60 		 * @function
 61 		 */
 62 		getConstrOpVariables : function(ConstrOp, constrOpVariables) {
 63 			if (ConstrOp.type() == ObjectType.BIN_OP) {//11
 64 				var binOpLHS = ConstrOp.getLhs();
 65 				if (binOpLHS.type() == ObjectType.EXP_VAR)
 66 					constrOpVariables.add(binOpLHS.value());
 67 				this.getConstrOpVariables(ConstrOp.getRhs(), constrOpVariables);
 68 			} else if (ConstrOp.type() == ObjectType.EXP_VAR) {
 69 				constrOpVariables.add(ConstrOp.value());
 70 			}
 71 			return constrOpVariables;
 72 		},
 73 		/**
 74 		 * @name getConstrEqRHSVariables
 75 		 * @memberOf ExpressionTreeHelper
 76 		 * @method 
 77 		 * @description It evaluates the ConstrOp and Adds the Variable in the ConstrOp to the Set - constrOpVariables and returns the Set.
 78 		 * @param {ConstrEq} the ConstrOp Object
 79 		 * @param {HashSet} constrEqVariables Set of Objects, It will keep add to the existing set.
 80 		 * @return {HashSet} A Set contains all the ConstrEq varibles.   
 81 		 * @function
 82 		 */
 83 		getConstrEqRHSVariables : function(ConstrEq, constrEqVariables) {
 84 			var rhs = ConstrEq.getRhs();
 85 			if (rhs.type() == ObjectType.BIN_OP) {//11
 86 				var binOpLHS = rhs.getLhs();
 87 				if (binOpLHS.type() == ObjectType.EXP_VAR)
 88 					constrEqVariables.add(binOpLHS.value());
 89 				this.getConstrOpVariables(rhs.getRhs(), constrEqVariables);
 90 			} else if (rhs.type() == ObjectType.EXP_VAR) {
 91 				constrEqVariables.add(rhs.value());
 92 			}
 93 			return constrEqVariables;
 94 		},
 95 		/**
 96 		 * @name getConstrEqRHSVariables
 97 		 * @memberOf ExpressionTreeHelper
 98 		 * @method 
 99 		 * @description It evaluates the Binary Relation and Returns the Set-binRelVariables of Variable Names used in the Binary Relation.
100 		 * @param {BinRel} the Binary Relation Object
101 		 * @param {HashSet} binRelVariables Set of Objects, It will keep add to the existing set.
102 		 * @return {HashSet} A Set contains all the ConstrEq varibles.   
103 		 * @function
104 		 */
105 		getBinRelVariables : function(binRel, binRelVariables) {
106 			binRelVariables = this.getConstrOpVariables(binRel.getLhs(), binRelVariables);
107 			binRelVariables = this.getConstrOpVariables(binRel.getRhs(), binRelVariables);
108 			return binRelVariables;
109 		}
110 	};
111 	return ExpressionTreeHelper;
112 
113 });