summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESTruthTable.java
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESTruthTable.java')
-rw-r--r--org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESTruthTable.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESTruthTable.java b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESTruthTable.java
new file mode 100644
index 0000000..94e7574
--- /dev/null
+++ b/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESTruthTable.java
@@ -0,0 +1,86 @@
+/*******************************************************************************
+ * 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<IVariable> {
+ private List<Minterm<IVariable>> minterms;
+ private List<IVariable> variables;
+
+ public ESTruthTable(Set<ExecutionState> es) {
+ minterms = new ArrayList<Minterm<IVariable>>();
+ variables = new ArrayList<IVariable>();
+
+ 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<IVariable, Value> values = new HashMap<IVariable, Value>();
+ for (IVariable var : variables) {
+ values.put(var, Value.DONTCARE);
+ }
+ minterms.add(new Minterm<IVariable>(values));
+ }
+
+ // Set actual truth values
+ Iterator<ExecutionState> it = es.iterator();
+ for (int i = 0; i < es.size(); i++) {
+ Minterm<IVariable> term = minterms.get(i);
+ ExecutionState e = it.next();
+ for (ExecutionStateClause c : e.getClauses()) {
+ term.setValue(c.getVariable(), c.isTrue() ? Value.TRUE : Value.FALSE);
+ }
+ }
+ }
+
+ public List<Minterm<IVariable>> getMinterms() {
+ return minterms;
+ }
+
+ public List<IVariable> getVariables() {
+ return variables;
+ }
+
+ @Override
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ for (IVariable var : variables) {
+ buf.append(var.getName());
+ buf.append(" ");
+ }
+ buf.append("\n");
+ for (Minterm<IVariable> term : minterms) {
+ for (IVariable var : variables) {
+ buf.append(term.getValue(var));
+ buf.append(" ");
+ }
+ buf.append("\n");
+ }
+ return buf.toString();
+ }
+
+}