Search in sources :

Example 16 with AtomModelWrapper

use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.

the class GoalInstantiationProducer method createGoalInstantiationResults.

private Collection<GoalInstantiationResult> createGoalInstantiationResults(Dataset atom) {
    AtomModelWrapper atomWrapper = new AtomModelWrapper(atom);
    Collection<GoalInstantiationResult> results = new LinkedList<>();
    for (Resource goal : atomWrapper.getGoals()) {
        results.add(findInstantiationForGoal(goal));
    }
    return results;
}
Also used : Resource(org.apache.jena.rdf.model.Resource) AtomModelWrapper(won.protocol.util.AtomModelWrapper) LinkedList(java.util.LinkedList)

Example 17 with AtomModelWrapper

use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.

the class WonMessageSlipComputerTests method loadDatasetAndReplaceAtomURI.

private Dataset loadDatasetAndReplaceAtomURI(String file, URI atomURI) throws IOException {
    Dataset atomDataset = loadDataset(file);
    AtomModelWrapper amw = new AtomModelWrapper(atomDataset);
    amw.setAtomURI(atomURI);
    return atomDataset;
}
Also used : Dataset(org.apache.jena.query.Dataset) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 18 with AtomModelWrapper

use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.

the class AtomService method replaceAtom.

public Atom replaceAtom(final WonMessage wonMessage, WonAclEvaluator evaluator, OperationRequest request) throws NoSuchAtomException {
    Dataset newAtomContent = wonMessage.getMessageContent();
    List<WonMessage.AttachmentHolder> attachmentHolders = wonMessage.getAttachments();
    // remove attachment and its signature from the atomContent
    removeAttachmentsFromAtomContent(newAtomContent, attachmentHolders);
    final AtomModelWrapper atomModelWrapper = new AtomModelWrapper(newAtomContent);
    URI atomURI = getAtomURIAndCheck(atomModelWrapper);
    if (evaluator != null) {
        checkReplaceAllowed(newAtomContent, atomURI, evaluator, request);
    }
    checkCanThisMessageCreateOrModifyThisAtom(wonMessage, atomURI);
    checkResourcesInAtomContent(atomModelWrapper);
    URI messageURI = wonMessage.getMessageURI();
    atomModelWrapper.renameResourceWithPrefix(messageURI.toString(), atomURI.toString());
    final Atom atom = getAtomRequired(atomURI);
    DatasetHolder datasetHolder = atom.getDatatsetHolder();
    Dataset oldAtomDataset = atom.getDatatsetHolder().getDataset();
    URI keyGraphUri = WonRelativeUriHelper.createKeyGraphURIForAtomURI(atom.getAtomURI());
    // store the atom content
    Iterator<String> oldGraphNames = oldAtomDataset.listNames();
    String derivedDataName = atom.getAtomURI().toString() + "#derivedData";
    while (oldGraphNames.hasNext()) {
        String oldGraphName = oldGraphNames.next();
        if (oldGraphName.equals(derivedDataName)) {
            newAtomContent.addNamedModel(oldGraphName, oldAtomDataset.getNamedModel(oldGraphName));
            continue;
        }
        if (!newAtomContent.containsNamedModel(oldGraphName)) {
            // copy graphs not contained in the message from old to new state
            // ie, graphs with same name are overwritten
            newAtomContent.addNamedModel(oldGraphName, oldAtomDataset.getNamedModel(oldGraphName));
        }
    }
    if (!newAtomContent.containsNamedModel(keyGraphUri.toString())) {
        throw new WonProtocolException(String.format("Cannot replace %s, operation results in atom without key graph", atomURI));
    }
    // replace attachments
    List<DatasetHolder> attachments = new ArrayList<>(attachmentHolders.size());
    for (WonMessage.AttachmentHolder attachmentHolder : attachmentHolders) {
        datasetHolder = new DatasetHolder(attachmentHolder.getDestinationUri(), attachmentHolder.getAttachmentDataset());
        attachments.add(datasetHolder);
    }
    // rename the content graphs and signature graphs so they start with the atom
    // uri
    // analyzed change in socket data
    Collection<String> sockets = getSocketsAndCheck(atomModelWrapper, atomURI);
    List<Socket> existingSockets = socketRepository.findByAtomURI(atomURI);
    Set<Socket> newSocketEntities = determineNewSockets(atomURI, existingSockets, atomModelWrapper);
    Set<Socket> removedSockets = determineRemovedSockets(atomURI, existingSockets, atomModelWrapper);
    Set<Socket> changedSockets = determineAndModifyChangedSockets(atomURI, existingSockets, atomModelWrapper);
    // close connections for changed or removed sockets
    removedSockets.stream().filter(socket -> connectionService.existOpenConnections(atomURI, socket.getSocketURI())).findFirst().ifPresent(socket -> new IllegalSocketModificationException("Cannot remove socket " + socket.getSocketURI() + ": socket has connections in state " + ConnectionState.CONNECTED));
    changedSockets.stream().filter(socket -> connectionService.existOpenConnections(atomURI, socket.getSocketURI())).findFirst().ifPresent(socket -> new IllegalSocketModificationException("Cannot change socket " + socket.getSocketURI() + ": socket has connections in state " + ConnectionState.CONNECTED));
    // add everything to the atom model class and save it
    socketRepository.saveAll(newSocketEntities);
    socketRepository.saveAll(changedSockets);
    socketRepository.deleteAll(removedSockets);
    socketAclService.addLocalSocketAcls(atomURI, newSocketEntities, newAtomContent);
    socketAclService.removeLocalSocketAcls(atomURI, removedSockets, newAtomContent);
    datasetHolder.setDataset(newAtomContent);
    atom.setDatatsetHolder(datasetHolder);
    atom.setAttachmentDatasetHolders(attachments);
    dataDerivationService.deriveDataIfNecessary(atom);
    return atomRepository.save(atom);
}
Also used : Dataset(org.apache.jena.query.Dataset) URI(java.net.URI) WonMessage(won.protocol.message.WonMessage) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 19 with AtomModelWrapper

