Search in sources :

Example 21 with NeedModelWrapper

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

the class CreateNeedWithFacetsAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (ctx.getNeedProducer().isExhausted()) {
        logger.info("the bot's need producer is exhausted.");
        ctx.getEventBus().publish(new NeedProducerExhaustedEvent());
        return;
    }
    final Dataset needDataset = ctx.getNeedProducer().create();
    if (needDataset == null) {
        logger.warn("needproducer failed to produce a need model, aborting need creation");
        return;
    }
    URI needUriFromProducer = null;
    Resource needResource = WonRdfUtils.NeedUtils.getNeedResource(needDataset);
    if (needResource.isURIResource()) {
        needUriFromProducer = URI.create(needResource.getURI().toString());
        RdfUtils.replaceBaseURI(needDataset, needResource.getURI());
    } else {
        RdfUtils.replaceBaseResource(needDataset, needResource);
    }
    final URI needUriBeforeCreation = needUriFromProducer;
    NeedModelWrapper needModelWrapper = new NeedModelWrapper(needDataset);
    for (URI facetURI : facets) {
        WonRdfUtils.FacetUtils.addFacet(needModelWrapper.getNeedModel(), facetURI);
    }
    final Dataset needDatasetWithFacets = needModelWrapper.copyDataset();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(needDatasetWithFacets), 150));
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, needDatasetWithFacets);
    // remember the need URI so we can react to success/failure responses
    EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
    EventListener successCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            logger.debug("need creation successful, new need URI is {}", needURI);
            ctx.getEventBus().publish(new NeedCreatedEvent(needURI, wonNodeUri, needDatasetWithFacets, null, needUriBeforeCreation));
        }
    };
    EventListener failureCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
            logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
            EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
            ctx.getEventBus().publish(new NeedCreationFailedEvent(wonNodeUri, needUriBeforeCreation));
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
    logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
    ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
    logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) NeedProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedProducerExhaustedEvent) Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedModelWrapper(won.protocol.util.NeedModelWrapper) URI(java.net.URI) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) WonMessage(won.protocol.message.WonMessage) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) NeedProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedProducerExhaustedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)

Example 22 with NeedModelWrapper

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

the class TemplateBasedNeedProducer method wrapModel.

private Dataset wrapModel(final Dataset wrappedDataset) {
    if (this.templateModel != null) {
        // TODO: TEMPLATE BASED PRODUCER IS WEIRD NOW
        NeedModelWrapper needModelWrapper = new NeedModelWrapper(wrappedDataset);
        Model needModel = needModelWrapper.copyNeedModel(NeedGraphType.NEED);
        Model wrappedModel = RdfUtils.mergeModelsCombiningBaseResource(needModel, this.templateModel);
    }
    return wrappedDataset;
}
Also used : Model(org.apache.jena.rdf.model.Model) NeedModelWrapper(won.protocol.util.NeedModelWrapper)

Example 23 with NeedModelWrapper

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

the class TrigFileNeedProducer method readNeedFromFile.

@Override
public synchronized Dataset readNeedFromFile(final File file) throws IOException {
    logger.debug("processing as turtle file: {} ", file);
    try (FileInputStream fis = new FileInputStream(file)) {
        Dataset dataset = DatasetFactory.createGeneral();
        RDFDataMgr.read(dataset, fis, RDFFormat.TRIG.getLang());
        NeedModelWrapper needModelWrapper = new NeedModelWrapper(dataset);
        return needModelWrapper.copyDataset();
    } catch (Exception e) {
        logger.error("could not parse trig from file {} ", file, e);
        throw e;
    }
}
Also used : Dataset(org.apache.jena.query.Dataset) NeedModelWrapper(won.protocol.util.NeedModelWrapper) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 24 with NeedModelWrapper

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

the class ExecuteCreateNeedCommandAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof CreateNeedCommandEvent))
        return;
    CreateNeedCommandEvent createNeedCommandEvent = (CreateNeedCommandEvent) event;
    Dataset needDataset = createNeedCommandEvent.getNeedDataset();
    List<URI> facets = createNeedCommandEvent.getFacets();
    if (needDataset == null) {
        logger.warn("CreateNeedCommandEvent did not contain a need model, aborting need creation");
        getEventListenerContext().getEventBus().publish(new NeedCreationAbortedEvent(null, null, createNeedCommandEvent, "CreateNeedCommandEvent did not contain a need model, aborting need creation"));
        return;
    }
    URI needUriFromProducer = null;
    Resource needResource = WonRdfUtils.NeedUtils.getNeedResource(needDataset);
    if (needResource.isURIResource()) {
        needUriFromProducer = URI.create(needResource.getURI().toString());
        RdfUtils.replaceBaseURI(needDataset, needResource.getURI());
    } else {
        RdfUtils.replaceBaseResource(needDataset, needResource);
    }
    final URI needUriBeforeCreation = needUriFromProducer;
    NeedModelWrapper needModelWrapper = new NeedModelWrapper(needDataset);
    for (URI facetURI : facets) {
        WonRdfUtils.FacetUtils.addFacet(needModelWrapper.getNeedModel(), facetURI);
    }
    final Dataset needDatasetWithFacets = needModelWrapper.copyDataset();
    final URI wonNodeUri = getEventListenerContext().getNodeURISource().getNodeURI();
    logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(needDatasetWithFacets), 150));
    WonNodeInformationService wonNodeInformationService = getEventListenerContext().getWonNodeInformationService();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, needDatasetWithFacets, createNeedCommandEvent.isUsedForTesting(), createNeedCommandEvent.isDoNotMatch());
    // remember the need URI so we can react to success/failure responses
    EventBotActionUtils.rememberInList(getEventListenerContext(), needURI, createNeedCommandEvent.getUriListName());
    EventListener successCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            logger.debug("need creation successful, new need URI is {}", needURI);
            getEventListenerContext().getEventBus().publish(new CreateNeedCommandSuccessEvent(needURI, needUriBeforeCreation, createNeedCommandEvent));
        }
    };
    EventListener failureCallback = new EventListener() {

        @Override
        public void onEvent(Event event) throws Exception {
            String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
            logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
            getEventListenerContext().getEventBus().publish(new CreateNeedCommandFailureEvent(needURI, needUriBeforeCreation, createNeedCommandEvent, textMessage));
            EventBotActionUtils.removeFromList(getEventListenerContext(), needURI, createNeedCommandEvent.getUriListName());
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, getEventListenerContext());
    logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
    getEventListenerContext().getWonMessageSender().sendWonMessage(createNeedMessage);
    logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
Also used : NeedCreationAbortedEvent(won.bot.framework.eventbot.event.impl.command.create.NeedCreationAbortedEvent) Dataset(org.apache.jena.query.Dataset) CreateNeedCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandFailureEvent) Resource(org.apache.jena.rdf.model.Resource) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedModelWrapper(won.protocol.util.NeedModelWrapper) URI(java.net.URI) CreateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandEvent) WonMessage(won.protocol.message.WonMessage) CreateNeedCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandSuccessEvent) CreateNeedCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandFailureEvent) CreateNeedCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandSuccessEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) CreateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandEvent) NeedCreationAbortedEvent(won.bot.framework.eventbot.event.impl.command.create.NeedCreationAbortedEvent) EventListener(won.bot.framework.eventbot.listener.EventListener)

Example 25 with NeedModelWrapper

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

the class AnalyzeBehaviour method getUniqueGoalId.

private static String getUniqueGoalId(Resource goal, Dataset needDataset, URI connectionURI) {
    // TODO: GOAL STATE RETRIEVAL IS NOT BASED ON THE CORRECT URI SO FAR
    if (goal.getURI() != null) {
        return goal.getURI();
    } else {
        NeedModelWrapper needWrapper = new NeedModelWrapper(needDataset);
        StringWriter writer = new StringWriter();
        Model shapesModel = needWrapper.getShapesGraph(goal);
        if (shapesModel != null) {
            shapesModel.write(writer, "TRIG");
        }
        Model dataModel = needWrapper.getDataGraph(goal);
        if (dataModel != null) {
            dataModel.write(writer, "TRIG");
        }
        return connectionURI + "#" + writer.toString().replaceAll("\\R", " ");
    }
}
Also used : StringWriter(java.io.StringWriter) Model(org.apache.jena.rdf.model.Model) NeedModelWrapper(won.protocol.util.NeedModelWrapper)

Aggregations

NeedModelWrapper (won.protocol.util.NeedModelWrapper)27 Dataset (org.apache.jena.query.Dataset)14 Resource (org.apache.jena.rdf.model.Resource)12 Model (org.apache.jena.rdf.model.Model)9 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)6 URI (java.net.URI)5 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 GoalInstantiationProducer (won.utils.goals.GoalInstantiationProducer)4 GoalInstantiationResult (won.utils.goals.GoalInstantiationResult)4 WonMessage (won.protocol.message.WonMessage)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 Event (won.bot.framework.eventbot.event.Event)2 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)2 EventListener (won.bot.framework.eventbot.listener.EventListener)2 Coordinate (won.protocol.model.Coordinate)2 WonNodeInformationService (won.protocol.service.WonNodeInformationService)2 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)1 StringWriter (java.io.StringWriter)1