Search in sources :

Example 91 with RDFNode

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;
}
Also used : NodeIterator(org.apache.jena.rdf.model.NodeIterator) Model(org.apache.jena.rdf.model.Model) RdfUtils(won.protocol.util.RdfUtils) URI(java.net.URI) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode) HashSet(java.util.HashSet)

Example 92 with RDFNode

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;
}
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 93 with RDFNode

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()));
    });
}
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 94 with RDFNode

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;
}
Also used : NodeIterator(org.apache.jena.rdf.model.NodeIterator) Model(org.apache.jena.rdf.model.Model) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 95 with RDFNode

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);
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

RDFNode (org.apache.jena.rdf.model.RDFNode)145 Resource (org.apache.jena.rdf.model.Resource)51 Model (org.apache.jena.rdf.model.Model)30 Statement (org.apache.jena.rdf.model.Statement)27 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)22 Node (org.apache.jena.graph.Node)20 QuerySolution (org.apache.jena.query.QuerySolution)20 TextIndexException (org.apache.jena.query.text.TextIndexException)17 Property (org.apache.jena.rdf.model.Property)17 Literal (org.apache.jena.rdf.model.Literal)15 SecuredRDFNode (org.apache.jena.permissions.model.SecuredRDFNode)13 Dataset (org.apache.jena.query.Dataset)13 Triple (org.apache.jena.graph.Triple)12 QueryExecution (org.apache.jena.query.QueryExecution)10 StmtIterator (org.apache.jena.rdf.model.StmtIterator)9 Query (org.apache.jena.query.Query)8 NodeIterator (org.apache.jena.rdf.model.NodeIterator)8 RDFList (org.apache.jena.rdf.model.RDFList)8 AccessDeniedException (org.apache.jena.shared.AccessDeniedException)7