OWLAPI set DataProperty strict value on subclass assertion - java

I can use this code to Create the restricted data range :
public void testDatatypeRestriction() throws OWLException {
OWLOntologyManager m = create();
OWLOntology o = m.createOntology(EXAMPLE_IRI);
// Adults have an age greater than 18.
OWLDataProperty hasAge = df.getOWLDataProperty(IRI.create(EXAMPLE_IRI + "hasAge"));
// Create the restricted data range by applying the facet restriction
// with a value of 18 to int
OWLDataRange greaterThan18 = df.getOWLDatatypeRestriction(df.getIntegerOWLDatatype(), OWLFacet.MIN_INCLUSIVE, df
.getOWLLiteral(18));
// Now we can use this in our datatype restriction on hasAge
OWLClassExpression adultDefinition = df.getOWLDataSomeValuesFrom(hasAge, greaterThan18);
OWLClass adult = df.getOWLClass(IRI.create(EXAMPLE_IRI + "#Adult"));
OWLSubClassOfAxiom ax = df.getOWLSubClassOfAxiom(adult, adultDefinition);
m.applyChange(new AddAxiom(o, ax));
}
now I need to know how to programmatically define a class as subclass of a dataproperty assertion with strict value :
Alex:owlClass
HasAge:DataProperty
Alex HasAge value 35
P.S: both answers are correct.but #ssz answer is more closer answer to the question and it works with both hermit and pellet reasoner.
it seems there is some issue with pellet reasoning on axioms like hasAge xsd:integer [>=20 , <=20 ] . see here

The screenshot appears to show 8.4.3 Literal Value Restriction, see also OWL2 Quick Guide, Data Property Restrictions.
Programmatically this looks like following:
String EXAMPLE_IRI = "https://stackoverflow.com/questions/65793751#";
OWLDataFactory df = m.getOWLDataFactory();
OWLDataProperty hasAge = df.getOWLDataProperty(IRI.create(EXAMPLE_IRI + "hasAge"));
OWLClass adult = df.getOWLClass(IRI.create(EXAMPLE_IRI + "Adult"));
// SubClassOf(:Adult DataHasValue(:hasAge "35"^^xsd:integer))
OWLAxiom ax = df.getOWLSubClassOfAxiom(adult, df.getOWLDataHasValue(hasAge, df.getOWLLiteral(35)));
System.out.println(ax);
System.out.println("---------------");
OWLOntology ont = m.createOntology();
ont.add(ax);
ont.saveOntology(new ManchesterSyntaxDocumentFormat(), System.out);

Related

Conformal + Distortion Coordinate Transform

I am trying to convert geometry coordinate reference system of some land parcels in Australia into another using java geotools api and opengis libraries. i.e WSG84 (EPSG:4326) to GDA2020 / MGA zone 50 (EPSG:7850), or WSG84 (EPSG:4326) to GDA2020 / PCG2020 (EPSG:8031). By now the converted coordinates have some deviation from the original coordinates it suppose to be. Now my requirement is to perform Conformal + Distortion transformation explained in this article which is more accurate - https://www.icsm.gov.au/datum/gda-transformation-products-and-tools/transformation-grids
However I am not very sure on what changes I need for the current code to do above. I did some google for find some code examples but couldn't find what I wanted. Any help would be highly appreciated.
Query query = new Query();
DataStore dataStore = getDataStore();
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(featureTypeName);
query.setFilter(ECQL.toFilter("land_id='" + landID + "'"));
FeatureCollection collection = source.getFeatures(query);
FeatureIterator iterator = collection.features();
CoordinateReferenceSystem sourceCRS = collection.getSchema().getCoordinateReferenceSystem(); // WSG 84
List<Geometry> geometryList = new ArrayList<Geometry>();
CoordinateReferenceSystem targetCRS = getCRS(targetEPSGCode); // GDA2020 / MGA zone 50
while (iterator.hasNext())
{
Feature feature = (Feature) iterator.next();
GeometryAttribute geom = feature.getDefaultGeometryProperty();
Object geomVal = geom.getValue();
if (geomVal instanceof Geometry)
{
MathTransform mathTransform = CRS.findMathTransform(sourceCRS, targetCRS);
Geometry transformedGeometry = JTS.transform((Geometry) geomVal, mathTransform);
geometryList.add(transformedGeometry);
}
}
// Use geometryList for further stuff
You will need to download the required NTv2 grid file and save it to your projects src/main/resources/org/geotools/referencing/factory/gridshift folder and recompile the project. There is more discussion and some example code to help you check it is being picked up in this answer over on https://gis.stackexchange.com

