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;
}
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;
}
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);
}
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());
}
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();
}
Aggregations