Search in sources :

Example 11 with AtomModelWrapper

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

the class GoalInstantiationTest method example2_allInfoInTwoGoalsAndMessage.

@Test
public void example2_allInfoInTwoGoalsAndMessage() throws IOException {
    Dataset atom1 = loadDataset(baseFolder + "ex2_atom.trig");
    Dataset atom2 = loadDataset(baseFolder + "ex2_atom_debug.trig");
    Dataset conversationWithoutPickupTime = loadDataset(baseFolder + "ex1_conversation.trig");
    Dataset conversationWithPickupTime = loadDataset(baseFolder + "ex2_conversation.trig");
    // this conversation doas not contain the missing pickup time info so the goals
    // cannot be fulfilled
    GoalInstantiationProducer goalInstantiation = new GoalInstantiationProducer(atom1, atom2, conversationWithoutPickupTime, "http://example.org/", "http://example.org/blended/");
    Collection<GoalInstantiationResult> results = goalInstantiation.createAllGoalCombinationInstantiationResults();
    Assert.assertEquals(1, results.size());
    Assert.assertFalse(results.iterator().next().isConform());
    // this conversation contains the missing pickup info so the goals can be
    // fulfilled
    goalInstantiation = new GoalInstantiationProducer(atom1, atom2, conversationWithPickupTime, "http://example.org/", "http://example.org/blended/");
    results = goalInstantiation.createAllGoalCombinationInstantiationResults();
    Assert.assertEquals(1, results.size());
    Assert.assertTrue(results.iterator().next().isConform());
    // instantiation of goal of atom1 fails cause driver is missing
    AtomModelWrapper atomWrapper1 = new AtomModelWrapper(atom1);
    Resource goal = atomWrapper1.getGoals().iterator().next();
    GoalInstantiationResult result = goalInstantiation.findInstantiationForGoal(goal);
    Assert.assertFalse(result.isConform());
    Assert.assertEquals("hasDriver", result.getShaclReportWrapper().getValidationResults().iterator().next().getResultPath().getLocalName());
    // instantiation of goal of atom2 fails cause 3 attributes are missing:
    // location, time, client
    AtomModelWrapper atomWrapper2 = new AtomModelWrapper(atom2);
    goal = atomWrapper2.getGoals().iterator().next();
    result = goalInstantiation.findInstantiationForGoal(goal);
    Assert.assertFalse(result.isConform());
    Assert.assertEquals(3, result.getShaclReportWrapper().getValidationResults().size());
}
Also used : GoalInstantiationResult(won.utils.goals.GoalInstantiationResult) Dataset(org.apache.jena.query.Dataset) GoalInstantiationProducer(won.utils.goals.GoalInstantiationProducer) Resource(org.apache.jena.rdf.model.Resource) AtomModelWrapper(won.protocol.util.AtomModelWrapper) Test(org.junit.Test)

Example 12 with AtomModelWrapper

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

the class GoalInstantiationProducer method getAtomContentModelWithoutGoals.

private Model getAtomContentModelWithoutGoals(Dataset atom) {
    AtomModelWrapper atomWrapper = new AtomModelWrapper(atom);
    Model atom1Model = atomWrapper.copyAtomModel(AtomGraphType.ATOM);
    for (Resource goal : atomWrapper.getGoals()) {
        RdfUtils.removeResource(atom1Model, goal);
    }
    return atom1Model;
}
Also used : Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 13 with AtomModelWrapper

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

the class GoalInstantiationProducer method findInstantiationForGoalInDataset.

/**
 * Create a goal instantiation result from the attempt to instantiate one goal
 * with data given in the dataset The data is extracted and validated against
 * the shacl shape of the goal.
 *
 * @param atom Dataset of the atom to retrieve the goalShapesModel from
 * @param goal resource referencing goal from atom1 or atom2
 * @param model Model that should be checked for goal validity
 * @return a goal instantiation result whose input model can either conform to
 * its shacl shapes or not
 */
