use of org.h2.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);
}
}
use of org.h2.expression.Expression in project OpenNotebook by jaltekruse.
the class AssociativePropertiesWithVariables method generateExpression.
@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
Node[] n = new Node[2];
if (ExUtil.randomBoolean()) {
if (difficulty == ProblemGenerator.EASY) {
double[] numPair = ExUtil.pairOfCleanAddingNumbers(100);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(numPair[0]), ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12), new Number(numPair[1]));
} else if (difficulty == ProblemGenerator.MEDIUM) {
double[] numPair = ExUtil.pairOfCleanAddingNumbers(100);
double[] numPair2 = ExUtil.pairOfCleanAddingNumbers(200);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(numPair[0]), new Number(numPair2[1]), ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12), new Number(numPair[1]), new Number(numPair2[0]));
} else if (difficulty == ProblemGenerator.HARD) {
double[] numPair = ExUtil.pairOfCleanAddingNumbers(400);
double[] numPair2 = ExUtil.pairOfCleanAddingNumbers(200);
Vector<String> varNames = ExUtil.randomUniqueVarNames(2);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), new Number(numPair[0]), ExUtil.randomTerm(1, varNames.get(0), 2, 12), new Number(numPair2[1]), ExUtil.randomTerm(1, varNames.get(1), 3, 12), new Number(numPair[1]), new Number(numPair2[0]));
}
} else {
// otherwise create a problem testing the associative property of multiplication
if (difficulty == ProblemGenerator.EASY) {
double[] numPair = ExUtil.pairOfCleanFactors(20);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), new Number(numPair[0]), ExUtil.randomTerm(1, ExUtil.randomVarName(), 3, 12), new Number(numPair[1]));
} else if (difficulty == ProblemGenerator.MEDIUM) {
double[] numPair = ExUtil.pairOfCleanFactors(30);
Vector<String> varNames = ExUtil.randomUniqueVarNames(2);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), new Number(numPair[0]), ExUtil.randomTerm(1, varNames.get(0), 3, 9), ExUtil.randomTerm(1, varNames.get(1), 3, 12), new Number(numPair[1]));
} else if (difficulty == ProblemGenerator.HARD) {
double[] numPair = ExUtil.pairOfCleanFactors(40);
Vector<String> varNames = ExUtil.randomUniqueVarNames(2);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), new Number(numPair[0]), ExUtil.randomTerm(1, varNames.get(0), 3, 9), ExUtil.randomTerm(1, varNames.get(1), 3, 12), new Number(numPair[1]));
;
}
}
n[0] = ExUtil.randomlyAddParenthesis((Expression) n[0], 0, 3);
n[1] = n[0].smartNumericSimplify().standardFormat();
return n;
}
use of org.h2.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;
}
use of org.h2.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);
}
}
use of org.h2.expression.Expression in project OpenNotebook by jaltekruse.
the class GraphedCartFunction method draw.
@Override
public void draw(Graphics g) {
//used to temporarily store the value stored in the independent and dependent vars,
//this will allow it to be restored after graphing, so that if in the terminal a
//value was assigned to x, it will not be overriden by the action of graphing
// Number xVal = getIndependentVar().getValue();
// Number yVal = getDependentVar().getValue();
super.clearPts();
Graphics2D g2d = ((Graphics2D) g);
g.setColor(getColor());
if (hasFocus()) {
graph.LINE_SIZE = 3;
} else {
graph.LINE_SIZE = 2;
}
int tempLineSize = graph.LINE_SIZE;
double lastX = 0, lastY = 0, currX = 0, currY = 0;
Node origionalEx = null;
Node ex;
try {
lastX = graph.X_MIN;
origionalEx = Node.parseNode(getFuncEqtn());
ex = origionalEx.cloneNode();
ex = ex.replace(new Identifier(getIndependentVar()), new Number(lastX));
ex = ex.replace("π", new Number(Math.PI));
ex = ex.replace("e", new Number(Math.E));
ex = ex.numericSimplify();
if (ex instanceof Expression) {
if (((Expression) ex).getOperator() instanceof Operator.Equals) {
if (((Expression) ex).getChild(1) instanceof Number) {
lastY = ((Number) ((Expression) ex).getChild(1)).getValue();
}
}
}
if (gridxToScreen(lastX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(lastX) >= +graph.X_PIC_ORIGIN && gridyToScreen(lastY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(lastY) >= +graph.Y_PIC_ORIGIN) {
//if the current point is on the screen, add it to the list of points
addPt(gridxToScreen(lastX), gridyToScreen(lastY));
}
} catch (Exception e1) {
currX = graph.X_MIN;
lastY = graph.Y_MIN;
;
}
currX = graph.X_MIN;
boolean validExpression;
for (int i = 1; i < graph.X_SIZE; i += 1) {
validExpression = false;
try {
currX = currX + graph.X_PIXEL;
ex = origionalEx.cloneNode();
ex = ex.replace(getIndependentVar(), new Number(currX));
ex = ex.replace("π", new Number(Math.PI));
ex = ex.replace("e", new Number(Math.E));
ex = ex.numericSimplify();
} catch (Exception e) {
// TODO Auto-generated catch block
lastX = currX;
lastY = graph.Y_MIN;
continue;
}
if (ex instanceof Expression) {
if (((Expression) ex).getOperator() instanceof Operator.Equals) {
if (((Expression) ex).getChild(1) instanceof Number) {
currY = ((Number) ((Expression) ex).getChild(1)).getValue();
validExpression = true;
}
}
} else if (!validExpression) {
lastX = currX;
lastY = graph.Y_MIN;
continue;
}
if (gridxToScreen(currX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(currX) >= graph.X_PIC_ORIGIN && gridyToScreen(currY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(currY) >= graph.Y_PIC_ORIGIN) {
//if the current point is on the screen, add it to the list of points
if (lastY <= graph.Y_MIN) {
addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
}
if (lastY >= graph.Y_MAX) {
addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
}
addPt(gridxToScreen(currX), gridyToScreen(currY));
} else if (gridxToScreen(lastX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(lastX) >= graph.X_PIC_ORIGIN && gridyToScreen(lastY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(lastY) >= graph.Y_PIC_ORIGIN) {
//if the last point is on the screen, add the correct boundary for this point to the list
addPt(gridxToScreen(lastX), gridyToScreen(lastY));
if (currY <= graph.Y_MIN) {
addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
}
if (currY >= graph.Y_MAX) {
addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
}
} else if (lastY >= graph.Y_MAX && currY <= graph.Y_MIN) {
//if the last point was off the the top of the screen, and this one is off
//the bottom, add the two to the list of points
addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
} else if (currY >= graph.Y_MAX && lastY <= graph.Y_MIN) {
//if the last point was off the the bottom of the screen, and this one is off
//the top, add the two to the list of points
addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
}
if (isConnected()) {
drawLineSeg(lastX, lastY, currX, currY, getColor(), g);
}
lastX = currX;
lastY = currY;
}
// g2d.setStroke(new BasicStroke(graph.LINE_SIZE * graph.DOC_ZOOM_LEVEL,
// BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
// if ( this.getxVals().size() > 0){
// GeneralPath polyline =
// new GeneralPath(GeneralPath.WIND_EVEN_ODD, this.getxVals().size());
// polyline.moveTo (this.getxVals().get(0), this.getyVals().get(0));
// for (int i = 1; i < this.getxVals().size(); i++) {
// polyline.lineTo( getxVals().get(i), getyVals().get(i));
// };
// g2d.draw(polyline);
// }
graph.LINE_SIZE = 2;
g2d.setStroke(new BasicStroke(1));
}
Aggregations