use of de.neemann.digital.analyse.expression.ContextFiller in project Digital by hneemann.
the class IndependentCheckerTest method testRemoveVar.
public void testRemoveVar() {
Expression ex = and(a, b, c);
ContextFiller cf = new ContextFiller(a, b, c, d);
BoolTableExpression bte = new BoolTableExpression(ex, cf);
IndependentChecker ic = new IndependentChecker(bte);
BoolTable btr = ic.removeVar(3);
assertEquals(8, btr.size());
for (int i = 0; i < 7; i++) assertEquals(ThreeStateValue.zero, btr.get(i));
assertEquals(ThreeStateValue.one, btr.get(7));
}
use of de.neemann.digital.analyse.expression.ContextFiller in project Digital by hneemann.
the class MinimizerRegressionTest method performTestCalculation.
/**
* Generates a expression from the table and then checks if
* the expression reproduces the given table.
* Does not test if the expression is minimal.
*
* @param n the number of variables
* @param tab the truth table
* @throws ExpressionException
*/
private static void performTestCalculation(int n, byte[] tab, MinimizerInterface minimizer) throws ExpressionException, FormatterException {
ArrayList<Variable> v = vars(n);
final ExpressionListenerStore listener = new ExpressionListenerStore(null);
minimizer.minimize(v, new BoolTableByteArray(tab), "Y", listener);
Expression e = listener.getFirst();
assertNotNull(e);
ContextFiller context = new ContextFiller(v);
for (int i = 0; i < tab.length; i++) {
if (tab[i] <= 1)
assertEquals(tab[i] == 1, e.calculate(context.setContextTo(i)));
}
}
use of de.neemann.digital.analyse.expression.ContextFiller in project Digital by hneemann.
the class TruthTableTest method testGetByContext.
public void testGetByContext() throws Exception {
ArrayList<Variable> vars = Variable.vars(5);
TruthTable t = new TruthTable(vars).addResult();
BoolTableByteArray result = (BoolTableByteArray) t.getResult(0);
for (int i = 0; i < t.getRows(); i++) {
result.set(i, i % 3);
}
ContextFiller fc = new ContextFiller(vars);
for (int i = 0; i < t.getRows(); i++) {
fc.setContextTo(i);
assertEquals(i % 3, t.getByContext(0, fc));
}
}
use of de.neemann.digital.analyse.expression.ContextFiller in project Digital by hneemann.
the class BruteForceGetAllTest method performTestCalculation.
/*
public void testFull() throws ExpressionException, FormatterException {
new FullVariantDontCareCreator(4) {
@Override
public void handleTable(int n, int[] tab) throws ExpressionException {
performTestCalculation(n, tab);
}
}.create();
} /**/
private static void performTestCalculation(int n, byte[] tab) throws ExpressionException {
BruteForceGetAll ps = new BruteForceGetAll();
ArrayList<Variable> v = vars(n);
new QuineMcCluskey(v).fillTableWith(new BoolTableByteArray(tab)).simplify(ps);
ArrayList<ArrayList<TableRow>> solutions = ps.getAllSolutions();
if (solutions != null) {
for (ArrayList<TableRow> sol : solutions) {
Expression e = QuineMcCluskey.addAnd(null, sol, v);
ContextFiller context = new ContextFiller(v);
for (int i = 0; i < tab.length; i++) {
if (tab[i] <= 1) {
assertEquals(tab[i] == 1, e.calculate(context.setContextTo(i)));
}
}
}
}
}
use of de.neemann.digital.analyse.expression.ContextFiller in project Digital by hneemann.
the class FormatToTable method format.
@Override
public String format(Expression expression) throws FormatterException, ExpressionException {
StringBuilder sb = new StringBuilder();
ContextFiller cf = new ContextFiller(expression);
formatHead(sb, cf.getVarCount());
for (Variable v : cf) sb.append(formatVariable(v));
sb.append(formatResultVariable());
sb.append("\n");
formatTableStart(sb);
for (int i = 0; i < cf.getRowCount(); i++) {
cf.setContextTo(i);
for (Variable v : cf) sb.append(formatValue(cf.get(v)));
sb.append(formatResult(expression.calculate(cf)));
sb.append("\n");
}
formatEnd(sb);
return sb.toString();
}
Aggregations