Java Code Examples for org.eclipse.rdf4j.query.TupleQueryResult#hasNext()

The following examples show how to use org.eclipse.rdf4j.query.TupleQueryResult#hasNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractQueryResultIOTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void doTupleMissingStartQueryResult(TupleQueryResultFormat format, TupleQueryResult input,
		TupleQueryResult expected, List<String> links, String stylesheetUrl) throws QueryResultHandlerException,
		QueryEvaluationException, QueryResultParseException, UnsupportedQueryResultFormatException, IOException {
	ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
	TupleQueryResultWriter writer = QueryResultIO.createTupleWriter(format, out);
	// Test for handling when startDocument and startHeader are not called
	writer.startDocument();
	writer.handleStylesheet(stylesheetUrl);
	writer.startHeader();
	writer.handleLinks(links);
	writer.endHeader();
	try {
		while (input.hasNext()) {
			BindingSet bindingSet = input.next();
			writer.handleSolution(bindingSet);
		}
		writer.endQueryResult();
		fail("Expected exception when calling handleSolution without startQueryResult");
	} catch (IllegalStateException ise) {
		// Expected exception
	} finally {
		input.close();
	}
}
 
Example 2
Source File: SourceRanking.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
public static List<StatementSource> getRankedSources(Summary summary, String queryString, int k) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	List<StatementSource> rankedSources = new ArrayList<StatementSource>();
	TupleQuery tupleQuery = summary.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
	//System.out.println(queryString);
	TupleQueryResult result = tupleQuery.evaluate();
	int count = 0 ;
	while(result.hasNext() && count <k)
	{
		String endpoint = result.next().getValue("url").stringValue();
		String id = "sparql_" + endpoint.replace("http://", "").replace("/", "_");
		StatementSource src =  new StatementSource(id, StatementSourceType.REMOTE);
		rankedSources.add(src);
		count++;
	}

	return rankedSources;
}
 
Example 3
Source File: HibiscusSourceSelection.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 *  Get matching Subject authorities from a specific source for a triple pattern 
 * @param stmt Triple pattern
 * @param src Capable source 
 * @return List of authorities
 * @throws RepositoryException Repository Exception
 * @throws MalformedQueryException Memory Exception
 * @throws QueryEvaluationException Query Exception
 */

public ArrayList<String> FedSumD_getMatchingSbjAuthorities(StatementPattern stmt, StatementSource src) throws RepositoryException, MalformedQueryException, QueryEvaluationException 
{
	String endPointUrl = "http://"+src.getEndpointID().replace("sparql_", "");
	       endPointUrl = endPointUrl.replace("_", "/");
	ArrayList<String> sbjAuthorities = new ArrayList<String>();
	
	  String  queryString = getFedSumSbjAuthLookupQuery(stmt, endPointUrl) ;
	  
	     TupleQuery tupleQuery = getSummaryConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
	     TupleQueryResult result = tupleQuery.evaluate();
			   while(result.hasNext())
			     sbjAuthorities.add(result.next().getValue("sbjAuth").stringValue());
		      
			   			 return sbjAuthorities;
}
 
Example 4
Source File: TBSSSourceSelectionOriginal.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 *  Get matching Subject authorities from a specific source for a triple pattern 
 * @param stmt Triple pattern
 * @param src Capable source 
 * @return List of authorities
 */

public ArrayList<String> getFedSumDMatchingSbjAuthorities(StatementPattern stmt, StatementSource src)
{
	String endPointUrl = "http://" + src.getEndpointID().replace("sparql_", "");
	endPointUrl = endPointUrl.replace("_", "/");
	ArrayList<String> sbjAuthorities = new ArrayList<String>();

	String  queryString = getFedSumSbjAuthLookupQuery(stmt, endPointUrl) ;

	TupleQuery tupleQuery = getSummaryConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
	TupleQueryResult result = tupleQuery.evaluate();
	while (result.hasNext())
		sbjAuthorities.add(result.next().getValue("sbjAuth").stringValue());

	return sbjAuthorities;
}
 
