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

GraphInference without schema #19

Merged
merged 2 commits into from
Jun 12, 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
Expand Up @@ -53,30 +53,32 @@ public static void graphInference(Exchange exchange, GraphBean operationConfig)
graphInference(operationParams, exchange);
}
public static void graphInference(OperationParams params, Exchange exchange) throws Exception {
Repository schema = Utils.createSchemaRepository(params.endpointParams().triples(), params.exchange());
SchemaCachingRDFSInferencer inferencer = new SchemaCachingRDFSInferencer(new MemoryStore(), schema, params.endpointParams().allRules());
Repository inferenceRepo = new SailRepository(inferencer);
Repository inferenceRepo;
if (params.endpointParams.triples() != null) {
Repository schema = Utils.createSchemaRepository(params.endpointParams().triples(), params.exchange());
SchemaCachingRDFSInferencer inferencer = new SchemaCachingRDFSInferencer(new MemoryStore(), schema, params.endpointParams().allRules());
inferenceRepo = new SailRepository(inferencer);
} else
inferenceRepo = new SailRepository( new SchemaCachingRDFSInferencer(new MemoryStore()));
inferenceRepo.init();

Repository sourceRepo = params.graph().getRepository();
Repository targetRepo = inferenceRepo;
//Enable inference
// all statements from source graph to graph with inference enabled

// Enable inference
// all statements from source graph to graph with inference enabled
Model sourceModel = new TreeModel();
for (var st: sourceRepo.getConnection().getStatements(null, null, null, true))
{
sourceModel.add(st);
}
sourceRepo.getConnection()
.getStatements(null, null, null, true)
.forEach(sourceModel::add);

Utils.populateRepository(targetRepo, sourceModel);

//Copy back
// Copy back
Model targetModel = new TreeModel();
for (var st: targetRepo.getConnection().getStatements(null, null, null, true))
{
targetModel.add(st);
}
targetRepo.getConnection()
.getStatements(null, null, null, true)
.forEach(targetModel::add);

Utils.populateRepository(sourceRepo, targetModel);

Expand Down
107 changes: 95 additions & 12 deletions camel-chimera-graph/src/test/java/com/cefriel/GraphInferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,135 @@

package com.cefriel;

import com.cefriel.graph.MemoryRDFGraph;
import com.cefriel.graph.RDFGraph;
import com.cefriel.util.ChimeraResourceBean;
import com.cefriel.util.Utils;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryResult;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class GraphInferenceTest extends CamelTestSupport {
private static ChimeraResourceBean ontology;
private static ChimeraResourceBean triples1;
private static ChimeraResourceBean triples2;
private static ChimeraResourceBean triples;

@Produce("direct:start")
ProducerTemplate start;

@BeforeAll
static void fillBean(){
ontology = new ChimeraResourceBean(
"file://./src/test/resources/file/ontologies/ontology.owl",
"rdfxml");
triples1 = new ChimeraResourceBean(
"file://./src/test/resources/file/template/my-source.ttl",
"turtle");
triples2 = new ChimeraResourceBean(
triples = new ChimeraResourceBean(
"file://./src/test/resources/file/template/enrich.ttl",
"turtle");
}

@Test
public void testNoInference() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:noInference");

MemoryRDFGraph graph = new MemoryRDFGraph();
Utils.populateRepository(graph.getRepository(), triples, null);
start.sendBody("direct:noInference", graph);

mock.expectedMessageCount(1);

RDFGraph result = mock.getExchanges().get(0).getMessage().getBody(RDFGraph.class);
SimpleValueFactory vf = SimpleValueFactory.getInstance();
IRI demoInstance = vf.createIRI("http://sprint-transport.eu/data/AMV_STATION_INFERENCE_DEMO");
IRI classStation = vf.createIRI("http://vocab.gtfs.org/terms#Stop");
RepositoryResult<Statement> statements = result.getRepository().getConnection().getStatements(demoInstance, RDF.TYPE,
classStation);
assert (statements.stream().count() == 0);

mock.assertIsSatisfied();
}

@Test
public void testInference() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:inference");

MemoryRDFGraph graph = new MemoryRDFGraph();
Utils.populateRepository(graph.getRepository(), triples, null);
start.sendBody("direct:inference", graph);

mock.expectedMessageCount(1);

RDFGraph result = mock.getExchanges().get(0).getMessage().getBody(RDFGraph.class);
SimpleValueFactory vf = SimpleValueFactory.getInstance();
IRI demoInstance = vf.createIRI("http://sprint-transport.eu/data/AMV_STATION_INFERENCE_DEMO");
IRI classStation = vf.createIRI("http://vocab.gtfs.org/terms#Stop");
RepositoryResult<Statement> statements = result.getRepository().getConnection().getStatements(demoInstance, RDF.TYPE,
classStation);
assert (statements.stream().count() == 1);

mock.assertIsSatisfied();
}

@Test
public void testInferenceWithoutSchema() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:inferenceNoSchema");

MemoryRDFGraph graph = new MemoryRDFGraph();
SimpleValueFactory vf = SimpleValueFactory.getInstance();
Repository repo = graph.getRepository();
try (var connection = repo.getConnection()) {
connection.add(vf.createStatement(vf.createIRI("http://example.org/a"),
RDFS.SUBPROPERTYOF,
vf.createIRI("http://example.org/b")));

connection.add(vf.createStatement(vf.createIRI("http://example.org/b"),
RDFS.SUBPROPERTYOF,
vf.createIRI("http://example.org/c")));
}
start.sendBody("direct:inferenceNoSchema", graph);

// todo check that the inferred result is correct
mock.expectedMessageCount(1);

RDFGraph result = mock.getExchanges().get(0).getMessage().getBody(RDFGraph.class);
IRI a = vf.createIRI("http://example.org/a");
IRI c = vf.createIRI("http://example.org/c");
RepositoryResult<Statement> statements = result.getRepository().getConnection().getStatements(a, RDFS.SUBPROPERTYOF,
c);
assert (statements.stream().count() == 1);

mock.assertIsSatisfied();
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() {

getCamelContext().getRegistry().bind("ontology", ontology);
getCamelContext().getRegistry().bind("triples1", triples1);
getCamelContext().getRegistry().bind("triples2", triples2);
getCamelContext().getRegistry().bind("triples", triples);

from("direct:noInference")
.to("graph://add?chimeraResource=#bean:triples")
.to("mock:noInference");

from("graph://get")
.to("graph://add?chimeraResource=#bean:triples1")
.to("graph://add?chimeraResource=#bean:triples2")
from("direct:inference")
.to("graph://add?chimeraResource=#bean:triples")
.to("graph://inference?chimeraResource=#bean:ontology")
.to("mock:inference");

from("direct:inferenceNoSchema")
.to("graph://add?chimeraResource=#bean:triples")
.to("graph://inference")
.to("mock:inferenceNoSchema");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
<?xml version="1.0"?>
<rdf:RDF xmlns="http://sprint-transport.eu/ontology/example#"
xml:base="http://sprint-transport.eu/ontology/example"
<rdf:RDF xmlns="http://www.cefriel.com/ontology/example#"
xml:base="http://www.cefriel.com/ontology/example"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:gtfs="http://vocab.gtfs.org/terms#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://sprint-transport.eu/ontology/example">
<owl:Ontology rdf:about="http://www.cefriel.com/ontology/example">
<rdfs:comment>Ontology for chimera-example</rdfs:comment>
<rdfs:label>Ontology Chimera Example</rdfs:label>
</owl:Ontology>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Annotation properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->

<!-- http://sprint-transport.eu/ontology/examplew3.org/2000/01/rdf-schema#comment -->

<owl:AnnotationProperty rdf:about="http://www.w3.org/2000/01/rdf-schema#comment"/>

<!-- http://sprint-transport.eu/ontology/examplew3.org/2000/01/rdf-schema#label -->

<owl:AnnotationProperty rdf:about="http://www.w3.org/2000/01/rdf-schema#label"/>

<!--
///////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -54,8 +39,3 @@

<owl:Class rdf:about="http://vocab.gtfs.org/terms#Stop"/>
</rdf:RDF>



<!-- Generated by the OWL API (version 4.5.9.2019-02-01T07:24:44Z) https://github.com/owlcs/owlapi -->