Author: enridaga
Date: Fri Aug 12 13:04:03 2011
New Revision: 1157085
URL: http://svn.apache.org/viewvc?rev=1157085&view=rev
Log:
* Implementation of JenaReasoningService as basic interface for all Jena based
rasoning services
* Abstract implementation of the above interface
** Support all the three reasoning service methods (classify, enrich,
consistency check)
** Support for adding rules customizing the reasoner configuration
* Three kind of Jena reasoners (RDFS, OWL, OWLMini)
* Tests for all the classes
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/pom.xml (with
props)
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/AbstractJenaReasoningService.java
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLMiniReasoningService.java
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningService.java
(with props)
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningService.java
(with props)
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaReasoningService.java
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/filters/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/filters/PropertyFilter.java
(with props)
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningServiceTest.java
(with props)
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningServiceTest.java
(with props)
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaReasoningServiceTest.java
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/PropertyFilterTest.java
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestData.java
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestUtils.java
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/resources/
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/resources/foaf.rdf
(with props)
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/resources/generic.rdfs.rules
Modified:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/ (props changed)
Propchange: incubator/stanbol/branches/jena-reasoners/reasoners/jena/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Aug 12 13:04:03 2011
@@ -0,0 +1,3 @@
+.settings
+.classpath
+.project
Added: incubator/stanbol/branches/jena-reasoners/reasoners/jena/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/pom.xml?rev=1157085&view=auto
==============================================================================
--- incubator/stanbol/branches/jena-reasoners/reasoners/jena/pom.xml (added)
+++ incubator/stanbol/branches/jena-reasoners/reasoners/jena/pom.xml Fri Aug 12
13:04:03 2011
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <parent>
+ <artifactId>stanbol-parent</artifactId>
+ <groupId>org.apache.stanbol</groupId>
+ <version>0.9.0-incubating-SNAPSHOT</version>
+ <relativePath>../../../trunk/parent</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>org.apache.stanbol.reasoners.jena</artifactId>
+ <packaging>bundle</packaging>
+ <name>Apache Stanbol Reasoners: Jena</name>
+ <version>0.1</version>
+
+ <description>Methods to use the Jena rule-based reasoner</description>
+ <build>
+ <!-- make it an OSGi bundle -->
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <!-- Export-Package>
org.apache.stanbol.reasoners.jena.* </Export-Package -->
+
<Import-Package>*</Import-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+
+ <!-- Diagnostic dependencies -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <scope>runtime</scope>
+ </dependency>
+
+ <!-- Dependencise for testing -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+
<artifactId>org.apache.felix.scr.annotations</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.hp.hpl.jena</groupId>
+ <artifactId>jena</artifactId>
+ <type>jar</type>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+</project>
\ No newline at end of file
Propchange: incubator/stanbol/branches/jena-reasoners/reasoners/jena/pom.xml
------------------------------------------------------------------------------
svn:executable = *
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/AbstractJenaReasoningService.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/AbstractJenaReasoningService.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/AbstractJenaReasoningService.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/AbstractJenaReasoningService.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,250 @@
+package org.apache.stanbol.reasoners.jena;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.stanbol.reasoners.jena.filters.PropertyFilter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.rdf.model.InfModel;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.reasoner.InfGraph;
+import com.hp.hpl.jena.reasoner.Reasoner;
+import com.hp.hpl.jena.reasoner.ValidityReport;
+import com.hp.hpl.jena.reasoner.ValidityReport.Report;
+import com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner;
+import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
+import com.hp.hpl.jena.reasoner.rulesys.Rule;
+import com.hp.hpl.jena.vocabulary.RDF;
+
+/**
+ * Abstract implementation of the {@see JenaReasoningService} interface
+ */
+public abstract class AbstractJenaReasoningService implements
+ JenaReasoningService {
+ private Reasoner reasoner;
+ private static final Logger log = LoggerFactory
+ .getLogger(AbstractJenaReasoningService.class);
+
+ /**
+ * This constructor sets the given reasoner instance as the default
shared one.
+ *
+ * @param reasoner
+ */
+ protected AbstractJenaReasoningService(Reasoner reasoner) {
+ this.reasoner = reasoner;
+ }
+
+ /**
+ * Gets the Jena reasoner instance (to be used by subclasses)
+ *
+ * @return
+ */
+ protected Reasoner getReasoner() {
+ return reasoner;
+ }
+
+ /**
+ * Generic method to perform inferences
+ */
+ @Override
+ public InfModel run(Model data) {
+ InfGraph inferredGraph = this.reasoner.bind(data.getGraph());
+ return ModelFactory.createInfModel(inferredGraph);
+ }
+
+ /**
+ * This method performs inferences creating a new specialized reasoner,
+ * which extends the capabilities of the default one and adds the given
rule
+ * set.
+ *
+ * @param data
+ * @param rules
+ * @return
+ */
+ @Override
+ public InfModel run(Model data, List<Rule> rules) {
+ InfGraph inferredGraph =
customReasoner(rules).bind(data.getGraph());
+ return ModelFactory.createInfModel(inferredGraph);
+ }
+
+ /**
+ * This method is called to build a custom reasoner to be used with a
given
+ * rule set. Subclasses may want to specialize the default behavior,
which
+ * simply merge the standard rule set with the given list
+ *
+ * Note: to customized the default reasoner instance, we need to create
a
+ * new instance, to avoid to keep the configuration changes in the
standard
+ * shared instance. This is not much efficient, because we create a new
+ * reasoner for each call which includes a specified rule set.
+ *
+ * In the future, we may want to implement a way to deploy customized
shared
+ * reasoning services, based on a specific recipe.
+ *
+ * @param rules
+ * @return
+ */
+ protected Reasoner customReasoner(List<Rule> customRules) {
+ List<Rule> standardRules = ((FBRuleReasoner)
this.reasoner).getRules();
+ standardRules.addAll(customRules);
+ return new GenericRuleReasoner(standardRules);
+ }
+
+ /**
+ * Classification: 1) Perform reasoning 2) Returns only rdf:type
statements
+ *
+ * @param data
+ * @return
+ */
+ @Override
+ public Set<Statement> classify(Model data) {
+ return run(data).listStatements()
+ .filterKeep(new
PropertyFilter(RDF.type)).toSet();
+ }
+
+ /**
+ * Classification: 1) Perform reasoning on a reasoner customized with
the
+ * given rule set 2) Returns only rdf:type statements
+ *
+ * @param data
+ * @param rules
+ * @return
+ */
+ @Override
+ public Set<Statement> classify(Model data, List<Rule> rules) {
+ return run(data, rules).listStatements()
+ .filterKeep(new
PropertyFilter(RDF.type)).toSet();
+ }
+
+ /**
+ * Enriching: 1) Perform reasoning 2) Returns all the statements
(filtered =
+ * false) or only inferred ones (filtered = true)
+ *
+ * @param data
+ * @param rules
+ * @return
+ */
+ @Override
+ public Set<Statement> enrich(Model data, boolean filtered) {
+ Set<Statement> statements = run(data).listStatements().toSet();
+ if (filtered) {
+ return prune(data.listStatements().toSet(), statements);
+ }
+ return statements;
+ }
+
+ /**
+ * Removes the statements in the first set from the second set
+ *
+ * @param input
+ * @param statements
+ * @return
+ */
+ protected final Set<Statement> prune(Set<Statement> first,
+ Set<Statement> second) {
+ Set<Statement> remove = new HashSet<Statement>();
+ for (Statement s : second) {
+ if (first.contains(s)) {
+ remove.add(s);
+ }
+ }
+ second.removeAll(remove);
+ return second;
+ }
+
+ /**
+ * Enriching: 1) Perform reasoning 2) Returns the inferred statements
only.
+ * This is the same as enrich(data, true)
+ *
+ * @param data
+ * @return
+ */
+ public Set<Statement> enrich(Model data) {
+ return enrich(data, true);
+ }
+
+ /**
+ * 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)
+ *
+ * @param data
+ * @param rules
+ * @param filtered
+ * @return
+ */
+ @Override
+ public Set<Statement> enrich(Model data, List<Rule> rules, boolean
filtered) {
+ Set<Statement> statements = run(data,
rules).listStatements().toSet();
+ if (filtered) {
+ return prune(data.listStatements().toSet(), statements);
+ }
+ return statements;
+ }
+
+ /**
+ * Enriching: 1) Perform reasoning on a reasoner customized with the
given
+ * rule set 2) Returns the inferred statements only. This is the same as
+ * enrich(data, rules, true)
+ *
+ * @param data
+ * @param rules
+ * @return
+ */
+ @Override
+ public Set<Statement> enrich(Model data, List<Rule> rules) {
+ return enrich(data, rules, true);
+ }
+
+ /**
+ * Consistency check: whether this RDF is consistent or not
+ *
+ * @param data
+ * @return
+ */
+ @Override
+ public boolean isConsistent(Model data) {
+ return isConsistent(run(data).validate());
+ }
+
+ /**
+ * Consistency check: whether this RDF is consistent or not
+ *
+ * We decide to apply a strict meaning of consistency. The alternative
would
+ * be to use isValid() method, which tolerates classes that can't be
+ * instantiated
+ *
+ * @param data
+ * @param rules
+ * @return
+ */
+ @Override
+ public boolean isConsistent(Model data, List<Rule> rules) {
+ return isConsistent(run(data, rules).validate());
+ }
+
+ /**
+ * This internal method implements the logic of consistency.
+ *
+ * We decide to apply a strict meaning of consistency. The alternative
would
+ * be to use isValid() method, which tolerates classes that can't be
+ * instantiated.
+ *
+ * Subclasses may want to change this behavior.
+ *
+ * @param report
+ * @return
+ */
+ protected boolean isConsistent(ValidityReport report) {
+ Iterator<Report> it = report.getReports();
+ while(it.hasNext()){
+ log.info("Report: {}",it.next());
+ }
+ return report.isClean();
+ }
+}
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLMiniReasoningService.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLMiniReasoningService.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLMiniReasoningService.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLMiniReasoningService.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,14 @@
+package org.apache.stanbol.reasoners.jena;
+
+import com.hp.hpl.jena.reasoner.ReasonerRegistry;
+
+/**
+ * OWL mini reasoning service
+ */
+public class JenaOWLMiniReasoningService extends AbstractJenaReasoningService {
+
+ protected JenaOWLMiniReasoningService() {
+ super(ReasonerRegistry.getOWLMiniReasoner());
+ }
+
+}
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningService.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningService.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningService.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningService.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,12 @@
+package org.apache.stanbol.reasoners.jena;
+
+import com.hp.hpl.jena.reasoner.ReasonerRegistry;
+
+/**
+ * OWL Reasoning service
+ */
+public class JenaOWLReasoningService extends AbstractJenaReasoningService{
+ public JenaOWLReasoningService() {
+ super(ReasonerRegistry.getOWLReasoner());
+ }
+}
Propchange:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningService.java
------------------------------------------------------------------------------
svn:executable = *
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningService.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningService.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningService.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningService.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,12 @@
+package org.apache.stanbol.reasoners.jena;
+
+import com.hp.hpl.jena.reasoner.ReasonerRegistry;
+
+/**
+ * RDFS reasoning service
+ */
+public class JenaRDFSReasoningService extends AbstractJenaReasoningService{
+ public JenaRDFSReasoningService() {
+ super(ReasonerRegistry.getRDFSReasoner());
+ }
+}
Propchange:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningService.java
------------------------------------------------------------------------------
svn:executable = *
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaReasoningService.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaReasoningService.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaReasoningService.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/JenaReasoningService.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,68 @@
+package org.apache.stanbol.reasoners.jena;
+
+import java.util.List;
+import java.util.Set;
+
+import com.hp.hpl.jena.rdf.model.InfModel;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.reasoner.rulesys.Rule;
+
+/**
+ * Interface for a Jena based reasoning services
+ */
+public interface JenaReasoningService {
+
+ /**
+ * Runs the reasoner over the given input data
+ * s
+ * @param data
+ * @return
+ */
+ public abstract InfModel run(Model data);
+
+ /**
+ * This method returns only rdf:type statements
+ *
+ * @param data
+ * @return
+ */
+ public abstract Set<Statement> classify(Model data);
+
+ /**
+ * This method returns only inferred statements Same as enrich(data,
true);
+ *
+ * @param data
+ * @return
+ */
+ public abstract Set<Statement> enrich(Model data);
+
+ /**
+ * Whether the data is consistent
+ *
+ * @param data
+ * @return
+ */
+ public abstract boolean isConsistent(Model data);
+
+ /**
+ * This method returns all the inferred model as a set of statements. If
+ * filtered = true, then return only the inferred statements
+ *
+ * @param data
+ * @param filtered
+ * @return
+ */
+ public abstract Set<Statement> enrich(Model data, boolean filtered);
+
+ public abstract InfModel run(Model data, List<Rule> rules);
+
+ public abstract boolean isConsistent(Model data, List<Rule> rules);
+
+ public abstract Set<Statement> enrich(Model data, List<Rule> rules);
+
+ public abstract Set<Statement> enrich(Model data, List<Rule> rules,
boolean filtered);
+
+ public abstract Set<Statement> classify(Model data, List<Rule> rules);
+
+}
\ No newline at end of file
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/filters/PropertyFilter.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/filters/PropertyFilter.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/filters/PropertyFilter.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/filters/PropertyFilter.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,24 @@
+package org.apache.stanbol.reasoners.jena.filters;
+
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.util.iterator.Filter;
+
+/**
+ * A filter to get only statements with the given property
+ */
+public class PropertyFilter extends Filter<Statement>{
+ private Property property;
+
+ public PropertyFilter(Property property) {
+ this.property = property;
+ }
+
+ @Override
+ public boolean accept(Statement statement) {
+ /**
+ * Only statements with the given property
+ */
+ return statement.getPredicate().equals(property);
+ }
+}
Propchange:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/main/java/org/apache/stanbol/reasoners/jena/filters/PropertyFilter.java
------------------------------------------------------------------------------
svn:executable = *
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningServiceTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningServiceTest.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningServiceTest.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningServiceTest.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,119 @@
+package org.apache.stanbol.reasoners.jena;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.rdf.model.InfModel;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.reasoner.ValidityReport;
+import com.hp.hpl.jena.vocabulary.RDF;
+
+/**
+ * Tests specific for the JenaOWLReasoningService implementation
+ */
+public class JenaOWLReasoningServiceTest {
+
+ private JenaReasoningService reasoningService;
+
+ private static final Logger log = LoggerFactory
+ .getLogger(JenaOWLReasoningServiceTest.class);
+
+ /**
+ * We prepare some static variables to be used in tests
+ */
+ @Before
+ public void setUp() {
+
+ // The single instance
+ reasoningService = new JenaOWLReasoningService();
+
+ }
+
+ /**
+ * 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();
+ }
+
+ /**
+ * Tests the owl:disjointWith inferences
+ */
+ @Test
+ public void testDisjointness() {
+ log.info("Testing owl:disjointWith inferences with OWL
reasoner");
+
+ // We assure everything is clean
+ TestData.alexdma.removeProperties();
+ TestData.alexdma.addProperty(RDF.type,
TestData.foaf_Organization);
+ TestData.alexdma.addProperty(RDF.type, TestData.foaf_Person);
+
+ // Setup input for the reasoner
+ Model input = ModelFactory.createUnion(TestData.foaf,
TestData.alexdma.getModel());
+
+ // Can I be a foaf:Organization and a foaf:Person at the same
time?
+ log.info("Instantiating the OWL reasoner");
+
+ // Run the reasoner
+ InfModel inferred = reasoningService.run(input);
+
+// log.info("Statements: {}",
+// TestUtils.printStatements(inferred,
TestData.alexdma, RDF.type));
+ ValidityReport validity = inferred.validate();
+
+ log.info(
+ "Can I be a foaf:Organization and a foaf:Person
at the same time...? {}",
+ validity.isValid());
+ assertTrue(!validity.isValid());
+ // Clean shared resource
+ TestData.alexdma.removeProperties();
+ }
+
+ public void isNotConsistent(){
+ // Clean data
+ TestData.alexdma.removeProperties();
+
+ // Prepare data
+ TestData.alexdma.addProperty(RDF.type, TestData.foaf_Person);
+ TestData.alexdma.addProperty(RDF.type,
TestData.foaf_Organization);
+
+ // Setup input for the reasoner
+ Model input = ModelFactory.createUnion(TestData.foaf,
TestData.alexdma.getModel());
+
+ assertFalse(reasoningService.isConsistent(input));
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ }
+}
Propchange:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaOWLReasoningServiceTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningServiceTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningServiceTest.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningServiceTest.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningServiceTest.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,158 @@
+package org.apache.stanbol.reasoners.jena;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.rdf.model.InfModel;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.vocabulary.RDF;
+import com.hp.hpl.jena.vocabulary.RDFS;
+
+/**
+ * Tests specific for the JenaRDFSReasoningService implementation
+ *
+ */
+public class JenaRDFSReasoningServiceTest {
+
+ private JenaReasoningService reasoningService;
+
+ private static final Logger log = LoggerFactory
+ .getLogger(JenaRDFSReasoningServiceTest.class);
+
+ /**
+ * We prepare some static variables to be used in tests
+ */
+ @Before
+ public void setUp() {
+
+ // The single instance
+ reasoningService = new JenaRDFSReasoningService();
+
+ }
+
+ /**
+ * Tests rdfs:subClassOf inference with RDFSReasoner
+ */
+ @Test
+ public void testRDFSSubclassOf() {
+ log.info("Testing rdfs:subClassOf inference with RDFS
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());
+
+ // Is alexdma foaf:Agent?
+ 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 is also a foaf:Agent...(true)? {}",
+ inferred.contains(isAgent));
+ assertTrue(inferred.contains(isAgent));
+
+ // Reset resource to be clean for other tests
+ TestData.alexdma.removeProperties();
+ }
+
+ @Test
+ public void testRDFSRange() {
+ log.info("Testing rdfs:range inference with RDFS reasoner");
+
+ // Prepare data
+ TestData.alexdma.addProperty(TestData.foaf_knows,
TestData.enridaga);
+
+ // Setup input for the reasoner
+ Model input = ModelFactory.createUnion(TestData.foaf,
+ TestData.alexdma.getModel());
+
+ // Is enridaga a foaf:Person?
+ InfModel inferred = reasoningService.run(input);
+
+ Statement isPerson =
TestData.model.createStatement(TestData.enridaga,
+ RDF.type, TestData.foaf_Person);
+
+// log.info("Statements: {}", TestUtils.printStatements(inferred,
+// TestData.enridaga, RDF.type));
+ log.info("Is any rdfs:range of foaf:knows a
foaf:Person...(true)? {}",
+ inferred.contains(isPerson));
+
+ assertTrue(inferred.contains(isPerson));
+
+ // Reset resource to be clean for other tests
+ TestData.alexdma.removeProperties();
+ }
+
+ @Test
+ public void testRDFSDomain() {
+ log.info("Testing rdfs:domain inference with RDFS reasoner");
+
+ // Prepare data
+ TestData.alexdma.addProperty(TestData.foaf_knows,
TestData.enridaga);
+
+ // Setup input for the reasoner
+ Model input = ModelFactory.createUnion(TestData.foaf,
+ TestData.alexdma.getModel());
+
+ // Is alexdma a foaf:Person?
+ InfModel inferred = reasoningService.run(input);
+
+ Statement isPerson =
TestData.model.createStatement(TestData.alexdma,
+ RDF.type, TestData.foaf_Person);
+
+// log.info("Statements: {}", TestUtils.printStatements(inferred,
+// TestData.enridaga, RDF.type));
+ log.info("Is any rdfs:domain of foaf:knows a
foaf:Person...(true)? {}",
+ inferred.contains(isPerson));
+
+ assertTrue(inferred.contains(isPerson));
+
+ // Reset resource to be clean for other tests
+ TestData.alexdma.removeProperties();
+ }
+
+ @Test
+ public void testRDFSSubPropertyOf() {
+ log.info("Testing rdfs:subPropertyOf inference with RDFS
reasoner");
+
+ // We invent a property to extend foaf:knows
+ Property collegueOf =
TestData.model.createProperty(TestData.TEST_NS
+ + "collegueOf");
+ collegueOf.addProperty(RDFS.subPropertyOf, TestData.foaf_knows);
+
+ // Prepare data
+ TestData.alexdma.addProperty(collegueOf, TestData.enridaga);
+
+ // Setup input for the reasoner
+ Model input = ModelFactory.createUnion(TestData.foaf,
+ TestData.alexdma.getModel());
+
+ // Does alexdma know enridaga?
+ InfModel inferred = reasoningService.run(input);
+
+ Statement knowsHim =
TestData.model.createStatement(TestData.alexdma,
+ TestData.foaf_knows, TestData.enridaga);
+
+ // log.info("Statements: {}",
TestUtils.printStatements(inferred,
+ // TestData.enridaga, RDF.type));
+ log.info("Does alexdma foaf:knows enridaga?...(true)? {}",
+ inferred.contains(knowsHim));
+
+ assertTrue(inferred.contains(knowsHim));
+
+ // Reset resource to be clean for other tests
+ TestData.alexdma.removeProperties();
+ }
+}
Propchange:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaRDFSReasoningServiceTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaReasoningServiceTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaReasoningServiceTest.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaReasoningServiceTest.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/JenaReasoningServiceTest.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,532 @@
+package org.apache.stanbol.reasoners.jena;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.reasoner.Reasoner;
+import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasonerFactory;
+import com.hp.hpl.jena.reasoner.rulesys.Rule;
+import com.hp.hpl.jena.vocabulary.RDF;
+import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
+
+/**
+ * Here we develop tests related to the methods of the shared interface. All
the
+ * tests here should work at the simplest possible (RDFS) level, because
+ * inferences must be the same for all the standard Jena reasoners.
+ *
+ */
+public class JenaReasoningServiceTest {
+
+ private static final Logger log = LoggerFactory
+ .getLogger(JenaReasoningServiceTest.class);
+
+ private List<JenaReasoningService> reasoners;
+
+ /**
+ * This method instantiate the reasoning services to work with in this
tests
+ */
+ @Before
+ public void setUp() {
+ reasoners = new ArrayList<JenaReasoningService>();
+ reasoners.add(new JenaOWLReasoningService());
+ reasoners.add(new JenaRDFSReasoningService());
+ reasoners.add(new JenaOWLMiniReasoningService());
+ }
+
+ @Test
+ public void testClassify() {
+ log.info("Testing the classify() method with all available
reasoners");
+
+ for (JenaReasoningService service : reasoners) {
+ log.info("Testing : {}", service.getClass());
+ testClassify(service);
+ }
+ }
+
+ @Test
+ public void testEnrich() {
+ log.info("Testing the enrich() method with all available
reasoners");
+
+ for (JenaReasoningService service : reasoners) {
+ log.info("Testing : {}", service.getClass());
+ testEnrich(service);
+ }
+ }
+
+ @Test
+ public void testEnrich2() {
+ log.info("Testing the enrich() method (filtered = false) with
all available reasoners");
+
+ for (JenaReasoningService service : reasoners) {
+ log.info("Testing : {}", service.getClass());
+ testEnrich2(service);
+ }
+ }
+
+ @Test
+ public void testIsConsistent() {
+ log.info("Testing the isConsistent() method with all available
reasoners with consistent data");
+
+ /**
+ * Note: since the inconsistency depends on the reasoner type,
we move
+ * that check on the respective test classes
+ */
+ for (JenaReasoningService service : reasoners) {
+ log.info("Testing : {}", service.getClass());
+ testIsConsistent(service);
+ }
+ }
+
+ /**
+ * Tests the classify() method
+ */
+ private void testClassify(JenaReasoningService reasoningService) {
+ // 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());
+
+ // Run the method
+ Set<Statement> inferred = reasoningService.classify(input);
+
+ boolean foafAgentExists = false;
+ log.info("Check for statements to be rdf:type only");
+ 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);
+ }
+ assertTrue(stat.getPredicate().equals(RDF.type));
+ if (stat.getObject().isResource()) {
+ if
(stat.getObject().asResource().equals(TestData.foaf_Agent)) {
+ foafAgentExists = true;
+ }
+ }
+ }
+
+ log.info(
+ "Does the statement: example:me rdf:type
foaf:Agent exists (true)? {}",
+ foafAgentExists);
+ assertTrue(foafAgentExists);
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ }
+
+ /**
+ * 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());
+
+ // Run the method
+ Set<Statement> inferred = service.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);
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ }
+
+ /**
+ * Tests the enrich(Model data,boolean filtered) method
+ *
+ * @param service
+ */
+ private void testEnrich2(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());
+
+ // Run the method
+ Set<Statement> inferred = service.enrich(input, false);
+
+ // Prepare the input statements to check the output with
+ Set<Statement> inputStatements = input.listStatements().toSet();
+
+ log.info("All the input statements must be in the inferred
output");
+ Set<Statement> notInOutput = new HashSet<Statement>();
+ for (Statement stat : inputStatements) {
+ if (!inferred.contains(stat)) {
+ notInOutput.add(stat);
+ }
+ }
+
+ log.info("Are all input statements in the inferred set (true)?
{}",
+ notInOutput.isEmpty());
+ if (!notInOutput.isEmpty()) {
+ for (Statement bad : notInOutput) {
+ log.error("Found a statement not included in
output: {}", bad);
+ }
+ }
+ assertTrue(notInOutput.isEmpty());
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ }
+
+ /**
+ *
+ * @param service
+ */
+ private void testIsConsistent(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());
+
+ // Run the method
+ assertTrue(service.isConsistent(input));
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ }
+
+ /**
+ * Test the GenericRuleReasoner
+ *
+ */
+ @Test
+ public void testGenericRuleReasoner() {
+ log.info("Test a Generic Rule Reasoner with RDFS rules");
+
+ // Configuration
+ Model m = ModelFactory.createDefaultModel();
+ Resource configuration = m.createResource();
+ configuration.addProperty(ReasonerVocabulary.PROPruleMode,
"hybrid");
+ configuration.addProperty(ReasonerVocabulary.PROPruleSet,
+ "generic.rdfs.rules");
+
+ // Create an instance of such a reasoner
+ Reasoner reasoner =
GenericRuleReasonerFactory.theInstance().create(
+ configuration);
+
+ // This should behave as the RDFSReasoner
+ JenaReasoningService genericRdfs = new
AbstractJenaReasoningService(
+ reasoner) {
+ };
+
+ // Then test the three methods
+ testClassify(genericRdfs);
+ testEnrich(genericRdfs);
+ testIsConsistent(genericRdfs);
+ }
+
+ /**
+ * Run reasoner with a simple rule set as additional input
+ */
+ @Test
+ public void testRunWithRules() {
+ log.info("Testing the run(Model model, List<Rule> rules) method
with all available reasoners");
+
+ for (JenaReasoningService service : reasoners) {
+ log.info("Testing : {}", service.getClass());
+ testRunWithRules(service);
+ }
+ }
+
+ /**
+ * Check consistency with a simple rule set as additional input
+ */
+ @Test
+ public void testIsConsistentWithRules() {
+ log.info("Testing the isConsistent(Model model, List<Rule>
rules) method with all available reasoners");
+
+ for (JenaReasoningService service : reasoners) {
+ log.info("Testing : {}", service.getClass());
+ testIsConsistentWithRules(service);
+ }
+ }
+
+ /**
+ * Tests the isConsistent(Model,List<Rule>) method of a service
+ *
+ * @param service
+ */
+ private void testIsConsistentWithRules(JenaReasoningService service) {
+ log.info("Testing reasoner of type {}", service.getClass());
+
+ // Prepare the rule set
+ String source = ""
+ + "\n@prefix foaf: <"
+ + TestData.FOAF_NS
+ + ">."
+ + "\n@prefix ex: <"
+ + TestData.TEST_NS
+ + ">."
+ + "\n[rule1: (?a foaf:knows ?b) (?a
foaf:workplaceHomepage ?w) (?b foaf:workplaceHomepage ?w) -> (?a ex:collegueOf
?b)] "
+ + "\n[rule2: (?b foaf:knows ?a) -> (?a
foaf:knows ?b)] "
+ + "\n[rule3: (?a ex:collegueOf ?b) -> (?b
ex:collegueOf ?a)] ";
+
+ // 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");
+
+ // Prepare data
+ TestData.alexdma.addProperty(TestData.foaf_knows,
TestData.enridaga);
+ TestData.alexdma.addProperty(TestData.foaf_workplaceHomepage,
+ wphomepage);
+ TestData.enridaga.addProperty(TestData.foaf_workplaceHomepage,
+ wphomepage);
+
+ // Setup input for the reasoner
+ Model input =
ModelFactory.createUnion(TestData.enridaga.getModel(),
+ TestData.alexdma.getModel());
+ input = ModelFactory.createUnion(input, TestData.foaf);
+
+ // Run the method
+ boolean isConsistent = service.isConsistent(input, rules);
+
+ // Assert true
+ log.info("Is consistent (true)? {}", isConsistent);
+ assertTrue(isConsistent);
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ TestData.enridaga.removeProperties();
+ }
+
+ /**
+ * Tests the run(Model,List<Rule>) method of a service
+ *
+ * @param service
+ */
+ private void testRunWithRules(JenaReasoningService service) {
+
+ // Prepare the rule set
+ String source = ""
+ + "\n@prefix foaf: <"
+ + TestData.FOAF_NS
+ + ">."
+ + "\n@prefix ex: <"
+ + TestData.TEST_NS
+ + ">."
+ + "\n[rule1: (?a foaf:knows ?b) (?a
foaf:workplaceHomepage ?w) (?b foaf:workplaceHomepage ?w) -> (?a ex:collegueOf
?b)] "
+ + "\n[rule2: (?b foaf:knows ?a) -> (?a
foaf:knows ?b)] "
+ + "\n[rule3: (?a ex:collegueOf ?b) -> (?b
ex:collegueOf ?a)] ";
+
+ // 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");
+
+ // Prepare data
+ TestData.alexdma.addProperty(TestData.foaf_knows,
TestData.enridaga);
+ TestData.alexdma.addProperty(TestData.foaf_workplaceHomepage,
+ wphomepage);
+ TestData.enridaga.addProperty(TestData.foaf_workplaceHomepage,
+ wphomepage);
+
+ // Setup input for the reasoner
+ Model input =
ModelFactory.createUnion(TestData.enridaga.getModel(),
+ TestData.alexdma.getModel());
+ input = ModelFactory.createUnion(input, TestData.foaf);
+
+ // Run the method
+ Set<Statement> inferred = service.run(input,
rules).listStatements()
+ .toSet();
+
+ // Expected statements
+ Property collegueOf =
TestData.model.createProperty(TestData.TEST_NS
+ + "collegueOf");
+ Set<Statement> expected = new HashSet<Statement>();
+ expected.add(TestData.model.createStatement(TestData.alexdma,
+ collegueOf, TestData.enridaga));
+ expected.add(TestData.model.createStatement(TestData.enridaga,
+ collegueOf, TestData.alexdma));
+
+ 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());
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ TestData.enridaga.removeProperties();
+ }
+
+ /**
+ * Check consistency with a simple rule set as additional input
+ */
+ @Test
+ public void testClassifyWithRules() {
+ log.info("Testing the classify(Model model, List<Rule> rules)
method with all available reasoners");
+
+ for (JenaReasoningService service : reasoners) {
+ log.info("Testing : {}", service.getClass());
+ testClassifyWithRule(service);
+ }
+ }
+
+ /**
+ * 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);
+
+ // Run the method
+ Set<Statement> inferred = service.classify(input, rules);
+
+ // 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);
+
+ // Clean data
+ TestData.alexdma.removeProperties();
+ TestData.enridaga.removeProperties();
+ }
+}
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/PropertyFilterTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/PropertyFilterTest.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/PropertyFilterTest.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/PropertyFilterTest.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,32 @@
+package org.apache.stanbol.reasoners.jena;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Set;
+
+import org.apache.stanbol.reasoners.jena.filters.PropertyFilter;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.rdf.model.Statement;
+/**
+ * This class tests the PropertyFilter
+ */
+public class PropertyFilterTest {
+
+ private static final Logger log = LoggerFactory
+ .getLogger(PropertyFilterTest.class);
+
+ @Test
+ public void test() {
+ PropertyFilter filter = new PropertyFilter(
+ TestData.foaf_firstname);
+ log.info("Testing the {} class", filter.getClass());
+ Set<Statement> output =
TestData.alexdma.getModel().listStatements()
+ .filterKeep(filter).toSet();
+ for(Statement statement : output){
+
assertTrue(statement.getPredicate().equals(TestData.foaf_firstname));
+ }
+ }
+}
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestData.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestData.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestData.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestData.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,84 @@
+package org.apache.stanbol.reasoners.jena;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Resource;
+
+/**
+ * We share this data between all the tests. Note: take care, do not change the
+ * content of the data, for example clean the 'resource' object after you
change it:
+ * resource.removeProperties();
+ */
+public class TestData {
+
+ private static final Logger log =
LoggerFactory.getLogger(TestData.class);
+
+ /**
+ * Constants (local resources)
+ */
+ static final String LOCAL_RESOURCE_FOAF = "foaf.rdf";
+ static final String LOCAL_RESOURCE_GENERIC_RDFS_RULES =
"generic.rdfs.rules";
+ /**
+ * Constants
+ */
+ static final String TEST_NS = "http://www.example.org/";
+ static final String FOAF_NS = "http://xmlns.com/foaf/0.1/";
+ static final String FOAF_firstName = FOAF_NS + "firstName";
+ static final String FOAF_familyName = FOAF_NS + "familyName";
+ static final String FOAF_name = FOAF_NS + "name";
+ static final String FOAF_knows = FOAF_NS + "knows";
+ static final String FOAF_Agent = FOAF_NS + "Agent";
+ static final String FOAF_Person = FOAF_NS + "Person";
+ static final String FOAF_Organization = FOAF_NS + "Organization";
+ static final String FOAF_workplaceHomepage = FOAF_NS +
"workplaceHomepage";
+
+ /**
+ * Data
+ */
+ static Model model; // Just a default model for utilities
+ static Model foaf; // The foaf ontology
+ static Resource alexdma; // A simple resource as subject for
+ static Resource enridaga; // A simple resource as subject for
+ // tests
+ static Resource foaf_Person; // the foaf:Person class
+ static Resource foaf_Agent; // the foaf:Agent class
+ static Resource foaf_Organization; // the foaf:Organization class
+ static Property foaf_firstname;
+ static Property foaf_knows;
+ static Property foaf_workplaceHomepage;
+
+ static {
+ TestData.model = ModelFactory.createDefaultModel();
+
+ TestData.alexdma =
TestData.model.createResource(TestData.TEST_NS
+ + "alexdma");
+ TestData.enridaga =
TestData.model.createResource(TestData.TEST_NS
+ + "enridaga");
+ TestData.foaf_Agent = TestData.model
+ .createResource(TestData.FOAF_Agent);
+ TestData.foaf_Person = TestData.model
+ .createResource(TestData.FOAF_Person);
+ TestData.foaf_Organization = TestData.model
+ .createResource(TestData.FOAF_Organization);
+ TestData.foaf_firstname = TestData.model
+ .createProperty(TestData.FOAF_firstName);
+ TestData.foaf_knows = TestData.model
+ .createProperty(TestData.FOAF_knows);
+ TestData.foaf_workplaceHomepage = TestData.model
+
.createProperty(TestData.FOAF_workplaceHomepage);
+
+ TestData.foaf = ModelFactory.createDefaultModel();
+
+ log.debug(
+ "Loading FOAF ontology from {}",
+
TestData.class.getResource(System.getProperty("file.separator")
+ + LOCAL_RESOURCE_FOAF));
+ TestData.foaf.read(TestData.class.getResource(
+ System.getProperty("file.separator") +
LOCAL_RESOURCE_FOAF)
+ .toString());
+ }
+}
Added:
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestUtils.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestUtils.java?rev=1157085&view=auto
==============================================================================
---
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestUtils.java
(added)
+++
incubator/stanbol/branches/jena-reasoners/reasoners/jena/src/test/java/org/apache/stanbol/reasoners/jena/TestUtils.java
Fri Aug 12 13:04:03 2011
@@ -0,0 +1,81 @@
+package org.apache.stanbol.reasoners.jena;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.InputStreamReader;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.StmtIterator;
+import com.hp.hpl.jena.reasoner.rulesys.Rule;
+import com.hp.hpl.jena.util.PrintUtil;
+
+public class TestUtils {
+
+ /**
+ * utility to print lists of statements
+ *
+ * @param model
+ * @param resource
+ * @param property
+ * @return
+ */
+ public static String printStatements(Model model, Resource resource,
+ Property property) {
+
+ Resource value = null;
+
+ String output = "";
+ StmtIterator iterator;
+ if (resource == null && property == null && value == null) {
+ iterator = model.listStatements();
+ } else {
+ iterator = model.listStatements(resource, property,
value);
+ }
+ while (iterator.hasNext()) {
+ Statement stmt = iterator.nextStatement();
+ output += System.getProperty("line.separator") + " "
+ + PrintUtil.print(stmt);
+ }
+ return output;
+ }
+
+ /**
+ * Check whether a set of expected statements are present in the result
set.
+ * Returns a set of expected statements which are NOT present in the
result.
+ *
+ * @param result
+ * @param expected
+ * @return
+ */
+ public static Set<Statement> expectedStatementsCheck(
+ Set<Statement> inferred, Set<Statement> expected) {
+ Set<Statement> notInInferred = new HashSet<Statement>();
+ for (Statement stat : expected) {
+ if (!inferred.contains(stat)) {
+ notInInferred.add(stat);
+ }
+ }
+ return notInInferred;
+ }
+
+ /**
+ * This utility gets a jena rules file content string and parse it to
return
+ * a List of Rule objects
+ *
+ * @param source
+ * @return
+ */
+ public static List<Rule> parseRuleStringAsFile(String source) {
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ new ByteArrayInputStream(source.getBytes())));
+ Rule.Parser parser = Rule.rulesParserFromReader(br);
+ List<Rule> rules = Rule.parseRules(parser);
+ return rules;
+ }
+}