问题
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