Search in sources :

Example 51 with Statement

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();
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) StatementImpl(org.apache.jena.rdf.model.impl.StatementImpl) ResultSet(org.apache.jena.query.ResultSet) Model(org.apache.jena.rdf.model.Model) PropertyImpl(org.apache.jena.rdf.model.impl.PropertyImpl) QueryExecution(org.apache.jena.query.QueryExecution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 52 with Statement

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;
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) URI(java.net.URI) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 53 with Statement

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;
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) Triple(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Triple) StmtIterator(org.apache.jena.rdf.model.StmtIterator) NamedGraph(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.NamedGraph) Statement(org.apache.jena.rdf.model.Statement) Prefix(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix)

Example 54 with Statement

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()));
    });
}
Also used : Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) RDFNode(org.apache.jena.rdf.model.RDFNode) HashSet(java.util.HashSet)

Example 55 with Statement

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"));
}
Also used : Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) WonMessageValidator(won.protocol.validation.WonMessageValidator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Statement (org.apache.jena.rdf.model.Statement)72 Model (org.apache.jena.rdf.model.Model)43 StmtIterator (org.apache.jena.rdf.model.StmtIterator)28 Test (org.junit.Test)25 Dataset (org.apache.jena.query.Dataset)24 Resource (org.apache.jena.rdf.model.Resource)19 WonMessageValidator (won.protocol.validation.WonMessageValidator)15 Property (org.apache.jena.rdf.model.Property)13 RDFNode (org.apache.jena.rdf.model.RDFNode)13 ArrayList (java.util.ArrayList)11 Ignore (org.junit.Ignore)11 Triple (org.apache.jena.graph.Triple)7 BaseTest (org.apache.jena.atlas.junit.BaseTest)6 Iterator (java.util.Iterator)4 RdfUtils (won.protocol.util.RdfUtils)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 OntModel (org.apache.jena.ontology.OntModel)2 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)2 GraphCollection (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection)1