Search in sources :

Example 1 with Not

use of de.neemann.digital.analyse.expression.Not in project Digital by hneemann.

the class CircuitBuilder method createFragment.

private Fragment createFragment(Expression expression) throws BuilderException {
    if (expression instanceof Operation) {
        Operation op = (Operation) expression;
        ArrayList<Fragment> frags = getOperationFragments(op);
        if (op instanceof Operation.And)
            return new FragmentExpression(frags, new FragmentVisualElement(And.DESCRIPTION, frags.size(), shapeFactory));
        else if (op instanceof Operation.Or)
            return new FragmentExpression(frags, new FragmentVisualElement(Or.DESCRIPTION, frags.size(), shapeFactory));
        else if (op instanceof Operation.XOr)
            return new FragmentExpression(frags, new FragmentVisualElement(XOr.DESCRIPTION, frags.size(), shapeFactory));
        else
            throw new BuilderException(Lang.get("err_builder_operationNotSupported", op.getClass().getSimpleName()));
    } else if (expression instanceof Not) {
        Not n = (Not) expression;
        if (n.getExpression() instanceof Variable) {
            FragmentVariable fragmentVariable = new FragmentVariable((Variable) n.getExpression(), true);
            fragmentVariables.add(fragmentVariable);
            return fragmentVariable;
        } else if (n.getExpression() instanceof Operation.And) {
            ArrayList<Fragment> frags = getOperationFragments((Operation) n.getExpression());
            return new FragmentExpression(frags, new FragmentVisualElement(NAnd.DESCRIPTION, frags.size(), shapeFactory));
        } else if (n.getExpression() instanceof Operation.Or) {
            ArrayList<Fragment> frags = getOperationFragments((Operation) n.getExpression());
            return new FragmentExpression(frags, new FragmentVisualElement(NOr.DESCRIPTION, frags.size(), shapeFactory));
        } else if (n.getExpression() instanceof Operation.XOr) {
            ArrayList<Fragment> frags = getOperationFragments((Operation) n.getExpression());
            return new FragmentExpression(frags, new FragmentVisualElement(XNOr.DESCRIPTION, frags.size(), shapeFactory));
        }
        return new FragmentExpression(createFragment(n.getExpression()), new FragmentVisualElement(de.neemann.digital.core.basic.Not.DESCRIPTION, shapeFactory));
    } else if (expression instanceof Variable) {
        FragmentVariable fragmentVariable = new FragmentVariable((Variable) expression, false);
        fragmentVariables.add(fragmentVariable);
        return fragmentVariable;
    } else if (expression instanceof Constant) {
        long val = 0;
        if (((Constant) expression).getValue())
            val = 1;
        return new FragmentVisualElement(Const.DESCRIPTION, shapeFactory).setAttr(Keys.VALUE, val);
    } else
        throw new BuilderException(Lang.get("err_builder_exprNotSupported", expression.getClass().getSimpleName()));
}
Also used : BuilderException(de.neemann.digital.builder.BuilderException) Not(de.neemann.digital.analyse.expression.Not)

Example 2 with Not

use of de.neemann.digital.analyse.expression.Not in project Digital by hneemann.

the class FuseMapFiller method fillExpression.

/**
 * Fills an expression to the fuse map
 *
 * @param offs         number of first fuse of first product term to use
 * @param exp          the expression
 * @param productTerms the number of product terms available
 * @throws FuseMapFillerException EquationHandlerException
 */
public void fillExpression(int offs, Expression exp, int productTerms) throws FuseMapFillerException {
    ArrayList<Expression> terms;
    if (exp instanceof Operation.Or) {
        Operation.Or or = (Operation.Or) exp;
        terms = or.getExpressions();
    } else {
        terms = new ArrayList<>();
        terms.add(exp);
    }
    if (terms.size() > productTerms)
        throw new FuseMapFillerException("only " + productTerms + " product terms supported but " + terms.size() + " needed!");
    int fusesInTerm = varsConnectedToMap * 2;
    for (Expression e : terms) {
        for (int i = 0; i < fusesInTerm; i++) fuseMap.setFuse(offs + i, true);
        ArrayList<Expression> ands;
        if (e instanceof Operation.And) {
            ands = ((Operation.And) e).getExpressions();
        } else {
            ands = new ArrayList<>();
            ands.add(e);
        }
        for (Expression v : ands) {
            Variable var;
            boolean invert = false;
            if (v instanceof Variable)
                var = (Variable) v;
            else if (v instanceof Not) {
                Expression n = ((Not) v).getExpression();
                if (n instanceof Variable) {
                    var = (Variable) n;
                    invert = true;
                } else {
                    throw new FuseMapFillerException("NOT does not contain a variable!");
                }
            } else
                throw new FuseMapFillerException("only VAR or NOT VAR allowed!");
            MapEntry entry = varMap.get(var);
            if (entry == null)
                throw new FuseMapFillerException("VAR " + var + " not found in term list!");
            int fuse = entry.getFuse(invert);
            fuseMap.setFuse(offs + fuse, false);
        }
        offs += fusesInTerm;
    }
}
Also used : Variable(de.neemann.digital.analyse.expression.Variable) Operation(de.neemann.digital.analyse.expression.Operation) Not(de.neemann.digital.analyse.expression.Not) Expression(de.neemann.digital.analyse.expression.Expression)

Aggregations

Not (de.neemann.digital.analyse.expression.Not)2 Expression (de.neemann.digital.analyse.expression.Expression)1 Operation (de.neemann.digital.analyse.expression.Operation)1 Variable (de.neemann.digital.analyse.expression.Variable)1 BuilderException (de.neemann.digital.builder.BuilderException)1