OWL replace object and data property value

I've written this code to replace object property value:
public void changeObjectPropertyValue(String ind, String propertyFragment, String newValueFragment) {
OWLNamedIndividual individualToReplaceValueOn = factory.getOWLNamedIndividual(prefix + ind);
OWLNamedIndividual newValueInd = factory.getOWLNamedIndividual(prefix + newValueFragment);
OWLObjectProperty theObjectProperty = factory.getOWLObjectProperty(prefix + propertyFragment);
OWLIndividual theOldValue = EntitySearcher.getObjectPropertyValues(individualToReplaceValueOn, theObjectProperty, ont).findFirst().get();
OWLAxiom oldAxiom = factory.getOWLObjectPropertyAssertionAxiom(
theObjectProperty,
individualToReplaceValueOn,
theOldValue);
OWLAxiom newAxiom = factory.getOWLObjectPropertyAssertionAxiom(
theObjectProperty,
individualToReplaceValueOn,
newValueInd);
List<OWLOntologyChange> changes = new Vector<OWLOntologyChange>();
changes.add(new RemoveAxiom(ont, oldAxiom));
changes.add(new AddAxiom(ont, newAxiom));
manager.applyChanges(changes);
}
I want to know if this is a correct way to replace value and if there is a method in OWLAPI library to do this?
This is correct - and the only way to do this sort of changes in OWL API. Axioms are immutable objects, so there is no other way to modify an axiom than recreating it and changing the parts that need modifying in the process.

How can I retrieve <oboInOwl:hasExactSynonym> from every <rdf:Description> using OWL API

