use of expression.Expression in project OpenNotebook by jaltekruse.
the class AdditionPropertyOfEquality method generateExpression.
@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
Node[] n = new Node[2];
if (difficulty == ProblemGenerator.EASY) {
double[] numPair = ExUtil.pairOfCleanAddingNumbers(100);
if (ExUtil.randomBoolean()) {
numPair[1] = -1 * numPair[1];
}
n[1] = new Expression(new Operator.Equals(), ExUtil.randomVar(), new Number(numPair[0]));
n[0] = ((Expression) n[1]).applyOpToBothSides(new Operator.Addition(), new Number(numPair[1]), true);
n[0] = n[0].smartNumericSimplify();
} else if (difficulty == ProblemGenerator.MEDIUM) {
n[1] = new Expression(new Operator.Equals(), ExUtil.randomVar(), new Number(ExUtil.randomInt(5, 30, true)));
n[0] = ((Expression) n[1]).applyOpToBothSides(new Operator.Addition(), new Number(ExUtil.randomInt(5, 30, true)), true);
n[0] = n[0].smartNumericSimplify();
} else if (difficulty == ProblemGenerator.HARD) {
n[1] = new Expression(new Operator.Equals(), ExUtil.randomVar(), new Number(ExUtil.randomInt(5, 30, true)));
Expression sumNode = ExUtil.randomAdditionOrSubtraction(5, 30);
Node sum = sumNode.numericSimplify();
n[0] = n[1].cloneNode();
Node newLeft = null, newRight = null;
if (ExUtil.randomBoolean()) {
newLeft = ((Expression) n[0]).getChild(0).addNodeToExpression(sumNode);
newRight = ((Expression) n[0]).getChild(1).addNodeToExpression(sum);
} else {
newLeft = ((Expression) n[0]).getChild(0).addNodeToExpression(sum);
newRight = ((Expression) n[0]).getChild(1).addNodeToExpression(sumNode);
}
n[0] = new Expression(new Operator.Equals(), newLeft, newRight);
}
n[0] = ExUtil.flipSides((Expression) n[0]);
return n;
}
use of expression.Expression in project OpenNotebook by jaltekruse.
the class VariableValueInsertionProblem method generateProblem.
public GeneratedProblem generateProblem(int difficulty) {
Grouping newProblem = new Grouping(getParentContainer(), getxPos(), getyPos() + getHeight() + bufferSpace, getWidth(), getHeight());
String s;
Node n = null;
Vector<String> varNames = new Vector<>();
Vector<Number> varVals = new Vector<>();
for (StringAttribute strAtt : (Vector<StringAttribute>) getScripts().getValues()) {
s = strAtt.getValue();
if (s == null || s.equals("")) {
continue;
}
try {
n = Node.parseNode(s);
//sub in variables already assigned in previous scripts
for (int i = 0; i < varNames.size(); i++) {
n = n.replace(varNames.get(i), varVals.get(i));
}
n = n.numericSimplify();
if (n instanceof Expression) {
Expression ex = (Expression) n;
if (ex.getOperator() instanceof Operator.Equals) {
if (ex.getChild(0) instanceof Identifier) {
Identifier var = (Identifier) ex.getChild(0);
if (ex.getChild(1) instanceof Number) {
varNames.add(var.getIdentifier());
// this causes a lot of unneeded parenthesis
// but without it, you cannot sub in a value
// where there is an implied parenthesis
// ex.getChild(1).setDisplayParentheses(true);
varVals.add((Number) ex.getChild(1));
}
}
}
}
} catch (NodeException e) {
JOptionPane.showMessageDialog(null, "Error generating a problem, check scripts.", ERROR, JOptionPane.ERROR_MESSAGE);
}
}
MathObject newObj = null;
for (MathObject mObj : getObjects()) {
newObj = mObj.clone();
for (int i = 0; i < varNames.size(); i++) {
newObj = subInVal(varNames.get(i), varVals.get(i), newObj);
}
//shift object down so it doesn't overlap the current problem
newObj.setyPos(newObj.getyPos() + getHeight() + bufferSpace);
//this line sets the bounds to the actual space it takes to render them
if (getParentContainer() != null) {
// if this problem formula is in the background storage for a document
getParentDoc().getDocViewerPanel().drawObjectInBackground(newObj);
} else {
// if this problem formula is actually on a document
getProblemHoldingDocument().getDocViewerPanel().drawObjectInBackground(newObj);
}
newObj.setParentContainer(newProblem.getParentContainer());
newProblem.addObjectFromPage(newObj);
}
return new GeneratedProblem(newProblem.getParentContainer(), this.getProblemID(), newProblem);
}
use of expression.Expression in project siena by mandubian.
the class FullText method parseKey.
/**
* Parse a primary key condition into the primary key columns.
*
* @param conn the database connection
* @param key the primary key condition as a string
* @return an array containing the column name list and the data list
*/
protected static Object[][] parseKey(Connection conn, String key) {
ArrayList<String> columns = New.arrayList();
ArrayList<String> data = New.arrayList();
JdbcConnection c = (JdbcConnection) conn;
Session session = (Session) c.getSession();
Parser p = new Parser(session);
Expression expr = p.parseExpression(key);
addColumnData(columns, data, expr);
Object[] col = new Object[columns.size()];
columns.toArray(col);
Object[] dat = new Object[columns.size()];
data.toArray(dat);
Object[][] columnData = { col, dat };
return columnData;
}
use of expression.Expression in project ignite by apache.
the class GridSqlQueryParser method parseInsert.
/**
* @param insert Insert.
* @see <a href="http://h2database.com/html/grammar.html#insert">H2 insert spec</a>
*/
private GridSqlInsert parseInsert(Insert insert) {
GridSqlInsert res = (GridSqlInsert) h2ObjToGridObj.get(insert);
if (res != null)
return res;
res = new GridSqlInsert();
h2ObjToGridObj.put(insert, res);
Table srcTbl = INSERT_TABLE.get(insert);
GridSqlElement tbl = parseTable(srcTbl);
res.into(tbl).direct(INSERT_DIRECT.get(insert)).sorted(INSERT_SORTED.get(insert));
Column[] srcCols = INSERT_COLUMNS.get(insert);
GridSqlColumn[] cols = new GridSqlColumn[srcCols.length];
for (int i = 0; i < srcCols.length; i++) {
cols[i] = new GridSqlColumn(srcCols[i], tbl, null, null, srcCols[i].getName());
cols[i].resultType(fromColumn(srcCols[i]));
}
res.columns(cols);
List<Expression[]> srcRows = INSERT_ROWS.get(insert);
if (!srcRows.isEmpty()) {
List<GridSqlElement[]> rows = new ArrayList<>(srcRows.size());
for (Expression[] srcRow : srcRows) {
GridSqlElement[] row = new GridSqlElement[srcRow.length];
for (int i = 0; i < srcRow.length; i++) row[i] = parseExpression(srcRow[i], false);
rows.add(row);
}
res.rows(rows);
} else {
res.rows(Collections.<GridSqlElement[]>emptyList());
res.query(parseQuery(INSERT_QUERY.get(insert)));
}
return res;
}
use of 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);
}
}
Aggregations