use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class JenaReasoningServiceTest method testClassifyWithRule.
/**
* Tests the classify(Model data, List<Rule> rules) method
*/
private void testClassifyWithRule(JenaReasoningService service) {
log.info("Testing {}", service.getClass());
// Prepare the rule set
String source = "" + "\n@prefix rdf: <" + RDF.getURI() + ">." + "\n@prefix foaf: <" + TestData.FOAF_NS + ">." + "\n@prefix ex: <" + TestData.TEST_NS + ">." + "\n[rule: (?a foaf:workplaceHomepage ?w) (?w rdf:type ex:SWResearchLab) -> (?a rdf:type ex:SWResearcher)] ";
// log.info("This is the ruleset: \n {}", source);
List<Rule> rules = TestUtils.parseRuleStringAsFile(source);
log.info("Loaded {} rules", rules.size());
// Clean data
TestData.alexdma.removeProperties();
TestData.enridaga.removeProperties();
Resource wphomepage = TestData.model.createResource("http://stlab.istc.cnr.it");
Resource swResearchLab = TestData.model.createResource(TestData.TEST_NS + "SWResearchLab");
// Prepare data
TestData.alexdma.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
TestData.enridaga.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
wphomepage.addProperty(RDF.type, swResearchLab);
// Setup input for the reasoner
Model input = ModelFactory.createUnion(TestData.enridaga.getModel(), TestData.alexdma.getModel());
input = ModelFactory.createUnion(input, wphomepage.getModel());
input = ModelFactory.createUnion(input, TestData.foaf);
try {
// Run the method
Set<Statement> inferred = service.runTask(ReasoningService.Tasks.CLASSIFY, input, rules, false, null);
// Expected statements
Resource swResearcher = TestData.model.createResource(TestData.TEST_NS + "SWResearcher");
Set<Statement> expected = new HashSet<Statement>();
expected.add(TestData.model.createStatement(TestData.alexdma, RDF.type, swResearcher));
expected.add(TestData.model.createStatement(TestData.enridaga, RDF.type, swResearcher));
log.info("All the expected statements must be in the inferred output");
Set<Statement> notInOutput = TestUtils.expectedStatementsCheck(inferred, expected);
log.info("Are all expected statements in the inferred set (true)? {}", notInOutput.isEmpty());
if (!notInOutput.isEmpty()) {
for (Statement bad : notInOutput) {
log.error("The following statement is not included in the reasoner output: {}", bad);
}
}
assertTrue(notInOutput.isEmpty());
// There must be only rdf:type output
boolean onlyRdf = true;
for (Statement stat : inferred) {
// Here we want only rdf:type statements
if (!stat.getPredicate().equals(RDF.type)) {
log.error("This statement is not rdf:type: {}", stat);
}
if (!stat.getPredicate().equals(RDF.type)) {
onlyRdf = false;
}
}
log.info("Check for statements to be rdf:type only (true): {}", onlyRdf);
assertTrue(onlyRdf);
} catch (ReasoningServiceException e) {
log.error("Error thrown: {}", e);
assertTrue(false);
} catch (InconsistentInputException e) {
log.error("Error thrown: {}", e);
assertTrue(false);
} catch (UnsupportedTaskException e) {
log.error("Error thrown: {}", e);
assertTrue(false);
}
// Clean data
TestData.alexdma.removeProperties();
TestData.enridaga.removeProperties();
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class JenaReasoningServiceTest method testEnrich.
/**
* Tests the enrich(Model data) method
*
* @param service
*/
private void testEnrich(JenaReasoningService service) {
// Clean data
TestData.alexdma.removeProperties();
// Prepare data
TestData.alexdma.addProperty(RDF.type, TestData.foaf_Person);
// Setup input for the reasoner
Model input = ModelFactory.createUnion(TestData.foaf, TestData.alexdma.getModel());
try {
// Run the method
Set<Statement> inferred = service.runTask(ReasoningService.Tasks.ENRICH, input);
// Prepare the input statements to check the output with
Set<Statement> inputStatements = input.listStatements().toSet();
boolean onlyInferred = true;
log.info("Check for statements to be only the inferred ones");
Set<Statement> badOnes = new HashSet<Statement>();
for (Statement stat : inferred) {
// Must not be a statement in input
if (inputStatements.contains(stat)) {
onlyInferred = false;
badOnes.add(stat);
}
}
log.info("Are there only inferred statements (true)? {}", onlyInferred);
if (!onlyInferred) {
for (Statement bad : badOnes) {
log.error("Found a bad statement in output: {}", bad);
}
}
assertTrue(onlyInferred);
} catch (ReasoningServiceException e) {
log.error("Error thrown: {}", e);
assertTrue(false);
} catch (InconsistentInputException e) {
log.error("Error thrown: {}", e);
assertTrue(false);
} catch (UnsupportedTaskException e) {
log.error("Error thrown: {}", e);
assertTrue(false);
}
// Clean data
TestData.alexdma.removeProperties();
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class AbstractJenaReasoningService method enrich.
/**
* Enriching: 1) Perform reasoning on a reasoner customized with the given rule set 2) Returns all the
* statements (filtered = false) or only inferred ones (filtered = true)
*
* This is a default implementation of task {@see ReasoningService.Tasks.ENRICH} when a set of rules is
* given. Subclasses may want to change it.
*
* @param data
* @param rules
* @param filtered
* @return
*/
protected Set<Statement> enrich(Model data, List<Rule> rules, boolean filtered) {
log.debug(" enrich(Model data, List<Rule> rules, boolean filtered)");
// We keep the original list to prune the data after, if necessary
if (filtered) {
Set<Statement> original = new HashSet<Statement>();
original.addAll(data.listStatements().toSet());
log.debug(" original statements are: {}", original.size());
InfModel i = run(data, rules);
Set<Statement> inferred = i.listStatements().toSet();
log.debug(" inferred statements are: {}", inferred.size());
return prune(original, inferred);
} else {
return run(data, rules).listStatements().toSet();
}
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class AbstractJenaReasoningService method enrich.
/**
* Enriching: 1) Perform reasoning 2) Returns all the statements (filtered = false) or only inferred ones
* (filtered = true)
*
* This is a default implementation of task {@see ReasoningService.Tasks.ENRICH}. Subclasses may want to
* change it.
*
* @param data
* @param rules
* @return
*/
protected Set<Statement> enrich(Model data, boolean filtered) {
log.debug(" enrich(Model data, boolean filtered)");
// We keep the original list to prune the data after, if necessary
if (filtered) {
Set<Statement> original = new HashSet<Statement>();
original.addAll(data.listStatements().toSet());
log.debug(" original statements are: {}", original.size());
InfModel i = run(data);
Set<Statement> inferred = i.listStatements().toSet();
log.debug(" inferred statements are: {}", inferred.size());
return prune(original, inferred);
} else {
return run(data).listStatements().toSet();
}
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class JenaOWLReasoningServiceTest method testSubclassOf.
/**
* Tests rdfs:subClassOf inference with OWLReasoner
*/
@Test
public void testSubclassOf() {
log.info("Testing rdfs:subClassOf inference with OWL reasoner");
// Prepare data
TestData.alexdma.addProperty(RDF.type, TestData.foaf_Person);
// Setup input for the reasoner
Model input = ModelFactory.createUnion(TestData.foaf, TestData.alexdma.getModel());
// Am I a foaf:Agent?
log.info("Instantiating the OWL reasoner");
InfModel inferred = reasoningService.run(input);
Statement isAgent = TestData.model.createStatement(TestData.alexdma, RDF.type, TestData.foaf_Agent);
// log.info("Statements: {}",
// TestUtils.printStatements(inferred, TestData.alexdma, RDF.type));
log.info("Is any foaf:Person a foaf:Agent...? {}", inferred.contains(isAgent));
assertTrue(inferred.contains(isAgent));
// Reset resource to be clean for other tests
TestData.alexdma.removeProperties();
}
Aggregations