I am new to OWL API hence I am facing some issues for retrieving data.
Suppose I have the following data:
<rdf:Description rdf:about="http://purl.obolibrary.org/obo/GO_0044297">
<oboInOwl:creation_date>"2010-02-05T10:37:16Z"</oboInOwl:creation_date>
<obo:IAO_0000115>"The portion of a cell bearing surface projections such as axons, dendrites, cilia, or flagella that includes the nucleus, but excludes all cell projections."</obo:IAO_0000115>
<oboInOwl:hasOBONamespace>"cellular_component"</oboInOwl:hasOBONamespace>
<oboInOwl:hasDbXref>"Wikipedia:Cell_body"</oboInOwl:hasDbXref>
<oboInOwl:hasDbXref>"FMA:67301"</oboInOwl:hasDbXref>
<oboInOwl:hasExactSynonym>"cell soma"</oboInOwl:hasExactSynonym>
<rdfs:label>"cell body"</rdfs:label>
<rdfs:subClassOf>http://purl.obolibrary.org/obo/GO_0044464</rdfs:subClassOf>
<oboInOwl:hasDbXref>"FBbt:00005107"</oboInOwl:hasDbXref>
<rdf:type>http://www.w3.org/2002/07/owl#Class</rdf:type>
<oboInOwl:id>"GO:0044297"</oboInOwl:id>
<rdfs:comment>"Note that 'cell body' and 'cell soma' are not used in the literature for cells that lack projections, nor for some cells (e.g. yeast with mating projections) that do have projections."</rdfs:comment>
<oboInOwl:created_by>"xyz"</oboInOwl:created_by>
<oboInOwl:inSubset>http://purl.obolibrary.org/obo/go#goslim_pir</oboInOwl:inSubset>
</rdf:Description>
<rdf:Description rdf:about="http://purl.obolibrary.org/obo/GO_0071509">
<oboInOwl:hasRelatedSynonym>"activation of MAPKK activity involved in mating response"</oboInOwl:hasRelatedSynonym>
<rdfs:subClassOf>http://purl.obolibrary.org/obo/GO_0090028</rdfs:subClassOf>
<oboInOwl:hasOBONamespace>"biological_process"</oboInOwl:hasOBONamespace>
<oboInOwl:hasExactSynonym>"activation of MAP kinase kinase activity during conjugation with cellular fusion"</oboInOwl:hasExactSynonym>
<oboInOwl:hasExactSynonym>"conjugation with cellular fusion, activation of MAPKK activity"</oboInOwl:hasExactSynonym>
<rdfs:label>"activation of MAPKK activity involved in conjugation with cellular fusion"</rdfs:label>
<rdf:type>http://www.w3.org/2002/07/owl#Class</rdf:type>
<oboInOwl:id>"GO:0071509"</oboInOwl:id>
<oboInOwl:creation_date>"2010-01-05T02:09:58Z"</oboInOwl:creation_date>
<oboInOwl:hasExactSynonym>"conjugation with cellular fusion, activation of MAP kinase kinase activity"</oboInOwl:hasExactSynonym>
<oboInOwl:created_by>"midori"</oboInOwl:created_by>
<obo:IAO_0000115>"Any process that initiates the activity of the inactive enzyme MAP kinase kinase in the context of conjugation with cellular fusion."</obo:IAO_0000115>
<rdfs:subClassOf>http://purl.obolibrary.org/obo/GO_0000186</rdfs:subClassOf>
</rdf:Description>
For each of the "rdf:description" I want to retrieve its corresponding "rdf:label", "oboInOwl:hasExactSynonym" and "rdfs:subClassOf" using OWL API in java.
So far I can get all labels but not the linkages as to which label is for which description.
Currently my code looks like:
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology pizzaOntology = manager.loadOntologyFromOntologyDocument(f);
Set<OWLOntology> allOntologies = manager.getImportsClosure(pizzaOntology);
//System.out.println(allOntologies);
OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(pizzaOntology);
//pizzaOntology
OWLDataFactory factory = manager.getOWLDataFactory();
Set<OWLAxiom> axiom = pizzaOntology.getAxioms();
for (OWLAxiom o : axiom) {
AxiomType<?> at = o.getAxiomType();
//System.out.println("Annotation type is "+at+" for "+o);
if (at == AxiomType.ANNOTATION_ASSERTION) {
OWLAnnotationAssertionAxiom ax = (OWLAnnotationAssertionAxiom) o;
//Check if the axiom is a label and write to file
if(ax.getProperty().toString().contains("hasExactSynonym"))
System.out.println("Data is "+ax.getValue().toString());
if (ax.getProperty().equals(factory.getRDFSLabel())) {
String label = ax.getValue().toString();
label = label.toLowerCase();
label = label.replaceAll("[^\\p{L}\\p{Nd}]+", " ");
allLabels.add(label);
}
}
}
Can someone help me with some ideas about this?
This should help:
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology pizzaOntology = manager.loadOntologyFromOntologyDocument(f);
Set<OWLOntology> allOntologies = manager.getImportsClosure(pizzaOntology);
// System.out.println(allOntologies);
OWLReasonerFactory reasonerFactory = new ElkReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(pizzaOntology);
// pizzaOntology
OWLDataFactory factory = manager.getOWLDataFactory();
Map<OWLAnnotationSubject, String> allLabels = new HashMap<>();
Map<OWLAnnotationSubject, String> allExactSynonyms = new HashMap<>();
for (OWLAnnotationAssertionAxiom ax : pizzaOntology
.getAxioms(AxiomType.ANNOTATION_ASSERTION)) {
// Check if the axiom is a label and write to file
OWLAnnotationSubject subject = ax.getSubject();
if (ax.getProperty().toString().contains("hasExactSynonym")) {
allExactSynonyms.put(subject, ax.getValue().toString());
}
if (ax.getProperty().equals(factory.getRDFSLabel())) {
String label = ax.getValue().toString();
label = label.toLowerCase();
label = label.replaceAll("[^\\p{L}\\p{Nd}]+", " ");
allLabels.put(subject, label);
}
}
The two maps hold the relation between an IRI (the annotation subject in this case is always an IRI - annotation are attached to IRIs of classes, not to classes themselves) and the value of a property. If you match the label value with the value of a property for an IRI, you can find the other value through the IRI in the map.

Printing Superclasses of Pizza ontology using OWL-API and Hermit

