SmartDataAnalytics/jena-sparql-api

SPARQLContainement example issue

Closed this issue · 6 comments

In the readme it states that SparqlQueryContainmentUtils has the method checkContainment.
But this is not the case.
Is there another class which has this method or is the function missing/renamed?

Same here. I would like to check if two sparql queries are equivalent or not, and I think this function could be used to do that. Nonetheless, the class SparqlQueryContainmentUtils does not contain the method checkContainment. How can we do that with the latest version of the library?

There are two test case runners TestSparqlQueryContainmentSimple.java and TestSparqlQueryContainmentWithInrialpesQcBenchmark.java

The mere boolean test is available in

boolean actualVerdict = SparqlQueryContainmentUtils.tryMatch(viewQuery, userQuery);

However, the first test runner creates a TreeMapping object which contains the variable mapping. Note that besides variables mentioned in the query itself, additional intermediate variables ?v_{number} may get allocated in the process; especially every constant in a triple pattern is normalized into an appropriate filter, so { ?x C ?y}' becomes { ?x ?v_1 ?y FILTER(?v_1 = C)`

    @Test
    public void test() {
        printOutQueryContainments("SELECT * { ?a ?b ?c }", "SELECT * { ?s ?p ?o . FILTER(?p = <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>) }");
    }

Output:

TreeMapping [overallMatching={?c=?o, ?b=?p, ?a=?s, ?v_1=?v_1}]

Cool; its quite some time since I wrote the code :)
Ah, and all triple patterns are expanded to quads - so this is where the v_1 in above example comes from - its the implicit variable for the graph component.

Hi @Aklakan, thanks for your response and for your fix! However, I am having troubles executing the following code, taken from the example:

	public static void main(String[] args) {
		String vStr = "SELECT * { ?a ?b ?c }";
		String qStr = "SELECT * { ?s ?p ?o . FILTER(?p = <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>) }";
		boolean isContained = SparqlQueryContainmentUtils.tryMatch(vStr, qStr);
	}

I get the following parser exception:

Exception in thread "main" org.apache.jena.query.QueryParseException: Encountered " "select" "SELECT "" at line 1, column 11.
Was expecting one of:
    <IRIref> ...
    <PNAME_NS> ...
    <PNAME_LN> ...
    <BLANK_NODE_LABEL> ...
    <VAR1> ...
    <VAR2> ...
    "graph" ...
    "optional" ...
    "filter" ...
    "true" ...
    "false" ...
    <INTEGER> ...
    <DECIMAL> ...
    <DOUBLE> ...
    <INTEGER_POSITIVE> ...
    <DECIMAL_POSITIVE> ...
    <DOUBLE_POSITIVE> ...
    <INTEGER_NEGATIVE> ...
    <DECIMAL_NEGATIVE> ...
    <DOUBLE_NEGATIVE> ...
    <STRING_LITERAL1> ...
    <STRING_LITERAL2> ...
    <STRING_LITERAL_LONG1> ...
    <STRING_LITERAL_LONG2> ...
    "(" ...
    <NIL> ...
    "{" ...
    "}" ...
    "[" ...
    <ANON> ...
    
	at org.apache.jena.sparql.lang.ParserSPARQL10.perform(ParserSPARQL10.java:99)
	at org.apache.jena.sparql.lang.ParserSPARQL10.parse$(ParserSPARQL10.java:52)
	at org.apache.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:34)
	at org.apache.jena.query.QueryFactory.parse(QueryFactory.java:147)
	at org.aksw.jena_sparql_api.stmt.SparqlQueryParserImpl.apply(SparqlQueryParserImpl.java:34)
	at org.aksw.jena_sparql_api.stmt.SparqlQueryParserImpl.apply(SparqlQueryParserImpl.java:13)
	at org.aksw.jena_sparql_api.stmt.SparqlElementParserImpl.apply(SparqlElementParserImpl.java:35)
	at org.aksw.jena_sparql_api.stmt.SparqlElementParserImpl.apply(SparqlElementParserImpl.java:11)
	at org.aksw.jena_sparql_api.query_containment.core.SparqlQueryContainmentUtils.tryMatch(SparqlQueryContainmentUtils.java:89)
	at test.Test.main(Test.java:24)

This is executed in a test project that only contains this library. The unique dependency in my pom is:

		<dependency>
			<groupId>org.aksw.jena-sparql-api</groupId>
			<artifactId>jena-sparql-api-query-containment</artifactId>
			<version>3.14.0-1</version>
		</dependency>

Maybe I am doing something wrong?

Well, I had to create a Query object from the strings as follows:

Query vStr = QueryFactory.create("SELECT * { ?a ?b ?c }", Syntax.syntaxSPARQL_10);
Query qStr = QueryFactory.create("SELECT * { ?s ?p ?o . FILTER(?p = <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>) }", Syntax.syntaxSPARQL_10);

And then it worked.