Search in sources :

Example 91 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project Digital by hneemann.

the class MergeAssignements method merge.

private HDLNodeAssignment merge(HDLNodeAssignment host, HDLNodeAssignment include) {
    final Expression expression = host.getExpression();
    final HDLNet obsoleteNet = include.getOutput().getNet();
    expression.replace(obsoleteNet, include.getExpression());
    HDLNodeAssignment node = new HDLNodeAssignment("merged expression", null, name -> host.getOutput().getBits());
    node.setExpression(expression);
    circuit.removeNet(obsoleteNet);
    node.addPort(host.getOutput());
    for (HDLPort i : host.getInputs()) if (i.getNet() != obsoleteNet)
        node.addPort(i);
    for (HDLPort i : include.getInputs()) if (!node.hasInput(i))
        node.addPort(i);
    else
        i.getNet().remove(i);
    return node;
}
Also used : Expression(de.neemann.digital.hdl.model2.expression.Expression)

Example 92 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project OpenNotebook by jaltekruse.

the class NegationGraphic method requestSize.

@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
    g.setFont(f);
    setFont(f);
    space = (int) (1 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL);
    Node tempChild = ((Expression) getValue()).getChild(0);
    NodeGraphic childValGraphic = null;
    int[] childSize = { 0, 0 };
    int[] symbolSize = { 0, 0 };
    int[] totalSize = { 0, 0 };
    symbolSize[0] = (int) Math.round(super.getRootNodeGraphic().getStringWidth("-", f)) + 2 * space;
    symbolSize[1] = super.getRootNodeGraphic().getFontHeight(f);
    childValGraphic = makeNodeGraphic(tempChild);
    setChildGraphic(childValGraphic);
    super.getRootNodeGraphic().getComponents().add(childValGraphic);
    childSize = childValGraphic.requestSize(g, f, x1 + symbolSize[0], y1);
    // set the west and east fields for inside an outside of the expression
    setMostInnerWest(this);
    setEast(childValGraphic);
    childValGraphic.setWest(this);
    setMostInnerEast(childValGraphic);
    symbolY1 = y1 + childValGraphic.getUpperHeight() - (int) Math.round(symbolSize[1] / 2.0);
    symbolY2 = symbolY1 + symbolSize[1];
    symbolX1 = x1;
    symbolX2 = x1 + symbolSize[0];
    setUpperHeight(childValGraphic.getUpperHeight());
    setLowerHeight(childValGraphic.getLowerHeight());
    totalSize[0] = symbolSize[0] + childSize[0];
    totalSize[1] = childSize[1];
    super.setX1(x1);
    super.setY1(y1);
    super.setX2(x1 + totalSize[0]);
    super.setY2(y1 + totalSize[1]);
    return totalSize;
}
Also used : Expression(expression.Expression) Node(expression.Node)

Example 93 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project OpenNotebook by jaltekruse.

the class ExpressionObject method performSpecialObjectAction.

