use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class ReasoningServiceExecutor method executeJenaReasoningService.
/**
* Execute a JenaReasoningService
*
* TODO: Add parameter to decide if the output graph must be deleted if exists
*
* @param s
* @param input
* @param rules
* @return
* @throws ReasoningServiceException
* @throws UnsupportedTaskException
*/
private ReasoningServiceResult<Model> executeJenaReasoningService(String task, JenaReasoningService s, Model input, List<Rule> rules, String targetGraphID, boolean filtered, Map<String, List<String>> parameters) throws ReasoningServiceException, UnsupportedTaskException {
// Check task: this is managed directly by the endpoint
if (task.equals(ReasoningServiceExecutor.TASK_CHECK)) {
log.debug("Task is '{}'", ReasoningServiceExecutor.TASK_CHECK);
try {
boolean is = s.isConsistent(input);
return new ReasoningServiceResult<Model>(ReasoningServiceExecutor.TASK_CHECK, is);
} catch (ReasoningServiceException e) {
log.error("Error thrown: {}", e);
throw e;
}
}
try {
Set<Statement> result = s.runTask(task, input, rules, filtered, parameters);
if (result == null) {
log.error("Result is null");
throw new RuntimeException("Result is null.");
}
Model outputModel = ModelFactory.createDefaultModel();
outputModel.add(result.toArray(new Statement[result.size()]));
// If target is null, then get back results, elsewhere put it in
// target graph
log.debug("Prepare output");
if (targetGraphID == null) {
log.debug("Returning {} statements", result.size());
return new ReasoningServiceResult<Model>(task, true, outputModel);
} else {
save(outputModel, targetGraphID);
return new ReasoningServiceResult<Model>(task, true);
}
} catch (ReasoningServiceException e) {
log.error("Error thrown: {}", e);
throw e;
} catch (InconsistentInputException e) {
log.debug("The input is not consistent");
return new ReasoningServiceResult<Model>(ReasoningServiceExecutor.TASK_CHECK, false);
} catch (UnsupportedTaskException e) {
log.error("Error thrown: {}", e);
throw e;
} catch (IOException e) {
throw new ReasoningServiceException(e);
}
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class ReasoningServiceExecutor method execute.
/**
* General method for execution, delegates to specific implementations.
*
* @param task
* @param service
* @param targetGraphID
* @param parameters
* @return
* @throws ReasoningServiceException
* @throws UnsupportedTaskException
* @throws InconsistentInputException
*/
private ReasoningServiceResult<?> execute(String task, ReasoningService<?, ?, ?> service, String targetGraphID, Map<String, List<String>> parameters) throws ReasoningServiceException, UnsupportedTaskException, InconsistentInputException {
long start = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
log.debug("[start] Execution: {}", service.getClass().getCanonicalName());
log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
log.debug("-----------------------------------------------------");
log.debug("execute()");
log.debug(" > task: {}", task);
log.debug(" > service: {}", service.getClass().getCanonicalName());
log.debug(" > target: {}", targetGraphID);
log.debug(" > parameters:");
for (Entry<String, List<String>> e : parameters.entrySet()) {
log.debug(" >> {}: {}", e.getKey());
for (String v : e.getValue()) {
log.debug(" >>> value: {}", v);
}
}
log.debug(" > input providers:");
for (ReasoningServiceInputProvider p : inmgr.getProviders()) {
log.debug(" >> {}", p.getClass().getCanonicalName());
}
log.debug("-----------------------------------------------------");
}
ReasoningServiceResult<?> result = null;
/**
* TODO Switch this into the ReasoningService implementation
*/
if (service instanceof JenaReasoningService) {
Model input = ModelFactory.createDefaultModel();
synchronized (inmgr) {
Iterator<Statement> statements = inmgr.getInputData(Statement.class);
while (statements.hasNext()) {
input.add(statements.next());
}
}
List<Rule> rules = null;
synchronized (inmgr) {
Iterator<Rule> rulesI = inmgr.getInputData(Rule.class);
while (rulesI.hasNext()) {
Rule o = rulesI.next();
log.debug("Rule: {}", o);
if (rules == null) {
rules = new ArrayList<Rule>();
}
rules.add(o);
}
}
if (log.isDebugEnabled()) {
log.debug("Input size is {} statements", input.listStatements().toSet().size());
}
result = executeJenaReasoningService(task, (JenaReasoningService) service, input, rules, targetGraphID, true, parameters);
} else if (service instanceof OWLApiReasoningService) {
OWLOntology input;
try {
input = OWLManager.createOWLOntologyManager().createOntology();
} catch (OWLOntologyCreationException e) {
throw new ReasoningServiceException(e);
}
synchronized (inmgr) {
Iterator<OWLAxiom> statements = inmgr.getInputData(OWLAxiom.class);
while (statements.hasNext()) {
input.getOWLOntologyManager().addAxiom(input, statements.next());
}
}
// FIXME Please check if this is really necessary!!!
input = input.getOWLOntologyManager().getOntology(input.getOntologyID());
List<SWRLRule> rules = null;
synchronized (inmgr) {
Iterator<SWRLRule> rulesI = inmgr.getInputData(SWRLRule.class);
while (rulesI.hasNext()) {
if (rules == null) {
rules = new ArrayList<SWRLRule>();
}
rules.add(rulesI.next());
}
}
if (log.isDebugEnabled()) {
log.debug("Input size is {} statements", input.getAxiomCount());
}
result = executeOWLApiReasoningService(task, (OWLApiReasoningService) service, input, rules, targetGraphID, true, parameters);
} else
throw new UnsupportedOperationException("Service implementation not supported!");
if (log.isDebugEnabled()) {
log.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
long end = System.currentTimeMillis();
log.debug("[end] In time: {}ms", (end - start));
log.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
return result;
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class TestUtils method printStatements.
/**
* 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;
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class JenaRDFSReasoningServiceTest method testRDFSDomain.
@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();
}
use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.
the class JenaReasoningServiceTest method testRunWithRules.
/**
* 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();
}
Aggregations