summaryrefslogtreecommitdiffstats
path: root/org.eclipse.cdt.codan.extension/src/org/eclipse/cdt/codan/extension/ESSimplifier.java
blob: 9e3ed5fe020629c28049c6b3a9d56b71fa527424 (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
87
88
89
90
91
92
93
94
/*******************************************************************************
 * 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<IVariable> {
	private Map<ExecutionState, Minterm<IVariable>> esMapping;

	public ESSimplifier(ESTruthTable tt) {
		super(tt);
		esMapping = new HashMap<ExecutionState, Minterm<IVariable>>();
	}

	public Set<ExecutionState> getExecutionStateCover() {
		Set<ExecutionState> ret = new HashSet<ExecutionState>();
		List<Minterm<IVariable>> implicantCover = getImplicantCover();
		for (Minterm<IVariable> 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<Minterm<IVariable>> implied = getImplications(term);
			for (Minterm<IVariable> minterm : implied) {
				ExecutionState e = ((ESTruthTable) truthTable).getExecutionState(minterm);
				es.getTruthAssignments().putAll(e.getTruthAssignments());
			}
			ret.add(es);
			esMapping.put(es, term);
		}
		return ret;
	}
	
	public Set<ExecutionState> getImplications(ExecutionState primeImplicant) {
		Set<ExecutionState> ret = new HashSet<ExecutionState>();
		Minterm<IVariable> piTerm = esMapping.get(primeImplicant);
		List<Minterm<IVariable>> impls = getImplications(piTerm);
		for (Minterm<IVariable> 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<IVariable> 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();
	}
}