/*======================================================================== 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.IO; using CLab; using CLab.Exceptions; using CLab.CSP; using CLab.Parsers; using Casper; using Casper.Exceptions; using buddy_sharp; namespace ClabTest { /// /// This is an example of how to use the CLab# 1.0 library for solving configuration problems. /// We provide sample code for choosing either CSP or BDD approach, as well showing how the user /// amy manually add constraints and extra rules to the CP file, and simulate the configuration /// process by setting one value at a time. /// class ClabTestCSP { static void Main(string[] args) { ClabTestCSP ctCSP = new ClabTestCSP(); String xmlFile = "..\\..\\..\\configuration_files\\printerExample.xml"; CLabTextParser textParser = new CLabTextParser(); Console.WriteLine("Starting Clab#..."); try { Clab clab = new Clab(xmlFile); //Using BDD: clab.InitializeProblemSolver(CLabModus.bdd_modus); //Or using CSP, just uncomment the line below: //clab.InitializeProblemSolver(CLabModus.csp_modus); //BDD compilation methods: clab.SetBddCompileMethod(BddCompileMethod.cm_ascending); //clab.SetBddCompileMethod(BddCompileMethod.cm_static); //clab.SetBddCompileMethod(BddCompileMethod.cm_dynamic); //Only experiment with the following properites if the BDDs are building too slowly //due to the problem size. //clab.Maxincrease = 100000; //clab.Initbddnodes = 200000; //clab.Initdbcache = 5000; //Setting the variable order for CSP search //Minimum width order: clab.SetCSPVariableOrdering(CLab.CspVariableOrdering.vo_minwidth); //For static variable order, uncomment the following line: //clab.SetCSPVariableOrdering(CLab.CspVariableOrdering.vo_static); //This line prints out the progress of the search clab.StatusEvent += new Clab.StatusEventHandler(ctCSP.PrintStatus); //Adding the rule ' "User" == "Visitor" ' manually can be done like this: CLab.Data.ExpressionBinary binaryRule = new CLab.Data.ExpressionBinary( new CLab.Data.ExpressionId("User"), Common.ExprType.et_eq, new CLab.Data.ExpressionId("Visitor")); //Adding the created rule to the CP-structure: clab.Cp.AddRule(binaryRule); //Start the Valid Domains Computation, independent of using CSP or BDD approach ValidDomains vd = clab.GetValidDomains(); //Print out contents of the CP object //Prints header if (!clab.Cp.Description.Equals("") | !clab.Cp.Author.Equals("") | !clab.Cp.Date.Equals("")) { Console.WriteLine("*****"); if (!clab.Cp.Description.Equals("")) { Console.WriteLine("* Description:\t{0}", clab.Cp.Description); } if (!clab.Cp.Author.Equals("")) { Console.WriteLine("* Author:\t{0}", clab.Cp.Author); } if (!clab.Cp.Date.Equals("")) { Console.WriteLine("* Date:\t\t{0}", clab.Cp.Date); } Console.WriteLine("*****"); } //Print types Console.WriteLine("\n*** TYPES ***\n"); for (int i = 0; i < clab.Cp.Types.Count; i++) { Console.WriteLine(clab.Cp.Types[i]); } //Print variables Console.WriteLine("\n*** VARIABLES ***\n"); for (int i = 0; i < clab.Cp.Variables.Count; i++) { Console.WriteLine(clab.Cp.Variables[i]); } //Print rules Console.WriteLine("\n*** RULES ***\n"); for (int i = 0; i < clab.Cp.Rules.Count; i++) { Console.WriteLine(clab.Cp.Rules[i]); } Console.WriteLine("\n\nValid domains after computation:"); for(int i = 0; i < vd.ValidDoms.Count;i++) { ValidDomain v = (ValidDomain)vd.ValidDoms[i]; Console.Write("\n"+v.VarName + ":"); for(int j = 0; j < v.Domains.Count; j++) { Console.Write(" " + v.Domains[j]); } } Console.WriteLine("\n\n"); Console.WriteLine("\n\nValid domains after adding user rule \"Papersize\" == \"A4\":\n"); //One can manually reduce a domain to become a single value, in this case we set //"Papersize == A4": //This will not be added to the CP.Rules-list, it works directly on //reducing the domain of the variabl to become Papersize = {A4}. //This simulates the interactive selection during a configuration session. vd = clab.GetValidDomainsExtraRule("Papersize", "A4"); for (int i = 0; i < vd.ValidDoms.Count; i++) { ValidDomain v = (ValidDomain)vd.ValidDoms[i]; Console.Write("\n" + v.VarName + ":"); for (int j = 0; j < v.Domains.Count; j++) { Console.Write(" " + v.Domains[j]); } } Console.WriteLine(""); } catch (ClabException e) { Console.WriteLine(e.Message); if (true) //Set to false to disable detailed printout. { Console.WriteLine("\n\nDetailed printout of exception:\n"); Exception inner = e.InnerException; while (inner.InnerException != null) inner = inner.InnerException; Console.WriteLine(e); } } catch (CasperException e) { Console.WriteLine(e.Message); if (true && e.InnerException != null) //Set to false to disable detailed printout. { Console.WriteLine("\n\nDetailed printout of exception:\n"); Exception inner = e.InnerException; while (inner.InnerException != null) inner = inner.InnerException; Console.WriteLine(e); } } } public void PrintStatus(int var) { Console.WriteLine("Currently checked variable: " + var); } } }