use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.
the class DatasetToDatasetBySparqlGSPOSelectFunction method apply.
@Override
public Dataset apply(Dataset dataset) {
dataset.begin(ReadWrite.READ);
Dataset result = DatasetFactory.createGeneral();
result.begin(ReadWrite.WRITE);
Query query = QueryFactory.create(sparql);
try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset)) {
ResultSet resultSet = queryExecution.execSelect();
RDFNode currentProposal = null;
Model currentProposalContent = ModelFactory.createDefaultModel();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
RDFNode proposalNode = solution.get("g");
if (currentProposal == null) {
// first solution: remember uri of first proposal
currentProposal = proposalNode;
} else {
// the currentProposal URI and prepare a new empty model for the next proposal
if (!currentProposal.equals(proposalNode)) {
// we have seen all triples of currentProposal
result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
currentProposalContent = ModelFactory.createDefaultModel();
}
currentProposal = proposalNode;
}
// add current triple into currentAgreementModel
RDFNode s = solution.get("s");
RDFNode p = solution.get("p");
RDFNode o = solution.get("o");
Statement newStatement = new StatementImpl(s.asResource(), new PropertyImpl(p.asResource().getURI()), o);
currentProposalContent.add(newStatement);
}
// add the last model
if (currentProposal != null) {
result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
}
return result;
} finally {
dataset.commit();
result.commit();
}
}
use of org.apache.jena.rdf.model.Statement 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.Statement in project webofneeds by researchstudio-sat.
the class ModelConverter method modelToGraphCollection.
private static GraphCollection modelToGraphCollection(String name, Model model, Map<String, String> pm) {
// Convert each subj pred obj in Jena Statement into String and add to
// SigningFramework's NamedGraph.
// The simpler approach with just using Jena's writer and Signingframework's
// reader to transform data between data structures won't work since
// Signingframework has problems with recognizing the [] structure
GraphCollection graphc = new GraphCollection();
NamedGraph namedGraph = new NamedGraph(enclose(name, "<", ">"), 0, null);
StmtIterator iterator = model.listStatements();
while (iterator.hasNext()) {
Statement stmt = iterator.nextStatement();
String subjString = rdfNodeAsString(stmt.getSubject());
String objString = rdfNodeAsString(stmt.getObject());
String predString = enclose(stmt.getPredicate().asResource().getURI(), "<", ">");
Triple gcTriple = new Triple(subjString, predString, objString);
namedGraph.addTriple(gcTriple);
}
graphc.addGraph(namedGraph);
for (String prefix : pm.keySet()) {
graphc.addPrefix(new Prefix(prefix + ":", "<" + pm.get(prefix) + ">"));
}
return graphc;
}
use of org.apache.jena.rdf.model.Statement 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.Statement in project webofneeds by researchstudio-sat.
the class WonMessageValidatorTest method testMissingAndInvalidMessageDirection.
@Test
@Ignore
public void testMissingAndInvalidMessageDirection() throws IOException {
// create invalid dataset by removing a triple with message direction
Dataset invalidDataset = WonRdfUtils.MessageUtils.copyByDatasetSerialization(new WonMessage(createMessageDataset)).getCompleteDataset();
Model env1Model = invalidDataset.getNamedModel(CREATE_ENV1_NAME);
Model env2Model = invalidDataset.getNamedModel(CREATE_ENV2_NAME);
Statement stmtOld = env2Model.createStatement(ResourceFactory.createResource(CREATE_ENV1_ENV2_MSG_URI), RDF.type, WONMSG.TYPE_FROM_OWNER);
env1Model.remove(stmtOld);
env2Model.remove(stmtOld);
// validate this invalid dataset
WonMessageValidator validator = new WonMessageValidator();
StringBuilder message = new StringBuilder();
boolean valid = validator.validate(invalidDataset, message);
Assert.assertTrue("validation is expected to fail", !valid);
Assert.assertTrue(message.toString().contains("missing_direction"));
// create invalid dataset by adding a triple with invalid message direction
Statement stmtNew = env2Model.createStatement(ResourceFactory.createResource(CREATE_ENV1_ENV2_MSG_URI), RDF.type, ResourceFactory.createProperty("test:property:uri"));
env2Model.add(stmtNew);
// validate this invalid dataset
valid = validator.validate(invalidDataset, message);
Assert.assertTrue("validation is expected to fail", !valid);
Assert.assertTrue(message.toString().contains("invalid_direction"));
}
Aggregations