use of org.apache.jena.query.ResultSet in project eol-globi-data by jhpoelen.
the class DatasetImporterForSaproxylic method importStudy.
@Override
public void importStudy() throws StudyImporterException {
try {
final Model model = ModelFactory.createDefaultModel();
NavigableSet<Triple<String, String, String>> triples = DBMaker.newTempTreeSet();
Tripler add = triple -> {
Resource resource = model.createResource("sx:" + triple.getLeft());
Property property = model.createProperty("sx:" + triple.getMiddle());
String obj = triple.getRight();
if (StringUtils.startsWith(obj, "{")) {
Resource food = model.createResource("sx:" + obj);
model.add(resource, property, food);
} else if (StringUtils.startsWith(obj, "http")) {
model.add(resource, property, model.createResource(obj));
} else if (StringUtils.isNotBlank(obj)) {
model.add(resource, property, obj);
}
};
parseReferences(add, getDataset().retrieve(URI.create("sx_txt/Reference.txt")));
parseLocalities(add, getDataset().retrieve(URI.create("sx_txt/Locality.txt")));
parseTaxa(add, getDataset().retrieve(URI.create("sx_txt/Taxon.txt")));
parseTaxonRanks(add, getDataset().retrieve(URI.create("sx_txt/TaxonRank.txt")));
parseOccurrences(add, getDataset().retrieve(URI.create("sx_txt/Occurrence.txt")));
parseAssociations(add, getDataset().retrieve(URI.create("sx_txt/SX_Association.txt")));
parseInteractionTypeMap(add, getDataset().retrieve(URI.create("interaction_type_map.tsv")));
// associations -> occurrences, taxa, reference
String queryString = "SELECT ?sourceTaxonName ?sourceLifeStage ?interactionTypeId ?targetTaxonName ?targetLifeStage ?referenceCitation ?localityName ?studyTitle " + "WHERE {" + " ?interaction <sx:mentioned_by> ?studyTitle . " + " ?studyTitle <sx:hasName> ?referenceCitation . " + " ?sourceSpecimen <sx:participates_in> ?interaction . " + " ?targetSpecimen <sx:participates_in> ?interaction . " + " ?sourceSpecimen ?inter ?targetSpecimen . " + " ?inter <sx:equivalentTo> ?interactionTypeId . " + " ?sourceSpecimen <sx:classifiedAs> ?sourceTaxon . " + " ?sourceTaxon <sx:hasName> ?sourceTaxonName . " + " ?targetSpecimen <sx:classifiedAs> ?targetTaxon . " + " ?targetTaxon <sx:hasName> ?targetTaxonName . " + " ?sourceSpecimen <sx:inStage> ?sourceLifeStage . " + " ?targetSpecimen <sx:inStage> ?targetLifeStage . " + " ?targetSpecimen <sx:foundAt> ?locality . " + " ?locality <sx:hasName> ?localityName . " + "}";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
toInteractions(results);
qe.close();
} catch (IOException e) {
throw new StudyImporterException("failed to access resource", e);
}
}
use of org.apache.jena.query.ResultSet in project jena by apache.
the class Service method exec.
/**
* Executes a service operator
*
* @param op
* Service
* @param context
* Context
* @return Query iterator of service results
*/
public static QueryIterator exec(OpService op, Context context) {
if (context != null && context.isFalse(serviceAllowed))
throw new QueryExecException("SERVICE execution disabled");
if (!op.getService().isURI())
throw new QueryExecException("Service URI not bound: " + op.getService());
// This relies on the observation that the query was originally correct,
// so reversing the scope renaming is safe (it merely restores the
// algebra expression).
// Any variables that reappear should be internal ones that were hidden
// by renaming in the first place.
// Any substitution is also safe because it replaced variables by
// values.
Op opRemote = Rename.reverseVarRename(op.getSubOp(), true);
// JENA-494 There is a bug here that the renaming means that if this is
// deeply nested and joined to other things at the same level of you end
// up with the variables being disjoint and the same results
// The naive fix for this is to map the variables visible in the inner
// operator to those visible in the rewritten operator
// There may be some cases where the re-mapping is incorrect due to
// deeply nested SERVICE clauses
Map<Var, Var> varMapping = new HashMap<>();
Set<Var> originalVars = OpVars.visibleVars(op);
Set<Var> remoteVars = OpVars.visibleVars(opRemote);
boolean requiresRemapping = false;
for (Var v : originalVars) {
if (v.getName().contains("/")) {
// A variable which was scope renamed so has a different name
String origName = v.getName().substring(v.getName().lastIndexOf('/') + 1);
Var remoteVar = Var.alloc(origName);
if (remoteVars.contains(remoteVar)) {
varMapping.put(remoteVar, v);
requiresRemapping = true;
}
} else {
// A variable which does not have a different name
if (remoteVars.contains(v))
varMapping.put(v, v);
}
}
// Explain.explain("HTTP", opRemote, context) ;
Query query;
//@formatter:off
// Comment (for the future?)
// if ( false )
// {
// // ***** Interacts with substitution.
// Element el = op.getServiceElement().getElement() ;
// if ( el instanceof ElementSubQuery )
// query = ((ElementSubQuery)el).getQuery() ;
// else
// {
// query = QueryFactory.create() ;
// query.setQueryPattern(el) ;
// query.setResultVars() ;
// }
// }
// else
//@formatter:on
query = OpAsQuery.asQuery(opRemote);
Explain.explain("HTTP", query, context);
String uri = op.getService().getURI();
HttpQuery httpQuery = configureQuery(uri, context, query);
InputStream in = httpQuery.exec();
// Read the whole of the results now.
// Avoids the problems with calling back into the same system e.g.
// Fuseki+SERVICE <http://localhost:3030/...>
ResultSet rs = ResultSetFactory.fromXML(in);
QueryIterator qIter = QueryIter.materialize(new QueryIteratorResultSet(rs));
// And close connection now, not when qIter is closed.
IO.close(in);
// nested SERVICE clauses
if (requiresRemapping) {
qIter = QueryIter.map(qIter, varMapping);
}
return qIter;
}
use of org.apache.jena.query.ResultSet in project jena by apache.
the class StoreUtils method storeGraphNames.
public static Iterator<Node> storeGraphNames(Store store) {
List<Node> x = new ArrayList<Node>();
String qs = "SELECT ?g { GRAPH ?g { }}";
QueryExecution qExec = QueryExecutionFactory.create(qs, SDBFactory.connectDataset(store));
ResultSet rs = qExec.execSelect();
Var var_g = Var.alloc("g");
while (rs.hasNext()) {
Node n = rs.nextBinding().get(var_g);
x.add(n);
}
return x.iterator();
}
use of org.apache.jena.query.ResultSet in project jena by apache.
the class ExTDB5 method main.
public static void main(String... argv) {
// Direct way: Make a TDB-back Jena model in the named directory.
String directory = "MyDatabases/DB1";
Dataset dataset = TDBFactory.createDataset(directory);
// Potentially expensive query.
String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }";
// See http://incubator.apache.org/jena/documentation/query/app_api.html
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
int count = soln.getLiteral("count").getInt();
System.out.println("count = " + count);
}
} finally {
qexec.close();
}
// Close the dataset.
dataset.close();
}
use of org.apache.jena.query.ResultSet in project jena by apache.
the class TestResultSet method test_RS_9.
@Test
public void test_RS_9() {
ResultSet rs = ResultSetFactory.load("testing/ResultSet/output.srx");
test_RS_fmt(rs, ResultsFormat.FMT_RDF_XML, false);
}
Aggregations