/* * (non-Javadoc) * @see net.java.treaty.ContractVocabulary#getDomain(java.net.URI) */ public URI getDomain(URI relationshipOrProperty) throws TreatyException { OntModel model; model = getOntology(); ObjectProperty property; property = model.getObjectProperty(relationshipOrProperty.toString()); if (property != null) { try { return new URI(property.getDomain().getURI()); } catch (URISyntaxException e) { throw new TreatyException(e); } // and catch. } // no else. DatatypeProperty dataProperty = model.getDatatypeProperty(relationshipOrProperty.toString()); if (dataProperty != null) { try { return new URI(dataProperty.getDomain().getURI()); } catch (URISyntaxException e) { throw new TreatyException(e); } // end catch. } // no else. return null; }
@Test public void dataAssertionTest() throws FileNotFoundException { model.read( this.getClass().getClassLoader().getResourceAsStream("test/data/misc/decimal-int.owl"), null); final Individual entity = model.getIndividual("http://www.inmindcomputing.com/example/dataAssertion.owl#ENTITY"); final DatatypeProperty value = model.getDatatypeProperty( "http://www.inmindcomputing.com/example/dataAssertion.owl#dataAssertionValue"); Assert.assertTrue(value.isFunctionalProperty()); Assert.assertEquals(1, entity.listPropertyValues(value).toSet().size()); }
public static void addVocab(OntModel model) { OntClass ontCurrency = model.createClass(URI); DatatypeProperty AMNT = model.createDatatypeProperty(AMNT_URI); DatatypeProperty CURRENCY = model.createDatatypeProperty(CURRENCY_URI); AMNT.addDomain(ontCurrency); AMNT.addRange(XSD.xfloat); CURRENCY.addDomain(ontCurrency); CURRENCY.addRange(XSD.xstring); ontCurrency.addLabel(label, null); }
public void run() { while (pelletListener.isDirty()) { // pipeOpen = false; try { pelletListener.setDirty(false); inferenceRounds++; log.info("Getting new inferences"); long startTime = System.currentTimeMillis(); LinkedList<ObjectPropertyStatementPattern> irpl = new LinkedList<ObjectPropertyStatementPattern>(); if (inferenceReceivingPatternAllowSet != null) { irpl.addAll(inferenceReceivingPatternAllowSet); } else { irpl.add(ObjectPropertyStatementPatternFactory.getPattern(null, null, null)); } if (reasonerConfiguration.getQueryForAllObjectProperties()) { pelletModel.enterCriticalSection(Lock.READ); try { ClosableIterator closeIt = pelletModel.listObjectProperties(); try { for (Iterator objPropIt = closeIt; objPropIt.hasNext(); ) { ObjectProperty objProp = (ObjectProperty) objPropIt.next(); if (!("http://www.w3.org/2002/07/owl#".equals(objProp.getNameSpace()))) { irpl.add(ObjectPropertyStatementPatternFactory.getPattern(null, objProp, null)); } } } finally { closeIt.close(); } } finally { pelletModel.leaveCriticalSection(); } deletedObjectProperties.enterCriticalSection(Lock.WRITE); try { ClosableIterator sit = deletedObjectProperties.listSubjects(); try { while (sit.hasNext()) { Resource subj = (Resource) sit.next(); irpl.add( ObjectPropertyStatementPatternFactory.getPattern( null, ResourceFactory.createProperty(subj.getURI()), null)); } } finally { sit.close(); } deletedObjectProperties.removeAll(); } finally { deletedObjectProperties.leaveCriticalSection(); } } if (reasonerConfiguration.getQueryForAllDatatypeProperties()) { pelletModel.enterCriticalSection(Lock.READ); try { ClosableIterator closeIt = pelletModel.listDatatypeProperties(); try { for (Iterator dataPropIt = closeIt; dataPropIt.hasNext(); ) { DatatypeProperty dataProp = (DatatypeProperty) dataPropIt.next(); if (!("http://www.w3.org/2002/07/owl#".equals(dataProp.getNameSpace()))) { // TODO: THIS WILL WORK, BUT NEED TO GENERALIZE THE PATTERN CLASSES irpl.add( ObjectPropertyStatementPatternFactory.getPattern(null, dataProp, null)); } } } finally { closeIt.close(); } } finally { pelletModel.leaveCriticalSection(); } deletedDataProperties.enterCriticalSection(Lock.WRITE); try { ClosableIterator sit = deletedDataProperties.listSubjects(); try { while (sit.hasNext()) { Resource subj = (Resource) sit.next(); irpl.add( ObjectPropertyStatementPatternFactory.getPattern( null, ResourceFactory.createProperty(subj.getURI()), null)); } } finally { sit.close(); } deletedDataProperties.removeAll(); } finally { deletedDataProperties.leaveCriticalSection(); } } int addCount = 0; int retractCount = 0; // force new reasoner (disabled) if (false && !reasonerConfiguration.isIncrementalReasoningEnabled()) { Model baseModel = pelletModel.getBaseModel(); pelletModel = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); pelletModel.getDocumentManager().setProcessImports(false); pelletModel.add(baseModel); } pelletModel.enterCriticalSection(Lock.WRITE); try { pelletModel.rebind(); pelletModel.prepare(); } finally { pelletModel.leaveCriticalSection(); } for (Iterator<ObjectPropertyStatementPattern> patIt = irpl.iterator(); patIt.hasNext(); ) { ObjectPropertyStatementPattern pat = patIt.next(); if (log.isDebugEnabled()) { String subjStr = (pat.getSubject() != null) ? pat.getSubject().getURI() : "*"; String predStr = (pat.getPredicate() != null) ? pat.getPredicate().getURI() : "*"; String objStr = (pat.getObject() != null) ? pat.getObject().getURI() : "*"; log.debug("Querying for " + subjStr + " : " + predStr + " : " + objStr); } Model tempModel = ModelFactory.createDefaultModel(); pelletModel.enterCriticalSection(Lock.READ); try { ClosableIterator ci = pelletModel.listStatements(pat.getSubject(), pat.getPredicate(), pat.getObject()); try { for (ClosableIterator i = ci; i.hasNext(); ) { Statement stmt = (Statement) i.next(); boolean reject = false; // this next part is only needed if we're using Jena's OWL reasoner instead of // actually using Pellet try { if ((((Resource) stmt.getObject()).equals(RDFS.Resource))) { reject = true; } else if ((((Resource) stmt.getSubject()).equals(OWL.Nothing))) { reject = true; } else if ((((Resource) stmt.getObject()).equals(OWL.Nothing))) { reject = true; } } catch (Exception e) { } if (!reject) { tempModel.add(stmt); boolean fullModelContainsStatement = false; fullModel.enterCriticalSection(Lock.READ); try { fullModelContainsStatement = fullModel.contains(stmt); } finally { fullModel.leaveCriticalSection(); } if (!fullModelContainsStatement) { // in theory we should be able to lock only the inference model, but I'm not // sure yet if Jena propagates the locking upward fullModel.enterCriticalSection(Lock.WRITE); closePipe(); try { inferenceModel.add(stmt); addCount++; } finally { openPipe(); fullModel.leaveCriticalSection(); } } } } } finally { ci.close(); } } finally { pelletModel.leaveCriticalSection(); } // now we see what's in the inference model that isn't in the temp model and remove it try { Queue<Statement> localRemovalQueue = new LinkedList<Statement>(); inferenceModel.enterCriticalSection(Lock.READ); try { ClosableIterator ci = inferenceModel.listStatements( pat.getSubject(), pat.getPredicate(), pat.getObject()); try { for (ClosableIterator i = ci; i.hasNext(); ) { Statement stmt = (Statement) i.next(); if (!tempModel.contains(stmt)) { localRemovalQueue.add(stmt); } } } finally { ci.close(); } } finally { inferenceModel.leaveCriticalSection(); } for (Iterator<Statement> i = localRemovalQueue.iterator(); i.hasNext(); ) { fullModel.enterCriticalSection(Lock.WRITE); closePipe(); try { retractCount++; inferenceModel.remove(i.next()); } finally { openPipe(); fullModel.leaveCriticalSection(); } } localRemovalQueue.clear(); } catch (Exception e) { log.error("Error getting inferences", e); } tempModel = null; } this.pelletListener.isConsistent = true; this.pelletListener.inErrorState = false; this.pelletListener.explanation = ""; if (log.isDebugEnabled()) { log.info("Added " + addCount + " statements entailed by assertions"); log.info("Retracted " + retractCount + " statements no longer entailed by assertions"); log.info( "Done getting new inferences: " + (System.currentTimeMillis() - startTime) / 1000 + " seconds"); } } catch (InconsistentOntologyException ioe) { this.pelletListener.isConsistent = false; String explanation = ((PelletInfGraph) pelletModel.getGraph()).getKB().getExplanation(); this.pelletListener.explanation = explanation; log.error(ioe); log.error(explanation); } catch (Exception e) { this.pelletListener.inErrorState = true; log.error("Exception during inference", e); } finally { pelletListener.endReasoning(); } } }