Search in sources :

Example 1 with MemberExpr

use of org.n52.svalbard.odata.core.expr.MemberExpr in project arctic-sea by 52North.

the class STAQueryOptionVisitor method visitArithmeticExpr.

@Override
public ArithmeticExpr visitArithmeticExpr(STAQueryOptionsGrammar.ArithmeticExprContext ctx) {
    // arithmeticExpr
    // : (OP (SP)*)? (numericLiteral | memberExpr | negateExpr | arithmeticMethodCallExpr) (addExpr | subExpr |
    // mulExpr | divExpr | modExpr)? (OP (SP)*)?
    // ;
    ArithmeticExpr left;
    if (ctx.numericLiteral() != null) {
        left = new NumericValueExpr(ctx.numericLiteral().getText());
    } else if (ctx.memberExpr() != null) {
        left = new MemberExpr(ctx.memberExpr().getText());
    } else if (ctx.negateExpr() != null) {
        // TODO: check if this can be done nicer than subtracting from 0
        left = new SimpleArithmeticExpr(FilterConstants.SimpleArithmeticOperator.Sub, new NumericValueExpr(0), this.visitArithmeticExpr(ctx.negateExpr().arithmeticExpr()));
    } else {
        left = this.visitArithmeticMethodCallExpr(ctx.arithmeticMethodCallExpr());
    }
    ArithmeticExpr right;
    if (ctx.addExpr() != null) {
        right = visitAddExpr(ctx.addExpr());
        return new SimpleArithmeticExpr(FilterConstants.SimpleArithmeticOperator.Add, left, right);
    } else if (ctx.subExpr() != null) {
        right = visitSubExpr(ctx.subExpr());
        return new SimpleArithmeticExpr(FilterConstants.SimpleArithmeticOperator.Sub, left, right);
    } else if (ctx.mulExpr() != null) {
        right = visitMulExpr(ctx.mulExpr());
        return new SimpleArithmeticExpr(FilterConstants.SimpleArithmeticOperator.Mul, left, right);
    } else if (ctx.divExpr() != null) {
        right = visitDivExpr(ctx.divExpr());
        return new SimpleArithmeticExpr(FilterConstants.SimpleArithmeticOperator.Div, left, right);
    } else if (ctx.modExpr() != null) {
        right = visitModExpr(ctx.modExpr());
        return new SimpleArithmeticExpr(FilterConstants.SimpleArithmeticOperator.Mod, left, right);
    } else {
        // There is no right Expr. return left only
        return left;
    }
}
Also used : SimpleArithmeticExpr(org.n52.svalbard.odata.core.expr.arithmetic.SimpleArithmeticExpr) ArithmeticExpr(org.n52.svalbard.odata.core.expr.arithmetic.ArithmeticExpr) MemberExpr(org.n52.svalbard.odata.core.expr.MemberExpr) NumericValueExpr(org.n52.svalbard.odata.core.expr.arithmetic.NumericValueExpr) SimpleArithmeticExpr(org.n52.svalbard.odata.core.expr.arithmetic.SimpleArithmeticExpr)

Example 2 with MemberExpr

use of org.n52.svalbard.odata.core.expr.MemberExpr in project arctic-sea by 52North.

the class ODataFesParser method getMemberValuePair.

/**
 * Get the the pair of value and member expression from the to expressions or {@code Optional.empty()} if the
 * expression do not match the types.
 *
 * @param first  the first expression
 * @param second the second expression
 * @return the member-value-pair
 */
private static Optional<MemberValueExprPair> getMemberValuePair(Expr first, Expr second) {
    if (first.asMember().isPresent()) {
        MemberExpr member = first.asMember().get();
        Optional<TextExpr> valueOpt = second.asTextValue();
        return valueOpt.map(textExpr -> new MemberValueExprPair(member, textExpr));
    } else {
        MemberExpr member = second.asMember().get();
        Optional<TextExpr> valueOpt = first.asTextValue();
        return valueOpt.map(textExpr -> new MemberValueExprPair(member, textExpr));
    }
}
Also used : TextExpr(org.n52.svalbard.odata.core.expr.TextExpr) MemberExpr(org.n52.svalbard.odata.core.expr.MemberExpr)

Example 3 with MemberExpr

use of org.n52.svalbard.odata.core.expr.MemberExpr in project arctic-sea by 52North.

the class ExpandQueryOptionTest method complexExpandOption.

