/******************************************************************************* * 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.ArrayList; import java.util.HashMap; import java.util.Iterator; 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 ESTruthTable implements ITruthTable { private List> minterms; private Map, ExecutionState> esMapping; private List variables; public ESTruthTable(Set es) { minterms = new ArrayList>(); variables = new ArrayList(); esMapping = new HashMap, ExecutionState>(); for (ExecutionState e : es) { for (ExecutionStateClause c : e.getClauses()) { IVariable var = c.getVariable(); if (!variables.contains(var)) { variables.add(var); } } } // Initialize with Don't Cares for (int i = 0; i < es.size(); i++) { Map values = new HashMap(); for (IVariable var : variables) { values.put(var, Value.DONTCARE); } minterms.add(new Minterm(values)); } // Set actual truth values Iterator it = es.iterator(); for (int i = 0; i < es.size(); i++) { Minterm term = minterms.get(i); ExecutionState e = it.next(); for (ExecutionStateClause c : e.getClauses()) { term.setValue(c.getVariable(), c.isTrue() ? Value.TRUE : Value.FALSE); } esMapping.put(term, e); } } public List> getMinterms() { return minterms; } public List getVariables() { return variables; } public ExecutionState getExecutionState(Minterm minterm) { return esMapping.get(minterm); } @Override public String toString() { StringBuffer buf = new StringBuffer(); for (IVariable var : variables) { buf.append(var.getName()); buf.append(" "); } buf.append("\n"); for (Minterm term : minterms) { for (IVariable var : variables) { buf.append(term.getValue(var)); buf.append(" "); } buf.append("\n"); } return buf.toString(); } }