Search in sources :

Example 16 with NeedModelWrapper

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

the class GoalInstantiationProducer method getNeedContentModelWithoutGoals.

private Model getNeedContentModelWithoutGoals(Dataset need) {
    NeedModelWrapper needWrapper = new NeedModelWrapper(need);
    Model need1Model = needWrapper.copyNeedModel(NeedGraphType.NEED);
    for (Resource goal : needWrapper.getGoals()) {
        RdfUtils.removeResource(need1Model, goal);
    }
    return need1Model;
}
Also used : Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) NeedModelWrapper(won.protocol.util.NeedModelWrapper)

Example 17 with NeedModelWrapper

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

the class GoalInstantiationProducer method createAllGoalInstantiationResults.

/**
 * create all possible goal instantiations between two needs.
 * Including the separate goals of each need as well.
 *
 * @return
 */
public Collection<GoalInstantiationResult> createAllGoalInstantiationResults() {
    NeedModelWrapper needWrapper1 = new NeedModelWrapper(need1);
    NeedModelWrapper needWrapper2 = new NeedModelWrapper(need2);
    Collection<GoalInstantiationResult> results = new LinkedList<>();
    boolean addedGoal2s = false;
    for (Resource goal1 : needWrapper1.getGoals()) {
        results.add(findInstantiationForGoal(goal1));
        for (Resource goal2 : needWrapper2.getGoals()) {
            GoalInstantiationResult instantiationResult = findInstantiationForGoals(goal1, goal2);
            results.add(instantiationResult);
            if (!addedGoal2s) {
                results.add(findInstantiationForGoal(goal2));
            }
        }
        addedGoal2s = true;
    }
    if (!addedGoal2s) {
        for (Resource goal2 : needWrapper2.getGoals()) {
            results.add(findInstantiationForGoal(goal2));
        }
    }
    return results;
}
Also used : Resource(org.apache.jena.rdf.model.Resource) NeedModelWrapper(won.protocol.util.NeedModelWrapper) LinkedList(java.util.LinkedList)

Example 18 with NeedModelWrapper

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

the class GoalInstantiationTest method exampleTaxiFakeLocation_validity.

@Test
public void exampleTaxiFakeLocation_validity() throws IOException {
    Dataset taxiOffer = loadDataset(baseFolder + "ex7_taxioffer.trig");
    Dataset taxiDemand = loadDataset(baseFolder + "ex7_taxi.trig");
    GoalInstantiationProducer goalInstantiation = new GoalInstantiationProducer(taxiOffer, taxiDemand, null, "http://example.org/", "http://example.org/blended/");
    Collection<GoalInstantiationResult> results = goalInstantiation.createGoalInstantiationResultsForNeed1();
    for (GoalInstantiationResult res : results) {
        res.getInstanceModel().write(System.out, "TRIG");
        Assert.assertTrue(res.isConform());
        Coordinate departureAddress = getAddress(loadSparqlQuery("/won/utils/goals/extraction/address/northWestCornerQuery.rq"), res.getInstanceModel());
        Coordinate destinationAddress = getAddress(loadSparqlQuery("/won/utils/goals/extraction/address/southEastCornerQuery.rq"), res.getInstanceModel());
        Assert.assertEquals(departureAddress, new Coordinate(48.218727f, 16.360141f));
        Assert.assertEquals(destinationAddress, new Coordinate(48.218828f, 16.360241f));
    }
    NeedModelWrapper needWrapper1 = new NeedModelWrapper(taxiOffer);
    Resource goal = needWrapper1.getGoals().iterator().next();
    GoalInstantiationResult result = goalInstantiation.findInstantiationForGoal(goal);
    Assert.assertTrue(result.isConform());
    GoalInstantiationResult recheckResultModel = GoalInstantiationProducer.findInstantiationForGoalInDataset(taxiOffer, goal, result.getInstanceModel());
    Assert.assertTrue(recheckResultModel.isConform());
}
Also used : GoalInstantiationResult(won.utils.goals.GoalInstantiationResult) Coordinate(won.protocol.model.Coordinate) Dataset(org.apache.jena.query.Dataset) GoalInstantiationProducer(won.utils.goals.GoalInstantiationProducer) Resource(org.apache.jena.rdf.model.Resource) NeedModelWrapper(won.protocol.util.NeedModelWrapper) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) Test(org.junit.Test)

