private static FormulaNode parseFilter(String filter) { if(Strings.isNullOrEmpty(filter)) { return new ConstantNode(true); } try { return FormulaParser.parse(filter); } catch (FormulaException e) { // Failed to parse filter, falling back to denied access return new ConstantNode(false); } }
private static FormulaNode dateValue(FilterConfig filter) { // GXT serializes the date value as unix time value in milliseconds long time = Long.parseLong(filter.getValue()); Date date = new Date(time); LocalDate localDate = new LocalDate(date); return new FunctionCallNode(DateFunction.INSTANCE, new ConstantNode(localDate.getYear()), new ConstantNode(localDate.getMonthOfYear()), new ConstantNode(localDate.getDayOfMonth())); }
public static FormulaNode anyTrue(List<FormulaNode> nodes) { if(nodes.isEmpty()) { return new ConstantNode(false); } else { return binaryTree(OrFunction.INSTANCE, nodes); } }
private static ConstantNode numericValue(FilterConfig filter) { return new ConstantNode(Double.parseDouble(filter.getValue())); }
public static ConstantNode valueOf(FieldValue value) { if(value instanceof TextValue) { return new ConstantNode(((TextValue) value).asString()); } else if(value instanceof BooleanFieldValue) { return new ConstantNode(value == BooleanFieldValue.TRUE); } else if(value instanceof Quantity) { return new ConstantNode(value, new QuantityType()); } else if (value instanceof EnumValue) { return new ConstantNode(value, new EnumType()); } else { throw new IllegalArgumentException(value.getTypeClass().getId()); } }
public static FormulaNode toFormula(FormulaNode field, List<FilterConfig> filters) { List<FormulaNode> filterExprs = new ArrayList<>(); for (FilterConfig filter : filters) { if(!isEmpty(filter)) { filterExprs.add(ColumnFilterParser.toFormula(field, filter)); } } if(filterExprs.isEmpty()) { return new ConstantNode(true); } else { return Formulas.allTrue(filterExprs); } }
public static FormulaNode parse(String expression) { if(Strings.isNullOrEmpty(expression)) { return new ConstantNode((String)null); } FormulaLexer lexer = new FormulaLexer(expression); FormulaParser parser = new FormulaParser(lexer); return parser.parse(); } }
@Test public void ifCall() { assertThat( new FunctionCallNode(IfFunction.INSTANCE, new ConstantNode(true), new ConstantNode(1), new ConstantNode(0)).asExpression(), equalTo("if(true, 1, 0)") ); }
@Test public void equalsComparison() { assertThat( new FunctionCallNode(IfFunction.INSTANCE, new FunctionCallNode(EqualFunction.INSTANCE, new ConstantNode(true), new ConstantNode(false)), new ConstantNode(true), new ConstantNode(false)).asExpression(), equalTo("if(true == false, true, false)")); }
@Test public void parseEqualsSign() { expect("true==false", new FunctionCallNode( EqualFunction.INSTANCE, new ConstantNode(true), new ConstantNode(false))); }
private FormulaNode booleanLiteral() { Token token = lexer.next(); assert token.getType() == TokenType.BOOLEAN_LITERAL; boolean value = token.getString().toLowerCase().equals("true"); SourceRange source = new SourceRange(token); return new ConstantNode(value, source); }
@Test public void parseSimple() { expect("1", new ConstantNode(1)); expect("(1)", new GroupNode(new ConstantNode(1))); expect("1+2", new FunctionCallNode(PLUS, new ConstantNode(1), new ConstantNode(2))); }
@Test public void parseNested() { expect("(1+2)/3", new FunctionCallNode(ArithmeticFunctions.DIVIDE, new GroupNode( new FunctionCallNode(ArithmeticFunctions.BINARY_PLUS, new ConstantNode(1), new ConstantNode(2))), new ConstantNode(3))); }
private static FormulaNode stringContains(FormulaNode field, FilterConfig filter) { ConstantNode substring = new ConstantNode(Strings.nullToEmpty(filter.getValue())); FormulaNode string = field; return new FunctionCallNode(IsNumberFunction.INSTANCE, new FunctionCallNode(SearchFunction.INSTANCE, substring, string)); }
private FormulaNode enumLiteral() { Token token = lexer.next(); assert token.getType() == TokenType.SYMBOL; return new ConstantNode(token, new SourceRange(token)); }
private FormulaNode number() { Token token = lexer.next(); double value; try { value = Double.parseDouble(token.getString()); } catch (NumberFormatException e) { throw new FormulaSyntaxException(new SourceRange(token), "Invalid number '" + token.getString() + "': " + e.getMessage()); } return new ConstantNode(value, new SourceRange(token)); }
private FormulaNode stringLiteral() { Token token = lexer.next(); assert token.getType() == TokenType.STRING_LITERAL; return new ConstantNode(token.getString(), new SourceRange(token)); }
private QueryModel buildQuery(List<EffectiveTableColumn> columns, RecordRef recordRef) { QueryModel queryModel = buildQuery(columns); queryModel.setFilter(Formulas.equals(new SymbolNode("@parent"), new ConstantNode(recordRef.getRecordId().asString()))); return queryModel; }
@Test public void parseStringLiteral() { expect("A=='Foo'", call(EQ, symbol("A"), new ConstantNode("Foo"))); }
@Test public void parseCalc() { expect("{Exp}*{Alloc}*{InCostUnsp}/10000", new FunctionCallNode(FormulaFunctions.get("/"), new FunctionCallNode(FormulaFunctions.get("*"), new FunctionCallNode(FormulaFunctions.get("*"), new SymbolNode("Exp"), new SymbolNode("Alloc")), new SymbolNode("InCostUnsp")), new ConstantNode(10000))); }