use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class EntityOptimizerTest method getTupleExpr.
private TupleExpr getTupleExpr(String query) throws MalformedQueryException {
SPARQLParser sp = new SPARQLParser();
ParsedQuery pq = sp.parseQuery(query, null);
return pq.getTupleExpr();
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class FlattenedOptionalTest method testReOrderedBasicOptional.
@Test
public void testReOrderedBasicOptional() throws MalformedQueryException {
String query = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" ?e a ?c . " + //
" OPTIONAL{?e <uri:talksTo> ?l } . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + //
"}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
System.out.println(pq.getTupleExpr());
List<TupleExpr> joinArgs = getJoinArgs(pq.getTupleExpr(), new ArrayList<TupleExpr>());
// System.out.println(joinArgs);
FlattenedOptional optional = (FlattenedOptional) joinArgs.get(0);
TupleExpr sp1 = joinArgs.get(1);
TupleExpr sp2 = joinArgs.get(2);
Assert.assertEquals(false, optional.canRemoveTuple(sp1));
Assert.assertEquals(false, optional.canAddTuple(sp2));
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class FlattenedOptionalTest method testEqualsAndHashCode.
@Test
public void testEqualsAndHashCode() throws MalformedQueryException {
String query1 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" ?e <uri:worksAt> ?c . " + //
" OPTIONAL{?e <uri:talksTo> ?l } . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + //
"}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq1 = parser.parseQuery(query1, null);
List<TupleExpr> joinArgs1 = getJoinArgs(pq1.getTupleExpr(), new ArrayList<TupleExpr>());
String query2 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" ?e a ?c . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + //
" OPTIONAL{?e <uri:talksTo> ?l } . " + //
"}";
parser = new SPARQLParser();
ParsedQuery pq2 = parser.parseQuery(query2, null);
List<TupleExpr> joinArgs2 = getJoinArgs(pq2.getTupleExpr(), new ArrayList<TupleExpr>());
FlattenedOptional optional1 = (FlattenedOptional) joinArgs1.get(0);
FlattenedOptional optional2 = (FlattenedOptional) joinArgs2.get(0);
System.out.println(optional1 + " and " + optional2);
Assert.assertEquals(optional1, optional2);
Assert.assertEquals(optional1.hashCode(), optional2.hashCode());
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class JoinSegmentTest method testNoMatch.
@Test
public void testNoMatch() throws Exception {
String query1 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" Filter(?e = <uri:Bob>)" + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + //
" ?e a ?c . " + //
" ?e <uri:talksTo> ?l . " + //
"}";
String query2 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" Filter(?e = <uri:Bob>)" + //
" ?e a ?c . " + //
" ?e <uri:worksAt> ?l . " + //
"}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq1 = parser.parseQuery(query1, null);
ParsedQuery pq2 = parser.parseQuery(query2, null);
TupleExpr te1 = pq1.getTupleExpr();
TupleExpr te2 = pq2.getTupleExpr();
TopOfQueryFilterRelocator.moveFiltersToTop(te1);
TopOfQueryFilterRelocator.moveFiltersToTop(te1);
Filter filter1 = (Filter) ((Projection) te1).getArg();
Filter filter2 = (Filter) ((Projection) te2).getArg();
QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(filter1);
QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(filter2);
Assert.assertEquals(false, seg1.containsQuerySegment(seg2));
}
use of org.openrdf.query.parser.sparql.SPARQLParser in project incubator-rya by apache.
the class OptionalJoinSegmentTest method testBasicOptional.
@Test
public void testBasicOptional() throws MalformedQueryException {
String query1 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" ?e a ?c . " + //
" OPTIONAL{?e <uri:talksTo> ?l } . " + //
" ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l " + //
"}";
String query2 = //
"" + //
"SELECT ?e ?c ?l" + //
"{" + //
" ?e a ?c . " + //
" OPTIONAL{?e <uri:talksTo> ?l } . " + //
"}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq1 = parser.parseQuery(query1, null);
ParsedQuery pq2 = parser.parseQuery(query2, null);
TupleExpr te1 = pq1.getTupleExpr();
TupleExpr te2 = pq2.getTupleExpr();
Join join = (Join) ((Projection) te1).getArg();
LeftJoin lj = (LeftJoin) ((Projection) te2).getArg();
QuerySegment<ExternalTupleSet> seg1 = qFactory.getQuerySegment(join);
QuerySegment<ExternalTupleSet> seg2 = qFactory.getQuerySegment(lj);
Assert.assertEquals(true, seg1.containsQuerySegment(seg2));
Assert.assertEquals(join, seg1.getQuery().getTupleExpr());
SimpleExternalTupleSet pcj = new SimpleExternalTupleSet((Projection) te2);
List<QueryModelNode> nodes = seg1.getOrderedNodes();
QueryModelNode node = nodes.get(0);
seg1.replaceWithExternalSet(seg2, pcj);
Set<QueryModelNode> nodeSet = new HashSet<>();
nodeSet.add(node);
nodeSet.add(pcj);
Assert.assertEquals(nodeSet, seg1.getUnOrderedNodes());
}
Aggregations