Example 19 with NeedModelWrapper

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

the class CreateNeedMessageProcessor method storeNeed.

private Need storeNeed(final WonMessage wonMessage) {
    Dataset needContent = wonMessage.getMessageContent();
    List<WonMessage.AttachmentHolder> attachmentHolders = wonMessage.getAttachments();
    // remove attachment and its signature from the needContent
    removeAttachmentsFromNeedContent(needContent, attachmentHolders);
    URI needURI = getNeedURIFromWonMessage(needContent);
    if (!needURI.equals(wonMessage.getSenderNeedURI()))
        throw new IllegalArgumentException("receiverNeedURI and NeedURI of the content are not equal");
    Need need = new Need();
    need.setState(NeedState.ACTIVE);
    need.setNeedURI(needURI);
    // ToDo (FS) check if the WON node URI corresponds with the WON node (maybe earlier in the message layer)
    NeedEventContainer needEventContainer = needEventContainerRepository.findOneByParentUri(needURI);
    if (needEventContainer == null) {
        needEventContainer = new NeedEventContainer(need, need.getNeedURI());
    } else {
        throw new UriAlreadyInUseException("Found a NeedEventContainer for the need we're about to create (" + needURI + ") - aborting");
    }
    need.setWonNodeURI(wonMessage.getReceiverNodeURI());
    ConnectionContainer connectionContainer = new ConnectionContainer(need);
    need.setConnectionContainer(connectionContainer);
    need.setEventContainer(needEventContainer);
    // store the need content
    DatasetHolder datasetHolder = new DatasetHolder(needURI, needContent);
    // store attachments
    List<DatasetHolder> attachments = new ArrayList<>(attachmentHolders.size());
    for (WonMessage.AttachmentHolder attachmentHolder : attachmentHolders) {
        datasetHolder = new DatasetHolder(attachmentHolder.getDestinationUri(), attachmentHolder.getAttachmentDataset());
        attachments.add(datasetHolder);
    }
    // add everything to the need model class and save it
    need.setDatatsetHolder(datasetHolder);
    need.setAttachmentDatasetHolders(attachments);
    need = needRepository.save(need);
    connectionContainerRepository.save(connectionContainer);
    NeedModelWrapper needModelWrapper = new NeedModelWrapper(needContent);
    Collection<String> facets = needModelWrapper.getFacetUris();
    if (facets.size() == 0)
        throw new IllegalArgumentException("at least one property won:hasFacet required ");
    for (String facetUri : facets) {
        // TODO: check if there is a implementation for the facet on the node
        Facet f = new Facet();
        f.setNeedURI(needURI);
        f.setTypeURI(URI.create(facetUri));
        facetRepository.save(f);
    }
    return need;
}
Also used : Dataset(org.apache.jena.query.Dataset) ArrayList(java.util.ArrayList) NeedModelWrapper(won.protocol.util.NeedModelWrapper) URI(java.net.URI) UriAlreadyInUseException(won.protocol.message.processor.exception.UriAlreadyInUseException) WonMessage(won.protocol.message.WonMessage)

Example 20 with NeedModelWrapper

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

the class AbstractCreateNeedAction method createWonMessage.

protected WonMessage createWonMessage(WonNodeInformationService wonNodeInformationService, URI needURI, URI wonNodeURI, Dataset needDataset, final boolean usedForTesting, final boolean doNotMatch) throws WonMessageBuilderException {
    NeedModelWrapper needModelWrapper = new NeedModelWrapper(needDataset);
    if (doNotMatch) {
        needModelWrapper.addFlag(WON.NO_HINT_FOR_ME);
        needModelWrapper.addFlag(WON.NO_HINT_FOR_COUNTERPART);
    }
    if (usedForTesting) {
        needModelWrapper.addFlag(WON.USED_FOR_TESTING);
    }
    RdfUtils.replaceBaseURI(needDataset, needURI.toString());
    return WonMessageBuilder.setMessagePropertiesForCreate(wonNodeInformationService.generateEventURI(wonNodeURI), needURI, wonNodeURI).addContent(needModelWrapper.copyDataset()).build();
}
Also used : 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