/*======================================================================== Copyright (C) 2006 by Geir-Tore Lindsve, Torbjørn Meistad and Yngve Raudberget, hereby refered to as "the authors". All rights reserved Permission is hereby granted, without written agreement and without license or royalty fees, to use, reproduce, prepare derivative works, distribute, and display this software and its documentation for NONCOMMERCIAL RESEARCH AND EDUCATIONAL PURPOSES, provided that (1) the above copyright notice and the following two paragraphs appear in all copies of the source code and (2) redistributions, including without limitation binaries, reproduce these notices in the supporting documentation. IN NO EVENT SHALL THE AUTHORS, OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. ========================================================================*/ using System; using System.Collections.Generic; using System.Text; using System.Collections; using Iesi.Collections; using CLab.Data; using CLab.Exceptions; using Casper.Data; using Casper; namespace CLab.CSP { /// /// Class for making Casper's CSP representation of the expressions. /// . /// public class CSPExpressionBuilder { private CP cp; private CSPLayout layout; private CSPExpressions cspExpressions; private Symbols symbols; private Set variableSet; private ConstraintGraph constraintGraph; /// /// Initializes a new instance of the class. /// /// The layout. /// The instance for this problem. /// The instance for this problem. public CSPExpressionBuilder(CSPLayout layout, CP cp, Symbols symbols) { this.layout = layout; this.cp = cp; this.symbols = symbols; cspExpressions = new CSPExpressions(); constraintGraph = new ConstraintGraph(); } /// /// Gets the constraint graph. /// internal ConstraintGraph ConstraintGraph { get { return constraintGraph; } } /// /// Gets the CSP expressions. /// public CSPExpressions CSPExpressions { get { return cspExpressions; } } /// /// Builds the CSP expr for an expression. /// /// The expression. /// A /// public CSPExpr BuildCSPExpr(ExpressionId idexpr) { int variableID = (int)layout.CpVarToVarID[idexpr.Id]; if (symbols.RangeVariables.Contains(idexpr.Id.ToLower())) { variableSet.Add(variableID); return new CSPExprVarInt(variableID); } else if (symbols.BoolVariables.Contains(idexpr.Id.ToLower())) { variableSet.Add(variableID); return new CSPExprVarBool(variableID); } else throw new ClabInternalErrorException("Unknown variable type"); } /// /// Builds the CSP expr for an expression. /// /// The expression. /// A public CSPExpr BuildCSPExpr(ExpressionInt iExpr) { return new CSPExprValueInt(iExpr.IntegerValue); } /// /// Builds the CSP expr for an expression. /// /// The expression. /// A /// public CSPExpr BuildCSPExpr(ExpressionNotNeg nnExpr) { CSPExpr resLeft = nnExpr.Left.RunCSPBuildExpr(this); if (nnExpr.Type.Equals(Common.ExprType.et_neg)) return new CSPExprNeg(resLeft); else if (nnExpr.Type.Equals(Common.ExprType.et_not)) return new CSPExprNot(resLeft); else throw new ClabInternalErrorException("Unknown notneg type"); } /// /// Builds the CSP expr for an expression. /// /// The expression. /// A public CSPExpr BuildCSPExpr(ExpressionBinary bExpr) { CSPExpr resLeft; CSPExpr resRight; if ((bExpr.Type.Equals(Common.ExprType.et_eq) || bExpr.Type.Equals(Common.ExprType.et_ne)) && (bExpr.Left.Type.Equals(Common.ExprType.et_id) && bExpr.Right.Type.Equals(Common.ExprType.et_id))) { ExpressionId idLeft = (ExpressionId)bExpr.Left; ExpressionId idRight = (ExpressionId)bExpr.Right; String var = ""; String constant = ""; Boolean varconst = false; if (symbols.EnumerationVariables.Contains(idLeft.Id.ToLower())) { var = idLeft.Id; constant = idRight.Id; varconst = true; } else if (symbols.EnumerationVariables.Contains(idRight.Id.ToLower())) { var = idRight.Id; constant = idLeft.Id; varconst = true; } if (varconst) { int varID = (int)layout.CpVarToVarID[var]; int typeIndex = (int)layout.TypeNameToIndex[layout.VarIDToTypeName[varID]]; CSPTypeEnum te = (CSPTypeEnum)layout.Types[typeIndex]; int constantID = te.GetIntRepOfDomainString(constant); resLeft = new CSPExprVarEnum(varID); resRight = new CSPExprValueConst(constantID); variableSet.Add(varID); } else { resLeft = bExpr.Left.RunCSPBuildExpr(this); resRight = bExpr.Right.RunCSPBuildExpr(this); } } else { resLeft = bExpr.Left.RunCSPBuildExpr(this); resRight = bExpr.Right.RunCSPBuildExpr(this); } StaticData.Operators oper = ConvertOperator(bExpr.Type); return new CSPExprBin(resLeft, resRight, oper); } /// /// Builds csp expressions of all clab expressions defined in the instance. /// Also updates the Constraint graph of the problem. /// /// instance with all CSP expressions. /// public CSPExpressions BuildCSPExpressions() { List expressions = cp.Rules; for (int i = 0; i < expressions.Count; i++) { this.variableSet = new HybridSet(); try { CSPExpr exp = expressions[i].RunCSPBuildExpr(this); ConstraintGraph.UpdateAdjacencyList(this.variableSet); CSPExprWrapper wrapper = new CSPExprWrapper(exp, variableSet); cspExpressions.AddWrappedExpr(wrapper); } catch (ClabInternalErrorException) { throw; } } for (int i = 0; i < cp.Variables.Count; i++) { int varID = (int) layout.CpVarToVarID[cp.Variables[i].VariableName]; if (ConstraintGraph.CGraph[varID] == null) { ConstraintGraph.CGraph[varID] = new AdjacencyList(); } } return cspExpressions; } /// /// Builds a csp expression out of the passed in clab expression. /// Updates the constraint graph with new data from the current expression. /// /// The clab expression. /// The csp expression inside a object. /// public CSPExprWrapper BuildCSPExpression(Expression expr) { this.variableSet = new HybridSet(); try { CSPExpr cspExp = expr.RunCSPBuildExpr(this); ConstraintGraph.UpdateAdjacencyList(this.variableSet); CSPExprWrapper wrapper = new CSPExprWrapper(cspExp, variableSet); return wrapper; } catch (ClabInternalErrorException) { throw; } } /// /// Converts a clab operator to a Casper operator. . /// /// The input. /// public StaticData.Operators ConvertOperator(Common.ExprType input) { if (input.Equals(Common.ExprType.et_and)) return StaticData.Operators.and; else if (input.Equals(Common.ExprType.et_div)) return StaticData.Operators.div; else if (input.Equals(Common.ExprType.et_eq)) return StaticData.Operators.eq; else if (input.Equals(Common.ExprType.et_gt)) return StaticData.Operators.gt; else if (input.Equals(Common.ExprType.et_gte)) return StaticData.Operators.gte; else if (input.Equals(Common.ExprType.et_impl)) return StaticData.Operators.impl; else if (input.Equals(Common.ExprType.et_lt)) return StaticData.Operators.lt; else if (input.Equals(Common.ExprType.et_lte)) return StaticData.Operators.lte; else if (input.Equals(Common.ExprType.et_minus)) return StaticData.Operators.minus; else if (input.Equals(Common.ExprType.et_mod)) return StaticData.Operators.mod; else if (input.Equals(Common.ExprType.et_ne)) return StaticData.Operators.ne; else if (input.Equals(Common.ExprType.et_or)) return StaticData.Operators.or; else if (input.Equals(Common.ExprType.et_plus)) return StaticData.Operators.plus; else return StaticData.Operators.times; } } }