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