Example 5
Source File: AbstractNQuadsParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void parsePositiveNQuadsSyntaxTests(TestSuite suite, String fileBasePath, String testLocationBaseUri,
		RepositoryConnection con) throws Exception {
	StringBuilder positiveQuery = new StringBuilder();
	positiveQuery.append(" PREFIX mf:   <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>\n");
	positiveQuery.append(" PREFIX qt:   <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>\n");
	positiveQuery.append(" PREFIX rdft: <http://www.w3.org/ns/rdftest#>\n");
	positiveQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
	positiveQuery.append(" WHERE { \n");
	positiveQuery.append("     ?test a rdft:TestNQuadsPositiveSyntax . ");
	positiveQuery.append("     ?test mf:name ?testName . ");
	positiveQuery.append("     ?test mf:action ?inputURL . ");
	positiveQuery.append(" }");

	TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, positiveQuery.toString()).evaluate();

	// Add all positive parser tests to the test suite
	while (queryResult.hasNext()) {
		BindingSet bindingSet = queryResult.next();
		IRI nextTestUri = (IRI) bindingSet.getValue("test");
		String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
		String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), testLocationBaseUri);
		String nextInputURL = fileBasePath + nextTestFile;

		String nextBaseUrl = testLocationBaseUri + nextTestFile;

		suite.addTest(new PositiveParserTest(nextTestUri, nextTestName, nextInputURL, null, nextBaseUrl,
				createRDFParser(), createRDFParser()));
	}

	queryResult.close();

}
 
Example 6
Source File: TBSSSummariesGenerator.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Generate Mean wise independent permutation vector (MIPsV) for a predicate
 * @param pred Predicate name
 * @param endpoint SPARQL endpoint URL
 * @param d MipsVector Size can be fixed  e.g. 64 or in percentage e.g., 0.10 means 10%
 * @return MIPsV Mips Vector
 * @throws RepositoryException 
 * @throws MalformedQueryException 
 * @throws QueryEvaluationException 
 */
private String getMIPsV(String pred, String endpoint, double d) throws IOException
{
	ArrayList<Long> idsVector = new ArrayList<Long>() ;
	String MIPsV = "";
	String strQuery = getMIPsVQury(pred);
	SPARQLRepository repo = createSPARQLRepository(endpoint);
	TupleQuery query = repo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, strQuery); 
	TupleQueryResult rs = query.evaluate();
	while( rs.hasNext() ) 
	{
		BindingSet bset = rs.next();
		String sbj_obj = bset.getBinding("s").getValue().toString().concat(bset.getBinding("o").getValue().toString());
		idsVector.add((long) (sbj_obj.hashCode()));
	}
	long trplCount = idsVector.size();
	Collection c = new ArrayCollection(idsVector);
	MIPsynopsis synMIPs = null;
	if (d >= 1) {
		synMIPs = new MIPsynopsis(c, (int)d, 242); 
	} else {
	    synMIPs = new MIPsynopsis(c, (int)(Math.ceil(trplCount*d)), 242); 
	}
	long[] minValues = synMIPs.minValues;
	for (int i=0; i<minValues.length; i++)
		MIPsV = MIPsV.concat(minValues[i]+" ");

	return MIPsV.trim();
}
 
Example 7
Source File: AbstractNQuadsParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void parseNegativeNQuadsSyntaxTests(TestSuite suite, String fileBasePath, String testLocationBaseUri,
		RepositoryConnection con) throws Exception {
	StringBuilder negativeQuery = new StringBuilder();
	negativeQuery.append(" PREFIX mf:   <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>\n");
	negativeQuery.append(" PREFIX qt:   <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>\n");
	negativeQuery.append(" PREFIX rdft: <http://www.w3.org/ns/rdftest#>\n");
	negativeQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
	negativeQuery.append(" WHERE { \n");
	negativeQuery.append("     ?test a rdft:TestNQuadsNegativeSyntax . ");
	negativeQuery.append("     ?test mf:name ?testName . ");
	negativeQuery.append("     ?test mf:action ?inputURL . ");
	negativeQuery.append(" }");

	TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, negativeQuery.toString()).evaluate();

	// Add all negative parser tests to the test suite
	while (queryResult.hasNext()) {
		BindingSet bindingSet = queryResult.next();
		IRI nextTestUri = (IRI) bindingSet.getValue("test");
		String nextTestName = ((Literal) bindingSet.getValue("testName")).toString();
		String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), testLocationBaseUri);
		String nextInputURL = fileBasePath + nextTestFile;

		String nextBaseUrl = testLocationBaseUri + nextTestFile;

		suite.addTest(new NegativeParserTest(nextTestUri, nextTestName, nextInputURL, nextBaseUrl,
				createRDFParser(), FailureMode.DO_NOT_IGNORE_FAILURE));
	}

	queryResult.close();

}
 