public void performSpecialObjectAction(String s) {
    // actions in the future
    if (((StringAttribute) getAttributeWithName(EXPRESSION)).getValue() == null || ((StringAttribute) getAttributeWithName(EXPRESSION)).getValue().equals("")) {
        JOptionPane.showMessageDialog(null, "There is no expression to work with, enter one in the box below.", WARNING, JOptionPane.WARNING_MESSAGE);
        setActionCancelled(true);
        return;
    }
    if (s.equals(SIMPLIFY)) {
        try {
            getListWithName(STEPS).addValueWithString(Expression.parseNode(getLastStep()).smartNumericSimplify().toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error with expression simplification", ERROR, JOptionPane.WARNING_MESSAGE);
        }
    }
    if (s.equals(COMBINE_LIKE_TERMS)) {
        try {
            getListWithName(STEPS).addValueWithString(Expression.parseNode(getLastStep()).collectLikeTerms().simplify().toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error with combining like terms", ERROR, JOptionPane.WARNING_MESSAGE);
        }
    }
    if (s.equals(MAKE_INTO_PROBLEM)) {
        VariableValueInsertionProblem newProblem = new VariableValueInsertionProblem(getParentContainer(), getxPos(), getyPos(), getWidth(), getHeight());
        this.getParentContainer().getParentDoc().getDocViewerPanel().setFocusedObject(newProblem);
        newProblem.addObjectFromPage(this);
        getParentContainer().addObject(newProblem);
        getParentContainer().removeObject(this);
        return;
    } else if (s.equals(UNDO_STEP)) {
        int size = getListWithName(STEPS).getValues().size();
        if (size > 0) {
            getListWithName(STEPS).getValues().remove(size - 1);
        } else {
            JOptionPane.showMessageDialog(null, "No steps to undo.", WARNING, JOptionPane.WARNING_MESSAGE);
            setActionCancelled(true);
        }
        return;
    } else if (s.equals(SUB_IN_VALUE)) {
        String variableStr = "";
        Node substitute = null;
        boolean foundVar;
        do {
            variableStr = (String) JOptionPane.showInputDialog(null, "Enter a variable to replace, variables are case sensitive 'a' is not the same as 'A'.", null, JOptionPane.PLAIN_MESSAGE, null, null, null);
            if (variableStr == null) {
                setActionCancelled(true);
                return;
            }
            if (variableStr.length() != 1 || !Character.isLetter(variableStr.charAt(0))) {
                JOptionPane.showMessageDialog(null, "Need to enter a single letter.", WARNING, JOptionPane.WARNING_MESSAGE);
            }
            foundVar = Node.parseNode(getLastStep()).containsIdentifier(variableStr);
            if (!foundVar) {
                JOptionPane.showMessageDialog(null, "Variable not found in expression.", WARNING, JOptionPane.WARNING_MESSAGE);
            }
        } while (variableStr.length() != 1 || !Character.isLetter(variableStr.charAt(0)) && !foundVar);
        substitute = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser("Enter value or expression to substitute.");
        if (substitute == null) {
            setActionCancelled(true);
            return;
        }
        substitute.setDisplayParentheses(true);
        try {
            getListWithName(STEPS).addValueWithString(Node.parseNode(getLastStep()).replace(variableStr, substitute).toStringRepresentation());
        } catch (Exception e) {
            // this should not throw an error, as both the expression and the one being
            // Substituted have both been checked for validity
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
            setActionCancelled(true);
        }
        return;
    } else if (s.equals(MODIFY_EXPRESSION)) {
        Node newNode = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser("Modify the expression.", getLastStep());
        if (newNode == null) {
            setActionCancelled(true);
            return;
        }
        try {
            getListWithName(STEPS).addValueWithString(newNode.toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        }
    } else if (s.equals(MANUALLY_TYPE_STEP)) {
        Node newNode = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser("Type the entire next line.");
        if (newNode == null) {
            setActionCancelled(true);
            return;
        }
        try {
            getListWithName(STEPS).addValueWithString(newNode.toStringRepresentation());
            return;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        }
    }
    // all of the rest of the operations require an equals sign
    Node n = null;
    try {
        String expression = ((StringAttribute) getAttributeWithName(EXPRESSION)).getValue();
        if (!expression.equals("")) {
            if (getListWithName(STEPS).getValues().isEmpty()) {
                n = Node.parseNode(((StringAttribute) getAttributeWithName(EXPRESSION)).getValue());
            } else {
                n = Node.parseNode(getLastStep());
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Previous expression has an error.", ERROR, JOptionPane.ERROR_MESSAGE);
        setActionCancelled(true);
        return;
    }
    if (!(n instanceof Expression && ((Expression) n).getOperator() instanceof Operator.Equals)) {
        // the expression does not have an equals sign
        JOptionPane.showMessageDialog(null, "Expression requires an equal sign for that operation.", ERROR, JOptionPane.ERROR_MESSAGE);
        setActionCancelled(true);
        return;
    }
    Expression ex = (Expression) n;
    Operator o = null;
    if (s.equals(OTHER_OPERATIONS)) {
        Object[] operations = { "sqrt", "sin", "cos", "tan" };
        String op = (String) JOptionPane.showInputDialog(null, "Choose an operation to apply to both sides", "Operation Selection", JOptionPane.PLAIN_MESSAGE, null, operations, "sqrt");
        if (op == null || op.equals("")) {
            setActionCancelled(true);
            return;
        }
        if (op.equals("sqrt"))
            o = new Operator.SquareRoot();
        else if (op.equals("sin"))
            o = new Operator.Sine();
        else if (op.equals("cos"))
            o = new Operator.Cosine();
        else if (op.equals("tan"))
            o = new Operator.Tangent();
        Expression newLeft = new Expression(o);
        Vector<Node> left = new Vector<>();
        Node newChild = ex.getChild(0);
        if (!op.equals("sqrt")) {
            newChild.setDisplayParentheses(true);
        }
        left.add(newChild);
        newLeft.setChildren(left);
        Expression newRight = new Expression(o);
        Vector<Node> right = new Vector<>();
        newChild = ex.getChild(1);
        if (!op.equals("sqrt")) {
            newChild.setDisplayParentheses(true);
        }
        right.add(newChild);
        newRight.setChildren(right);
        Vector<Node> exChildren = new Vector<>();
        exChildren.add(newLeft);
        exChildren.add(newRight);
        ex.setChildren(exChildren);
        try {
            getListWithName(STEPS).addValueWithString(ex.toStringRepresentation());
        } catch (NodeException e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        } catch (AttributeException e) {
            JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
        }
        return;
    }
    try {
        if (s.equals(ADD_TO_BOTH_SIDES) || s.equals(SUBTRACT_FROM_BOTH_SIDES) || s.equals(DIVIDE_BOTH_SIDES) || s.equals(MULTIPLY_BOTH_SIDES)) {
            String message = "";
            if (s.equals(ADD_TO_BOTH_SIDES)) {
                o = new Operator.Addition();
                message = "Add to both sides";
            } else if (s.equals(SUBTRACT_FROM_BOTH_SIDES)) {
                o = new Operator.Subtraction();
                message = "Subtract from both sides";
            } else if (s.equals(DIVIDE_BOTH_SIDES)) {
                o = new Operator.Division();
                message = "Divide both sides by";
            } else if (s.equals(MULTIPLY_BOTH_SIDES)) {
                o = new Operator.Multiplication();
                message = "Multiply both sides by";
            }
            Node newNode = this.getParentPage().getParentDoc().getDocViewerPanel().getNotebook().getNotebookPanel().getExpressionFromUser(message);
            if (newNode == null) {
                setActionCancelled(true);
                return;
            }
            ex = ex.applyOpToBothSides(o, newNode, true);
            try {
                getListWithName(STEPS).addValueWithString(ex.toStringRepresentation());
            } catch (AttributeException e) {
                JOptionPane.showMessageDialog(null, ERROR_WITH_EXPRESSION, WARNING, JOptionPane.WARNING_MESSAGE);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error with operation.", ERROR, JOptionPane.ERROR_MESSAGE);
    }
}
Also used : Operator(expression.Operator) Multiplication(expression.Operator.Multiplication) Node(expression.Node) StringAttribute(doc.attributes.StringAttribute) NodeException(expression.NodeException) AttributeException(doc.attributes.AttributeException) AttributeException(doc.attributes.AttributeException) NodeException(expression.NodeException) Expression(expression.Expression) Vector(java.util.Vector)

Example 94 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project OpenNotebook by jaltekruse.

the class ExUtil method randomTerm.

public static Node randomTerm(int degree, String var, int minCoefficient, int maxCoefficient) {
    Number num = new Number(randomInt(minCoefficient, maxCoefficient, true));
    if (degree == 0) {
        return num;
    }
    Expression ex = new Expression(new Operator.Multiplication());
    ex.getChildren().add(num);
    try {
        if (degree == 1) {
            ex.getChildren().add(new Identifier(var));
            return ex;
        } else {
            ex.getChildren().add(new Expression(new Operator.Exponent(), new Identifier(var), new Number(degree)));
            return ex;
        }
    } catch (NodeException e) {
        throw new RuntimeException(e);
    }
}
Also used : Operator(expression.Operator) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) NodeException(expression.NodeException)

Example 95 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project OpenNotebook by jaltekruse.

the class ExUtil method randomAdditionOrSubtraction.

public static Expression randomAdditionOrSubtraction(int minVal, int maxVal) {
    Number num1 = new Number(randomInt(minVal, maxVal, true)), num2 = new Number(randomInt(minVal, maxVal, true));
    Expression sumNode = null;
    if (ExUtil.randomBoolean()) {
        sumNode = new Expression(new Operator.Addition(), num1, num2);
    } else {
        sumNode = new Expression(new Operator.Subtraction(), num1, num2);
    }
    return sumNode;
}
Also used : Number(expression.Number) Expression(expression.Expression)

Aggregations

Expression (org.h2.expression.Expression)84 ValueExpression (org.h2.expression.ValueExpression)53 Column (org.h2.table.Column)45 ExpressionColumn (org.h2.expression.ExpressionColumn)38 IndexColumn (org.h2.table.IndexColumn)24 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)18 ValueString (org.h2.value.ValueString)18 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)17 TableFilter (org.h2.table.TableFilter)15 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)14 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)14 Expression (expression.Expression)13 Table (org.h2.table.Table)13 Value (org.h2.value.Value)12 ArrayList (java.util.ArrayList)11 Node (expression.Node)10 RangeTable (org.h2.table.RangeTable)10 Number (expression.Number)9 CreateTable (org.h2.command.ddl.CreateTable)9