public static GoalInstantiationResult findInstantiationForGoalInDataset(Dataset atom, Resource goal, Model model) {
    AtomModelWrapper atomWrapper = new AtomModelWrapper(atom);
    Model goalShapesModel;
    if (atomWrapper.getGoals().contains(goal)) {
        goalShapesModel = atomWrapper.getShapesGraph(goal);
    } else {
        throw new IllegalArgumentException("problem to identify goal resource in the atom model");
    }
    if (goalShapesModel == null) {
        throw new IllegalArgumentException("shapes model for goal not found");
    }
    Model extractedModel = GoalUtils.extractGoalData(model, goalShapesModel);
    return new GoalInstantiationResult(extractedModel, goalShapesModel);
}
Also used : Model(org.apache.jena.rdf.model.Model) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 14 with AtomModelWrapper

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

the class GoalInstantiationProducer method findInstantiationForGoal.

/**
 * Create a goal instantiation result from the attempt to instantiate one goal
 * with data of two atoms using all the atom data, the conversation data and the
 * shapes data of the goal. The data is extracted and validated against the
 * shacl shape of the goal.
 *
 * @param goal resource referencing goal from atom1 or atom2
 * @return a goal instantiation result whose input model can either conform to
 * its shacl shapes or not
 */
public GoalInstantiationResult findInstantiationForGoal(Resource goal) {
    AtomModelWrapper atomWrapper1 = new AtomModelWrapper(atom1);
    AtomModelWrapper atomWrapper2 = new AtomModelWrapper(atom2);
    Model goalShapesModel = null;
    Model goalDataModel = null;
    if (atomWrapper1.getGoals().contains(goal) && !atomWrapper2.getGoals().contains(goal)) {
        goalShapesModel = atomWrapper1.getShapesGraph(goal);
        goalDataModel = atomWrapper1.getDataGraph(goal);
    } else if (atomWrapper2.getGoals().contains(goal) && !atomWrapper1.getGoals().contains(goal)) {
        goalShapesModel = atomWrapper2.getShapesGraph(goal);
        goalDataModel = atomWrapper2.getDataGraph(goal);
    } else {
        throw new IllegalArgumentException("problem to identify goal resource in one of the two atom models");
    }
    if (goalShapesModel == null) {
        throw new IllegalArgumentException("shapes model for goal not found");
    }
    Model combinedModelWithGoalData = ModelFactory.createDefaultModel();
    combinedModelWithGoalData.add(combinedModelWithoutGoals);
    if (goalDataModel != null) {
        combinedModelWithGoalData.add(goalDataModel);
    }
    Model extractedModel = GoalUtils.extractGoalData(combinedModelWithGoalData, goalShapesModel);
    return new GoalInstantiationResult(extractedModel, goalShapesModel);
}
Also used : Model(org.apache.jena.rdf.model.Model) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 15 with AtomModelWrapper

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

the class GoalInstantiationProducer method createAllGoalCombinationInstantiationResults.

/**
 * create all possible goal instantiations between two atoms. That means trying
 * to combine each two goals of the two atoms.
 *
 * @return
 */
public Collection<GoalInstantiationResult> createAllGoalCombinationInstantiationResults() {
    AtomModelWrapper atomWrapper1 = new AtomModelWrapper(atom1);
    AtomModelWrapper atomWrapper2 = new AtomModelWrapper(atom2);
    Collection<GoalInstantiationResult> results = new LinkedList<>();
    for (Resource goal1 : atomWrapper1.getGoals()) {
        for (Resource goal2 : atomWrapper2.getGoals()) {
            GoalInstantiationResult instantiationResult = findInstantiationForGoals(goal1, goal2);
            results.add(instantiationResult);
        }
    }
    return results;
}
Also used : Resource(org.apache.jena.rdf.model.Resource) AtomModelWrapper(won.protocol.util.AtomModelWrapper) LinkedList(java.util.LinkedList)

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