Example 8
Source File: StatsGenerator.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
private static Set<String> getActualResults(String queryName) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	Set<String> actualResults = new HashSet<String>() ;
	
	String queryString = "PREFIX bigrdfbench: <http://bigrdfbench.aksw.org/schema/> \n"
			+ "			Select ?names ?values \n"
			+ "			WHERE \n"
			+ "			{\n"
			+ "			?s bigrdfbench:queryName <http://aksw.org/bigrdfbench/query/"+queryName+">.\n"
			+ "			<http://aksw.org/bigrdfbench/query/"+queryName+"> bigrdfbench:bindingNames ?names.\n"
			+ "			<http://aksw.org/bigrdfbench/query/"+queryName+"> bigrdfbench:bindingValues ?values\n"
			+ "			}";
	 
 	    TupleQuery tupleQuery = ResultsLoader.con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
 		 TupleQueryResult res = tupleQuery.evaluate();
 		   while(res.hasNext())
 		   {
 			  BindingSet result = res.next();
 		     String[] bindingNames =  result.getValue("names").stringValue().replace("'", "\"").replace("[", "").replace("]", "").split(";");
 		     String[] bindingValues =  result.getValue("values").stringValue().replace("'", "\"").replace("[", "").replace("]", "").split(";");
 		     String actualResult = "[";
 		     for(int i=0 ; i < bindingNames.length;i++)
 		     {
 		    	if(i<bindingNames.length-1)
 		    	 actualResult= actualResult+bindingNames[i]+"="+bindingValues[i]+";";
 		    	else
 		    		actualResult= actualResult+bindingNames[i]+"="+bindingValues[i]+"]";
 		     }
 		     actualResults.add(actualResult);
 		  //   System.out.println(actualResult);
 		   }
 		//   System.out.println(actualResults.size());
	return actualResults;
}
 
Example 9
Source File: TurtleParserTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void parseNegativeNTriplesSyntaxTests(TestSuite suite, String fileBasePath, String testBaseUrl,
		String manifestBaseUrl, RepositoryConnection con) throws Exception {
	StringBuilder negativeQuery = new StringBuilder();
	negativeQuery.append(" PREFIX mf:   <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>\n");
	negativeQuery.append(" PREFIX qt:   <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>\n");
	negativeQuery.append(" PREFIX rdft: <http://www.w3.org/ns/rdftest#>\n");
	negativeQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
	negativeQuery.append(" WHERE { \n");
	negativeQuery.append("     ?test a rdft:TestNTriplesNegativeSyntax . ");
	negativeQuery.append("     ?test mf:name ?testName . ");
	negativeQuery.append("     ?test mf:action ?inputURL . ");
	negativeQuery.append(" }");

	TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, negativeQuery.toString()).evaluate();

	// Add all negative parser tests to the test suite
	while (queryResult.hasNext()) {
		BindingSet bindingSet = queryResult.next();
		IRI nextTestUri = (IRI) bindingSet.getValue("test");
		String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
		String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), manifestBaseUrl);
		String nextInputURL = fileBasePath + nextTestFile;

		String nextBaseUrl = testBaseUrl + nextTestFile;

		suite.addTest(new NegativeParserTest(nextTestUri, nextTestName, nextInputURL, nextBaseUrl,
				createNTriplesParser(), FailureMode.DO_NOT_IGNORE_FAILURE));
	}

	queryResult.close();

}
 
Example 10
Source File: SummaryGenerator.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
static boolean hasNext(TupleQueryResult r) {
    try {
        return r.hasNext();
    } catch (Exception e) {
        log.warn("exception during the hasNext call", e);
        return false;
    }
}
 