@Test
public void complexExpandOption() {
    init(ODataConstants.QueryOptions.EXPAND + EQ + "Observations($filter=result eq 1;$expand=FeatureOfInterest;$select=id;$orderby=id;" + "$skip=5;$top=10;$count=true),ObservedProperty");
    QueryOptions options = (QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor());
    Assertions.assertTrue(options.hasExpandFilter());
    Assertions.assertTrue(options.getExpandFilter() instanceof ExpandFilter);
    Set<ExpandItem> items = (options.getExpandFilter().getItems());
    Assertions.assertNotNull(items);
    Assertions.assertEquals(2, items.size());
    for (ExpandItem obs : items) {
        if (obs.getPath().equals("Observations")) {
            Assertions.assertEquals("Observations", obs.getPath());
            Assertions.assertTrue(obs.getQueryOptions().hasExpandFilter());
            Assertions.assertTrue(obs.getQueryOptions().hasFilterFilter());
            Assertions.assertTrue(obs.getQueryOptions().hasSelectFilter());
            Assertions.assertTrue(obs.getQueryOptions().hasOrderByFilter());
            Assertions.assertTrue(obs.getQueryOptions().hasSkipFilter());
            Assertions.assertTrue(obs.getQueryOptions().hasTopFilter());
            Assertions.assertTrue(obs.getQueryOptions().hasCountFilter());
            Set<FilterClause> filters = new HashSet<>();
            filters.add(new FilterFilter(new ComparisonExpr(FilterConstants.ComparisonOperator.PropertyIsEqualTo, new MemberExpr("result"), new NumericValueExpr("1"))));
            filters.add(new ExpandFilter(new ExpandItem("FeatureOfInterest", new QueryOptions("", null))));
            filters.add(new SelectFilter("id"));
            filters.add(new OrderByFilter(new OrderProperty("id")));
            filters.add(new SkipTopFilter(FilterConstants.SkipTopOperator.Skip, 5L));
            filters.add(new SkipTopFilter(FilterConstants.SkipTopOperator.Top, 10L));
            filters.add(new CountFilter(true));
            Assertions.assertEquals(new QueryOptions("", filters), obs.getQueryOptions());
        } else if (obs.getPath().equals("ObservedProperty")) {
            Assertions.assertEquals("ObservedProperty", obs.getPath());
            Assertions.assertNotNull(obs.getQueryOptions());
            Assertions.assertTrue(obs.getQueryOptions().hasTopFilter());
            Assertions.assertFalse(obs.getQueryOptions().hasExpandFilter());
            Assertions.assertFalse(obs.getQueryOptions().hasFilterFilter());
            Assertions.assertFalse(obs.getQueryOptions().hasSelectFilter());
            Assertions.assertFalse(obs.getQueryOptions().hasOrderByFilter());
            Assertions.assertFalse(obs.getQueryOptions().hasSkipFilter());
            Assertions.assertFalse(obs.getQueryOptions().hasCountFilter());
        } else {
            Assertions.fail("Did not find expected expandItem!");
        }
    }
}
Also used : ComparisonExpr(org.n52.svalbard.odata.core.expr.bool.ComparisonExpr) SkipTopFilter(org.n52.shetland.filter.SkipTopFilter) NumericValueExpr(org.n52.svalbard.odata.core.expr.arithmetic.NumericValueExpr) QueryOptions(org.n52.shetland.oasis.odata.query.option.QueryOptions) ExpandFilter(org.n52.shetland.filter.ExpandFilter) ExpandItem(org.n52.shetland.filter.ExpandItem) MemberExpr(org.n52.svalbard.odata.core.expr.MemberExpr) FilterClause(org.n52.shetland.ogc.filter.FilterClause) SelectFilter(org.n52.shetland.filter.SelectFilter) OrderProperty(org.n52.shetland.filter.OrderProperty) FilterFilter(org.n52.shetland.filter.FilterFilter) CountFilter(org.n52.shetland.filter.CountFilter) OrderByFilter(org.n52.shetland.filter.OrderByFilter) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with MemberExpr

use of org.n52.svalbard.odata.core.expr.MemberExpr in project arctic-sea by 52North.

the class ExpandQueryOptionTest method testValidExpandOption.

