package fr.lifl.stc.stan.implicitFlow.QuineMcCluskey; import fr.lifl.stc.stan.implicitFlow.data.BooleanVariable; import fr.lifl.stc.stan.implicitFlow.data.Minterm; import java.util.Vector; /** * * @author dorina * @date december 2005 * */ public class Implicant { private int implicant; private int mask; private boolean reductible; /** * * @param implicant * @param mask */ public Implicant(int implicant, int mask) { this.implicant = implicant; this.mask = mask; reductible = false; } /** * * @param m * @param v */ public Implicant(Minterm m, Vector v) { int i = 0; BooleanVariable tmp; for(; i < v.size(); i++) { implicant <<= 1; mask <<= 1; tmp = v.elementAt(i); if(m.contains(tmp) ){ implicant |= 1; mask |= 1; } else if(m.contains(tmp.getNegation())) { mask |= 1; } } reductible = false; } /** * the implicant resulting from two adjacent implicants * @param i * @param j */ public Implicant(Implicant i, Implicant j){ this.mask = (i.getImplicant() ^ j.getImplicant()) ^ i.getMask(); this.implicant = i.getImplicant() & j.getImplicant(); this.reductible = false; } /** * ab-dc * @param n total number of variables * @return the number of variables on which the implicant it does not depend * (the number of - ) * */ public int getSize(int n){ int i = 0; int tmp = mask; while(tmp!=0) { if(tmp%2 == 1) i++; tmp = tmp/2; } return n-i; } /** * * @return */ public int getNumberTrueVariables(){ int i = 0; int tmp = implicant & mask; while(tmp!=0) { if(tmp%2 == 1) i++; tmp = tmp/2; } return i; } /** * * @return */ public int getMask() { return this.mask; } /** * * @return */ public int getImplicant(){ return this.implicant; } /** * */ public boolean equals(Object o) { if(!(o instanceof Implicant)) return false; Implicant tmp = (Implicant)o; return (this.getMask()== tmp.getMask() && this.getImplicant() == tmp.getImplicant()); } /** * * @return */ public boolean isReductible(){ return reductible; } /** * * */ public void setReductibleEnabled() { this.reductible = true; } /** * * @param impl * @return */ public boolean isAdjacent(Implicant impl){ if(this.getMask() != impl.getMask()) return false; int x = this.getImplicant(); int y = this.getImplicant(); return ( (( (x&y) ==x) && ((x|y)==y)) || (((x&y)==y) && ((x|y)==x))); } /** * * @param i * @return true if this covers i */ public boolean covers(Implicant i){ if((this.mask | i.getMask()) != i.getMask()) return false; //return (((this.implicant | i.getImplicant())&this.getMask()) == this.implicant); return (this.implicant & this.mask) == ( i.getImplicant() & this.mask); } /** * * @param v * @return */ public Minterm toMinterm(Vector v){ Minterm m = new Minterm(); if(this.mask == 0) { m.insertOnTop(BooleanVariable.condition_true); return m; } int pos = v.size()-1; int impl = this.implicant; int msk = this.mask; while(msk!=0 && pos>=0) { if(msk%2 == 1) { BooleanVariable tmp = v.elementAt(pos); if(impl %2 == 0) tmp = tmp.getNegation(); m.insertOnTop(tmp); } msk /=2; impl /=2; pos--; } return m; } public String toString(){ String s = ""; s = Integer.toBinaryString(this.implicant); String m= Integer.toBinaryString(this.mask); //for(int ) //FIXME return "("+s+", "+m+")"; } }