Example 11
Source File: TurtleParserTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void parsePositiveNTriplesSyntaxTests(TestSuite suite, String fileBasePath, String testBaseUrl,
		String manifestBaseUrl, RepositoryConnection con) throws Exception {
	StringBuilder positiveQuery = new StringBuilder();
	positiveQuery.append(" PREFIX mf:   <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>\n");
	positiveQuery.append(" PREFIX qt:   <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>\n");
	positiveQuery.append(" PREFIX rdft: <http://www.w3.org/ns/rdftest#>\n");
	positiveQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
	positiveQuery.append(" WHERE { \n");
	positiveQuery.append("     ?test a rdft:TestNTriplesPositiveSyntax . ");
	positiveQuery.append("     ?test mf:name ?testName . ");
	positiveQuery.append("     ?test mf:action ?inputURL . ");
	positiveQuery.append(" }");

	TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, positiveQuery.toString()).evaluate();

	// Add all positive parser tests to the test suite
	while (queryResult.hasNext()) {
		BindingSet bindingSet = queryResult.next();
		IRI nextTestUri = (IRI) bindingSet.getValue("test");
		String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
		String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), manifestBaseUrl);
		String nextInputURL = fileBasePath + nextTestFile;

		String nextBaseUrl = testBaseUrl + nextTestFile;

		suite.addTest(new PositiveParserTest(nextTestUri, nextTestName, nextInputURL, null, nextBaseUrl,
				createNTriplesParser(), createNTriplesParser()));
	}

	queryResult.close();

}
 
Example 12
Source File: FedSumGenerator.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Write all the distinct object authorities having predicate p. 
 * consider <http://www.dbpedia.com/schema/xyz> rdf:type <http://www.dbpedia.com/schema/actor> 
 *  we are interested in http://www.dbpedia.com part of object. Note: for rdf:type we store all the classes 
 * @param predicate  Predicate
 * @param endpoint Endpoint URL
 * 
 * @throws MalformedQueryException 
 * @throws RepositoryException 
 * @throws QueryEvaluationException 
 * @throws IOException 
 */
public void writeObjAuthority(String predicate, String endpoint) throws RepositoryException, MalformedQueryException, QueryEvaluationException, IOException {
	ArrayList<String> objAuthorities = new ArrayList<String>();
	String strQuery = getObjAuthorityQury(predicate);
	SPARQLRepository repo = new SPARQLRepository(endpoint);
	TupleQuery query = repo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, strQuery); 
	TupleQueryResult res = query.evaluate();
	 while (res.hasNext()) 
	 {
		if (predicate.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"))
			objAuthorities.add(res.next().getValue("o").toString());
		else
		{
		 String[] objPrts = res.next().getValue("o").toString().split("/");
		 if ((objPrts.length>1))
		 {
		   String objAuth =objPrts[0]+"//"+objPrts[2];
		   if(!objAuthorities.contains(objAuth))
		   objAuthorities.add(objAuth);  
		}
		 }
	  }
	 repo.getConnection().close();
	 if(!objAuthorities.isEmpty())
	 {
	   bw.newLine();
	   bw.append("           ds:objAuthority ");

	   for(int authority=0; authority<objAuthorities.size();authority++)
	  {
		  String strAuth = objAuthorities.get(authority);
		   if(authority==objAuthorities.size()-1)
			  bw.write("<"+strAuth + "> ; ");
		 else
			 bw.write("<"+ strAuth+ ">, ");
	  }
	 }
	}
 
Example 13
Source File: AbstractNTriplesParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void parsePositiveNTriplesSyntaxTests(TestSuite suite, String fileBasePath, String testLocationBaseUri,
		RepositoryConnection con) throws Exception {
	StringBuilder positiveQuery = new StringBuilder();
	positiveQuery.append(" PREFIX mf:   <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>\n");
	positiveQuery.append(" PREFIX qt:   <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>\n");
	positiveQuery.append(" PREFIX rdft: <http://www.w3.org/ns/rdftest#>\n");
	positiveQuery.append(" SELECT ?test ?testName ?inputURL ?outputURL \n");
	positiveQuery.append(" WHERE { \n");
	positiveQuery.append("     ?test a rdft:TestNTriplesPositiveSyntax . ");
	positiveQuery.append("     ?test mf:name ?testName . ");
	positiveQuery.append("     ?test mf:action ?inputURL . ");
	positiveQuery.append(" }");

	TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SPARQL, positiveQuery.toString()).evaluate();

	// Add all positive parser tests to the test suite
	while (queryResult.hasNext()) {
		BindingSet bindingSet = queryResult.next();
		IRI nextTestUri = (IRI) bindingSet.getValue("test");
		String nextTestName = ((Literal) bindingSet.getValue("testName")).getLabel();
		String nextTestFile = removeBase(((IRI) bindingSet.getValue("inputURL")).toString(), testLocationBaseUri);
		String nextInputURL = fileBasePath + nextTestFile;

		String nextBaseUrl = testLocationBaseUri + nextTestFile;

		suite.addTest(new PositiveParserTest(nextTestUri, nextTestName, nextInputURL, null, nextBaseUrl,
				createRDFParser(), createRDFParser()));
	}

	queryResult.close();

}
 
