Search in sources :

Example 11 with ContextFiller

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));
}
Also used : Expression(de.neemann.digital.analyse.expression.Expression) ContextFiller(de.neemann.digital.analyse.expression.ContextFiller)

Example 12 with ContextFiller

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)));
    }
}
Also used : Variable(de.neemann.digital.analyse.expression.Variable) Expression(de.neemann.digital.analyse.expression.Expression) ContextFiller(de.neemann.digital.analyse.expression.ContextFiller) ExpressionListenerStore(de.neemann.digital.gui.components.table.ExpressionListenerStore)

Example 13 with ContextFiller

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));
    }
}
Also used : Variable(de.neemann.digital.analyse.expression.Variable) BoolTableByteArray(de.neemann.digital.analyse.quinemc.BoolTableByteArray) ContextFiller(de.neemann.digital.analyse.expression.ContextFiller)

Example 14 with ContextFiller

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)));
                }
            }
        }
    }
}
Also used : QuineMcCluskey(de.neemann.digital.analyse.quinemc.QuineMcCluskey) Variable(de.neemann.digital.analyse.expression.Variable) BoolTableByteArray(de.neemann.digital.analyse.quinemc.BoolTableByteArray) Expression(de.neemann.digital.analyse.expression.Expression) ContextFiller(de.neemann.digital.analyse.expression.ContextFiller) TableRow(de.neemann.digital.analyse.quinemc.TableRow) ArrayList(java.util.ArrayList)

Example 15 with ContextFiller

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();
}
Also used : Variable(de.neemann.digital.analyse.expression.Variable) ContextFiller(de.neemann.digital.analyse.expression.ContextFiller)

Aggregations

ContextFiller (de.neemann.digital.analyse.expression.ContextFiller)17 Expression (de.neemann.digital.analyse.expression.Expression)10 Variable (de.neemann.digital.analyse.expression.Variable)10 ArrayList (java.util.ArrayList)6 BoolTableByteArray (de.neemann.digital.analyse.quinemc.BoolTableByteArray)4 TruthTable (de.neemann.digital.analyse.TruthTable)3 FormatToExpression (de.neemann.digital.analyse.expression.format.FormatToExpression)2 ExpressionException (de.neemann.digital.analyse.expression.ExpressionException)1 QuineMcCluskey (de.neemann.digital.analyse.quinemc.QuineMcCluskey)1 TableRow (de.neemann.digital.analyse.quinemc.TableRow)1 ThreeStateValue (de.neemann.digital.analyse.quinemc.ThreeStateValue)1 PrimeSelectorDefault (de.neemann.digital.analyse.quinemc.primeselector.PrimeSelectorDefault)1 ExpressionListenerStore (de.neemann.digital.gui.components.table.ExpressionListenerStore)1