/*======================================================================== 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.Collections; using System.Text; using CLab.Data; using CLab.Exceptions; using Casper.Data; using Iesi.Collections; namespace CLab.CSP { /// /// Class representing and making the layout of the CSP problem. /// The class has mapping from the CLab representation to the CSP /// representation and vice versa. /// public class CSPLayout { private StringComparer stringComparer; private List varIDToCPVar; private Hashtable cpVarToVarID; private List varIDToTypeName; private Hashtable typeNameToIndex; private List varIDToCasperVarDomIndex; private CP cp; private List types; private List casperVarDoms; /// /// Initializes a new instance of the class. /// /// The instance of the problem. public CSPLayout(CP cp) { this.cp = cp; stringComparer = StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture, true); varIDToCPVar = new List(); cpVarToVarID = new Hashtable(stringComparer); varIDToTypeName = new List(); typeNameToIndex = new Hashtable(stringComparer); varIDToCasperVarDomIndex = new List(); types = new List(); casperVarDoms = new List(); } /// /// Makes the type mapping, from , to /// . This method is for internal use of this class. /// private void MakeTypeMapping() { List cpTypes = cp.Types; for (int i = 0; i < cpTypes.Count; i++) { typeNameToIndex[cpTypes[i].TypeName] = i; if (cpTypes[i].GetType() == typeof(TypeEnum)) { TypeEnum te = (TypeEnum)cpTypes[i]; CSPTypeEnum cspEnum = new CSPTypeEnum(te.TypeName); for (int j = 0; j < te.EnumeratorsList.Count; j++) { cspEnum.AddConstant(te.EnumeratorsList[j]); } this.types.Add(cspEnum); } else //TypeRange { TypeRange tr = (TypeRange)cpTypes[i]; CSPTypeRange cspRange = new CSPTypeRange(tr.TypeName, tr.StartOfRange, tr.EndOfRange); this.types.Add(cspRange); } } this.types.Add(new CSPTypeBool("bool")); typeNameToIndex["bool"] = this.types.Count - 1; } /// /// Makes the casper variables with domain values, , /// and the mapping from representation, to this representation. /// The method is for internal use of this class. /// /// List of casper variable and domain representation. public List MakeCasperVarDoms() { MakeTypeMapping(); List variables = cp.Variables; for (int i = 0; i < variables.Count; i++) { varIDToCPVar.Add(variables[i].VariableName); cpVarToVarID[variables[i].VariableName] = i; varIDToTypeName.Add(variables[i].TypeName); CSPType type = types[(int) typeNameToIndex[variables[i].TypeName]]; List casperDomVals = new List(); if (type.GetType() == typeof(CLab.CSP.CSPTypeRange)) { CSPTypeRange tr = (CSPTypeRange)type; int j; int end; if (tr.Start < tr.End) { j = tr.Start; end = tr.End; } else { j = tr.End; end = tr.Start; } for (; j <= end; j++) { casperDomVals.Add(j); } } else { //enum and bool for (int j = 0; j <= type.End; j++) { casperDomVals.Add(j); } } CasperVarDom var = new CasperVarDom(i, casperDomVals); casperVarDoms.Add(var); varIDToCasperVarDomIndex.Add(casperVarDoms.Count - 1); } return casperVarDoms; } /// /// Gets the property /// to the property mapping. /// public List VarIDToCPVar { get { return varIDToCPVar; } } /// /// Gets the property /// to the property mapping. /// public Hashtable CpVarToVarID { get { return cpVarToVarID; } } /// /// Gets the property /// to the property mapping. /// public List VarIDToTypeName { get { return varIDToTypeName; } } /// /// Gets the property to the /// index of this type mapping. /// /// The index of the type name to. public Hashtable TypeNameToIndex { get { return typeNameToIndex; } } /// /// Gets the list of CSP types. /// /// The types. public List Types { get { return types; } } /// /// Gets the variable name, (), /// from the passed inn variable ID, (). /// /// The var ID. /// public String GetCPVarFromVarID(int varID) { return varIDToCPVar[varID]; } /// /// Gets the variable ID, (), /// from the passed in variable name, (). /// /// Name of the cp var. /// public int GetVarIDFromCPVar(String cpVarName) { return (int) cpVarToVarID[cpVarName]; } /// /// Gets the domain value's string representation. /// /// Name of the CLab variable. /// The domain int value. /// The domain value's string representation. public String GetDomainStringFromDomainInt(String cpVarName, int domainInt) { String tName = GetTypeName(cpVarName); int typeIndex = (int) typeNameToIndex[tName]; CSPType type = types[typeIndex]; try { return type.GetStringRepOfDomainInt(domainInt); } catch (ClabException) { throw; } } /// /// Gets the domain value's int representation. /// /// Name of the CLab variable. /// The domain string value. /// The domain value's int representation. public int GetDomainIntFromDomainString(String cpVarName, String domainString) { String tName = GetTypeName(cpVarName); int typeIndex = (int)typeNameToIndex[tName]; CSPType type = types[typeIndex]; try { return type.GetIntRepOfDomainString(domainString); } catch (ClabException) { throw; } } /// /// Gets the name of the variable's type. /// /// The CLab variable name. /// The type name. public String GetTypeName(String varName) { int varID = (int) cpVarToVarID[varName]; return varIDToTypeName[varID]; } /// /// Gets the type format. . /// /// Name of the type. /// A constant representing the type format. public CPTypes GetTypeFormat(String typeName) { int index = (int) typeNameToIndex[typeName]; CSPType cspT = types[index]; return cspT.TypeFormat(); } /// /// Gets the index of the object with /// the name "cpVarName". /// /// Name of the Clab variable. /// The index. public int GetCasperVarDomIndex(String cpVarName) { return varIDToCasperVarDomIndex[GetVarIDFromCPVar(cpVarName)]; } /// /// Returns a that represents the current . /// /// /// A that represents the current . /// public override string ToString() { String retur = "Type mapping: \n"; IDictionaryEnumerator en = typeNameToIndex.GetEnumerator(); retur += "typeNameToIndex: "; while (en.MoveNext()) { retur += en.Key + " - " + en.Value + ", "; } retur += "\nTypes: "; for (int i = 0; i < types.Count; i++) { retur += i + " - " + types[i].TypeName + ", "; } retur += "\n\nVariable mapping: \n"; retur += "VarIDToCPVar: "; for (int i = 0; i < varIDToCPVar.Count; i++) { retur += i + " - " + varIDToCPVar[i] + ", "; } retur += "\nvarIDToTypeName: "; for (int i = 0; i < varIDToTypeName.Count; i++) { retur += i + " - " + varIDToTypeName[i] + ", "; } return retur; } } }