I try to print the Superclasses of standard Pizza ontology downloaded from here . I am using OWL API 3.4.3 and Hermit 1.3.8.1 (reasoner).
The following code snippet is used to print the required Superclasses of class "Food".
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
IRI ontologyIRI =IRI.create("file:/D:/pizza.owl.xml");
//IRI ontologyIRI =IRI.create("http://www.co-ode.org/ontologies/pizza/pizza.owl");
OWLOntology ontology = manager.loadOntology(ontologyIRI);
OWLReasoner reasoner = new Reasoner.ReasonerFactory().createReasoner(ontology);
OWLDataFactory df = manager.getOWLDataFactory();
try{
reasoner.precomputeInferences(InferenceType.CLASS_ASSERTIONS);
//following Lines are to see super classes of Container
OWLClass clsA = df.getOWLClass(IRI.create(ontologyIRI + "#Food"));
Set<OWLClassExpression> superClasses = clsA.getSuperClasses(ontology);
System.out.println("in TRY 1");
//System.out.println("Hello World\n"+superClasses.iterator().toString());
for (OWLClassExpression g : superClasses) {
System.out.println("The superclasses are:"+g);
}
}
catch (Exception e) {
e.printStackTrace();
}
I do not get any compilation error. The result is in TRY 1. The content inside for loop has not been printed.
In protege 5.0, I have seen two Superclasses of Food class; namely DomainConcept and owl:Thing. Why these two names has not been printed by the program?
Where am I doing wrong?
Thanks for any kind of help.
Your ontology IRI is a local file name. When you use it to obtain a class IRI, you're getting a different IRI from the one actually used in the ontology. Check what IRI the class actually has and that should fix your issue.

Java ini4j - reading multiple options from .ini file

I'm trying to read multiple values using ini4j, the docs say it should be possible with the Options class.
Here is my sample .ini file (./dwarfs.ini)
[dopey]
age = 23
fortuneNumber = 11
fortuneNumber = 33
fortuneNumber = 55
here is the code to read it:
Ini ini = new Ini();
Config conf = new Config();
conf.setMultiOption(true);
ini.setConfig(conf);
ini.load(new FileReader("./dwarfs.ini"));
but fortuneNumber property is just 55 after reading and I'd want it to be an array or a list, anything.
The web presence for the ini4j project contains (among others) very simple tutorials. One of these tutorials explains, how to retrieve multiple values. How could you expect to get a list or an array, when using a fetch method that returns a single reference? Look at the API!
In the tutorial, there is a part explaining multi values:
[ini4j] library introduces MultiMap interface, which is extends normal
Map, but allows multiply values per keys. You can simply index values
for a given key, similar to indexed properties in JavaBeans api.
There is also an example:
String n1 = sneezy.get("fortuneNumber", 0); // = 11
String n2 = sneezy.get("fortuneNumber", 1); // = 22
String n3 = sneezy.get("fortuneNumber", 2); // = 33
String n4 = sneezy.get("fortuneNumber", 3); // = 44
In this example, sneezy is a Section, but it should also work with an Ini.
And just to make it complete: An Ini also knows a method List<V> getAll(Object key).
to handle mutliple properties in section use following code :
Ini oINI = new Wini();
Config conf = new Config();
conf.setMultiOption(true);
oINI.setConfig(conf);
oINI.load(new File("....../myfile.ini"));
Do not directly open the INI file in the class creation, set options before, otherwise the options won't be used and by default MultiOption si set to "false".
Have searched a while for this solution.
you need setConfig first, and then load file.
you need use List to handle multiple same values.
here the example:
Ini ini = new Wini();
Config config = new Config();
config.setMultiOption(true);
config.setMultiSection(true);
ini.setConfig(config); //set config
ini.load(new File(filename)); // load AFTER setConfig
for (String sectionName : ini.keySet()) {
List<Section> sectionList = ini.getAll(sectionName); // use List
for (Section section : sectionList) {
for (String optionName : section.keySet()) {
List<String> optionList = section.getAll(optionName);
for (String optionVaule : optionList) {
System.out.printf("%s %s %s\n", sectionName, optionName, optionVaule);
}
}
}
}
here the out put:
dopey age 23
dopey fortuneNumber 11
dopey fortuneNumber 33
dopey fortuneNumber 55

Categories

Resources