Example 14
Source File: AbstractQueryResultIOTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void doTupleLinksAndStylesheetMultipleEndHeaders(TupleQueryResultFormat format, TupleQueryResult input,
		TupleQueryResult expected, List<String> links, String stylesheetUrl) throws QueryResultHandlerException,
		QueryEvaluationException, QueryResultParseException, UnsupportedQueryResultFormatException, IOException {
	ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
	TupleQueryResultWriter writer = QueryResultIO.createTupleWriter(format, out);
	// Test for handling when startDocument and startHeader are not called
	writer.handleStylesheet(stylesheetUrl);
	writer.startQueryResult(input.getBindingNames());
	writer.handleLinks(links);
	writer.endHeader();
	writer.endHeader();
	try {
		while (input.hasNext()) {
			BindingSet bindingSet = input.next();
			writer.handleSolution(bindingSet);
		}
	} finally {
		input.close();
	}
	writer.endQueryResult();

	// System.out.println("output: " + out.toString("UTF-8"));

	ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
	TupleQueryResult output = parseTupleInternal(format, in);

	assertQueryResultsEqual(expected, output);
}
 
Example 15
Source File: HibiscusSourceSelection.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Search HiBISCuS index for the given triple pattern p with sbj authority and obj authority.
 * Note: sa, oa can be null i.e. for unbound tuple 
 * @param stmt Statement pattern	
 * @param sa Subject authority
 * @param p Predicate
 * @param oa Object authority
 * @throws QueryEvaluationException Query Error
 * @throws MalformedQueryException  Memory Error
 * @throws RepositoryException  Repository Erro
 */
public void FedSumLookup(StatementPattern stmt, String sa, String p, String oa) throws QueryEvaluationException, RepositoryException, MalformedQueryException {
	    String  queryString = getFedSumLookupQuery(sa,p,oa) ;
	    TupleQuery tupleQuery = getSummaryConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
		 TupleQueryResult result = tupleQuery.evaluate();
		   while(result.hasNext())
		   {
			  String endpoint = result.next().getValue("url").stringValue();
				String id = "sparql_" + endpoint.replace("http://", "").replace("/", "_");
				addSource(stmt, new StatementSource(id, StatementSourceType.REMOTE));
		   }
	}
 
Example 16
Source File: WikiDataReification.java    From inception with Apache License 2.0 4 votes vote down vote up
private List<Statement> getStatementsById(RepositoryConnection aConnection, KnowledgeBase kb,
        String aStatementId)
{
    ValueFactory vf = aConnection.getValueFactory();
    
    String QUERY = String
        .join("\n",
            "SELECT DISTINCT ?s ?p ?ps ?o WHERE {",
            "  ?s  ?p  ?id .",
            "  ?id ?ps ?o .",
            "  FILTER(STRSTARTS(STR(?ps), STR(?ps_ns)))",
            "}",
            "LIMIT 10");
    Resource id = vf.createBNode(aStatementId);
    TupleQuery tupleQuery = aConnection.prepareTupleQuery(QueryLanguage.SPARQL, QUERY);
    tupleQuery.setBinding("id", id);
    tupleQuery.setBinding("ps_ns", vf.createIRI(PREDICATE_NAMESPACE));

    tupleQuery.setIncludeInferred(false);
    TupleQueryResult result;

    try {
        result = tupleQuery.evaluate();
    }
    catch (QueryEvaluationException e) {
        log.warn("No such statementId in knowledge base", e);
        return null;
    }

    List<Statement> statements = new ArrayList<>();
    while (result.hasNext()) {
        BindingSet bindings = result.next();
        Binding s = bindings.getBinding("s");
        Binding p = bindings.getBinding("p");
        Binding o = bindings.getBinding("o");
        Binding ps = bindings.getBinding("ps");

        IRI instance = vf.createIRI(s.getValue().stringValue());
        IRI predicate = vf.createIRI(p.getValue().stringValue());
        Statement root = vf.createStatement(instance, predicate, id);

        IRI valuePredicate = vf.createIRI(ps.getValue().stringValue());
        Value object = o.getValue();
        Statement valueStatement = vf.createStatement(id, valuePredicate, object);
        statements.add(root);
        statements.add(valueStatement);
    }
    return statements;
}
 
