protected int verifyQueryResult( CloseableIteration<? extends BindingSet, QueryEvaluationException> resultIter, int expectedBindingCount) throws QueryEvaluationException { int resultCount = 0; while (resultIter.hasNext()) { BindingSet resultBindings = resultIter.next(); resultCount++; assertEquals( "Wrong number of binding names for binding set", expectedBindingCount, resultBindings.getBindingNames().size()); int bindingCount = 0; Iterator<Binding> bindingIter = resultBindings.iterator(); while (bindingIter.hasNext()) { bindingIter.next(); bindingCount++; } assertEquals("Wrong number of bindings in binding set", expectedBindingCount, bindingCount); } return resultCount; }
public Collection<String> getAllDisjointProperties(String startingProperty) throws RepositoryException, MalformedQueryException, QueryEvaluationException { Set<String> allDisjointProperties = new HashSet<>(); RepositoryConnection con = this.repository.getConnection(); try { TupleQuery allDisjointPropertiesQuery = con.prepareTupleQuery( QueryLanguage.SPARQL, "SELECT ?items " + "WHERE { ?y a owl:AllDisjointProperties ." + "?y owl:members ?members ." + "?members rdf:rest*/rdf:first <" + startingProperty + "> ." + "?members rdf:rest*/rdf:first ?items ." + "FILTER (?items != <" + startingProperty + ">) .}"); TupleQueryResult allDisjointPropertiesResult = allDisjointPropertiesQuery.evaluate(); try { while (allDisjointPropertiesResult.hasNext()) { BindingSet bindingSet = allDisjointPropertiesResult.next(); Value name = bindingSet.getValue("items"); allDisjointProperties.add(name.toString()); } } finally { allDisjointPropertiesResult.close(); } } finally { con.close(); } return allDisjointProperties; }
private void splitJourneyLocation( ConcurrentMap<String, IRI> variableTypes, List<LDObject> journeyLocation, List<LDObject> journeyDatas, BindingSet bindings) { LDObject locationsData = new LDObject(); LDObject journeyData = new LDObject(); bindings .getBindingNames() .stream() .forEach( bindingName -> { org.openrdf.model.Value value = bindings.getBinding(bindingName).getValue(); QueryResultsParser.updateVariableTypes(variableTypes, bindingName, value); if (Arrays.asList("id", "creationDate", "name", "status").contains(bindingName)) { journeyData.addKeyValue(new KeyValue(bindingName, value.stringValue())); } else { locationsData.addKeyValue(new KeyValue(bindingName, value.stringValue())); } if ("locationId".equalsIgnoreCase(bindingName)) { journeyData.addKeyValue(new KeyValue(bindingName, value.stringValue())); } }); journeyLocation.add(locationsData); journeyDatas.add(journeyData); }
/** * Execute a SELECT SPARQL query against the graphs * * @param qs SELECT SPARQL query * @return list of solutions, each containing a hashmap of bindings */ public List<HashMap<String, Value>> runSPARQL(String qs) { try { RepositoryConnection con = currentRepository.getConnection(); try { TupleQuery query = con.prepareTupleQuery(org.openrdf.query.QueryLanguage.SPARQL, qs); TupleQueryResult qres = query.evaluate(); ArrayList<HashMap<String, Value>> reslist = new ArrayList<HashMap<String, Value>>(); while (qres.hasNext()) { BindingSet b = qres.next(); Set<String> names = b.getBindingNames(); HashMap<String, Value> hm = new HashMap<String, Value>(); for (String n : names) { hm.put(n, b.getValue(n)); } reslist.add(hm); } return reslist; } finally { con.close(); } } catch (Exception e) { e.printStackTrace(); } return null; }
public Collection<String> getIntersectionOf(String startingClass) throws RepositoryException, MalformedQueryException, QueryEvaluationException { Set<String> intersectionOfClasses = new HashSet<>(); RepositoryConnection con = this.repository.getConnection(); try { TupleQuery intersectionOfQuery = con.prepareTupleQuery( QueryLanguage.SPARQL, "SELECT ?c WHERE { ?c owl:intersectionOf ?l . " + "?l rdf:rest*/rdf:first <" + startingClass + "> .}"); TupleQueryResult intersectionOfResult = intersectionOfQuery.evaluate(); try { while (intersectionOfResult.hasNext()) { BindingSet bindingSet = intersectionOfResult.next(); Value name = bindingSet.getValue("c"); intersectionOfClasses.add(name.toString()); // System.out.println("intersections Ofs : " + name); } } finally { intersectionOfResult.close(); } } finally { con.close(); } return intersectionOfClasses; }
public void tupleQuery() throws QueryEvaluationException, RepositoryException, MalformedQueryException { // /query repo // con.setNamespace("onto", // "<http://it.unibz.krdb/obda/ontologies/test/translation/onto2.owl#>"); // System.out.println(con.getNamespaces().next().toString()); String queryString = "PREFIX : \n<http://it.unibz.krdb/obda/ontologies/test/translation/onto2.owl#>\n " + "SELECT ?x ?y WHERE { ?x a :Person. ?x :age ?y } "; // String queryString = // "SELECT ?x ?y WHERE { ?x a onto:Person. ?x onto:age ?y } "; TupleQuery tupleQuery = (con).prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult result = tupleQuery.evaluate(); System.out.println(result.getBindingNames()); while (result.hasNext()) { BindingSet bindingSet = result.next(); Value valueOfX = bindingSet.getValue("x"); Literal valueOfY = (Literal) bindingSet.getValue("y"); System.out.println(valueOfX.stringValue() + ", " + valueOfY.floatValue()); } result.close(); }
private void calculateGraphsAndDataSets() { try { RepositoryConnection conn = repository.getConnection(); TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, SparqlUtils.PREFIXES + QUERY); TupleQueryResult results = q.evaluate(); graphs = new LinkedList<DataCubeGraph>(); datasets = new LinkedList<DataSet>(); String lastG = null; String lastDSD = null; SparqlDCGraph dcGraph; SparqlStructure dcStructure = null; Collection<DataSet> graphDataSets = null; Collection<Structure> graphStructures = null; Collection<DataSet> structDataSets = null; while (results.hasNext()) { BindingSet set = results.next(); String g = set.getValue("g").stringValue(); String ds = set.getValue("ds").stringValue(); String dsd = set.getValue("dsd").stringValue(); if (!g.equals(lastG)) { // new Graph dcGraph = new SparqlDCGraph(repository, g); graphDataSets = new LinkedList<DataSet>(); graphStructures = new LinkedList<Structure>(); dcGraph.setDatasets(graphDataSets); dcGraph.setStructures(graphStructures); graphs.add(dcGraph); // new structure dcStructure = new SparqlStructure(repository, dsd, g); structDataSets = new LinkedList<DataSet>(); dcStructure.setDatasets(structDataSets); graphStructures.add(dcStructure); } else if (!dsd.equals(lastDSD)) { // new structure dcStructure = new SparqlStructure(repository, dsd, g); structDataSets = new LinkedList<DataSet>(); dcStructure.setDatasets(structDataSets); graphStructures.add(dcStructure); } SparqlDataSet dcDataSet = new SparqlDataSet(repository, ds, g); dcDataSet.setStructure(dcStructure); graphDataSets.add(dcDataSet); structDataSets.add(dcDataSet); datasets.add(dcDataSet); lastG = g; lastDSD = dsd; } } catch (RepositoryException ex) { Logger.getLogger(SparqlDCRepository.class.getName()).log(Level.SEVERE, null, ex); } catch (MalformedQueryException ex) { Logger.getLogger(SparqlDCRepository.class.getName()).log(Level.SEVERE, null, ex); } catch (QueryEvaluationException ex) { Logger.getLogger(SparqlDCRepository.class.getName()).log(Level.SEVERE, null, ex); } }
@Override protected BindingSet getNextElement() throws QueryEvaluationException { if (hashTable == null) { setupHashTable(); } while (currentScanElem == null) { if (scanList.size() > 0) { currentScanElem = scanList.remove(0); } else { if (restIter.hasNext()) { currentScanElem = restIter.next(); } else { // no more elements available return null; } } if (currentScanElem instanceof EmptyBindingSet) { // the empty bindingset should be merged with all bindingset in // the hash table hashTableValues = new ArrayList<BindingSet>(); for (BindingSet key : hashTable.keySet()) { hashTableValues.addAll(hashTable.get(key)); } } else { BindingSet key = calcKey(currentScanElem, joinAttributes); if (hashTable.containsKey(key)) { hashTableValues = new ArrayList<BindingSet>(hashTable.get(key)); } else { currentScanElem = null; hashTableValues = null; } } } BindingSet nextHashTableValue = hashTableValues.remove(0); QueryBindingSet result = new QueryBindingSet(currentScanElem); for (String name : nextHashTableValue.getBindingNames()) { Binding b = nextHashTableValue.getBinding(name); if (!result.hasBinding(name)) { result.addBinding(b); } } if (hashTableValues.size() == 0) { // we've exhausted the current scanlist entry currentScanElem = null; hashTableValues = null; } return result; }
public static RootOAMOData parseOAMORootData(TupleQueryResult qres) { RootOAMOData rootOAMOData = new RootOAMOData(); try { // while (qres.hasNext()) // { BindingSet b = qres.next(); Set names = b.getBindingNames(); for (Object n : names) { if (((String) n).equalsIgnoreCase("oamoName")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); rootOAMOData.setOamoName(str); System.out.print("oamoName: " + rootOAMOData.getOamoName()); } else if (((String) n).equalsIgnoreCase("userID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); rootOAMOData.setUserID(str); System.out.print("userID: " + rootOAMOData.getUserID()); } // else if(((String) n).equalsIgnoreCase("serviceID")) // { // String str = (b.getValue((String) n)==null) ? null : // b.getValue((String) n).stringValue(); // rootOAMOData.setServiceID(str); // System.out.print("serviceID: "+rootOAMOData.getServiceID()); // } else if (((String) n).equalsIgnoreCase("oamoDesc")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); rootOAMOData.setOamoDesc(str); System.out.println("oamoDesc : " + rootOAMOData.getOamoDesc() + " "); } else if (((String) n).equalsIgnoreCase("oamoGraphMeta")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); rootOAMOData.setOamoGraphMeta(str); System.out.println("oamoGraphMeta : " + rootOAMOData.getOamoGraphMeta() + " "); } } // }//while return rootOAMOData; } catch (QueryEvaluationException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }
/** * * Prints the query results of result. * * @param result * @throws QueryEvaluationException */ public static void printTupleQueryResult(TupleQueryResult result) throws QueryEvaluationException { while (result.hasNext()) { /* * iterate over the result and print the * bindings. */ BindingSet bindingSet = result.next(); Value valueOfX = bindingSet.getValue("x"); Value valueOfY = bindingSet.getValue("y"); System.out.print("value of ?x " + valueOfX + " ++++++ "); System.out.println("value of ?y " + valueOfY); } }
@Test public void testAddWhileQuerying() throws Exception { // Add some data to the repository con.begin(); con.addStatement(painter, RDF.TYPE, RDFS.CLASS); con.addStatement(painting, RDF.TYPE, RDFS.CLASS); con.addStatement(picasso, RDF.TYPE, painter); con.addStatement(guernica, RDF.TYPE, painting); con.addStatement(picasso, paints, guernica); con.commit(); ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT C FROM {} rdf:type {C}", null); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false); con.begin(); while (iter.hasNext()) { BindingSet bindings = iter.next(); Value c = bindings.getValue("C"); if (c instanceof Resource) { con.addStatement((Resource) c, RDF.TYPE, RDFS.CLASS); } } con.commit(); assertEquals(3, countElements(con.getStatements(null, RDF.TYPE, RDFS.CLASS, false))); // simulate auto-commit tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, "SELECT P FROM {} P {}", null); iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false); while (iter.hasNext()) { BindingSet bindings = iter.next(); Value p = bindings.getValue("P"); if (p instanceof URI) { con.begin(); con.addStatement((URI) p, RDF.TYPE, RDF.PROPERTY); con.commit(); } } assertEquals(2, countElements(con.getStatements(null, RDF.TYPE, RDF.PROPERTY, false))); }
@Override public CloseableIteration<BindingSet, QueryEvaluationException> evaluate( String sparqlQueryString, BindingSet bindings, String baseUri, QueryType type, Service service) throws QueryEvaluationException { RepositoryConnection conn = endpoint.getConn(); try { TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQueryString, baseUri); Iterator<Binding> bIter = bindings.iterator(); while (bIter.hasNext()) { Binding b = bIter.next(); if (service.getServiceVars().contains(b.getName())) query.setBinding(b.getName(), b.getValue()); } TupleQueryResult qRes = query.evaluate(); return new InsertBindingsIteration(qRes, bindings); } catch (OpenRDFException e) { throw new QueryEvaluationException(e); } }
/** * * Prints a single given assignment. * * @param assignment */ public static void printBindingSet(BindingSet assignment) { if (assignment == null) { System.out.println("null"); } else { Value valueOfX = assignment.getValue("x"); Value valueOfY = assignment.getValue("y"); System.out.print("value of ?x " + valueOfX + " ++++++ "); System.out.println("value of ?y " + valueOfY); } }
private BindingSet calcKey(BindingSet bindings, Set<String> commonVars) { QueryBindingSet q = new QueryBindingSet(); for (String varName : commonVars) { Binding b = bindings.getBinding(varName); if (b != null) { q.addBinding(b); } } return q; }
protected void testValueRoundTrip(Resource subj, URI pred, Value obj) throws Exception { con.begin(); con.addStatement(subj, pred, obj); con.commit(); CloseableIteration<? extends Statement, SailException> stIter = con.getStatements(null, null, null, false); try { assertTrue(stIter.hasNext()); Statement st = stIter.next(); assertEquals(subj, st.getSubject()); assertEquals(pred, st.getPredicate()); assertEquals(obj, st.getObject()); assertTrue(!stIter.hasNext()); } finally { stIter.close(); } ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery( QueryLanguage.SERQL, "SELECT S, P, O FROM {S} P {O} WHERE P = <" + pred.stringValue() + ">", null); CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false); try { assertTrue(iter.hasNext()); BindingSet bindings = iter.next(); assertEquals(subj, bindings.getValue("S")); assertEquals(pred, bindings.getValue("P")); assertEquals(obj, bindings.getValue("O")); assertTrue(!iter.hasNext()); } finally { iter.close(); } }
public static ArrayList<QueryData> parseOSMOQueryData(TupleQueryResult qres) { ArrayList<QueryData> queryDataList = new ArrayList<QueryData>(); try { while (qres.hasNext()) { BindingSet b = qres.next(); Set names = b.getBindingNames(); QueryData queryData = new QueryData(); for (Object n : names) { if (((String) n).equalsIgnoreCase("queryID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); queryData.setId(str); System.out.print("srvcStatus id: " + queryData.getId() + " "); } else if (((String) n).equalsIgnoreCase("queryString")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); queryData.setQueryString(str); System.out.print("srvcStatusTime : " + queryData.getQueryString() + " "); } } queryDataList.add(queryData); } // while return queryDataList; } catch (QueryEvaluationException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }
private RDFRectangle toRectangle(StatementPattern pattern, BindingSet bindings) { Value sVal = pattern.getSubjectVar().getValue(); Value pVal = pattern.getPredicateVar().getValue(); Value oVal = pattern.getObjectVar().getValue(); RDFURIRange subjectRange; List<String> list = new ArrayList<String>(); if (sVal == null) { if (bindings.hasBinding(pattern.getSubjectVar().getName())) list.add(bindings.getValue(pattern.getSubjectVar().getName()).stringValue()); } else list.add(sVal.stringValue()); if (!list.isEmpty()) subjectRange = new RDFURIRange(list); else subjectRange = new RDFURIRange(); ExplicitSetRange<URI> predicateRange; Set<URI> set = new HashSet<URI>(); if (pVal == null) { if (bindings.hasBinding(pattern.getPredicateVar().getName())) set.add((URI) bindings.getValue(pattern.getPredicateVar().getName())); } else set.add((URI) pVal); if (!set.isEmpty()) predicateRange = new ExplicitSetRange<URI>(set); else predicateRange = new ExplicitSetRange<>(); RDFValueRange objectRange = new RDFValueRange(); if (oVal == null) { if (bindings.hasBinding(pattern.getObjectVar().getName())) objectRange = fillObjectRange(bindings.getValue(pattern.getObjectVar().getName())); } else objectRange = fillObjectRange(oVal); return new RDFRectangle(subjectRange, predicateRange, objectRange); }
public TestSuite createTestSuite() throws Exception { // Create test suite TestSuite suite = new TestSuite(N3ParserTestCase.class.getName()); // Add the manifest to a repository and query it Repository repository = new SailRepository(new MemoryStore()); repository.initialize(); RepositoryConnection con = repository.getConnection(); URL url = url(MANIFEST_URL); con.add(url, base(MANIFEST_URL), RDFFormat.TURTLE); // Add all positive parser tests to the test suite String query = "SELECT testURI, inputURL, outputURL " + "FROM {testURI} rdf:type {n3test:PositiveParserTest}; " + " n3test:inputDocument {inputURL}; " + " n3test:outputDocument {outputURL} " + "USING NAMESPACE n3test = <http://www.w3.org/2004/11/n3test#>"; TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query).evaluate(); while (queryResult.hasNext()) { BindingSet bindingSet = queryResult.next(); String testURI = bindingSet.getValue("testURI").toString(); String inputURL = bindingSet.getValue("inputURL").toString(); String outputURL = bindingSet.getValue("outputURL").toString(); suite.addTest(new PositiveParserTest(testURI, inputURL, outputURL)); } queryResult.close(); // Add all negative parser tests to the test suite query = "SELECT testURI, inputURL " + "FROM {testURI} rdf:type {n3test:NegativeParserTest}; " + " n3test:inputDocument {inputURL} " + "USING NAMESPACE n3test = <http://www.w3.org/2004/11/n3test#>"; queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query).evaluate(); while (queryResult.hasNext()) { BindingSet bindingSet = queryResult.next(); String testURI = bindingSet.getValue("testURI").toString(); String inputURL = bindingSet.getValue("inputURL").toString(); suite.addTest(new NegativeParserTest(testURI, inputURL)); } queryResult.close(); con.close(); repository.shutDown(); return suite; }
public static BindingSet project( ProjectionElemList projElemList, BindingSet sourceBindings, BindingSet parentBindings) { QueryBindingSet resultBindings = new QueryBindingSet(parentBindings); for (ProjectionElem pe : projElemList.getElements()) { Value targetValue = sourceBindings.getValue(pe.getSourceName()); if (targetValue != null) { // Potentially overwrites bindings from super resultBindings.setBinding(pe.getTargetName(), targetValue); } } return resultBindings; }
@Override protected BindingSet convert(BindingSet bIn) throws QueryEvaluationException { QueryBindingSet res = new QueryBindingSet(); int bIndex = -1; Iterator<Binding> bIter = bIn.iterator(); while (bIter.hasNext()) { Binding b = bIter.next(); String name = b.getName(); bIndex = Integer.parseInt(name.substring(name.lastIndexOf("_") + 1)); res.addBinding(name.substring(0, name.lastIndexOf("_")), b.getValue()); } if (bIndex > -1) res.addAll(bindings.get(bIndex)); return res; }
private void evaluateIntoStatements(ParsedGraphQuery query, Collection<Statement> statements) throws SailException, RDFHandlerException, QueryEvaluationException { CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter = getWrappedConnection() .evaluate(query.getTupleExpr(), null, EmptyBindingSet.getInstance(), true); try { ValueFactory vf = getValueFactory(); while (bindingsIter.hasNext()) { BindingSet bindings = bindingsIter.next(); Value subj = bindings.getValue("subject"); Value pred = bindings.getValue("predicate"); Value obj = bindings.getValue("object"); if (subj instanceof Resource && pred instanceof IRI && obj != null) { statements.add(vf.createStatement((Resource) subj, (IRI) pred, obj)); } } } finally { bindingsIter.close(); } }
public static ArrayList<RootOsmoData> parseOSMOListOfOAMO(TupleQueryResult qres) { ArrayList<RootOsmoData> osmoDataList = new ArrayList<RootOsmoData>(); try { while (qres.hasNext()) { BindingSet b = qres.next(); Set names = b.getBindingNames(); RootOsmoData osmoData = new RootOsmoData(); for (Object n : names) { if (((String) n).equalsIgnoreCase("serviceID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); osmoData.setId(str); System.out.print("serviceID: " + osmoData.getId() + " "); } else if (((String) n).equalsIgnoreCase("srvcName")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); osmoData.setName(str); System.out.print("srvcName : " + osmoData.getName() + " "); } else if (((String) n).equalsIgnoreCase("srvcDesc")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); osmoData.setDesc(str); System.out.print("srvcDesc : " + osmoData.getDesc() + " "); } } osmoDataList.add(osmoData); } // while return osmoDataList; } catch (QueryEvaluationException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }
public static ArrayList<WidgetPresentationData> parseWidgetPreListByService( TupleQueryResult qres) { ArrayList<WidgetPresentationData> widgetPresentationDataList = new ArrayList<WidgetPresentationData>(); try { while (qres.hasNext()) { BindingSet b = qres.next(); Set names = b.getBindingNames(); WidgetPresentationData widgetPreData = new WidgetPresentationData(); for (Object n : names) { if (((String) n).equalsIgnoreCase("widgetPreID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); widgetPreData.setId(str); System.out.print("widgetPreID: " + widgetPreData.getId() + " "); } else if (((String) n).equalsIgnoreCase("widgetID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); widgetPreData.setWidgetID(str); System.out.print("widgetID: " + widgetPreData.getWidgetID() + " "); } else if (((String) n).equalsIgnoreCase("widgetAttrID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); widgetPreData.setWidgetAttrID(str); System.out.print("widgetAttr: " + widgetPreData.getWidgetAttrID() + " "); } } widgetPresentationDataList.add(widgetPreData); } // while return widgetPresentationDataList; } catch (QueryEvaluationException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }
public static ArrayList<WidgetAttr> parseWidgetAttributes(TupleQueryResult qres) { ArrayList<WidgetAttr> widgetAttrList = new ArrayList<WidgetAttr>(); try { while (qres.hasNext()) { BindingSet b = qres.next(); Set names = b.getBindingNames(); WidgetAttr widgetAttr = new WidgetAttr(); for (Object n : names) { if (((String) n).equalsIgnoreCase("widgetAttrID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); widgetAttr.setId(str); System.out.print("widgetattr id: " + widgetAttr.getId() + " "); } else if (((String) n).equalsIgnoreCase("widgetAttrName")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); widgetAttr.setName(str); System.out.print("widgetAttrName: " + widgetAttr.getName() + " "); } else if (((String) n).equalsIgnoreCase("widgetAttrDesc")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); widgetAttr.setValue(str); System.out.print("widgetAttrDesc: " + widgetAttr.getValue() + " "); } } widgetAttrList.add(widgetAttr); } // while return widgetAttrList; } catch (QueryEvaluationException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }
public static ArrayList<ServiceStatusData> parseServiceStatusOfOSMO(TupleQueryResult qres) { ArrayList<ServiceStatusData> serviceStatusDataList = new ArrayList<ServiceStatusData>(); try { while (qres.hasNext()) { BindingSet b = qres.next(); Set names = b.getBindingNames(); ServiceStatusData srvcStatusData = new ServiceStatusData(); for (Object n : names) { if (((String) n).equalsIgnoreCase("srvcStatusID")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); srvcStatusData.setSrvcStatusID(str); System.out.print("srvcStatus id: " + srvcStatusData.getSrvcStatusID() + " "); } else if (((String) n).equalsIgnoreCase("srvcStatusTime")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); srvcStatusData.setSrvcStatusTime(str); System.out.print("srvcStatusTime : " + srvcStatusData.getSrvcStatusTime() + " "); } else if (((String) n).equalsIgnoreCase("srvcStatus")) { String str = (b.getValue((String) n) == null) ? null : b.getValue((String) n).stringValue(); srvcStatusData.setSrvcStatus(str); System.out.print("srvcStatusStatus : " + srvcStatusData.getSrvcStatus() + " "); } } serviceStatusDataList.add(srvcStatusData); } // while return serviceStatusDataList; } catch (QueryEvaluationException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }
private void compareTupleQueryResults( TupleQueryResult queryResult, TupleQueryResult expectedResult) throws Exception { // Create MutableTupleQueryResult to be able to re-iterate over the // results MutableTupleQueryResult queryResultTable = new MutableTupleQueryResult(queryResult); MutableTupleQueryResult expectedResultTable = new MutableTupleQueryResult(expectedResult); boolean resultsEqual; if (laxCardinality) { resultsEqual = QueryResultUtil.isSubset(queryResultTable, expectedResultTable); } else { resultsEqual = QueryResultUtil.equals(queryResultTable, expectedResultTable); if (checkOrder) { // also check the order in which solutions occur. queryResultTable.beforeFirst(); expectedResultTable.beforeFirst(); while (queryResultTable.hasNext()) { BindingSet bs = queryResultTable.next(); BindingSet expectedBs = expectedResultTable.next(); if (!bs.equals(expectedBs)) { resultsEqual = false; break; } } } } if (!resultsEqual) { queryResultTable.beforeFirst(); expectedResultTable.beforeFirst(); /* * StringBuilder message = new StringBuilder(128); * message.append("\n============ "); message.append(getName()); * message.append(" =======================\n"); * message.append("Expected result: \n"); while * (expectedResultTable.hasNext()) { * message.append(expectedResultTable.next()); message.append("\n"); * } message.append("============="); StringUtil.appendN('=', * getName().length(), message); * message.append("========================\n"); * message.append("Query result: \n"); while * (queryResultTable.hasNext()) { * message.append(queryResultTable.next()); message.append("\n"); } * message.append("============="); StringUtil.appendN('=', * getName().length(), message); * message.append("========================\n"); */ List<BindingSet> queryBindings = Iterations.asList(queryResultTable); List<BindingSet> expectedBindings = Iterations.asList(expectedResultTable); List<BindingSet> missingBindings = new ArrayList<BindingSet>(expectedBindings); missingBindings.removeAll(queryBindings); List<BindingSet> unexpectedBindings = new ArrayList<BindingSet>(queryBindings); unexpectedBindings.removeAll(expectedBindings); StringBuilder message = new StringBuilder(128); message.append("\n============ "); message.append(getName()); message.append(" =======================\n"); if (!missingBindings.isEmpty()) { message.append("Missing bindings: \n"); for (BindingSet bs : missingBindings) { message.append(bs); message.append("\n"); } message.append("============="); StringUtil.appendN('=', getName().length(), message); message.append("========================\n"); } if (!unexpectedBindings.isEmpty()) { message.append("Unexpected bindings: \n"); for (BindingSet bs : unexpectedBindings) { message.append(bs); message.append("\n"); } message.append("============="); StringUtil.appendN('=', getName().length(), message); message.append("========================\n"); } if (checkOrder && missingBindings.isEmpty() && unexpectedBindings.isEmpty()) { message.append("Results are not in expected order.\n"); message.append(" =======================\n"); message.append("query result: \n"); for (BindingSet bs : queryBindings) { message.append(bs); message.append("\n"); } message.append(" =======================\n"); message.append("expected result: \n"); for (BindingSet bs : expectedBindings) { message.append(bs); message.append("\n"); } message.append(" =======================\n"); System.out.print(message.toString()); } logger.error(message.toString()); fail(message.toString()); } /* * debugging only: print out result when test succeeds else { * queryResultTable.beforeFirst(); * * List<BindingSet> queryBindings = Iterations.asList(queryResultTable); * StringBuilder message = new StringBuilder(128); * * message.append("\n============ "); message.append(getName()); * message.append(" =======================\n"); * * message.append(" =======================\n"); * message.append("query result: \n"); for (BindingSet bs: * queryBindings) { message.append(bs); message.append("\n"); } * * System.out.print(message.toString()); } */ }
public static TestSuite suite(String manifestFileURL, Factory factory, boolean approvedOnly) throws Exception { logger.info("Building test suite for {}", manifestFileURL); TestSuite suite = new TestSuite(factory.getClass().getName()); // Read manifest and create declared test cases Repository manifestRep = new SailRepository(new MemoryStore()); manifestRep.initialize(); RepositoryConnection con = manifestRep.getConnection(); ManifestTest.addTurtle(con, new URL(manifestFileURL), manifestFileURL); suite.setName(getManifestName(manifestRep, con, manifestFileURL)); // Extract test case information from the manifest file. Note that we // only // select those test cases that are mentioned in the list. StringBuilder query = new StringBuilder(512); query.append( " SELECT DISTINCT testURI, testName, resultFile, action, queryFile, defaultGraph, ordered "); query.append(" FROM {} rdf:first {testURI} "); if (approvedOnly) { query.append(" dawgt:approval {dawgt:Approved}; "); } query.append(" mf:name {testName}; "); query.append(" mf:result {resultFile}; "); query.append(" [ mf:checkOrder {ordered} ]; "); query.append(" [ mf:requires {Requirement} ];"); query.append(" mf:action {action} qt:query {queryFile}; "); query.append(" [qt:data {defaultGraph}]; "); query.append(" [sd:entailmentRegime {Regime} ]"); // skip tests involving CSV result files, these are not query tests query.append(" WHERE NOT resultFile LIKE \"*.csv\" "); // skip tests involving JSON, sesame currently does not have a // SPARQL/JSON // parser. query.append(" AND NOT resultFile LIKE \"*.srj\" "); // skip tests involving entailment regimes query.append(" AND NOT BOUND(Regime) "); // skip test involving basic federation, these are tested separately. query.append(" AND (NOT BOUND(Requirement) OR (Requirement != mf:BasicFederation)) "); query.append(" USING NAMESPACE "); query.append(" mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "); query.append(" dawgt = <http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#>, "); query.append(" qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>, "); query.append(" sd = <http://www.w3.org/ns/sparql-service-description#>, "); query.append(" ent = <http://www.w3.org/ns/entailment/> "); TupleQuery testCaseQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString()); query.setLength(0); query.append(" SELECT graph "); query.append(" FROM {action} qt:graphData {graph} "); query.append(" USING NAMESPACE "); query.append(" qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>"); TupleQuery namedGraphsQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString()); query.setLength(0); query.append("SELECT 1 "); query.append(" FROM {testURI} mf:resultCardinality {mf:LaxCardinality}"); query.append( " USING NAMESPACE mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>"); TupleQuery laxCardinalityQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString()); logger.debug("evaluating query.."); TupleQueryResult testCases = testCaseQuery.evaluate(); while (testCases.hasNext()) { BindingSet bindingSet = testCases.next(); URI testURI = (URI) bindingSet.getValue("testURI"); String testName = bindingSet.getValue("testName").toString(); String resultFile = bindingSet.getValue("resultFile").toString(); String queryFile = bindingSet.getValue("queryFile").toString(); URI defaultGraphURI = (URI) bindingSet.getValue("defaultGraph"); Value action = bindingSet.getValue("action"); Value ordered = bindingSet.getValue("ordered"); logger.debug("found test case : {}", testName); // Query named graphs namedGraphsQuery.setBinding("action", action); TupleQueryResult namedGraphs = namedGraphsQuery.evaluate(); DatasetImpl dataset = null; if (defaultGraphURI != null || namedGraphs.hasNext()) { dataset = new DatasetImpl(); if (defaultGraphURI != null) { dataset.addDefaultGraph(defaultGraphURI); } while (namedGraphs.hasNext()) { BindingSet graphBindings = namedGraphs.next(); URI namedGraphURI = (URI) graphBindings.getValue("graph"); logger.debug(" adding named graph : {}", namedGraphURI); dataset.addNamedGraph(namedGraphURI); } } // Check for lax-cardinality conditions boolean laxCardinality = false; laxCardinalityQuery.setBinding("testURI", testURI); TupleQueryResult laxCardinalityResult = laxCardinalityQuery.evaluate(); try { laxCardinality = laxCardinalityResult.hasNext(); } finally { laxCardinalityResult.close(); } // if this is enabled, Sesame passes all tests, showing that the // only // difference is the semantics of arbitrary-length // paths /* * if (!laxCardinality) { // property-path tests always with lax * cardinality because Sesame filters out duplicates by design if * (testURI.stringValue().contains("property-path")) { * laxCardinality = true; } } */ // check if we should test for query result ordering boolean checkOrder = false; if (ordered != null) { checkOrder = Boolean.parseBoolean(ordered.stringValue()); } SPARQLQueryTest test = factory.createSPARQLQueryTest( testURI.toString(), testName, queryFile, resultFile, dataset, laxCardinality, checkOrder); if (test != null) { suite.addTest(test); } } testCases.close(); con.close(); manifestRep.shutDown(); logger.info("Created test suite with " + suite.countTestCases() + " test cases."); return suite; }
public void test_query() throws Exception { final BigdataSail sail = getSail(); try { sail.initialize(); if (!((BigdataSail) sail).database.getStatementIdentifiers()) { log.warn("Statement identifiers are not enabled"); return; } /* * Load data into the sail. */ { final DataLoader dataLoader = sail.database.getDataLoader(); dataLoader.loadData( "/com/bigdata/rdf/sail/provenance01.ttlx", "" /*baseURL*/, ServiceProviderHook.TURTLE_RDR); } /* * Serialize as RDF/XML. * * Note: This is just for debugging. */ if (log.isInfoEnabled()) { final BigdataStatementIterator itr = sail.database.getStatements(null, null, null); final String rdfXml; try { final Writer w = new StringWriter(); // final RDFXMLWriter rdfWriter = new RDFXMLWriter(w); final RDFWriterFactory writerFactory = RDFWriterRegistry.getInstance().get(RDFFormat.RDFXML); assertNotNull(writerFactory); final RDFWriter rdfWriter = writerFactory.getWriter(w); rdfWriter.startRDF(); while (itr.hasNext()) { final BigdataStatementImpl stmt = (BigdataStatementImpl) itr.next(); // only write the explicit statements. if (!stmt.isExplicit()) continue; rdfWriter.handleStatement(stmt); } rdfWriter.endRDF(); rdfXml = w.toString(); } finally { itr.close(); } // write the rdf/xml log.info(rdfXml); } final SailConnection conn = sail.getConnection(); try { final URI y = new URIImpl("http://www.foo.org/y"); final URI B = new URIImpl("http://www.foo.org/B"); final URI dcCreator = new URIImpl("http://purl.org/dc/terms/creator"); final Literal bryan = new LiteralImpl("bryan"); final Literal mike = new LiteralImpl("mike"); /* * This is a hand-coded query. * * Note: When statement identifiers are enabled, the only way to * bind the context position is to already have a statement on hand - * there is no index which can be used to look up a statement by its * context and the context is always a blank node. */ // final TupleExpr tupleExpr = // new Projection( // new Join(// // new StatementPattern(// // new Var("X", y),// // new Var("1", RDF.TYPE),// // new Var("2", B),// // new Var("SID")),// unbound. // new StatementPattern(// // new Var("SID"),// // new Var("3", dcCreator),// // new Var("Y"))), // new ProjectionElemList(new ProjectionElem[] { new ProjectionElem( "Y" // )})); // final String q = "select ?Y where { ?SID <"+dcCreator+"> ?Y . graph ?SID { // <"+y+"> <"+RDF.TYPE+"> <"+B+"> . } }"; final String q = "select ?Y where { <<<" + y + "> <" + RDF.TYPE + "> <" + B + ">>> <" + dcCreator + "> ?Y . }"; /* * Create a data set consisting of the contexts to be queried. * * Note: a [null] DataSet will cause context to be ignored when the * query is processed. */ // final DatasetImpl dataSet = null; //new DatasetImpl(); // // final BindingSet bindingSet = new QueryBindingSet(); // // final CloseableIteration<? extends BindingSet, QueryEvaluationException> itr = // conn // .evaluate(tupleExpr, dataSet, bindingSet, true/* includeInferred */); final TupleQuery tq = new BigdataSailRepository(sail) .getReadOnlyConnection() .prepareTupleQuery(QueryLanguage.SPARQL, q); final TupleQueryResult itr = tq.evaluate(); if (log.isInfoEnabled()) log.info("Verifying query."); /* * These are the expected results for the query (the bindings for Y). */ final Set<Value> expected = new HashSet<Value>(); expected.add(bryan); expected.add(mike); /* * Verify that the query results is the correct solutions. */ final int nresults = expected.size(); try { int i = 0; while (itr.hasNext()) { final BindingSet solution = itr.next(); if (log.isInfoEnabled()) log.info("solution[" + i + "] : " + solution); final Value actual = solution.getValue("Y"); assertTrue("Not expecting Y=" + actual, expected.remove(actual)); i++; } assertEquals("#results", nresults, i); } finally { itr.close(); } } finally { conn.close(); } } finally { sail.__tearDownUnitTest(); } }
protected void getTerms( Repository repos, String name, String uri, List<RDFSClass> classes, List<RDFSProperty> properties) throws VocabularyImportException { try { RepositoryConnection con = repos.getConnection(); try { TupleQuery query = con.prepareTupleQuery(QueryLanguage.SPARQL, CLASSES_QUERY_P1 + uri + CLASSES_QUERY_P2); TupleQueryResult res = query.evaluate(); Set<String> seen = new HashSet<String>(); while (res.hasNext()) { BindingSet solution = res.next(); String clazzURI = solution.getValue("resource").stringValue(); if (seen.contains(clazzURI)) { continue; } seen.add(clazzURI); String label = getFirstNotNull( new Value[] {solution.getValue("en_label"), solution.getValue("label")}); String description = getFirstNotNull( new Value[] { solution.getValue("en_definition"), solution.getValue("definition"), solution.getValue("en_description"), solution.getValue("description") }); RDFSClass clazz = new RDFSClass(clazzURI, label, description, name, uri); classes.add(clazz); } query = con.prepareTupleQuery( QueryLanguage.SPARQL, PROPERTIES_QUERY_P1 + uri + PROPERTIES_QUERY_P2); res = query.evaluate(); seen = new HashSet<String>(); while (res.hasNext()) { BindingSet solution = res.next(); String propertyUri = solution.getValue("resource").stringValue(); if (seen.contains(propertyUri)) { continue; } seen.add(propertyUri); String label = getFirstNotNull( new Value[] {solution.getValue("en_label"), solution.getValue("label")}); String description = getFirstNotNull( new Value[] { solution.getValue("en_definition"), solution.getValue("definition"), solution.getValue("en_description"), solution.getValue("description") }); RDFSProperty prop = new RDFSProperty(propertyUri, label, description, name, uri); properties.add(prop); } } catch (Exception ex) { throw new VocabularyImportException( "Error while processing vocabulary retrieved from " + uri, ex); } finally { con.close(); } } catch (RepositoryException ex) { throw new VocabularyImportException( "Error while processing vocabulary retrieved from " + uri, ex); } }
/** Copied from org.openrdf.query.QueryResultUtil */ private static boolean bindingSetsMatch(final BindingSet bs1, final BindingSet bs2) { if (bs1.size() != bs2.size()) { return false; } for (Binding binding1 : bs1) { Value value1 = binding1.getValue(); Value value2 = bs2.getValue(binding1.getName()); if ((value1 instanceof BNode) && (value2 instanceof BNode)) { // BNode mappedBNode = bNodeMapping.get(value1); // // if (mappedBNode != null) { // // bNode 'value1' was already mapped to some other bNode // if (!value2.equals(mappedBNode)) { // // 'value1' and 'value2' do not match // return false; // } // } else { // // 'value1' was not yet mapped, we need to check if 'value2' // // is a // // possible mapping candidate // if (bNodeMapping.containsValue(value2)) { // // 'value2' is already mapped to some other value. // return false; // } // } return value1.equals(value2); } else { // values are not (both) bNodes if ((value1 instanceof Literal) && (value2 instanceof Literal)) { // do literal value-based comparison for supported datatypes Literal leftLit = (Literal) value1; Literal rightLit = (Literal) value2; URI dt1 = leftLit.getDatatype(); URI dt2 = rightLit.getDatatype(); if ((dt1 != null) && (dt2 != null) && dt1.equals(dt2) && XMLDatatypeUtil.isValidValue(leftLit.getLabel(), dt1) && XMLDatatypeUtil.isValidValue(rightLit.getLabel(), dt2)) { Integer compareResult = null; if (dt1.equals(XMLSchema.DOUBLE)) { compareResult = Double.compare(leftLit.doubleValue(), rightLit.doubleValue()); } else if (dt1.equals(XMLSchema.FLOAT)) { compareResult = Float.compare(leftLit.floatValue(), rightLit.floatValue()); } else if (dt1.equals(XMLSchema.DECIMAL)) { compareResult = leftLit.decimalValue().compareTo(rightLit.decimalValue()); } else if (XMLDatatypeUtil.isIntegerDatatype(dt1)) { compareResult = leftLit.integerValue().compareTo(rightLit.integerValue()); } else if (dt1.equals(XMLSchema.BOOLEAN)) { Boolean leftBool = Boolean.valueOf(leftLit.booleanValue()); Boolean rightBool = Boolean.valueOf(rightLit.booleanValue()); compareResult = leftBool.compareTo(rightBool); } else if (XMLDatatypeUtil.isCalendarDatatype(dt1)) { XMLGregorianCalendar left = leftLit.calendarValue(); XMLGregorianCalendar right = rightLit.calendarValue(); compareResult = left.compare(right); } if (compareResult != null) { if (compareResult.intValue() != 0) { return false; } } else if (!value1.equals(value2)) { return false; } } else if (!value1.equals(value2)) { return false; } } else if (!value1.equals(value2)) { return false; } } } return true; }