I'm trying to write an OWLObjectPropertyExpression on OWL Ontology object. If I had an OWL Class I use something like the following:
OWLOntologyManager managerWriter = OWLManager.createOWLOntologyManager();
OWLOntology ontoWrite=managerWriter.createOntology();
OWLDataFactory factory = manager.getOWLDataFactory();
managerWriter.addAxiom(ontoWrite,factory.getOWLDeclarationAxiom(factory.getOWLClass((cl.getIRI()))));
But what should I write if I want to write an OWLObjectPropertyExpression ?
Thanks in advance !.
The following code snippet illustrate an example of usage and creation of an OWL expression using the OWL API (taken and adapted from there):
//OWL Expression we would like to create:
//in OWL Functional syntax: ObjectIntersectionOf(A ObjectSomeValuesFrom(R B))
//in Manchester syntax: A and R some B
PrefixManager pm = new DefaultPrefixManager("http://example.org/");
OWLClass A = factory.getOWLClass(":A", pm);
OWLObjectProperty R = factory.getOWLObjectProperty(":R", pm);
OWLClass B = factory.getOWLClass(":B", pm);
//The expression
OWLClassExpression expression =
factory.getOWLObjectIntersectionOf(A, factory.getOWLObjectSomeValuesFrom(R, B));
//Create a class in order to use the expression
OWLClass C = factory.getOWLClass(":C", pm);
// Declare an equivalentClass axiom
//Just there to show how an example on how to use the expression
OWLAxiom definition = factory.getOWLEquivalentClassesAxiom(C, expression);
manager.addAxiom(ontology, definition);
Related
I am reading the attached university-bench ontology file (which I generated from UBA1.7 lubm) in java using owlapi. But it is not reading any axiom like subclass etc. And it is also not giving me any error. Can anyone tell me what I am doing wrong. The below code I used to retrieve the subclass axioms from this ontology but it return me nothing/ blank output. I wanted to output subclass, disjoint class, sub property, disjoint property, anonymous superclass axioms. but currently I am unable to get anything out from the ontology.
When I use the ontology which is created by me using protege. The below code works fine. But when i try to execute the ontology generated from UBA1.7 it gives me nothing.
public static void axioms(File ontology) throws OWLOntologyCreationException {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(ontology);
OWLDataFactory df = manager.getOWLDataFactory();
for (final OWLSubClassOfAxiom subClasse : ontology.getAxioms(AxiomType.SUBCLASS_OF))
{
if (subClasse.getSuperClass() instanceof OWLClass && subClasse.getSubClass() instanceof OWLClass)
{
System.out.println(subClasse.getSubClass() + " extends " + subClasse.getSuperClass());
}
}
}
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);
I'm trying to create an sql sentence using querydsl. What I'm trying to get is:
SELECT P.KEY, COUNT(P.VALUE)
FROM RESOURCES R JOIN PROPERTIES P ON R.ID = P.ID
WHERE P.KEY = "key" AND p.VALUE = "value"
GROUP BY P.VALUE;
I've tried to write some querydsl code:
String s = queryFactory
.query()
.from(QResource.resource)
.join(QProperty.property)
.where(QResource.resource.properties.any().key.eq("key").and(QResource.resource.properties.any().value.eq("value")))
.groupBy(QProperty.property.value)
.select(QProperty.property.key, QProperty.property.value.count())
.toString();
I'm guessing it can be simplified and by other hand I don't quite see if it's well querydsl-coded.
Any ideas?
A more simplified version would be:
QResource r = QResource.resource;
QProperty p = QProperty.property;
queryFactory
.select(p.key, p.value.count())
.from(r)
.join(p).on(r.id.eq(p.id))
.where(p.key.eq("key"), p.value.eq("value"))
.groupBy(p.value)
.fetchOne();
Adding to #natros answer, Boolean operators can be used for 'and' logic
QResource r = QResource.resource;
QProperty p = QProperty.property;
queryFactory
.select(p.key, p.value.count())
.from(r)
.join(p).on(r.id.eq(p.id))
.where(p.key.eq("key").and(p.value.eq("value")))
.groupBy(p.value)
.fetchOne();
Boolean builders can be also used for complex logics
BooleanBuilder builder = new BooleanBuilder();
builder.and(p.key.eq("key"));
builder.and(p.value.eq("value"));
queryFactory
.select(p.key, p.value.count())
.from(r)
.join(p).on(r.id.eq(p.id))
.where(builder)
.groupBy(p.value)
.fetchOne();
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.
I am trying to create and store an ontology file in functional format using OWL API:
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.createOntology();
OWLDataFactory factory = manager.getOWLDataFactory();
PrefixManager pm = new FunctionalSyntaxDocumentFormat();
pm.setDefaultPrefix(" :");
OWLClass item = factory.getOWLClass(IRI.create("item"), pm);
manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(item));
manager.saveOntology(ontology, new FunctionalSyntaxDocumentFormat(), new FileOutputStream("FileName"))
The result in the saved file for this axiom is this:
Declaration(Class(< :item>))
How do I get rid of the < > brackets around entities? It happens to all entities that I create, and it is preventing my file from being parsed correctly.
Two issues: there should not be a space in the default prefix, and the prefix manager you are setting the prefix on must be the same used in the call to saveOntology(). You can just pass the first functional document format to the last method in your code.
Edit: After trying to run the code, I think there's a bit of a bug in the OWL API. It is necessary to set the format on the manager for the prefixes to be picked up properly. That should not be necessary. However, there's a workaround.
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.createOntology();
OWLDataFactory factory = manager.getOWLDataFactory();
FunctionalSyntaxDocumentFormat pm = new FunctionalSyntaxDocumentFormat();
pm.setPrefix(":", "http://test.owl/test#");
manager.setOntologyFormat(ontology, pm);
OWLClass item = factory.getOWLClass("item", pm);
manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(item));
manager.saveOntology(ontology, System.out);