@Test
public void testValidExpandOption() {
    String val, nested;
    ExpandFilter actual, reference;
    QueryOptions defaultQO = new QueryOptions("", null);
    Set<FilterClause> nestedFilters;
    // Simple
    val = "test";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    reference = new ExpandFilter(new ExpandItem(val, defaultQO));
    Assertions.assertEquals(reference, actual);
    // Resolve nested Expand
    val = "test";
    nested = "nested";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val + "/" + nested);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    nestedFilters = new HashSet<>();
    nestedFilters.add(new ExpandFilter(new ExpandItem(nested, defaultQO)));
    reference = new ExpandFilter(new ExpandItem(val, new QueryOptions("", nestedFilters)));
    Assertions.assertEquals(reference, actual);
    // Expand with queryOptions
    val = "test";
    String queryOption = "($filter=id eq '2')";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val + queryOption);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    nestedFilters = new HashSet<>();
    nestedFilters.add(new FilterFilter(new ComparisonExpr(FilterConstants.ComparisonOperator.PropertyIsEqualTo, new MemberExpr("id"), new StringValueExpr("2"))));
    reference = new ExpandFilter(new ExpandItem(val, new QueryOptions("", nestedFilters)));
    Assertions.assertEquals(reference, actual);
    // Expand with queryOptions
    val = "test";
    queryOption = "($top=52)";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val + queryOption);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    nestedFilters = new HashSet<>();
    nestedFilters.add(new SkipTopFilter(FilterConstants.SkipTopOperator.Top, 52L));
    reference = new ExpandFilter(new ExpandItem(val, new QueryOptions("", nestedFilters)));
    Assertions.assertEquals(reference, actual);
    // Expand with queryOptions
    val = "test";
    queryOption = "($skip=52)";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val + queryOption);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    nestedFilters = new HashSet<>();
    nestedFilters.add(new SkipTopFilter(FilterConstants.SkipTopOperator.Skip, 52L));
    reference = new ExpandFilter(new ExpandItem(val, new QueryOptions("", nestedFilters)));
    Assertions.assertEquals(reference, actual);
    // Expand with queryOptions
    val = "test";
    queryOption = "($select=id)";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val + queryOption);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    nestedFilters = new HashSet<>();
    nestedFilters.add(new SelectFilter("id"));
    reference = new ExpandFilter(new ExpandItem(val, new QueryOptions("", nestedFilters)));
    Assertions.assertEquals(reference, actual);
    // Expand with queryOptions
    val = "test";
    queryOption = "($expand=nested)";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val + queryOption);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    nestedFilters = new HashSet<>();
    nestedFilters.add(new ExpandFilter(new ExpandItem(nested, defaultQO)));
    reference = new ExpandFilter(new ExpandItem(val, new QueryOptions("", nestedFilters)));
    Assertions.assertEquals(reference, actual);
    // Expand with queryOptions
    val = "test";
    queryOption = "($orderby=id asc)";
    init(ODataConstants.QueryOptions.EXPAND + EQ + val + queryOption);
    actual = ((QueryOptions) parser.queryOptions().accept(new STAQueryOptionVisitor())).getExpandFilter();
    nestedFilters = new HashSet<>();
    nestedFilters.add(new OrderByFilter(new OrderProperty("id", FilterConstants.SortOrder.ASC)));
    reference = new ExpandFilter(new ExpandItem(val, new QueryOptions("", nestedFilters)));
    Assertions.assertEquals(reference, actual);
}
Also used : StringValueExpr(org.n52.svalbard.odata.core.expr.StringValueExpr) ComparisonExpr(org.n52.svalbard.odata.core.expr.bool.ComparisonExpr) SkipTopFilter(org.n52.shetland.filter.SkipTopFilter) QueryOptions(org.n52.shetland.oasis.odata.query.option.QueryOptions) ExpandFilter(org.n52.shetland.filter.ExpandFilter) ExpandItem(org.n52.shetland.filter.ExpandItem) MemberExpr(org.n52.svalbard.odata.core.expr.MemberExpr) FilterClause(org.n52.shetland.ogc.filter.FilterClause) SelectFilter(org.n52.shetland.filter.SelectFilter) OrderProperty(org.n52.shetland.filter.OrderProperty) FilterFilter(org.n52.shetland.filter.FilterFilter) OrderByFilter(org.n52.shetland.filter.OrderByFilter) Test(org.junit.jupiter.api.Test)

Aggregations

MemberExpr (org.n52.svalbard.odata.core.expr.MemberExpr)4 Test (org.junit.jupiter.api.Test)2 ExpandFilter (org.n52.shetland.filter.ExpandFilter)2 ExpandItem (org.n52.shetland.filter.ExpandItem)2 FilterFilter (org.n52.shetland.filter.FilterFilter)2 OrderByFilter (org.n52.shetland.filter.OrderByFilter)2 OrderProperty (org.n52.shetland.filter.OrderProperty)2 SelectFilter (org.n52.shetland.filter.SelectFilter)2 SkipTopFilter (org.n52.shetland.filter.SkipTopFilter)2 QueryOptions (org.n52.shetland.oasis.odata.query.option.QueryOptions)2 FilterClause (org.n52.shetland.ogc.filter.FilterClause)2 NumericValueExpr (org.n52.svalbard.odata.core.expr.arithmetic.NumericValueExpr)2 ComparisonExpr (org.n52.svalbard.odata.core.expr.bool.ComparisonExpr)2 HashSet (java.util.HashSet)1 CountFilter (org.n52.shetland.filter.CountFilter)1 StringValueExpr (org.n52.svalbard.odata.core.expr.StringValueExpr)1 TextExpr (org.n52.svalbard.odata.core.expr.TextExpr)1 ArithmeticExpr (org.n52.svalbard.odata.core.expr.arithmetic.ArithmeticExpr)1 SimpleArithmeticExpr (org.n52.svalbard.odata.core.expr.arithmetic.SimpleArithmeticExpr)1