Search in sources :

Example 6 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class TestPredicateExpressionParser method testAndAnd.

@Test
public void testAndAnd() {
    final Predicate parsed = PredicateExpressionParser.parse("com.linkedin.data.it.AlwaysTruePredicate & com.linkedin.data.it.AlwaysTruePredicate & com.linkedin.data.it.AlwaysFalsePredicate");
    Assert.assertEquals(parsed.getClass(), AndPredicate.class);
    final List<Predicate> children = ((AndPredicate) parsed).getChildPredicates();
    Assert.assertEquals(children.get(0).getClass(), AlwaysTruePredicate.class);
    Assert.assertEquals(children.get(1).getClass(), AlwaysTruePredicate.class);
    Assert.assertEquals(children.get(2).getClass(), AlwaysFalsePredicate.class);
}
Also used : AndPredicate(com.linkedin.data.it.AndPredicate) AndPredicate(com.linkedin.data.it.AndPredicate) NotPredicate(com.linkedin.data.it.NotPredicate) AlwaysTruePredicate(com.linkedin.data.it.AlwaysTruePredicate) AlwaysFalsePredicate(com.linkedin.data.it.AlwaysFalsePredicate) OrPredicate(com.linkedin.data.it.OrPredicate) Predicate(com.linkedin.data.it.Predicate) Test(org.testng.annotations.Test)

Example 7 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class TestPredicateExpressionParser method testAnd.

@Test
public void testAnd() {
    final Predicate parsed = PredicateExpressionParser.parse("com.linkedin.data.it.AlwaysTruePredicate & com.linkedin.data.it.AlwaysFalsePredicate");
    Assert.assertEquals(parsed.getClass(), AndPredicate.class);
    final List<Predicate> children = ((AndPredicate) parsed).getChildPredicates();
    Assert.assertEquals(children.get(0).getClass(), AlwaysTruePredicate.class);
    Assert.assertEquals(children.get(1).getClass(), AlwaysFalsePredicate.class);
}
Also used : AndPredicate(com.linkedin.data.it.AndPredicate) AndPredicate(com.linkedin.data.it.AndPredicate) NotPredicate(com.linkedin.data.it.NotPredicate) AlwaysTruePredicate(com.linkedin.data.it.AlwaysTruePredicate) AlwaysFalsePredicate(com.linkedin.data.it.AlwaysFalsePredicate) OrPredicate(com.linkedin.data.it.OrPredicate) Predicate(com.linkedin.data.it.Predicate) Test(org.testng.annotations.Test)

Example 8 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class TestPredicateExpressionParser method testNotParenOr.

@Test
public void testNotParenOr() {
    final Predicate parsed = PredicateExpressionParser.parse("!(com.linkedin.data.it.AlwaysTruePredicate | com.linkedin.data.it.AlwaysFalsePredicate)");
    Assert.assertEquals(parsed.getClass(), NotPredicate.class);
    final Predicate intermediate = ((NotPredicate) parsed).getChildPredicate();
    Assert.assertEquals(intermediate.getClass(), OrPredicate.class);
    final List<Predicate> children = ((OrPredicate) intermediate).getChildPredicates();
    Assert.assertEquals(children.get(0).getClass(), AlwaysTruePredicate.class);
    Assert.assertEquals(children.get(1).getClass(), AlwaysFalsePredicate.class);
}
Also used : OrPredicate(com.linkedin.data.it.OrPredicate) NotPredicate(com.linkedin.data.it.NotPredicate) AndPredicate(com.linkedin.data.it.AndPredicate) NotPredicate(com.linkedin.data.it.NotPredicate) AlwaysTruePredicate(com.linkedin.data.it.AlwaysTruePredicate) AlwaysFalsePredicate(com.linkedin.data.it.AlwaysFalsePredicate) OrPredicate(com.linkedin.data.it.OrPredicate) Predicate(com.linkedin.data.it.Predicate) Test(org.testng.annotations.Test)

Example 9 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class PredicateExpressionParser method parse.

public static Predicate parse(String expression) {
    final Stack<Predicate> predicateStack = new Stack<Predicate>();
    final Stack<Character> operatorStack = new Stack<Character>();
    final String trimmedExpression = expression.replaceAll("\\s", "");
    final StringTokenizer tokenizer = new StringTokenizer(trimmedExpression, OPERATORS, true);
    boolean isTokenMode = true;
    while (true) {
        final Character operator;
        final String token;
        if (isTokenMode) {
            if (tokenizer.hasMoreTokens()) {
                token = tokenizer.nextToken();
            } else {
                break;
            }
            if (OPERATORS.contains(token)) {
                operator = token.charAt(0);
            } else {
                operator = null;
            }
        } else {
            operator = operatorStack.pop();
            token = null;
        }
        isTokenMode = true;
        if (operator == null) {
            try {
                predicateStack.push(Class.forName(token).asSubclass(Predicate.class).newInstance());
            } catch (ClassCastException e) {
                throw new RuntimeException(token + " must implement " + Predicate.class.getName(), e);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            if (operatorStack.empty() || operator == '(') {
                operatorStack.push(operator);
            } else if (operator == ')') {
                while (operatorStack.peek() != '(') {
                    evaluate(predicateStack, operatorStack);
                }
                operatorStack.pop();
            } else {
                if (OPERATOR_PRECEDENCE.get(operator) < OPERATOR_PRECEDENCE.get(operatorStack.peek())) {
                    evaluate(predicateStack, operatorStack);
                    isTokenMode = false;
                }
                operatorStack.push(operator);
            }
        }
    }
    while (!operatorStack.empty()) {
        evaluate(predicateStack, operatorStack);
    }
    if (predicateStack.size() > 1) {
        throw new RuntimeException("Invalid logical expression");
    }
    return predicateStack.pop();
}
Also used : StringTokenizer(java.util.StringTokenizer) Stack(java.util.Stack) Predicate(com.linkedin.data.it.Predicate)

Example 10 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class PredicateExpressionParser method evaluate.

private static void evaluate(Stack<Predicate> predicateStack, Stack<Character> operatorStack) {
    final char operator = operatorStack.pop();
    final Predicate evaluatedPredicate;
    switch(operator) {
        case '&':
        case '|':
            evaluatedPredicate = evaluateMultiaryOperator(predicateStack, operatorStack, operator);
            break;
        case '!':
            evaluatedPredicate = Predicates.not(predicateStack.pop());
            break;
        default:
            throw new RuntimeException("Unknown operator: " + operator);
    }
    predicateStack.push(evaluatedPredicate);
}
Also used : Predicate(com.linkedin.data.it.Predicate)

Aggregations

Predicate (com.linkedin.data.it.Predicate)33 Test (org.testng.annotations.Test)28 AlwaysFalsePredicate (com.linkedin.data.it.AlwaysFalsePredicate)26 AlwaysTruePredicate (com.linkedin.data.it.AlwaysTruePredicate)26 AndPredicate (com.linkedin.data.it.AndPredicate)26 NotPredicate (com.linkedin.data.it.NotPredicate)26 OrPredicate (com.linkedin.data.it.OrPredicate)26 DataSchema (com.linkedin.data.schema.DataSchema)4 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)4 SchemaParser (com.linkedin.data.schema.SchemaParser)4 DataMap (com.linkedin.data.DataMap)3 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 Schema (org.apache.avro.Schema)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 DataElement (com.linkedin.data.element.DataElement)1 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1