Search in sources :

Example 21 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class UnaryPostGraphic method requestSize.

@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
    g.setFont(f);
    setFont(f);
    space = (int) (2 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL);
    Node tempChild = getValue().getChild(0);
    NodeGraphic childValGraphic = null;
    int[] childSize = { 0, 0 };
    int[] symbolSize = { 0, 0 };
    int[] totalSize = { 0, 0 };
    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(childValGraphic);
    setWest(childValGraphic.getMostInnerEast());
    childValGraphic.getMostInnerEast().setEast(this);
    setMostInnerEast(this);
    symbolSize[0] = super.getRootNodeGraphic().getStringWidth(value.getOperator().getSymbol(), f);
    symbolSize[1] = super.getRootNodeGraphic().getFontHeight(f);
    symbolY1 = y1 + childValGraphic.getUpperHeight() - (int) Math.round(symbolSize[1] / 2.0);
    symbolY2 = symbolY1 + symbolSize[1];
    symbolX1 = x1 + space + childSize[0];
    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 : Node(expression.Node)

Example 22 with Node

use of expression.Node 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 23 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class ExUtil method randomTerm.

public static Node randomTerm(int maxDegree, int minCoefficient, int maxCoefficient, VarList variables) {
    Vector<Node> parts = new Vector<>();
    // add the coefficient
    parts.add(new Number(randomInt(minCoefficient, maxCoefficient, true)));
    // keep a list of the variables that have already been used
    Vector<String> usedVars = new Vector<>();
    int degree = 0;
    while (degree < maxDegree && variables.size() > usedVars.size()) {
    }
    return null;
}
Also used : Number(expression.Number) Node(expression.Node) Vector(java.util.Vector)

Example 24 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class ExUtil method randomExpression.

public static Node randomExpression(String[] ops, String[] vars, int numOps, double maxAbsVal, int minNumVal, int maxNumVal, boolean excludeZero, boolean subtractNegatives, boolean addNegatives, boolean includeFractions) {
    Node n;
    n = new Number(randomInt(minNumVal, maxNumVal, excludeZero));
    for (int i = 0; i < numOps; i++) {
        n = addRandomOp(n, ops, vars, minNumVal, maxNumVal, maxAbsVal, excludeZero, subtractNegatives, addNegatives);
    }
    return n;
}
Also used : Number(expression.Number) Node(expression.Node)

Example 25 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class ExUtil method randomPolynomial.

public static Node randomPolynomial(int maxDegree, int minCoefficient, int maxCoefficient, int minNumberTerms, int maxNumberTerms, VarList variables) {
    Node n = null;
    int numTerms = randomInt(minNumberTerms, maxNumberTerms, true);
    for (int i = 0; i < numTerms; i++) {
    // add a new term
    }
    return n;
}
Also used : Node(expression.Node)

Aggregations

Node (expression.Node)29 Expression (expression.Expression)10 NodeException (expression.NodeException)10 Number (expression.Number)9 Operator (expression.Operator)7 Vector (java.util.Vector)6 StringAttribute (doc.attributes.StringAttribute)4 AttributeException (doc.attributes.AttributeException)3 Identifier (expression.Identifier)2 BasicStroke (java.awt.BasicStroke)2 Graphics2D (java.awt.Graphics2D)2 Point (java.awt.Point)2 RootNodeGraphic (math_rendering.RootNodeGraphic)2 ListAttribute (doc.attributes.ListAttribute)1 ExpressionObject (doc.mathobjects.ExpressionObject)1 GeneratedProblem (doc.mathobjects.GeneratedProblem)1 Multiplication (expression.Operator.Multiplication)1 Color (java.awt.Color)1 Cursor (math_rendering.Cursor)1