Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ protected void copy(GroupGraphPattern original) {
@Override
public GroupGraphPattern and(GraphPattern... patterns) {
if (isEmpty() && patterns.length == 1 && (isGGP(patterns[0]))) {
copy(GraphPatterns.extractOrConvertToGGP(patterns[0]));
GroupGraphPattern ggp = GraphPatterns.extractOrConvertToGGP(patterns[0]);
// Only copy if it's a plain GroupGraphPattern, not a specialized subclass
// like FilterExistsGraphPattern or MinusGraphPattern which override getQueryString()
if (ggp.getClass() == GroupGraphPattern.class) {
copy(ggp);
} else {
addElements(patterns);
}
} else {
addElements(patterns);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.rdf4j.sparqlbuilder.core.SparqlBuilder;
import org.eclipse.rdf4j.sparqlbuilder.core.Variable;
import org.eclipse.rdf4j.sparqlbuilder.examples.BaseExamples;
import org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPatternNotTriples;
import org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPatterns;
import org.eclipse.rdf4j.sparqlbuilder.graphpattern.TriplePattern;
import org.eclipse.rdf4j.sparqlbuilder.rdf.Iri;
Expand Down Expand Up @@ -53,4 +54,68 @@ public void example_issue_1481() {
"WHERE { OPTIONAL { ?subject ?predicate <http://my-example.com/anyIRI/> . }\n" +
"OPTIONAL { ?subject <http://my-example.com/anyIRI/> ?object . } }");
}

@Test
public void example_broken_filter_not_exists() {
// given
Iri subjectIri = iri("http://my-example.com/anyIRI/");
Iri classIri = iri("http://my-example.com/SomeClass/");
TriplePattern triple = subjectIri.isA(classIri);

String queryString = Queries.MODIFY()
.insert(triple)
.where(GraphPatterns.filterNotExists(triple))
.getQueryString();

assertEquals("INSERT { <http://my-example.com/anyIRI/> a <http://my-example.com/SomeClass/> . }\n" +
// the WHERE clause is incorrectly generated:
// "WHERE { <http://my-example.com/anyIRI/> a <http://my-example.com/SomeClass/> . }",
// should be:
"WHERE { FILTER NOT EXISTS { <http://my-example.com/anyIRI/> a <http://my-example.com/SomeClass/> . } }",
queryString
);
}

@Test
public void test_GraphPatternNotTriples_getQueryString() {
// given
Iri subjectIri = iri("http://my-example.com/anyIRI/");
Iri classIri = iri("http://my-example.com/SomeClass/");
TriplePattern triple = subjectIri.isA(classIri);

String queryString = GraphPatterns.filterNotExists(triple).getQueryString();

assertEquals(
"FILTER NOT EXISTS { <http://my-example.com/anyIRI/> a <http://my-example.com/SomeClass/> . }",
queryString
);
}

@Test
public void test_GraphPatterns_and_getQueryString() {
GraphPatternNotTriples actual = GraphPatterns.and();
assertEquals("{}", actual.getQueryString());
}

@Test
public void test_GraphPatterns_and_FilterExistsGraphPattern_getQueryString() {

TriplePattern triple = iri("http://my-example.com/anyIRI/").isA(iri("http://my-example.com/SomeClass/"));

// emptyGraphPattern by itself yields "{}", see test_GraphPatterns_and_getQueryString
GraphPatternNotTriples emptyGraphPattern = GraphPatterns.and();

// filterNotExists by itself yields "FILTER NOT EXISTS { ... }", see test_GraphPatternNotTriples_getQueryString
GraphPatternNotTriples filterNotExists = GraphPatterns.filterNotExists(triple);

// this is the cause oft the failing example_broken_filter_not_exists test
GraphPatternNotTriples withFilterNotExists = emptyGraphPattern.and(filterNotExists);

String actual = withFilterNotExists.getQueryString();

assertEquals(
"{ FILTER NOT EXISTS { <http://my-example.com/anyIRI/> a <http://my-example.com/SomeClass/> . } }",
actual
);
}
}
Loading