0

I am using Apache Jena. I have created the data property, its range as xsd:string and restriction that is added as a superclass to specific (already created) class:

DatatypeProperty dataProperty = model.createDatatypeProperty(baseURI + possibleProperty);
dataProperty.setRange(XSD.xstring);
MaxCardinalityRestriction restriction = model.createMaxCardinalityRestriction(null, dataProperty, 1);
itemClass.addSuperClass(restriction);

When I open the generated ontology in Protege the mentioned restriction looks like:

DataProperty_Name max 1 Literal

My aim is to get it with included data type (that is specified in the range of the data property), e.g. I am expecting:

DataProperty_Name max 1 string

Application that uses the ontology needs to know restriction's data type. Do you maybe know what I need to change in my code to get data type (e.g. string) in the restriction instead of Literal?

Thanks, Darko

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
user1794211
  • 65
  • 1
  • 6
  • 2
    OWL max cardinality is not a restriction on the datatype as you already recognized. What you're after is known as OWL data range, in particular, FacetRestriction: https://www.w3.org/TR/owl2-syntax/#Data_Ranges . Note, the Ontology API is not complete for OWL 2 (at least that's what I read some years ago). I can see an interface `DataRange`, but it has only support for OneOf: https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/ontology/DataRange.html – UninformedUser Oct 08 '16 at 16:13
  • 1
    I think, you should ask this on the Jena mailing list, maybe there is also support for min, max, etc. – UninformedUser Oct 08 '16 at 16:14
  • Thanks, I will also try to get answer on the Jena mailing list. – user1794211 Oct 08 '16 at 21:19

2 Answers2

0

As suggested, I have asked the question on Jena mail list and have got an answer that Jena doesn't support OWL 2 which is where DataRange is defined. That just means there's not convenience functions but you can still achieve the desired affect by working at the RDF level, it's just verbose. Look up the OWL 2 specs to see what RDF triples are needed to represent your desired DataRange, then use the general Model API to assert those triples.

I followed this advice and succeeded to solve my problem with the following code:

MaxCardinalityRestriction restriction = model.createMaxCardinalityRestriction(null, existingDataProperty, 1);                                               
restriction.removeAll(OWL.cardinality);
restriction.addLiteral(OWL2.maxQualifiedCardinality, 1);
restriction.addProperty(OWL2.onDataRange, XSD.xstring);
itemClass.addSuperClass(restriction); 

Thanks, Darko

user1794211
  • 65
  • 1
  • 6
  • 1
    Ok, then I misunderstood your question, sorry for that. I thought you want to add a restriction on the datatype, e.g. `int[>500]` or `string[length>5]`. And this is what's not possible with the Jena Ontology layer. Your task indeed works as you already found out. – UninformedUser Oct 11 '16 at 07:51
0

You may take a look at ONT-API project. There is an analogy of org.apache.jena.ontology.OntModel - com.github.owlcs.ontapi.jena.model.OntModel - but for OWL2 specification (native Jena mode is for OWL1).

Example:

    String uri = "https://stackoverflow.com/questions/39926809";
    String ns = uri + "#";
    OntModel m = OntManagers.createONT().createGraphModel(uri)
            .setNsPrefixes(OntModelFactory.STANDARD)
            .setNsPrefix("test", ns);

    OntDataProperty prop = m.createDataProperty(ns + "prop").addRange(XSD.xstring);
    OntDataRange dt = prop.ranges().findFirst().orElseThrow(AssertionError::new);
    m.createOntClass(ns + "clazz")
            .addSuperClass(m.createDataMaxCardinality(prop, 1, dt));
    m.write(System.out, "ttl");

Output:

@prefix test:  <https://stackoverflow.com/questions/39926809#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

<https://stackoverflow.com/questions/39926809>
        a       owl:Ontology .

test:prop  a        owl:DatatypeProperty ;
        rdfs:range  xsd:string .

test:clazz  a            owl:Class ;
        rdfs:subClassOf  [ a                            owl:Restriction ;
                           owl:maxQualifiedCardinality  "1"^^xsd:nonNegativeInteger ;
                           owl:onDataRange              xsd:string ;
                           owl:onProperty               test:prop
                         ] .
ssz
  • 835
  • 1
  • 6
  • 18