Example 17
Source File: FedSumSourceSelection.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
	 *  Get matching object predicate authorities from a specific source for a triple pattern 
	 * @param stmt Triple pattern
	 * @param src Capable source 
	 * @param v Vertex
	 * @return List of authorities
	 * @throws RepositoryException
	 * @throws MalformedQueryException
	 * @throws QueryEvaluationException
	 */
	public ArrayList<String> FedSumD_getMatchingObjAuthorities(StatementPattern stmt, StatementSource src, Vertex v) throws RepositoryException, MalformedQueryException, QueryEvaluationException 
	{
		String endPointUrl = "http://"+src.getEndpointID().replace("sparql_", "");
	    endPointUrl = endPointUrl.replace("_", "/");
	    String p = null;
	   ArrayList<String> objAuthorities = new ArrayList<String>();
	   if (stmt.getPredicateVar().getValue()!=null)
			p = stmt.getPredicateVar().getValue().stringValue();
			else
				p =stmt.getPredicateVar().getName().toString(); 
	   String  queryString = getFedSumObjAuthLookupQuery(stmt, endPointUrl,v) ;
	    TupleQuery tupleQuery = FedSumConfig.con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
		 TupleQueryResult result = tupleQuery.evaluate();
		 if(p.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) //for rdf:type
			 
		 {
			 while(result.hasNext())
			   {
				    String o = result.next().getValue("objAuth").stringValue();
				 	String[] objPrts = o.split("/");
				 	 objAuthorities.add(objPrts[0]+"//"+objPrts[2]);
			   }
			 
		 }
//		 else if(p.equals(v.label)) // node is a predicate of a triple pattern. enable this code if you are interested in looking for predicate join triples. 
//		 {
//			 while(result.hasNext())
//			   {
//				    String o = result.next().getValue("predAuth").stringValue();
//				 	String[] objPrts = o.split("/");
//				 	 objAuthorities.add(objPrts[0]+"//"+objPrts[2]);
//			   } 
//		 }
		 else
		 {
		   while(result.hasNext())
		   {
			   objAuthorities.add(result.next().getValue("objAuth").stringValue());
			
		   }
		 }
	return objAuthorities;
	}
 
Example 18
Source File: QueryEvaluation.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
public Map<String, List<List<Object>>> evaluate(String queries, String cfgName, List<String> endpoints) throws Exception {
	List<List<Object>> report = new ArrayList<List<Object>>();
	List<List<Object>> sstreport = new ArrayList<List<Object>>();
	Map<String, List<List<Object>>> result = new HashMap<String, List<List<Object>>>();
	result.put("report", report);
	result.put("sstreport", sstreport);
	
	List<String> qnames = Arrays.asList(queries.split(" "));
	for (String curQueryName : qnames)
	{
		List<Object> reportRow = new ArrayList<Object>();
		report.add(reportRow);
		String curQuery = qp.getQuery(curQueryName);
		reportRow.add(curQueryName);
		
		List<Object> sstReportRow = new ArrayList<Object>();
		sstreport.add(sstReportRow);
		sstReportRow.add(curQueryName);
		
		Config config = new Config(cfgName);
		SailRepository repo = null;
		TupleQueryResult res = null;
		
		try {
			repo = FedXFactory.initializeSparqlFederation(config, endpoints);
			TupleQuery query = repo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, curQuery);
			
		   	long startTime = System.currentTimeMillis();
		   	res = query.evaluate();
		    long count = 0;
		
		    while (res.hasNext()) {
		    	BindingSet row = res.next();
		    	System.out.println(count+": "+ row);
		    	count++;
		    }
		  
		    long runTime = System.currentTimeMillis() - startTime;
		    reportRow.add((Long)count); reportRow.add((Long)runTime);
		    sstReportRow.add((Long)count);
		    sstReportRow.add(QueryInfo.queryInfo.get().numSources.longValue());
		    sstReportRow.add(QueryInfo.queryInfo.get().totalSources.longValue());
		    log.info(curQueryName + ": Query exection time (msec): "+ runTime + ", Total Number of Records: " + count + ", Source count: " + QueryInfo.queryInfo.get().numSources.longValue());
		    //log.info(curQueryName + ": Query exection time (msec): "+ runTime + ", Total Number of Records: " + count + ", Source Selection Time: " + QueryInfo.queryInfo.get().getSourceSelection().time);
		} catch (Throwable e) {
			e.printStackTrace();
			log.error("", e);
			File f = new File("results/" + curQueryName + ".error.txt");
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			PrintStream ps = new PrintStream(os);
			e.printStackTrace(ps);
			ps.flush();
			FileUtils.write(f, os.toString("UTF8"));
			reportRow.add(null); reportRow.add(null);
		} finally {
			if (null != res) {
	    		res.close();
	    	}
			
	    	if (null != repo) {
	    	    repo.shutDown();
	    	}
        }
	}
	return result;
}
 
