public static void logResults(String name, ResultSetRewindable results) { if (log.isLoggable(Level.WARNING)) { log.warning(name + " (" + results.size() + ")"); results.reset(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ResultSetFormatter.output(out, results, ResultsFormat.FMT_RDF_TTL); log.warning("\n" + out.toString()); } }
public static Collection<Map<String, RDFNode>> results(ResultSetRewindable rs) { rs.reset(); List<String> vars = rs.getResultVars(); Set<Map<String, RDFNode>> results = new HashSet<Map<String, RDFNode>>(); while (rs.hasNext()) { QuerySolution qs = rs.nextSolution(); Map<String, RDFNode> result = new HashMap<String, RDFNode>(); for (String var : vars) { result.put(var, qs.get(var)); } results.add(solutionMap(qs, vars)); } return results; }
// TEMPORARY private boolean checkResultsByModel( Query query, Model expectedModel, ResultSetRewindable results) { // Fudge - can't cope with ordered results properly. The output writer for ResultSets does nto // add rs:index. results.reset(); Model actualModel = ResultSetFormatter.toModel(results); // Tidy the models. // Very regretable. expectedModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSet); expectedModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSolution); expectedModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultBinding); expectedModel.removeAll(null, ResultSetGraphVocab.size, (RDFNode) null); expectedModel.removeAll(null, ResultSetGraphVocab.index, (RDFNode) null); actualModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSet); actualModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultSolution); actualModel.removeAll(null, RDF.type, ResultSetGraphVocab.ResultBinding); actualModel.removeAll(null, ResultSetGraphVocab.size, (RDFNode) null); actualModel.removeAll(null, ResultSetGraphVocab.index, (RDFNode) null); boolean b = expectedModel.isIsomorphicWith(actualModel); if (!b) { System.out.println("---- Expected"); expectedModel.write(System.out, "TTL"); System.out.println("---- Actual"); actualModel.write(System.out, "TTL"); System.out.println("----"); } return b; }
private static ResultSetRewindable unique(ResultSetRewindable results) { // VERY crude. Utilises the fact that bindings have value equality. List<Binding> x = new ArrayList<Binding>(); Set<Binding> seen = new HashSet<Binding>(); for (; results.hasNext(); ) { Binding b = results.nextBinding(); if (seen.contains(b)) continue; seen.add(b); x.add(b); } QueryIterator qIter = new QueryIterPlainWrapper(x.iterator()); ResultSet rs = new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter); return ResultSetFactory.makeRewindable(rs); }
void printFailedResultSetTest( Query query, QueryExecution qe, ResultSetRewindable qrExpected, ResultSetRewindable qrActual) { PrintStream out = System.out; out.println(); out.println("======================================="); out.println("Failure: " + description()); out.println("Query: \n" + query); // if ( qe != null && qe.getDataset() != null ) // { // out.println("Data: \n"+qe.getDataset().asDatasetGraph()) ; // } out.println("Got: " + qrActual.size() + " --------------------------------"); qrActual.reset(); ResultSetFormatter.out(out, qrActual, query.getPrefixMapping()); qrActual.reset(); out.flush(); out.println("Expected: " + qrExpected.size() + " -----------------------------"); qrExpected.reset(); ResultSetFormatter.out(out, qrExpected, query.getPrefixMapping()); qrExpected.reset(); out.println(); out.flush(); }
private ResultSetRewindable convertToStrings(ResultSetRewindable resultsActual) { List<Binding> bindings = new ArrayList<Binding>(); while (resultsActual.hasNext()) { Binding b = resultsActual.nextBinding(); BindingMap b2 = BindingFactory.create(); for (String vn : resultsActual.getResultVars()) { Var v = Var.alloc(vn); Node n = b.get(v); String s; if (n == null) s = ""; else if (n.isBlank()) s = "_:" + n.getBlankNodeLabel(); else s = NodeFunctions.str(n); b2.add(v, NodeFactory.createLiteral(s)); } bindings.add(b2); } ResultSet rs = new ResultSetStream( resultsActual.getResultVars(), null, new QueryIterPlainWrapper(bindings.iterator())); return ResultSetFactory.makeRewindable(rs); }
void runTestSelect(Query query, QueryExecution qe) throws Exception { // Do the query! ResultSetRewindable resultsActual = ResultSetFactory.makeRewindable(qe.execSelect()); qe.close(); if (results == null) return; // Assumes resultSetCompare can cope with full isomorphism possibilities. ResultSetRewindable resultsExpected; if (results.isResultSet()) resultsExpected = ResultSetFactory.makeRewindable(results.getResultSet()); else if (results.isModel()) resultsExpected = ResultSetFactory.makeRewindable(results.getModel()); else { fail("Wrong result type for SELECT query"); resultsExpected = null; // Keep the compiler happy } if (query.isReduced()) { // Reduced - best we can do is DISTINCT resultsExpected = unique(resultsExpected); resultsActual = unique(resultsActual); } // Hack for CSV : tests involving bNodes need manually checking. if (testItem.getResultFile().endsWith(".csv")) { resultsActual = convertToStrings(resultsActual); resultsActual.reset(); int nActual = ResultSetFormatter.consume(resultsActual); int nExpected = ResultSetFormatter.consume(resultsExpected); resultsActual.reset(); resultsExpected.reset(); assertEquals("CSV: Different number of rows", nExpected, nActual); boolean b = resultSetEquivalent(query, resultsExpected, resultsActual); if (!b) System.out.println("Manual check of CSV results required: " + testItem.getName()); return; } boolean b = resultSetEquivalent(query, resultsExpected, resultsActual); if (!b) { resultsExpected.reset(); resultsActual.reset(); boolean b2 = resultSetEquivalent(query, resultsExpected, resultsActual); printFailedResultSetTest(query, qe, resultsExpected, resultsActual); } assertTrue("Results do not match: " + testItem.getName(), b); return; }
public PagingLoadResult<Example> getCurrentQueryResult(PagingLoadConfig config) throws SPARQLQueryException { logger.debug("Retrieving results for current query."); List<Example> queryResult = new ArrayList<Example>(); String currentQuery = exampleFinder.getCurrentQuery(); logger.debug("Current query:\n"); logger.debug(currentQuery); int limit = config.getLimit(); int offset = config.getOffset(); int totalLength = 10; try { ResultSetRewindable rs = SparqlQuery.convertJSONtoResultSet( selectCache.executeSelectQuery(endpoint, getCountQuery(currentQuery))); totalLength = rs.next().getLiteral(rs.getResultVars().get(0)).getInt(); } catch (Exception e) { e.printStackTrace(); } try { ResultSetRewindable rs = SparqlQuery.convertJSONtoResultSet( selectCache.executeSelectQuery( endpoint, modifyQuery(currentQuery + " LIMIT " + limit + " OFFSET " + offset))); String uri; String label = ""; String imageURL = ""; String comment = ""; QuerySolution qs; while (rs.hasNext()) { qs = rs.next(); uri = qs.getResource("x0").getURI(); label = qs.getLiteral("label").getLexicalForm(); queryResult.add(new Example(uri, label, imageURL, comment)); } } catch (Exception e) { logger.error("Error while getting result for query \n" + currentQuery, e); } PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(queryResult); result.setOffset(offset); result.setTotalLength(totalLength); return result; }
public static boolean assertEquals(ResultSet expectedResults, ResultSet computedResults) { ResultSetRewindable expected = ResultSetFactory.makeRewindable(expectedResults); ResultSetRewindable computed = ResultSetFactory.makeRewindable(computedResults); if (expected.size() != computed.size()) { logResults("Expected", expected); logResults("Real", computed); Assert.fail("Expected " + expected.size() + " but got " + computed.size()); } List<String> vars = expected.getResultVars(); Collection<Map<String, RDFNode>> results = results(computed); for (expected.reset(); expected.hasNext(); ) { QuerySolution qs = expected.nextSolution(); Map<String, RDFNode> map = solutionMap(qs, vars); if (!results.contains(map)) { logResults("Expected", expected); logResults("Real", computed); Assert.fail("Solution not found: " + map); Assert.fail("Expected " + expected.size() + " but got " + computed.size()); } } return true; }
public PagingLoadResult<Example> getSPARQLQueryResultWithProperties( String query, List<String> properties, PagingLoadConfig config) throws AutoSPARQLException { List<Example> queryResult = new ArrayList<Example>(); // properties.remove("label"); int limit = config.getLimit(); int offset = config.getOffset(); int totalLength = 10; if (currentQueryResultSize == -1) { try { ResultSetRewindable rs = SparqlQuery.convertJSONtoResultSet( selectCache.executeSelectQuery(endpoint, getCountQuery(query))); currentQueryResultSize = rs.next().getLiteral(rs.getResultVars().get(0)).getInt(); } catch (Exception e) { e.printStackTrace(); currentQueryResultSize = 10; } } totalLength = currentQueryResultSize; List<String> propertiesToDo = new ArrayList<String>(properties); for (Map<String, Object> prop2Value : propertiesCache.values()) { propertiesToDo.removeAll(prop2Value.keySet()); } if (propertiesToDo.size() > 0) { String queryTriples = query.substring(18, query.length() - 1); StringBuilder newQuery = new StringBuilder(); Map<String, String> var2URIMap = new HashMap<String, String>(propertiesToDo.size()); if (propertiesToDo.size() == 1 && propertiesToDo.get(0).equals(RDFS.label.getURI())) { newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL{"); newQuery.append(queryTriples); newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n"); newQuery .append("OPTIONAL{?x0 <") .append("http://dbpedia.org/ontology/thumbnail") .append("> ?imageURL.}\n"); newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))"); newQuery.append("}"); } else { for (String property : propertiesToDo) { var2URIMap.put( property2LabelMap.get(property).replace(" ", "_").replace("(", "").replace(")", ""), property); } newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL "); for (String var : var2URIMap.keySet()) { newQuery.append("?").append(var).append(" "); newQuery.append("?").append(var).append("_label "); } newQuery.append("{"); newQuery.append(queryTriples); newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n"); newQuery .append("OPTIONAL{?x0 <") .append("http://dbpedia.org/ontology/thumbnail") .append("> ?imageURL.}\n"); for (Entry<String, String> entry : var2URIMap.entrySet()) { newQuery .append("OPTIONAL{?x0 <") .append(entry.getValue()) .append("> ?") .append(entry.getKey()) .append(".}\n"); } for (Entry<String, String> entry : var2URIMap.entrySet()) { newQuery .append("OPTIONAL{?") .append(entry.getKey()) .append(" <") .append(RDFS.label) .append("> ?") .append(entry.getKey()) .append("_label.\n"); newQuery.append("FILTER(LANGMATCHES(LANG(?" + entry.getKey() + "_label),'en'))}\n"); } newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))"); newQuery.append("}"); } logger.debug("Query with properties:\n" + newQuery.toString()); try { ResultSetRewindable rs = SparqlQuery.convertJSONtoResultSet( selectCache.executeSelectQuery(endpoint, modifyQuery(newQuery + " LIMIT 1000"))); String uri; String label = ""; String imageURL = ""; QuerySolution qs; RDFNode object; while (rs.hasNext()) { qs = rs.next(); uri = qs.getResource("x0").getURI(); label = qs.getLiteral("label").getLexicalForm(); imageURL = qs.getResource("imageURL") != null ? qs.getResource("imageURL").getURI() : ""; Map<String, Object> properties2Value = propertiesCache.get(uri); if (properties2Value == null) { properties2Value = new HashMap<String, Object>(); properties2Value.put(RDFS.label.getURI(), label); properties2Value.put("http://dbpedia.org/ontology/thumbnail", imageURL); propertiesCache.put(uri, properties2Value); } Object value; String property; for (Entry<String, String> entry : var2URIMap.entrySet()) { value = ""; property = entry.getValue(); object = qs.get(entry.getKey() + "_label"); if (object == null) { object = qs.get(entry.getKey()); } if (object != null) { if (object.isURIResource()) { value = object.asResource().getURI(); } else if (object.isLiteral()) { Literal lit = object.asLiteral(); // if(lit.getDatatypeURI().equals(XSD.BOOLEAN)){ // property2DatatypeMap.put(property, Boolean.class); // value = lit.getBoolean(); // } else if(lit.getDatatypeURI().equals(XSD.INT)){ // property2DatatypeMap.put(property, Integer.class); // value = lit.getInt(); // } else if(lit.getDatatypeURI().equals(XSD.DOUBLE)){ // property2DatatypeMap.put(property, Double.class); // value = lit.getDouble(); // } else if(lit.getDatatypeURI().equals(XSD.FLOAT)){ // property2DatatypeMap.put(property, Float.class); // value = lit.getFloat(); // } else { // property2DatatypeMap.put(property, String.class); // value = object.asLiteral().getLexicalForm(); // } value = object.asLiteral().getLexicalForm(); } } Object oldValue = properties2Value.get(property); if (oldValue != null && value != null) { value = oldValue + ", " + value; } properties2Value.put(property, value); } } } catch (Exception e) { logger.error("Error while getting result for query \n" + newQuery, e); } } Example example; int cnt = 0; for (Entry<String, Map<String, Object>> uri2PropertyValues : propertiesCache.entrySet()) { // if(cnt++ == limit+offset){ // break; // } // if(cnt > offset){ example = new Example(); example.setAllowNestedValues(false); example.set("uri", uri2PropertyValues.getKey()); Object value; String property; for (Entry<String, Object> property2Value : uri2PropertyValues.getValue().entrySet()) { property = property2Value.getKey(); value = property2Value.getValue(); // if(value == null){ // Class cls = property2DatatypeMap.get(property); // if(cls == String.class){ // value = ""; // } else if(cls == Integer.class){ // value = Integer.valueOf(-1); // } else if(cls == Double.class){ // value = Double.valueOf(-1); // } else if(cls == Float.class){ // value = Float.valueOf(-1); // } else if(cls == Boolean.class){ // value = Boolean.FALSE; // } // } example.set(property, value); } queryResult.add(example); // } } if (config.getSortInfo().getSortField() != null) { final String sortField = config.getSortInfo().getSortField(); Collections.sort( queryResult, config .getSortInfo() .getSortDir() .comparator( new Comparator<Example>() { @Override public int compare(Example o1, Example o2) { return ((String) o1.get(sortField)).compareTo((String) o2.get(sortField)); } })); } int start = config.getOffset(); int end = queryResult.size(); if (limit > 0) { end = Math.min(start + limit, end); } // queryResult = queryResult.subList(start, end); ArrayList<Example> tmp = new ArrayList<Example>(); for (int i = start; i < end; i++) { tmp.add(queryResult.get(i)); } PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(tmp); result.setOffset(offset); result.setTotalLength(totalLength); return result; }