use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.

the class ExecuteCreateAtomCommandAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof CreateAtomCommandEvent)) {
        return;
    }
    EventListenerContext ctx = getEventListenerContext();
    CreateAtomCommandEvent createAtomCommandEvent = (CreateAtomCommandEvent) event;
    Dataset atomDataset = createAtomCommandEvent.getAtomDataset();
    if (atomDataset == null) {
        logger.warn("CreateAtomCommandEvent did not contain an atom model, aborting atom creation");
        ctx.getEventBus().publish(new AtomCreationAbortedEvent(null, null, createAtomCommandEvent, "CreateAtomCommandEvent did not contain an atom model, aborting atom creation"));
        return;
    }
    URI atomUriFromProducer = null;
    Resource atomResource = WonRdfUtils.AtomUtils.getAtomResource(atomDataset);
    if (atomResource.isURIResource()) {
        atomUriFromProducer = URI.create(atomResource.getURI());
        RdfUtils.replaceBaseURI(atomDataset, atomResource.getURI(), true);
    } else {
        RdfUtils.replaceBaseResource(atomDataset, atomResource, true);
    }
    final URI atomUriBeforeCreation = atomUriFromProducer;
    AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
    final Dataset atomDatasetWithSockets = atomModelWrapper.copyDatasetWithoutSysinfo();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    logger.debug("creating atom on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(atomDatasetWithSockets), 150));
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    final URI atomURI = wonNodeInformationService.generateAtomURI(wonNodeUri);
    RdfUtils.renameResourceWithPrefix(atomDataset, atomResource.getURI(), atomURI.toString());
    WonMessage createAtomMessage = createWonMessage(atomURI, atomDatasetWithSockets);
    createAtomMessage = ctx.getWonMessageSender().prepareMessage(createAtomMessage);
    // remember the atom URI so we can react to success/failure responses
    ctx.getBotContextWrapper().rememberAtomUri(atomURI);
    EventListener successCallback = event12 -> {
        logger.debug("atom creation successful, new atom URI is {}", atomURI);
        ctx.getEventBus().publish(new CreateAtomCommandSuccessEvent(atomURI, atomUriBeforeCreation, createAtomCommandEvent));
    };
    EventListener failureCallback = event1 -> {
        String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event1).getFailureMessage());
        logger.debug("atom creation failed for atom URI {}, original message URI {}: {}", new Object[] { atomURI, ((FailureResponseEvent) event1).getOriginalMessageURI(), textMessage });
        ctx.getEventBus().publish(new CreateAtomCommandFailureEvent(atomURI, atomUriBeforeCreation, createAtomCommandEvent, textMessage));
        ctx.getBotContextWrapper().removeAtomUri(atomURI);
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createAtomMessage, successCallback, failureCallback, ctx);
    logger.debug("registered listeners for response to message URI {}", createAtomMessage.getMessageURI());
    ctx.getWonMessageSender().sendMessage(createAtomMessage);
    logger.debug("atom creation message sent with message URI {}", createAtomMessage.getMessageURI());
}
Also used : WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) WonMessage(won.protocol.message.WonMessage) WonMessageBuilder(won.protocol.message.builder.WonMessageBuilder) Resource(org.apache.jena.rdf.model.Resource) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonNodeInformationService(won.protocol.service.WonNodeInformationService) CreateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent) WONMATCH(won.protocol.vocabulary.WONMATCH) Logger(org.slf4j.Logger) AtomCreationAbortedEvent(won.bot.framework.eventbot.event.impl.command.create.AtomCreationAbortedEvent) MethodHandles(java.lang.invoke.MethodHandles) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) EventBotActionUtils(won.bot.framework.eventbot.action.EventBotActionUtils) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonRdfUtils(won.protocol.util.WonRdfUtils) AtomModelWrapper(won.protocol.util.AtomModelWrapper) Event(won.bot.framework.eventbot.event.Event) RdfUtils(won.protocol.util.RdfUtils) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) Dataset(org.apache.jena.query.Dataset) CreateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent) Resource(org.apache.jena.rdf.model.Resource) WonNodeInformationService(won.protocol.service.WonNodeInformationService) URI(java.net.URI) AtomCreationAbortedEvent(won.bot.framework.eventbot.event.impl.command.create.AtomCreationAbortedEvent) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) WonMessage(won.protocol.message.WonMessage) AtomModelWrapper(won.protocol.util.AtomModelWrapper) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)

