package fr.lifl.stc.stan.implicitFlow.data; import java.util.HashSet; import java.util.Iterator; import java.util.Vector; import fr.lifl.stc.stan.implicitFlow.QuineMcCluskey.Minimization; /** * * @author dorina * */ public class BooleanExpression { //vector if Minterm HashSet minterms; public static BooleanExpression expression_true = new BooleanExpression(BooleanVariable.condition_true); public static BooleanExpression expression_false = new BooleanExpression(BooleanVariable.condition_false); /** * creates a boolean expression and initializes it with 0 * */ public BooleanExpression(){ minterms = new HashSet(); Minterm tmp = new Minterm(BooleanVariable.condition_false); minterms.add(tmp); } /** * * @param b */ public BooleanExpression(BooleanVariable b){ minterms = new HashSet(); minterms.add(new Minterm(b)); } /** * * @param b */ public BooleanExpression(BooleanExpression b) { minterms = new HashSet(); Iterator it = b.minterms.iterator(); while(it.hasNext()) { Minterm tmp = it.next(); minterms.add(new Minterm(tmp)); } } /** * * @return number of minterms in expression */ public int size(){ return minterms.size(); } /** * * @return */ public Minterm[] getMinterms(){ Minterm[] tmp = new Minterm[this.minterms.size()]; Iterator it = this.minterms.iterator(); int i = 0; while(it.hasNext()) { tmp[i++] = it.next(); } return tmp; } /** * * @param b */ /* public void and(BooleanExpression b){ this.and(b, true); } */ /** * * @param b * @param simplify */ public void and(BooleanExpression b, boolean simplify){ //FIXME BooleanExpression result = new BooleanExpression(); Iterator it = this.minterms.iterator(); Iterator aux; while(it.hasNext()) { aux = b.minterms.iterator(); Minterm current = it.next(); while(aux.hasNext()) { Minterm tmp = new Minterm(current); tmp.and(aux.next()); result.or(tmp, false); } } //FIXME do we need this ??? if(simplify) result.simplify(); this.minterms.clear(); it = result.minterms.iterator(); while(it.hasNext()) { this.minterms.add(it.next()); } } /** * * @param b */ /* public void or(BooleanExpression b){ this.or(b, true); } */ public void or(BooleanExpression b, boolean simplify){ if(this.isTrue()) return; Iterator it = b.minterms.iterator(); while(it.hasNext()) { Minterm tmp = it.next(); this.or(tmp, false); } if(simplify) simplify(); } /** * * @param b */ public void or(BooleanVariable b){ or(new Minterm(b), true); } /** * * @param m */ /* public void or(Minterm m){ this.or(m, true); } */ /** * * @param m * @param simplify */ public void or(Minterm m, boolean simplify){ if(this.isTrue()) return; //check if it's 0 if(m.size()==0 || m.isFalse()) return; //check if it's 1 if(this.isFalse() || m.isTrue()) { minterms.clear(); minterms.add(new Minterm(m)); return; } //check if already exists Iterator it = minterms.iterator(); while(it.hasNext()) { Minterm tmp = it.next(); if(tmp.greaterOrEqualTo(m)) return; } minterms.add(new Minterm(m)); if(simplify) simplify(); } /** * * @param b * @return */ public boolean greaterThan(BooleanExpression b){ //FIXME if(this.isTrue()) { return !b.isTrue(); } if(this.isFalse()){ return !b.isFalse(); } if(this.equals(b)) return false; BooleanExpression tmp = new BooleanExpression(this); tmp.or(b, false); return this.equals(tmp); } /** * */ public boolean equals(Object obj){ if(!(obj instanceof BooleanExpression)) return false; BooleanExpression b = (BooleanExpression)obj; if(this.size() != b.size()) return false; Minterm m[]=b.getMinterms(); boolean bool[] = new boolean[m.length]; Iterator it = minterms.iterator(); while(it.hasNext()) { Minterm current = it.next(); boolean found = false; for(int i = 0 ; i < m.length; i++) { if((!bool[i]) && current.equals(m[i])){ bool[i] = true; found = true; break; } } if(!found) return false; } for(int i = 0 ; i < m.length; i++) { if(!bool[i]) return false; } return true; } /** * * @return */ public boolean isTrue(){ if(size() != 1) return false; Minterm m = (Minterm)minterms.toArray()[0]; return m.isTrue(); } /** * * @return */ public boolean isFalse(){ if(size()!=1) return false; Minterm m = (Minterm)minterms.toArray()[0]; return m.isFalse(); } /** * * */ public void simplify(){ if(this.isFalse() || this.isTrue() || this.size()==0) return; //System.err.println("must simplify"+this+" "+this.size()); //FIXME Minimization m = new Minimization(this); m.minimize(); Minterm[] terms = m.getEssentialMinterms(); this.minterms.clear(); for(int i = 0; i < terms.length; i++) this.minterms.add(terms[i]); //System.err.println("after simplify"+this+" "+this.size()); } /** * */ public String toString(){ String s = "";//" size = "+this.minterms.size()+" "; Iterator it = this.minterms.iterator(); while(it.hasNext()) { Minterm tmp = it.next(); s += tmp; if(it.hasNext()) s+= " + "; } return s; } public Vector getDistinctBooleanVariables(){ Vector v = new Vector(); Iterator it = this.minterms.iterator(); while(it.hasNext()) { Minterm tmp = it.next(); for(int i = 0; i < tmp.size(); i++) { BooleanVariable b = (BooleanVariable)tmp.terms.elementAt(i); b = b.getAfirmation(); if(!v.contains(b)) insertVariableInOrderedVector(v, b); } } return v; } private void insertVariableInOrderedVector(Vector v, BooleanVariable b) { int i = 0; for(; i < v.size(); i++) { if( b.compareTo(v.elementAt(i)) == -1 ) break; } v.add(i,b); } }