Search in sources :

Example 11 with NodeIterator

use of org.apache.jena.rdf.model.NodeIterator in project webofneeds by researchstudio-sat.

the class RdfDatasetAttachmentConverter method getObjectOfPropertyAsString.

private String getObjectOfPropertyAsString(Model model, Property property) {
    NodeIterator nodeIteratr = model.listObjectsOfProperty(property);
    if (!nodeIteratr.hasNext())
        return null;
    String ret = nodeIteratr.next().asLiteral().getString();
    if (nodeIteratr.hasNext()) {
        throw new IncorrectPropertyCountException("found more than one property of cnt:bytes", 1, 2);
    }
    return ret;
}
Also used : NodeIterator(org.apache.jena.rdf.model.NodeIterator) IncorrectPropertyCountException(won.protocol.exception.IncorrectPropertyCountException)

Example 12 with NodeIterator

use of org.apache.jena.rdf.model.NodeIterator 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 13 with NodeIterator

use of org.apache.jena.rdf.model.NodeIterator in project webofneeds by researchstudio-sat.

the class TwoPhaseCommitDeactivateOnCloseAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    // If we receive a close event, it must carry a commit message.
    if (event instanceof CloseFromOtherNeedEvent) {
        URI needURI = ((CloseFromOtherNeedEvent) event).getNeedURI();
        WonMessage wonMessage = ((CloseFromOtherNeedEvent) event).getWonMessage();
        NodeIterator ni = RdfUtils.visitFlattenedToNodeIterator(wonMessage.getMessageContent(), new RdfUtils.ModelVisitor<NodeIterator>() {

            @Override
            public NodeIterator visit(final Model model) {
                return model.listObjectsOfProperty(model.createProperty(WON_TX.COORDINATION_MESSAGE.getURI()));
            }
        });
        assert ni.hasNext() : "no additional content found in close message, expected a commit";
        String coordinationMessageUri = ni.toList().get(0).asResource().getURI().toString();
        assert coordinationMessageUri.equals(WON_TX.COORDINATION_MESSAGE_COMMIT.getURI().toString()) : "expected a " + "Commmit message";
        getEventListenerContext().getWonMessageSender().sendWonMessage(createWonMessage(needURI));
        getEventListenerContext().getEventBus().publish(new NeedDeactivatedEvent(needURI));
    }
}
Also used : NodeIterator(org.apache.jena.rdf.model.NodeIterator) CloseFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent) WonMessage(won.protocol.message.WonMessage) Model(org.apache.jena.rdf.model.Model) WonRdfUtils(won.protocol.util.WonRdfUtils) RdfUtils(won.protocol.util.RdfUtils) URI(java.net.URI) NeedDeactivatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent)

Example 14 with NodeIterator

use of org.apache.jena.rdf.model.NodeIterator 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 15 with NodeIterator

use of org.apache.jena.rdf.model.NodeIterator in project jena by apache.

the class SecurityEvaluatorAssembler method open.

// initialization and registration is performed by SecuredAssembler
@Override
public SecurityEvaluator open(Assembler a, Resource root, Mode mode) {
    Literal className = getUniqueLiteral(root, EVALUATOR_CLASS);
    if (className == null) {
        throw new AssemblerException(root, String.format(NO_X_PROVIDED, EVALUATOR_CLASS, root));
    }
    Class<?> clazz;
    try {
        clazz = Class.forName(className.getString());
    } catch (ClassNotFoundException e1) {
        throw new AssemblerException(root, String.format("Can not locate class %s as specified by %s in %s", className, EVALUATOR_CLASS, root));
    }
    if (!SecurityEvaluator.class.isAssignableFrom(clazz)) {
        throw new AssemblerException(root, String.format("Class %s as specified by %s in %s does not implement SecurityEvaluator", className, EVALUATOR_CLASS, root));
    }
    // get the arguments as specified.
    List<Object> args = new ArrayList<>();
    Resource argRes = getUniqueResource(root, ARGUMENT_LIST);
    if (argRes != null) {
        Seq seq = argRes.as(Seq.class);
        NodeIterator iter = seq.iterator();
        RDFNode n = null;
        while (iter.hasNext()) {
            n = iter.next();
            if (n.isLiteral()) {
                args.add(n.asLiteral().getValue());
            } else if (n.isResource()) {
                args.add(a.open(a, n.asResource(), mode));
            } else {
                throw new AssemblerException(root, String.format("%s must be a literal or a resource", n));
            }
        }
    }
    for (Constructor<?> c : clazz.getConstructors()) {
        if (c.getParameterTypes().length == args.size()) {
            try {
                if (args.size() == 0) {
                    return (SecurityEvaluator) c.newInstance();
                }
                return (SecurityEvaluator) c.newInstance(args.toArray());
            } catch (InstantiationException e) {
                throw new AssemblerException(root, e.getMessage(), e);
            } catch (IllegalAccessException e) {
                throw new AssemblerException(root, e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                throw new AssemblerException(root, e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new AssemblerException(root, e.getMessage(), e);
            }
        }
    }
    throw new AssemblerException(root, String.format("Class %s does not have a %s argument constructor", className, args.size()));
}
Also used : NodeIterator(org.apache.jena.rdf.model.NodeIterator) AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) InvocationTargetException(java.lang.reflect.InvocationTargetException) Literal(org.apache.jena.rdf.model.Literal) Seq(org.apache.jena.rdf.model.Seq) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

NodeIterator (org.apache.jena.rdf.model.NodeIterator)16 RDFNode (org.apache.jena.rdf.model.RDFNode)8 Model (org.apache.jena.rdf.model.Model)6 Literal (org.apache.jena.rdf.model.Literal)5 Resource (org.apache.jena.rdf.model.Resource)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 RdfUtils (won.protocol.util.RdfUtils)3 HashSet (java.util.HashSet)2 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)2 Container (org.apache.jena.rdf.model.Container)2 NeedDeactivatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent)2 CloseFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent)2 IncorrectPropertyCountException (won.protocol.exception.IncorrectPropertyCountException)2 WonMessage (won.protocol.message.WonMessage)2 WonRdfUtils (won.protocol.util.WonRdfUtils)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Set (java.util.Set)1 AssemblerException (org.apache.jena.assembler.exceptions.AssemblerException)1 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)1