Example 19
Source File: Test.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    //QueryProvider qp;
    //qp = new QueryProvider("../queries/");
    
    String host = "localhost";
    
    List<String> endpointsMin = Arrays.asList(
            "http://" + host + ":8890/sparql",
            "http://" + host + ":8891/sparql",
            "http://" + host + ":8892/sparql"
            /*
            , "http://" + host + ":8893/sparql",
            "http://" + host + ":8894/sparql",
            "http://" + host + ":8895/sparql",
            "http://" + host + ":8896/sparql",
            "http://" + host + ":8897/sparql",
            "http://" + host + ":8898/sparql"
            */
    );
    
    FedXSailRepository rep = FedXFactory.initializeFederation("costfed.props", new DefaultEndpointListProvider(endpointsMin));
    //FedXSailRepository rep = FedXFactory.initializeFederation("costfed.props");
    try {
        RepositoryConnection conn = rep.getConnection();
        String testQuery = " SELECT  ?s ?Concept WHERE   { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?Concept } ";
        String query0 = "PREFIX  owl:  <http://www.w3.org/2002/07/owl#>\r\n" + 
                "PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n" + 
                "PREFIX  foaf: <http://xmlns.com/foaf/0.1/>\r\n" + 
                "\r\n" + 
                "SELECT DISTINCT  ?a ?v0 WHERE   {\r\n" + 
                "    ?a rdf:type foaf:Person .\r\n" + 
                "    ?a foaf:name ?v0\r\n" + 
                "}\r\n"; 
                //"OFFSET  10000\r\n" + 
                //"LIMIT   1000 ";
        
        String curQuery = testQuery;
                //qp.getQuery("S1");
        TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, curQuery); 
        long startTime = System.currentTimeMillis();
        TupleQueryResult res = query.evaluate();
        long count = 0;
        while (res.hasNext()) {
            BindingSet row = res.next();
            count++;
        }
        log.info("RESULT: " + count);
        //while (true) {
        //    Thread.sleep(1000);
        //    log.info("tick");
        //}
    } catch (Exception e) {
        
        log.error("", e);
        e.printStackTrace();
    } finally {
        rep.shutDown();
    }
}
 
Example 20
Source File: SparqlRepositoryTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) throws Exception {

		ExecutorService executor = Executors.newFixedThreadPool(20);

		SPARQLRepository repo = new SPARQLRepository("http://dbpedia.org/sparql");
		repo.init();
		final RepositoryConnection conn = repo.getConnection();

		TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL,
				"SELECT DISTINCT ?President ?Party ?Articles ?TopicPage WHERE {  ?President <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/PresidentsOfTheUnitedStates> . }");
		TupleQueryResult res = query.evaluate();
		List<IRI> list = new ArrayList<>();
		while (res.hasNext()) {
			list.add((IRI) res.next().getValue("President"));
		}
		res.close();

		System.out.println("Retrieved " + list.size() + " instances");
		List<Future<?>> tasks = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			for (final IRI instance : list) {
				tasks.add(executor.submit(() -> {
					try {
						Thread.sleep(new Random().nextInt(300));
						BooleanQuery bq = conn.prepareBooleanQuery(QueryLanguage.SPARQL, "ASK { <"
								+ instance.stringValue()
								+ "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/PresidentsOfTheUnitedStates> }");
						bq.evaluate();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}));

			}
		}
		System.out.println("All tasks submitted, awating termination.");
//		TupleQueryResult qRes2 = query.evaluate();
//		while (qRes2.hasNext()) {
//			qRes2.next();
//		}
		for (Future<?> t : tasks) {
			t.get();
		}
		System.out.println("Done");
		executor.shutdown();
		System.exit(1);
	}