@Override public void addLocalImport(final String nextImport) { this.localImports.add(nextImport); final OntModel nextModel = SpinUtils.loadModelFromClasspath(nextImport); if (nextModel != null) { SpinInferencingRuleImpl.log.info( "adding model to registry and ontology model list nextImport=" + nextImport + " nextModel.size()=" + nextModel.size()); this.ontologyModels.add(nextModel); this.getSpinModuleRegistry().registerAll(nextModel, nextImport); } else { SpinInferencingRuleImpl.log.error("Failed to load import from URL nextImport=" + nextImport); } this.getSpinModuleRegistry().init(); }
/** * See OWLRLExample in spin-examples-1.2.0.jar * * <p>Currently limited to adding the resulting triples from the spin reasoning to the repository * * <p>TODO: add more modes, such as delete matching, add matching, only return matching triples * etc. * * @param inputRepository The OpenRDF repository to use for the input triples * @throws QueryAllException */ public Repository processSpinRules( final Repository inputRepository, final org.openrdf.model.Resource... contexts) throws QueryAllException { // Load domain model with imports // System.out.println("Loading domain ontology..."); // OntModel queryModel = // loadModelWithImports("http://www.co-ode.org/ontologies/pizza/2007/02/12/pizza.owl"); SpinInferencingRuleImpl.log.info("Loading jena model from sesame repository"); final OntModel queryModel = SpinUtils.addSesameRepositoryToJenaModel( inputRepository, ModelFactory.createDefaultModel(ReificationStyle.Minimal), "http://spin.example.org/", contexts); // Create and add Model for inferred triples final Model newTriples = ModelFactory.createDefaultModel(ReificationStyle.Minimal); queryModel.addSubModel(newTriples); SpinInferencingRuleImpl.log.info("Loading ontologies..."); // Register any new functions defined in OWL RL // NOTE: The source for these rules is given as "this" so that they can be retrieved in // future based on this object // Build one big union Model of everything final Graph[] graphs = new Graph[this.ontologyModels.size() + 1]; graphs[0] = queryModel.getGraph(); int i = 1; for (final OntModel nextModel : this.ontologyModels) { SpinInferencingRuleImpl.log.info("i=" + i + " nextModel.size()=" + nextModel.size()); graphs[i++] = nextModel.getGraph(); } final MultiUnion multiUnion = new MultiUnion(graphs); final Model unionModel = ModelFactory.createModelForGraph(multiUnion); final Set<Object> allowedRuleSources = new HashSet<Object>(); allowedRuleSources.addAll(this.localImports); // Collect rules (and template calls) defined in OWL RL final Map<CommandWrapper, Map<String, RDFNode>> initialTemplateBindings = new HashMap<CommandWrapper, Map<String, RDFNode>>(); final Map<Resource, List<CommandWrapper>> cls2Query = SPINQueryFinder.getClass2QueryMap( unionModel, queryModel, SPIN.rule, true, initialTemplateBindings, false, allowedRuleSources); final Map<Resource, List<CommandWrapper>> cls2Constructor = SPINQueryFinder.getClass2QueryMap( queryModel, queryModel, SPIN.constructor, true, initialTemplateBindings, false, allowedRuleSources); final SPINRuleComparator comparator = new DefaultSPINRuleComparator(queryModel); // Run all inferences SpinInferencingRuleImpl.log.info("Running SPIN inferences..."); SPINInferences.run( queryModel, newTriples, cls2Query, cls2Constructor, initialTemplateBindings, null, null, false, SPIN.rule, comparator, null, allowedRuleSources); SpinInferencingRuleImpl.log.info("Inferred triples: " + newTriples.size()); SpinInferencingRuleImpl.log.info("Query triples: " + queryModel.size()); final StmtIterator listStatements = newTriples.listStatements(); while (listStatements.hasNext()) { SpinInferencingRuleImpl.log.info(listStatements.next().toString()); } // Note: To optimise the process, we only add the new triples back into the original // repository return SpinUtils.addJenaModelToSesameRepository(newTriples, inputRepository); }