package data; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import logic.IterativeBruteForceSolver; import output.XMLizable; import generated.*; /** * class to represent a raw material * * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class RawMaterial implements XMLizable { generated.RawMaterial innerRawMaterial; /** * default constructor */ public RawMaterial() { this(new generated.RawMaterial()); setConstraints( new ArrayList() ); } public RawMaterial(generated.RawMaterial innerRawMaterial) { this.innerRawMaterial = innerRawMaterial; } /*======================================================================== | getters and setters +-----------------------------------------------------------------------*/ /** * get the inner representation of RawMaterial * * @return generated.RawMaterial : Inner representation of RawMaterial */ public generated.RawMaterial getInnerRawMaterial() { return innerRawMaterial; } /** * set the inner representation of the RawMaterial * * @param pType : RawMaterialType */ public void setType( generated.RawMaterial rawMaterial ) { this.innerRawMaterial = rawMaterial; } /** * get the constraints of this RawMaterial * * @return ArrayList of type Constraint */ public ArrayList getConstraints() { ArrayList constraints = new ArrayList(); Enumeration constraintList = this.innerRawMaterial.get_constraint_list().elements(); while(constraintList.hasMoreElements()) { constraints.add(new Constraint((generated.Constraint)constraintList.nextElement())); } return constraints; } /** * set the RawMaterial clauses to the given ArrayList * * @param pConstraints ArrayList of Constraint */ public void setConstraints ( ArrayList pConstraints ) { this.innerRawMaterial.set_constraint_list(new Constraint_List()); for(int i = 0; i < pConstraints.size(); i++) { this.innerRawMaterial.get_constraint_list().addElement(pConstraints.get(i).getInnerConstraint()); } } /*----------------------------------------------------------------------- | end getters and setters +======================================================================*/ /** * add a constraint to the list of constraint objects * * @param pConstraint : constraint to be added */ public void addConstraint( Constraint pConstraint ) { if(this.innerRawMaterial == null) this.innerRawMaterial = new generated.RawMaterial(); if(this.innerRawMaterial.get_constraint_list() == null) this.innerRawMaterial.set_constraint_list(new generated.Constraint_List()); this.innerRawMaterial.get_constraint_list().addElement(pConstraint.getInnerConstraint()); } /** * determine whether any free variables exist in this raw material * * @param pFinishedProduct : finished product to check raw material against * @return boolean */ public boolean anyFreeVariables( FinishedProduct pFinishedProduct ) { String firstVar = findFirstFreeVariable( pFinishedProduct ); return ( firstVar.compareTo( "" ) != 0 ); } /** * find the first free variable in this raw material * * @param pFinishedProduct : finished product to check raw material against * @return String : the actual variable that needs to be added to the finished product */ public String findFirstFreeVariable( FinishedProduct pFinishedProduct ) { boolean foundFreeVariable = false; String theFreeVariable = ""; Iterator contstraintIterator = getConstraints().iterator(); // iterate through clauses in this raw material while( contstraintIterator.hasNext() && !foundFreeVariable ) { Constraint rawMaterialClause = contstraintIterator.next(); Iterator literalsIterator = rawMaterialClause.getVariableList().iterator(); // if any variable in this clause is not in finished product, set foundFreeVariable to true while ( literalsIterator.hasNext() && !foundFreeVariable ) { Variable currentLiteral = literalsIterator.next(); String currentLiteralVar = currentLiteral.getVariableName(); if ( !pFinishedProduct.containsVariable( currentLiteralVar ) ) { foundFreeVariable = true; theFreeVariable = currentLiteralVar; } } } return theFreeVariable; } /** * generate the xml representation of a raw material * * @return String */ public String generateXML() { Iterator constraintIterator = getConstraints().iterator(); String ret = new String( "\n" ); while( constraintIterator.hasNext() ) { Constraint tmp = constraintIterator.next(); ret = ret.concat( tmp.generateXML() ); } ret = ret.concat( "\n" ); return ret; } /** * print out the raw material values to System.out */ public void print() { //Do not print Raw Material } /** * generate best assignment based on this raw material * * @return FinishedProduct */ public FinishedProduct generateFinishedProduct() { IterativeBruteForceSolver s = new IterativeBruteForceSolver(this); FinishedProduct bestAssignment = s.getBestAssignment(); return bestAssignment; } }