Search in sources :

Example 1 with ShaclReportWrapper

use of won.utils.shacl.ShaclReportWrapper in project webofneeds by researchstudio-sat.

the class GoalInstantiationProducer method findInstantiationForGoals.

/**
 * Create a goal instantiation result from the attempt to instantiate two goals of two needs using all
 * the need data, the conversation data and the data and shapes of the two goals.
 * If a model can be found that conforms to the shacl shapes of both needs this model is chosen to be returned
 * in the GoalInstantiationResult. If no conforming model can be found, the model with the least
 * shacl validation results (validation errors) is used.
 *
 * @param goal1 resource referencing goal from need1
 * @param goal2 resource referencing goal from need2
 * @return a goal instantiation result whose input model can either conform to its shacl shapes or not
 */
private GoalInstantiationResult findInstantiationForGoals(Resource goal1, Resource goal2) {
    NeedModelWrapper needWrapper1 = new NeedModelWrapper(need1);
    Model shapesModel1 = needWrapper1.getShapesGraph(goal1);
    Model dataModel1 = needWrapper1.getDataGraph(goal1);
    NeedModelWrapper needWrapper2 = new NeedModelWrapper(need2);
    Model shapesModel2 = needWrapper2.getShapesGraph(goal2);
    Model dataModel2 = needWrapper2.getDataGraph(goal2);
    if (shapesModel1 == null || shapesModel2 == null) {
        throw new IllegalArgumentException("shapes model for goal not found");
    }
    // create the combined model with need content, conversation data and the data of the two goals
    Model combinedModelWithGoalData = ModelFactory.createDefaultModel();
    combinedModelWithGoalData.add(combinedModelWithoutGoals);
    if (dataModel1 != null) {
        combinedModelWithGoalData.add(dataModel1);
    }
    if (dataModel2 != null) {
        combinedModelWithGoalData.add(dataModel2);
    }
    // validate the two goal shapes against the combined data model
    // and extract specific data for each goal using the shacl results
    Model extractedModel1 = GoalUtils.extractGoalData(combinedModelWithGoalData, shapesModel1);
    Model extractedModel2 = GoalUtils.extractGoalData(combinedModelWithGoalData, shapesModel2);
    Model combinedShapesModel = ModelFactory.createDefaultModel();
    combinedShapesModel.add(shapesModel1);
    combinedShapesModel.add(shapesModel2);
    int minValidationResults = Integer.MAX_VALUE;
    GoalInstantiationResult bestGoalInstantiationResult = null;
    // blend the two extracted graphs
    GraphBlendingIterator blendingIterator = new GraphBlendingIterator(extractedModel1, extractedModel2, variableUriPrefix, blendingUriPrefix);
    while (blendingIterator.hasNext()) {
        Model blendedModel = blendingIterator.next();
        // check if the blended model conforms to the combined shacl shapes of both needs
        Resource report = ValidationUtil.validateModel(blendedModel, combinedShapesModel, false);
        ShaclReportWrapper shaclReportWrapper = new ShaclReportWrapper(report);
        if (shaclReportWrapper.isConform()) {
            // if we found a blended model that is conform to the shacl shapes lets try to condense it
            // as far as possible to get the minimum model that is still conform to the shapes
            Function<Model, Boolean> modelTestingFunction = param -> GoalUtils.validateModelShaclConformity(param, combinedShapesModel);
            Model condensedModel = RdfUtils.condenseModelByIterativeTesting(blendedModel, modelTestingFunction);
            bestGoalInstantiationResult = new GoalInstantiationResult(condensedModel, combinedShapesModel);
        } else {
            // if the model is not conform save it if it has the least validation results found so far
            if (shaclReportWrapper.getValidationResults().size() < minValidationResults) {
                minValidationResults = shaclReportWrapper.getValidationResults().size();
                bestGoalInstantiationResult = new GoalInstantiationResult(blendedModel, combinedShapesModel);
            }
        }
    }
    return bestGoalInstantiationResult;
}
Also used : Collection(java.util.Collection) ShaclReportWrapper(won.utils.shacl.ShaclReportWrapper) Function(java.util.function.Function) NeedModelWrapper(won.protocol.util.NeedModelWrapper) Model(org.apache.jena.rdf.model.Model) RdfUtils(won.protocol.util.RdfUtils) DatasetFactory(org.apache.jena.query.DatasetFactory) Resource(org.apache.jena.rdf.model.Resource) ValidationUtil(org.topbraid.shacl.validation.ValidationUtil) ModelFactory(org.apache.jena.rdf.model.ModelFactory) LinkedList(java.util.LinkedList) Dataset(org.apache.jena.query.Dataset) NeedGraphType(won.protocol.model.NeedGraphType) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) NeedModelWrapper(won.protocol.util.NeedModelWrapper) ShaclReportWrapper(won.utils.shacl.ShaclReportWrapper)

Example 2 with ShaclReportWrapper

use of won.utils.shacl.ShaclReportWrapper in project webofneeds by researchstudio-sat.

the class GoalUtils method validateModelShaclConformity.

public static Boolean validateModelShaclConformity(Model dataModel, Model shaclShapesModel) {
    Resource report = ValidationUtil.validateModel(dataModel, shaclShapesModel, false);
    ShaclReportWrapper shaclReportWrapper = new ShaclReportWrapper(report);
    return shaclReportWrapper.isConform();
}
Also used : Resource(org.apache.jena.rdf.model.Resource) ShaclReportWrapper(won.utils.shacl.ShaclReportWrapper)

Aggregations

Resource (org.apache.jena.rdf.model.Resource)2 ShaclReportWrapper (won.utils.shacl.ShaclReportWrapper)2 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Function (java.util.function.Function)1 Dataset (org.apache.jena.query.Dataset)1 DatasetFactory (org.apache.jena.query.DatasetFactory)1 Model (org.apache.jena.rdf.model.Model)1 ModelFactory (org.apache.jena.rdf.model.ModelFactory)1 ValidationUtil (org.topbraid.shacl.validation.ValidationUtil)1 NeedGraphType (won.protocol.model.NeedGraphType)1 NeedModelWrapper (won.protocol.util.NeedModelWrapper)1 RdfUtils (won.protocol.util.RdfUtils)1