Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inferencing test case #176

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*/
package org.topbraid.shacl.testcases;

import org.apache.jena.graph.Triple;
import org.apache.jena.query.Dataset;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.vocabulary.RDF;
import org.topbraid.jenax.functions.CurrentThreadFunctionRegistry;
import org.topbraid.jenax.util.ARQFactory;
import org.topbraid.jenax.util.JenaUtil;
import org.topbraid.shacl.engine.ShapesGraph;
import org.topbraid.shacl.engine.ShapesGraphFactory;
import org.topbraid.shacl.rules.RuleEngine;
import org.topbraid.shacl.testcases.context.TestCaseContextFactory;
import org.topbraid.shacl.util.ModelPrinter;
import org.topbraid.shacl.util.SHACLUtil;
import org.topbraid.shacl.vocabulary.DASH;

import java.net.URI;
import java.util.LinkedList;
import java.util.List;

/**
* @author Ashley Caselli
*/
public class InferencingTestCaseType extends TestCaseType {

private static final List<TestCaseContextFactory> contextFactories = new LinkedList<>();


public static void registerContextFactory(TestCaseContextFactory factory) {
contextFactories.add(factory);
}


public InferencingTestCaseType() {
super(DASH.InferencingTestCase);
}


@Override
protected TestCase createTestCase(Resource graph, Resource resource) {
return new InferencingTestCase(graph, resource);
}


private static class InferencingTestCase extends TestCase {

InferencingTestCase(Resource graph, Resource resource) {
super(graph, resource);
}


@Override
public void run(Model results) {
Resource testCase = getResource();

Runnable tearDownCTFR = CurrentThreadFunctionRegistry.register(testCase.getModel());

Model dataModel = getResource().getModel();

Dataset dataset = ARQFactory.get().getDataset(dataModel);
URI shapesGraphURI = SHACLUtil.withShapesGraph(dataset);
ShapesGraph shapesGraph = ShapesGraphFactory.get().createShapesGraph(dataset.getNamedModel(shapesGraphURI.toString()));

RuleEngine ruleEngine = new RuleEngine(dataset, shapesGraphURI, shapesGraph, dataModel);
try {
ruleEngine.executeAll();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
tearDownCTFR.run();
}

Model actualResults = ruleEngine.getInferencesModel();
Model expectedModel = JenaUtil.createDefaultModel();

List<Statement> expectedResults = getResource().listProperties(DASH.expectedResult).toList();
boolean valid = false;
for (Statement s : expectedResults) {
valid = false;
expectedModel.add(s);
Resource expectedInferredNode = s.getObject().asResource();
Triple expectedInferredNodeAsTriple = Triple.create(expectedInferredNode.getProperty(RDF.subject).getObject().asNode(),
expectedInferredNode.getProperty(RDF.predicate).getObject().asNode(),
expectedInferredNode.getProperty(RDF.object).getObject().asNode());
if (actualResults.getGraph().contains(expectedInferredNodeAsTriple)) {
valid = true;
}
}

if (valid) {
createResult(results, DASH.SuccessTestCaseResult);
} else {
System.out.println("Expected: " + ModelPrinter.get().print(expectedModel) + "\nActual: " + ModelPrinter.get().print(actualResults));
Resource failure = createFailure(results,
"Mismatching inference results. Expected " + expectedModel.size() + " triples, found " + actualResults.size());
failure.addProperty(DASH.expectedResult, ModelPrinter.get().print(expectedModel));
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TestCaseTypes {
static {
types.add(new FunctionTestCaseType());
types.add(new GraphValidationTestCaseType());
//types.add(new InferencingTestCaseType());
types.add(new InferencingTestCaseType());
types.add(new JSTestCaseType());
types.add(new QueryTestCaseType());
}
Expand Down
65 changes: 38 additions & 27 deletions src/test/resources/sh/tests/rules/triple/person2schema.test.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
@prefix dash: <http://datashapes.org/dash#> .
@prefix ex: <http://datashapes.org/shasf/tests/rules/triple/person2schema.test#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix person: <http://datashapes.org/shasf/tests/rules/triple/person#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <http://schema.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

Expand All @@ -33,48 +35,55 @@
ex:InferencingTestCase
rdf:type dash:InferencingTestCase ;
dash:expectedResult [
rdf:object <http://datashapes.org/shasf/tests/rules/triple/person#JohnDoe-Address> ;
rdf:predicate <http://schema.org/address> ;
rdf:subject <http://datashapes.org/shasf/tests/rules/triple/person#JohnDoe> ;
rdf:object person:JohnDoe-Address ;
rdf:predicate schema:address ;
rdf:subject person:JohnDoe ;
] ;
dash:expectedResult [
rdf:object <http://schema.org/Person> ;
rdf:object schema:Person ;
rdf:predicate rdf:type ;
rdf:subject <http://datashapes.org/shasf/tests/rules/triple/person#JohnDoe> ;
rdf:subject person:JohnDoe ;
] ;
dash:expectedResult [
rdf:object <http://schema.org/PostalAddress> ;
rdf:object schema:PostalAddress ;
rdf:predicate rdf:type ;
rdf:subject <http://datashapes.org/shasf/tests/rules/triple/person#JohnDoe-Address> ;
rdf:subject person:JohnDoe-Address ;
] ;
dash:expectedResult [
rdf:object 12345 ;
rdf:predicate <http://schema.org/postalCode> ;
rdf:subject <http://datashapes.org/shasf/tests/rules/triple/person#JohnDoe-Address> ;
rdf:predicate schema:postalCode ;
rdf:subject person:JohnDoe-Address ;
] ;
dash:expectedResult [
rdf:object "Doe" ;
rdf:predicate <http://schema.org/familyName> ;
rdf:subject <http://datashapes.org/shasf/tests/rules/triple/person#JohnDoe> ;
rdf:predicate schema:familyName ;
rdf:subject person:JohnDoe ;
] ;
dash:expectedResult [
rdf:object "John" ;
rdf:predicate <http://schema.org/givenName> ;
rdf:subject <http://datashapes.org/shasf/tests/rules/triple/person#JohnDoe> ;
rdf:predicate schema:givenName ;
rdf:subject person:JohnDoe ;
] ;
.
person:JohnDoe
rdf:type person:Person ;
person:lastName "Doe" ;
person:firstName "John" ;
person:zipCode 12345 ;
rdfs:label "John Doe" ;
.
ex:Person2SchemaMappingShape
rdf:type sh:NodeShape ;
rdfs:label "Person-to-Schema Mapping Shape" ;
sh:rule [
rdf:type sh:TripleRule ;
sh:object <http://schema.org/Person> ;
sh:object schema:Person ;
sh:predicate rdf:type ;
sh:subject sh:this ;
] ;
sh:rule [
rdf:type sh:TripleRule ;
sh:object <http://schema.org/PostalAddress> ;
sh:object schema:PostalAddress ;
sh:predicate rdf:type ;
sh:subject [
ex:deriveURI (
Expand All @@ -86,25 +95,25 @@ ex:Person2SchemaMappingShape
sh:rule [
rdf:type sh:TripleRule ;
sh:object [
sh:path <http://datashapes.org/shasf/tests/rules/triple/person#firstName> ;
sh:path person:firstName ;
] ;
sh:predicate <http://schema.org/givenName> ;
sh:predicate schema:givenName ;
sh:subject sh:this ;
] ;
sh:rule [
rdf:type sh:TripleRule ;
sh:object [
sh:path <http://datashapes.org/shasf/tests/rules/triple/person#lastName> ;
sh:path person:lastName ;
] ;
sh:predicate <http://schema.org/familyName> ;
sh:predicate schema:familyName ;
sh:subject sh:this ;
] ;
sh:rule [
rdf:type sh:TripleRule ;
sh:object [
sh:path <http://datashapes.org/shasf/tests/rules/triple/person#zipCode> ;
sh:path person:zipCode ;
] ;
sh:predicate <http://schema.org/postalCode> ;
sh:predicate schema:postalCode ;
sh:subject [
ex:deriveURI (
sh:this
Expand All @@ -120,10 +129,10 @@ ex:Person2SchemaMappingShape
"-Address"
) ;
] ;
sh:predicate <http://schema.org/address> ;
sh:predicate schema:address ;
sh:subject sh:this ;
] ;
sh:targetClass <http://datashapes.org/shasf/tests/rules/triple/person#Person> ;
sh:targetClass person:Person ;
.
ex:deriveURI
rdf:type sh:SPARQLFunction ;
Expand All @@ -142,8 +151,10 @@ ex:deriveURI
] ;
sh:prefixes <http://datashapes.org/shasf/tests/rules/triple/person2schema.test> ;
sh:returnType rdfs:Resource ;
sh:select """SELECT ?result
WHERE {
BIND (IRI(CONCAT(str($base), $concat)) AS ?result)
}""" ;
sh:select """
SELECT ?result
WHERE {
BIND (IRI(CONCAT(str($base), $concat)) AS ?result)
}
""" ;
.