OWLAPI data types

柔情痞子 提交于 2019-12-13 06:12:36

问题


I need to get the OWLDatatype or a OWL2Datatype from the corresponding string.

For example, given xsd:string I would like to get the corresponding enum constant OWL2Datatype.XSD_STRING.

I tried in the following ways:

 - OWL2Datatype strDT = OWL2Datatype.valueOf("xsd:string")

 - OWL2Datatype strDT = OWL2Datatype.valueOf("string")

 - OWL2Datatype owl2dt = OWL2Datatype.valueOf(OWL2Datatype.XSD_STRING.getIRI().toString());

but they all fail throwing an exception:

java.lang.IllegalArgumentException: No enum const class org.semanticweb.owlapi.vocab.OWL2Datatype.string (this part changes according to what I passed - see above).

question number 1 The signature of the method valueOf is: OWLDatatype.valueOf(java.lang.String name), with the description "Returns the enum constant of this type with the specified name". How can I get the names of the OWL2Datatypes?

question number 2 I decided to switch to the class OWLDatatype and access the 'basic' data types using the methods of the class OWLDataFactory. The class offers methods for creating many different types (e.g., getDoubleOWLDatatype), except for the one corresponding to xsd:string. How can I create an OWLDatatype that corresponds to the xsd:string name?


回答1:


For question number 1, the signature is misleading: while it looks like valueOf() is defined by OWL2Datatype, it is actually defined for Enum - all java enumerations get it, and the string that will work for its argument is "XSD_STRING", i.e., the actual name of the variable in the enumeration.

For question number 2, the problem is that xsd is a well known namespace abbreviation but it's still arbitrary - in order to translate to the full IRI for the datatype you would need a ShortFormProvider to reverse the mapping. I can see that this would be a useful function to have but I'm not sure that it is offered at the moment. I'll raise an issue for it.

This has been added to the OWLAPI and now there are two methods to do what you need, illustrated in this test:

@Test
public void shouldParseXSDSTRING() {
    // given
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    String s = "xsd:string";
    // when
    XSDVocabulary v = XSDVocabulary.parseShortName(s);
    // then
    assertEquals(XSDVocabulary.STRING, v);
    assertEquals(OWL2Datatype.XSD_STRING.getDatatype(df),
            df.getOWLDatatype(v.getIRI()));
}

So, one way is through OWL2Datatype and an OWLDataFactory:

OWLDatatype string = OWL2Datatype.XSD_STRING.getDatatype(df)

And another is with XSDVocabulary and an OWLDataFactory:

OWLDatatype string = df.getOWLDatatype(XSDVocabulary.parseShortName("xsd:string"));


来源:https://stackoverflow.com/questions/16708737/owlapi-data-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!