Search in sources :

Example 6 with Expression

use of org.h2.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;
}
Also used : Operator(expression.Operator) Number(expression.Number) Expression(expression.Expression) Node(expression.Node)

Example 7 with Expression

use of org.h2.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);
}
Also used : Node(expression.Node) StringAttribute(doc.attributes.StringAttribute) NodeException(expression.NodeException) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) Vector(java.util.Vector)

Example 8 with Expression

use of org.h2.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;
}
Also used : ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) JdbcConnection(org.h2.jdbc.JdbcConnection) Session(org.h2.engine.Session) Parser(org.h2.command.Parser)

Example 9 with Expression

use of org.h2.expression.Expression in project ignite by apache.

the class DmlAstUtils method getFastUpdateArgs.

/**
     * @param update UPDATE statement.
     * @return {@code null} if given statement directly updates {@code _val} column with a literal or param value
     * and filters by single non expression key (and, optionally,  by single non expression value).
     */
public static FastUpdateArguments getFastUpdateArgs(GridSqlUpdate update) {
    IgnitePair<GridSqlElement> filter = findKeyValueEqualityCondition(update.where());
    if (filter == null)
        return null;
    if (update.cols().size() != 1)
        return null;
    Table tbl = update.cols().get(0).column().getTable();
    if (!(tbl instanceof GridH2Table))
        return null;
    GridH2RowDescriptor desc = ((GridH2Table) tbl).rowDescriptor();
    if (!desc.isValueColumn(update.cols().get(0).column().getColumnId()))
        return null;
    GridSqlElement set = update.set().get(update.cols().get(0).columnName());
    if (!(set instanceof GridSqlConst || set instanceof GridSqlParameter))
        return null;
    return new FastUpdateArguments(operandForElement(filter.getKey()), operandForElement(filter.getValue()), operandForElement(set));
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) FastUpdateArguments(org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)

Example 10 with Expression

use of org.h2.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;
}
Also used : RangeTable(org.h2.table.RangeTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) ArrayList(java.util.ArrayList) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) GridSqlType.fromColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) GridSqlType.fromExpression(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression) ValueExpression(org.h2.expression.ValueExpression)

Aggregations

Expression (expression.Expression)13 Node (expression.Node)10 Number (expression.Number)9 Expression (org.h2.expression.Expression)9 Operator (expression.Operator)8 ExpressionColumn (org.h2.expression.ExpressionColumn)7 ValueExpression (org.h2.expression.ValueExpression)7 Column (org.h2.table.Column)6 IndexColumn (org.h2.table.IndexColumn)6 NodeException (expression.NodeException)5 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)5 ArrayList (java.util.ArrayList)4 Vector (java.util.Vector)4 GridSqlType.fromColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromColumn)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 Table (org.h2.table.Table)4 Identifier (expression.Identifier)3 StringAttribute (doc.attributes.StringAttribute)2 IgniteException (org.apache.ignite.IgniteException)2 CreateTable (org.h2.command.ddl.CreateTable)2