1

How do I replicate the following filter from SPARQL in java using rd4j?

FILTER(?endDate > NOW() && ?startDate < NOW() && MONTH(?endDate) = MONTH(NOW()))

I have initialised expressions for MONTH() and NOW() as follows, and a graph pattern pattern, but don't know how to use them to extract the month from variable ?endDate and from the NOW() function

    Expression<?> nowFunc = Expressions.function(SparqlFunction.NOW );
    Expression<?> month = Expressions.function(SparqlFunction.MONTH);
    
    Expression<?> endDateGreaterThan = Expressions.gt(endDate, nowFunc);
    Expression<?> startDateLessThan = Expressions.lt(startDate, nowFunc);

The query I have so far is as follows, but I can't figure out how to implement the final part (****) of the filter into the query.

SelectQuery query = Queries.SELECT().prefix(ex).select(letter).where(pattern.filter(Expressions.
and(endDateGreaterThan, startDateLessThan, ****)
vaishalir
  • 55
  • 3
  • the signature is `Expressions.function(SparqlFunction function, Operand... operands)`, so you should provide the variable resp. the expression of `NOW()` as an operand of your `MONTH` expression – UninformedUser May 24 '21 at 16:31
  • 1
    Question was also asked (and answered) on RDF4J user list: https://groups.google.com/g/rdf4j-users/c/0b_2wQfMWkQ/m/C0MdQreLBwAJ – Jeen Broekstra May 25 '21 at 06:54

1 Answers1

0

It's no different from how you have set up your other expressions. You want an equals comparison operation between two value expressions (let's call them leftArg and rightArg), so:

Expression monthEquals = Expressions.equals(leftArg, rightArg);

Your leftArg and rightArg are both also functions, both with a single operand. The leftArg has a simple variable as its function operand:

Expression leftArg = Expressions.function(SparqlFunction.MONTH, endDate);

The rightArg has a NOW() function as its operand:

Expression rightArg = Expressions.function(SparqlFunction.MONTH,
        Expressions.function(SparqlFunction.NOW));

Just put it all together and you're done.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73