/******************************************************************************* * Copyright (c) 2009 Elliott Baron * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Elliott Baron - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.codan.extension; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.eclipse.cdt.codan.extension.Minterm.Value; import org.eclipse.cdt.core.dom.ast.IVariable; public class ESSimplifier extends QuineMcCluskeySimplifier { private Map> esMapping; public ESSimplifier(ESTruthTable tt) { super(tt); esMapping = new HashMap>(); } public Set getExecutionStateCover() { Set ret = new HashSet(); List> implicantCover = getImplicantCover(); for (Minterm term : implicantCover) { ExecutionState es = new ExecutionState(); for (IVariable var : truthTable.getVariables()) { Value val = term.getValue(var); if (val == Value.TRUE) { es.addClause(new ExecutionStateClause(var, true)); } else if (val == Value.FALSE) { es.addClause(new ExecutionStateClause(var, false)); } // Don't care about Don't cares } // Add truth assignments from implied minterms List> implied = getImplications(term); for (Minterm minterm : implied) { ExecutionState e = ((ESTruthTable) truthTable).getExecutionState(minterm); es.getTruthAssignments().putAll(e.getTruthAssignments()); } ret.add(es); esMapping.put(es, term); } return ret; } public Set getImplications(ExecutionState primeImplicant) { Set ret = new HashSet(); Minterm piTerm = esMapping.get(primeImplicant); List> impls = getImplications(piTerm); for (Minterm term : impls) { ExecutionState es = ((ESTruthTable) truthTable).getExecutionState(term); ret.add(es); } return ret; } @Override public String toString() { StringBuffer buf = new StringBuffer(); for (IVariable var : truthTable.getVariables()) { buf.append(var.getName()); buf.append(" "); } buf.append("\n"); for (Minterm term : truthTable.getMinterms()) { for (IVariable var : truthTable.getVariables()) { switch (term.getValue(var)) { case TRUE: buf.append("1"); break; case FALSE: buf.append("0"); break; default: buf.append("-"); } buf.append(" "); } buf.append("\n"); } return buf.toString(); } }