Example 20 with AtomModelWrapper

use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.

the class ExecuteReplaceCommandAction method createWonMessage.

private WonMessage createWonMessage(URI atomURI, URI wonNodeURI, Dataset atomDataset) throws WonMessageBuilderException {
    RdfUtils.replaceBaseURI(atomDataset, atomURI.toString(), true);
    AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
    return WonMessageBuilder.replace().atom(atomURI).content().dataset(atomModelWrapper.copyDatasetWithoutSysinfo()).build();
}
Also used : AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Aggregations

AtomModelWrapper (won.protocol.util.AtomModelWrapper)37 Dataset (org.apache.jena.query.Dataset)19 Resource (org.apache.jena.rdf.model.Resource)15 URI (java.net.URI)9 Model (org.apache.jena.rdf.model.Model)8 WonMessage (won.protocol.message.WonMessage)6 RdfUtils (won.protocol.util.RdfUtils)6 IOException (java.io.IOException)5 MethodHandles (java.lang.invoke.MethodHandles)5 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 GoalInstantiationProducer (won.utils.goals.GoalInstantiationProducer)5 GoalInstantiationResult (won.utils.goals.GoalInstantiationResult)5 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)4 java.util (java.util)4 Collectors (java.util.stream.Collectors)4 StringUtils (org.apache.commons.lang3.StringUtils)4 ActorRef (akka.actor.ActorRef)3