use of de.neemann.digital.hdl.model2.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 de.neemann.digital.hdl.model2.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 de.neemann.digital.hdl.model2.expression.Expression in project ignite by apache.
the class DmlAstUtils method elementOrDefault.
/**
* Do what we can to compute default value for this column (mimics H2 behavior).
* @see Table#getDefaultValue
* @see Column#validateConvertUpdateSequence
* @param el SQL element.
* @param col Column.
* @return {@link GridSqlConst#NULL}, if {@code el} is null, or {@code el} if
* it's not {@link GridSqlKeyword#DEFAULT}, or computed default value.
*/
private static GridSqlElement elementOrDefault(GridSqlElement el, GridSqlColumn col) {
if (el == null)
return GridSqlConst.NULL;
if (el != GridSqlKeyword.DEFAULT)
return el;
Column h2Col = col.column();
Expression dfltExpr = h2Col.getDefaultExpression();
Value dfltVal;
try {
dfltVal = dfltExpr != null ? dfltExpr.getValue(null) : null;
} catch (Exception ignored) {
throw new IgniteSQLException("Failed to evaluate default value for a column " + col.columnName());
}
if (dfltVal != null)
return new GridSqlConst(dfltVal);
int type = h2Col.getType();
DataType dt = DataType.getDataType(type);
if (dt.decimal)
dfltVal = ValueInt.get(0).convertTo(type);
else if (dt.type == Value.TIMESTAMP)
dfltVal = ValueTimestamp.fromMillis(U.currentTimeMillis());
else if (dt.type == Value.TIME)
dfltVal = ValueTime.fromNanos(0);
else if (dt.type == Value.DATE)
dfltVal = ValueDate.fromMillis(U.currentTimeMillis());
else
dfltVal = ValueString.get("").convertTo(type);
return new GridSqlConst(dfltVal);
}
use of de.neemann.digital.hdl.model2.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);
if (row[i] == null) {
throw new IgniteSQLException("Explicit DEFAULT values are unsupported for INSERT.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
}
rows.add(row);
}
res.rows(rows);
} else {
res.rows(Collections.<GridSqlElement[]>emptyList());
res.query(parseQuery(INSERT_QUERY.get(insert)));
}
return res;
}
use of de.neemann.digital.hdl.model2.expression.Expression in project ignite by apache.
the class DmlAstUtils method elementOrDefault.
/**
* Do what we can to compute default value for this column (mimics H2 behavior).
* @see Table#getDefaultValue
* @see Column#validateConvertUpdateSequence
* @param el SQL element.
* @param col Column.
* @return {@link GridSqlConst#NULL}, if {@code el} is null, or {@code el} if
* it's not {@link GridSqlKeyword#DEFAULT}, or computed default value.
*/
private static GridSqlElement elementOrDefault(GridSqlElement el, GridSqlColumn col) {
if (el == null)
return GridSqlConst.NULL;
if (el != GridSqlKeyword.DEFAULT)
return el;
Column h2Col = col.column();
Expression dfltExpr = h2Col.getDefaultExpression();
Value dfltVal;
try {
dfltVal = dfltExpr != null ? dfltExpr.getValue(null) : null;
} catch (Exception ignored) {
throw new IgniteSQLException("Failed to evaluate default value for a column " + col.columnName());
}
if (dfltVal != null)
return new GridSqlConst(dfltVal);
int type = h2Col.getType();
DataType dt = DataType.getDataType(type);
if (dt.decimal)
dfltVal = ValueInt.get(0).convertTo(type);
else if (dt.type == Value.TIMESTAMP)
dfltVal = ValueTimestamp.fromMillis(U.currentTimeMillis());
else if (dt.type == Value.TIME)
dfltVal = ValueTime.fromNanos(0);
else if (dt.type == Value.DATE)
dfltVal = ValueDate.fromMillis(U.currentTimeMillis());
else
dfltVal = ValueString.get("").convertTo(type);
return new GridSqlConst(dfltVal);
}
Aggregations