use of org.apache.jena.rdf.model.RDFNode in project webofneeds by researchstudio-sat.
the class LinkedDataSourceBase method getURIsToCrawl.
/**
* For the specified properties, finds their objects and adds the identified
* resources to the returned set if they are not contained in the specified
* exclude set.
*
* @param dataset
* @param excludedUris
* @param properties
* @return
*/
private Set<URI> getURIsToCrawl(Dataset dataset, Set<URI> excludedUris, final List<URI> properties) {
Set<URI> toCrawl = new HashSet<>();
for (int i = 0; i < properties.size(); i++) {
final URI property = properties.get(i);
NodeIterator objectIterator = RdfUtils.visitFlattenedToNodeIterator(dataset, new RdfUtils.ModelVisitor<NodeIterator>() {
@Override
public NodeIterator visit(final Model model) {
final Property p = model.createProperty(property.toString());
return model.listObjectsOfProperty(p);
}
});
for (; objectIterator.hasNext(); ) {
RDFNode objectNode = objectIterator.next();
if (objectNode.isURIResource()) {
URI discoveredUri = URI.create(objectNode.asResource().getURI());
if (!excludedUris.contains(discoveredUri)) {
toCrawl.add(discoveredUri);
}
}
}
}
return toCrawl;
}
use of org.apache.jena.rdf.model.RDFNode in project webofneeds by researchstudio-sat.
the class WonLinkedDataUtils method getWonNodeURIForNeedOrConnection.
public static URI getWonNodeURIForNeedOrConnection(final URI resURI, final Model resourceModel) {
assert resourceModel != null : "model must not be null";
// we didnt't get the queue name. Check if the model contains a triple <baseuri> won:hasWonNode
// <wonNode> and get the information from there.
logger.debug("getting WON node URI from model");
Resource baseResource = resourceModel.getResource(resURI.toString());
logger.debug("resourceModel: {}", RdfUtils.toString(resourceModel));
StmtIterator wonNodeStatementIterator = baseResource.listProperties(WON.HAS_WON_NODE);
if (!wonNodeStatementIterator.hasNext()) {
// no won:hasWonNode triple found. we can't do anything.
logger.debug("base resource {} has no won:hasWonNode property", baseResource);
return null;
}
Statement stmt = wonNodeStatementIterator.nextStatement();
RDFNode wonNodeNode = stmt.getObject();
if (!wonNodeNode.isResource()) {
logger.debug("won:hasWonNode property of base resource {} is not a resource", baseResource);
return null;
}
URI wonNodeUri = URI.create(wonNodeNode.asResource().getURI().toString());
logger.debug("obtained WON node URI: {}", wonNodeUri);
if (wonNodeStatementIterator.hasNext()) {
logger.warn("multiple WON node URIs found for resource {}, using first one: {} ", baseResource, wonNodeUri);
}
return wonNodeUri;
}
use of org.apache.jena.rdf.model.RDFNode in project webofneeds by researchstudio-sat.
the class WonMessageBuilderTest method check_get_content_in_message_with_content_dataset.
public void check_get_content_in_message_with_content_dataset(final WonMessage msg) {
Dataset actualContentDataset = msg.getMessageContent();
Assert.assertTrue("messageContent dataset of message with content has non-empty default graph", actualContentDataset.getDefaultModel().isEmpty());
Set<String> names = new HashSet<String>();
Iterators.addAll(names, actualContentDataset.listNames());
Assert.assertEquals("incorrect number of named graphs", names.size(), 2);
RdfUtils.toNamedModelStream(actualContentDataset, false).forEach(namedModel -> {
String graphUri = namedModel.getName();
Model model = namedModel.getModel();
Assert.assertTrue("model does not contain its own graph uri", model.containsResource(model.getResource(graphUri)));
Statement stmt = model.getResource(graphUri).getProperty(SKOS.related);
Assert.assertNotNull(stmt);
RDFNode otherGraph = stmt.getObject();
Assert.assertTrue("Reference to other model not found", actualContentDataset.containsNamedModel(otherGraph.asResource().getURI()));
});
}
use of org.apache.jena.rdf.model.RDFNode in project webofneeds by researchstudio-sat.
the class AgreementProtocolState method acceptProposal.
/**
* @param proposalUri
* @return true if the operation had any effect, false otherwise
*/
private boolean acceptProposal(URI proposalUri) {
boolean changedSomething = false;
// first process proposeToCancel triples - this avoids that a message can
// successfully propose to cancel itself, as agreements are only made after the
// cancellations are processed.
Model cancellationProposals = pendingProposals.getDefaultModel();
NodeIterator nIt = cancellationProposals.listObjectsOfProperty(cancellationProposals.getResource(proposalUri.toString()), WONAGR.PROPOSES_TO_CANCEL);
if (nIt.hasNext()) {
// remember that this proposal contained a cancellation
this.acceptedCancellationProposalUris.add(proposalUri);
changedSomething = true;
}
while (nIt.hasNext()) {
RDFNode agreementToCancelUri = nIt.next();
changedSomething = cancelAgreement(URI.create(agreementToCancelUri.asResource().getURI())) || changedSomething;
}
changedSomething = removeCancellationProposal(proposalUri) || changedSomething;
// move proposal to agreements
changedSomething = moveNamedGraph(proposalUri, pendingProposals, agreements) || changedSomething;
return changedSomething;
}
use of org.apache.jena.rdf.model.RDFNode in project legato by DOREMUS-ANR.
the class PropertyHandler method getScore.
public static double getScore(Resource property, Model model) {
/**
************
* Get distinct resources having the property "property"
*************
*/
List<Resource> resources = new ArrayList<Resource>();
String sparqlQueryString = "SELECT DISTINCT ?resource WHERE {" + "?resource <" + property + "> ?object" + "}";
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet queryResults = qexec.execSelect();
while (queryResults.hasNext()) {
QuerySolution qs = queryResults.nextSolution();
resources.add(qs.getResource("?resource"));
}
qexec.close();
/**
************
* Get all DISTINCT objects of the property "property"
*************
*/
List<RDFNode> disObj = new ArrayList<RDFNode>();
String sparqlQueryString2 = "SELECT DISTINCT ?object WHERE {" + "?resource <" + property + "> ?object" + "}";
Query query2 = QueryFactory.create(sparqlQueryString2);
QueryExecution qexec2 = QueryExecutionFactory.create(query2, model);
ResultSet queryResults2 = qexec2.execSelect();
while (queryResults2.hasNext()) {
QuerySolution qs = queryResults2.nextSolution();
disObj.add(qs.get("?object"));
}
qexec2.close();
/**
************
* Get all objects of the property "property"
*************
*/
List<RDFNode> obj = new ArrayList<RDFNode>();
String sparqlQueryString3 = "SELECT ?object WHERE {" + "?resource <" + property + "> ?object" + "}";
Query query3 = QueryFactory.create(sparqlQueryString3);
QueryExecution qexec3 = QueryExecutionFactory.create(query3, model);
ResultSet queryResults3 = qexec3.execSelect();
while (queryResults3.hasNext()) {
QuerySolution qs = queryResults3.nextSolution();
obj.add(qs.get("?object"));
}
qexec3.close();
double redondance = (obj.size() - disObj.size());
if (redondance == 0)
redondance = 1;
return (resources.size() / redondance);
}
Aggregations