public void testListHierarchyRoots1() { String doc = "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. " + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. " + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. " + "@prefix owl: <http://www.w3.org/2002/07/owl#>. " + "@prefix : <" + NS + ">. " + ":A a owl:Class. " ; OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null); m.read( new StringReader(doc), NS, "N3" ); OntClass a = m.getOntClass(NS+"A"); TestUtil.assertIteratorValues( this, m.listHierarchyRootClasses(), new Object[] {a} ); }
@Override public Iterator< ? extends Resource> doList( OntModel schema ) { Model data = ModelFactory.createDefaultModel(); Resource c = schema.createResource( "http://example.com/foo#AClass" ); Resource i = data.createResource( "http://example.com/foo#anInd" ); schema.add( c, RDF.type, OWL.Class ); data.add( i, RDF.type, c ); OntModel composite = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, schema ); composite.addSubModel( data ); return composite.listIndividuals(); } @Override
private Individual getCountry(String countryCode) { ResIterator hasCountryCode = getCountries().listSubjectsWithProperty(Geonames.countryCode, countryCode); if (hasCountryCode.hasNext()) { return getCountries().getIndividual(hasCountryCode.next().getURI()); } return null; }
/** Document has set an empty prefix for the model */ protected String getDefaultPrefixNamespace() { // alternatively, the default namespace may be set in the prefix mapping read from the input document String defaultNS = m_source.getNsPrefixURI( "" ); if (defaultNS == null) { defaultNS = m_source.getBaseModel().getNsPrefixURI( "" ); } return defaultNS; }
public static void main( String[] args ) { final OntModel ontModel = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM ); ontModel.setNsPrefix( "so", NS ); final OntClass classA = ontModel.createClass( NS+"A" ); final OntClass classB = ontModel.createClass( NS+"B" ); final OntClass classC = ontModel.createClass( NS+"C" ); final Individual x = ontModel.createIndividual( NS+"x", classA ); x.addRDFType( classB ); classC.createIndividual( NS+"x" ); ontModel.write( System.out, "RDF/XML" ); }
public static void main2( String[] args ) { final String NS = "http://stackoverflow.com/q/20194409/1281433/"; final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM ); OntProperty p = model.createOntProperty( NS+"PropertyName" ); p.addDomain( model.getOntClass( NS+"ClassName" )); p.addRange( RDFS.Literal ); model.write( System.out, "RDF/XML-ABBREV" ); }
@Before public void setup() { model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); model.read(new StringReader(MODEL_CONTENTS_N3), BASE_URI, "N3"); rdfService = new RDFServiceModel(model); }
@Test public void testBuildingProhibited(){ Model r = ModelFactory.createDefaultModel().read(new StringReader(n3), null, "N3"); OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); m.add( r.listStatements() ); Assert.assertTrue(m.size() > 4); ProhibitedFromSearch pfs = new ProhibitedFromSearch( SEARCH_CONFIG_URI , m); Assert.assertNotNull(pfs.prohibitedClasses); Assert.assertTrue(pfs.prohibitedClasses.size() == 4); Assert.assertTrue(pfs.isClassProhibitedFromSearch(TEST_CLASS)); Assert.assertTrue(!pfs.isClassProhibitedFromSearch("http://someOtherClass.com/test")); }
@Override public void testClassification(final String inputOnt, final String classifiedOnt) final OntModel premise = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); premise.read(inputOnt); premise.prepare(); final Model conclusion = ModelFactory.createDefaultModel(); conclusion.read(classifiedOnt); final StmtIterator stmtIter = conclusion.listStatements(); while (stmtIter.hasNext()) final Statement stmt = stmtIter.nextStatement(); if (stmt.getPredicate().equals(RDFS.subClassOf)) entailed = premise.contains(stmt.getSubject(), ReasonerVocabulary.directSubClassOf, stmt.getObject()); else if (stmt.getPredicate().equals(OWL.equivalentClass)) entailed = premise.contains(stmt); final Individual i = premise.getIndividual(stmt.getSubject().asResource().getURI()); System.out.println(i); System.out.println(stmt.getPredicate());
Model tempModel = ModelFactory.createDefaultModel(); OntModel ontModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM); DatasetWrapper w = getDatasetWrapper(); qexec = QueryExecutionFactory.create(QueryFactory.create(valuesOfProperty), dataset); tempModel = qexec.execConstruct(); ontModel.add(tempModel.listStatements()); Resource ontRes = ontModel.getResource(this.individualURI); StmtIterator sit = ontRes.listProperties(ontRes.getModel().getProperty(propertyURI)); while (sit.hasNext()) { Statement s = sit.nextStatement(); if (!s.getSubject().canAs(OntResource.class) || !s.getObject().canAs(OntResource.class)) { continue; try { subj = new IndividualSDB( s.getSubject().as(OntResource.class).getURI(), this.dwf, datasetMode, webappDaoFactory); } catch (IndividualNotFoundException e) { ObjectProperty op = webappDaoFactory.getObjectPropertyDao().getObjectPropertyByURI(s.getPredicate().getURI()); tempModel.close(); ontModel.close(); dataset.getLock().leaveCriticalSection(); w.close();
String versionInfo = null; Resource ontologyClass = m_source.getProfile().ONTOLOGY(); if (null != ontologyClass) { StmtIterator i = m_source.getBaseModel().listStatements( null, RDF.type, ontologyClass ); if (i.hasNext()) { Resource ont = i.nextStatement().getSubject(); StmtIterator j = m_source.getBaseModel().listStatements( ont, OWL.versionInfo, (RDFNode)null ); if (j.hasNext()) { versionInfo = j.nextStatement().getObject().asLiteral().getLexicalForm(); while (j.hasNext()) { System.err.print( " " ); System.err.print( j.nextStatement().getObject().toString() ); if (i.hasNext()) { System.err.println( "Warning: ambiguous owl:versionInfo - there is more than one owl:Ontology element." ); System.err.println( "Picking first choice: " + ont.getURI() + ". Other choices are:" ); while (i.hasNext()) { System.err.print( " " );
StmtIterator stmtIt = memoryModel.listStatements(); try { for (Statement stmt : stmtIt.toList() ) { boolean sFailed = false; boolean pFailed = false; String pURI = "???"; String oURI = "<bNode>"; if (stmt.getSubject().getURI() != null) { sFailed = checkURI(sURI = stmt.getSubject().getURI()); if (stmt.getPredicate().getURI() != null) { pFailed = checkURI(pURI = stmt.getPredicate().getURI()); if (stmt.getObject().isResource() && ((Resource)stmt.getObject()).getURI() != null) { oFailed = checkURI(oURI = ((Resource)stmt.getObject()).getURI()); stmtIt.close(); System.out.println("jenaOntModel"); int subModelCount = memoryModel.listSubModels().toList().size(); System.out.println("Submodels: "+subModelCount); try { memoryModel.write(out, "TTL"); out.flush(); out.close();
/** Document has an owl:Ontology or daml:Ontology element */ protected String getOntologyElementNamespace() { // if we are using an ontology model, we can get the namespace URI from the ontology element String uri = null; StmtIterator i = m_source.getBaseModel() .listStatements( null, RDF.type, m_source.getProfile().ONTOLOGY() ); if (i.hasNext()) { Resource ont = i.nextStatement().getSubject(); uri = ont.getURI(); // ensure ends with namespace separator char char ch = uri.charAt( uri.length() - 1 ); boolean endsWithNCNameCh = XMLChar.isNCName( ch ); uri = endsWithNCNameCh ? uri + "#" : uri; // check for ambiguous answers if (i.hasNext()) { System.err.println( "Warning: ambiguous default namespace - there is more than one owl:Ontology element." ); System.err.println( "Picking first choice: " + uri + ". Other choices are:" ); while (i.hasNext()) { System.err.print( " " ); System.err.print( i.nextStatement().getString() ); } System.err.println(); System.err.println( "Use the -a option to specify a particular namespace if required." ); } } return uri; }
Individual catalogObj; OntClass c; type = objectToCopy.getPropertyResourceValue(localCatalog.getOntProperty(Constants.RDF_TYPE)); c = localCatalog.createClass(getCatalogTypeForObject(type)); catalogObj = c.createIndividual(uriForCopiedResource); StmtIterator it = objectToCopy.listProperties(); while (it.hasNext()){ Statement s = it.next(); switch (s.getPredicate().getURI()) { case Constants.RDF_TYPE: catalogObj.addProperty(s.getPredicate(), localCatalog.createClass(getCatalogTypeForObject(s.getObject().asResource()))); break; case Constants.WINGS_DATA_PROP_HAS_ARGUMENT_ID: catalogObj.addProperty(s.getPredicate(), s.getObject()); catalogObj.addProperty(localCatalog.createObjectProperty(Constants.RDFS_LABEL), s.getObject()); break; case Constants.WINGS_PROP_COMP_HAS_LOCATION:
/** * Method that creates a unique hash for a template * @param templateInstance WINGS template instance (hashes have to be created from them) * @param wingsTemplateModel Model with the template loaded * @param wingsTaxonomy WINGS component catalog. Necessary to access the component I/O. * @return */ public static String createMD5ForTemplate(Individual templateInstance, OntModel wingsTemplateModel, OntModel wingsTaxonomy){ ArrayList<String> connectionsAndComponents = new ArrayList(); connectionsAndComponents.add(templateInstance.getLocalName()); //to do? if 2 templates are the same, should they be published separately? (Expanded templates are different all the time) StmtIterator propertiesIt = templateInstance.listProperties(wingsTemplateModel.getProperty(Constants.WINGS_PROP_HAS_LINK)); while(propertiesIt.hasNext()){ Statement s = propertiesIt.next(); connectionsAndComponents.add(s.getObject().asResource().getURI()); } //for retrieving components, a query is easier than asking nodes/hasComponent/hasComponentBinding String query = QueriesHashUtils.getComponentsURIsOfTemplate(); //assumption: there will be at least one component of a type ResultSet rs = ModelUtils.queryLocalRepository(query, wingsTemplateModel); while (rs.hasNext()){ QuerySolution qs = rs.next(); String wingsComponentURI = qs.getResource("?compURI").getURI(); Individual wingsComp = wingsTaxonomy.getIndividual(wingsComponentURI); if(wingsComp != null) connectionsAndComponents.add(HashUtils.createMD5ForWINGSComponent(wingsComp)); } return serializeArrayListAsMD5(connectionsAndComponents); }
Set<String> classes = new HashSet<>(); Set<String> individuals = new HashSet<>(); Set<Triple> triples = model.getGraph().find(Triple.ANY).toSet(); ExtendedIterator<OntClass> itClass = model.listNamedClasses(); while (itClass.hasNext()) { classes.add(itClass.next().getURI()); ExtendedIterator<Individual> itIndividuals = model.listIndividuals(); while (itIndividuals.hasNext()) { individuals.add(itIndividuals.next().getURI()); .listDatatypeProperties(); while (itDataProperties.hasNext()) { dataProperties.add(itDataProperties.next().getURI()); .listObjectProperties(); while (itObjectProperties.hasNext()) { objectProperties.add(itObjectProperties.next().getURI()); && !oUri.equals(OWL.Thing.getURI())) { model.getResource(oUri).addProperty(RDF.type,OWL.Class); classes.add(oUri); changes++; } else if (model.getResource(oUri).isLiteral()) { model.createDatatypeProperty(pUri); dataProperties.add(pUri);
/** * Test for a reported bug in delete */ public void testDeleteBug() { Model modelo = ModelFactory.createDefaultModel(); modelo.read("file:testing/reasoners/bugs/deleteBug.owl"); OntModel modeloOnt = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_RULE_INF, modelo ); Individual indi = modeloOnt.getIndividual("http://decsai.ugr.es/~ontoserver/bacarex2.owl#JS"); indi.remove(); ClosableIterator<Statement> it = modeloOnt.listStatements(indi, null, (RDFNode) null); boolean ok = ! it.hasNext(); it.close(); assertTrue(ok); }
model.read(_base + "anon_inverse.owl"); model.prepare(); final OntClass C = model.getOntClass(ns + "C"); final OntClass D = model.getOntClass(ns + "D"); final ObjectProperty r = model.getObjectProperty(ns + "r"); final Resource invR = posit.createResource(); invR.addProperty(OWL.inverseOf, r); final Resource restr = posit.createResource(); restr.addProperty(OWL.onProperty, invR); restr.addProperty(OWL.someValuesFrom, D); assertTrue(model.listStatements(C, RDFS.subClassOf, restr, posit).hasNext()); assertTrue(model.contains(model.getProperty(ns + "functionalP"), RDF.type, OWL.InverseFunctionalProperty)); assertTrue(model.contains(model.getProperty(ns + "inverseFunctionalP"), RDF.type, OWL.FunctionalProperty)); assertTrue(model.contains(model.getProperty(ns + "transitiveP"), RDF.type, OWL.TransitiveProperty)); assertTrue(model.contains(model.getProperty(ns + "symmetricP"), RDF.type, OWL.SymmetricProperty)); assertTrue(model.contains(model.getProperty(ns + "reflexiveP"), RDF.type, OWL2.ReflexiveProperty)); assertTrue(model.contains(model.getProperty(ns + "irreflexiveP"), RDF.type, OWL2.IrreflexiveProperty));
/** * Cycle bug in transitive reasoner */ public void testTransitiveCycleBug() { Model m = FileManager.get().loadModel( "file:testing/reasoners/bugs/unbroken.n3" ); OntModel om = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM_TRANS_INF, m ); OntClass rootClass = om.getOntClass( RDFS.Resource.getURI() ); Resource c = m.getResource("c"); Set<OntClass> direct = rootClass.listSubClasses( true ).toSet(); assertFalse( direct.contains( c ) ); } /**
public void testOwlThingNothingClass() { OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); Resource r = OWL.Thing.inModel( m ); OntClass thingClass = r.as( OntClass.class ); assertNotNull( thingClass ); r = OWL.Nothing.inModel( m ); OntClass nothingClass = r.as( OntClass.class ); assertNotNull( nothingClass ); OntClass c = m.getOntClass( OWL.Thing.getURI() ); assertNotNull( c ); assertEquals( c, OWL.Thing ); c = m.getOntClass( OWL.Nothing.getURI() ); assertNotNull( c ); assertEquals( c, OWL.Nothing ); }