use of de.neemann.digital.builder.BuilderException in project Digital by hneemann.
the class CircuitBuilder method addSequential.
/**
* Add a transition function of a state machine
*
* @param name name of the state
* @param expression the expression describing next state
* @return this for chained calls
* @throws BuilderException BuilderException
*/
@Override
public CircuitBuilder addSequential(String name, Expression expression) throws BuilderException {
boolean useDff = true;
if (useJKff) {
try {
DetermineJKStateMachine jk = new DetermineJKStateMachine(name, expression);
useDff = jk.isDFF();
if (!useDff) {
boolean isJequalK = new Equals(jk.getSimplifiedJ(), jk.getSimplifiedK()).isEqual();
if (isJequalK) {
Fragment frJ = createFragment(jk.getSimplifiedJ());
FragmentVisualElement ff = new FragmentVisualElement(FlipflopJK.DESCRIPTION, shapeFactory).ignoreInput(1).setAttr(Keys.LABEL, name);
flipflops.add(ff);
FragmentSameInValue fsv = new FragmentSameInValue(ff);
FragmentExpression fe = new FragmentExpression(fsv, new FragmentVisualElement(Tunnel.DESCRIPTION, shapeFactory).setAttr(Keys.NETNAME, name));
fragments.add(new FragmentExpression(frJ, fe));
} else {
Fragment frJ = createFragment(jk.getSimplifiedJ());
Fragment frK = createFragment(jk.getSimplifiedK());
FragmentVisualElement ff = new FragmentVisualElement(FlipflopJK.DESCRIPTION, shapeFactory).ignoreInput(1).setAttr(Keys.LABEL, name);
flipflops.add(ff);
FragmentExpression fe = new FragmentExpression(ff, new FragmentVisualElement(Tunnel.DESCRIPTION, shapeFactory).setAttr(Keys.NETNAME, name));
fragments.add(new FragmentExpression(Arrays.asList(frJ, frK), fe));
}
}
} catch (ExpressionException | FormatterException e) {
throw new BuilderException(e.getMessage());
}
}
if (useDff) {
Fragment fr = createFragment(expression);
FragmentVisualElement ff = new FragmentVisualElement(FlipflopD.DESCRIPTION, shapeFactory).setAttr(Keys.LABEL, name);
flipflops.add(ff);
FragmentExpression fe = new FragmentExpression(ff, new FragmentVisualElement(Tunnel.DESCRIPTION, shapeFactory).setAttr(Keys.NETNAME, name));
fragments.add(new FragmentExpression(fr, fe));
}
expression.traverse(variableVisitor);
sequentialVars.add(new Variable(name));
return this;
}
use of de.neemann.digital.builder.BuilderException 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()));
}
use of de.neemann.digital.builder.BuilderException in project Digital by hneemann.
the class BuilderExpressionCreator method create.
/**
* Fills the builder
*
* @param expressions the expressions to use
* @throws ExpressionException ExpressionException
* @throws FormatterException FormatterException
*/
public void create(ExpressionListenerStore expressions) throws ExpressionException, FormatterException {
if (expressions == null)
throw new ExpressionException(Lang.get("err_noExpressionsAvailable"));
ExpressionListener el = new ExpressionListener() {
@Override
public void resultFound(String name, Expression expression) throws FormatterException, ExpressionException {
if (!contained.contains(name)) {
contained.add(name);
try {
if (name.endsWith("n+1")) {
name = name.substring(0, name.length() - 2);
builder.addSequential(name, ExpressionModifier.modifyExpression(expression, modifier));
} else
builder.addCombinatorial(name, ExpressionModifier.modifyExpression(expression, modifier));
} catch (BuilderException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void close() {
}
};
if (useJKOptimizer)
el = new ExpressionListenerOptimizeJK(el);
expressions.replayTo(el);
}
Aggregations