summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESTruthTable.java
blob: 94e7574b5b9017ef0fbdb87dd314e2862483b8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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();
	}

}