0

I'm trying to create a individual (instance) using Jena with the method below:

public void createInstance(String name) {
        String NS = ontology.getNsPrefixURI("http://james.miranda.br/Onto");
        OntClass class = ontology.createClass(NS + "Requisito");
        Individual instance = class.createIndividual(NS + name);
        System.out.println("Instance created:" + instance.getURI());
    }

ontology is a OntModel instance based on this ontology (some terms are in Portuguese). This method is not working, because the getNsPrefixURI is returning null.

When I iterate over the classes using the code below:

ExtendedIterator<OntClass> classIterator = ontology.listClasses(); 
        while (classIterator.hasNext()) { 
            OntClass ontClass = classIterator.next(); 
            System.out.println(ontClass.toString()); 
        }

the (partial) result is:

http://james.miranda.br/Onto#Requisito

http://james.miranda.br/Onto#Micro

http://james.miranda.br/Onto#Certo

http://james.miranda.br/Onto#Objetivo

http://james.miranda.br/Onto#Individuo

Using getNsPrefixURI("") I have the NS http://www.w3.org/2002/07/owl and my method does not work too. I was looking for how to define the base uri here in SO, but the solution did not work in my case.

Trying to get the all the namespaces, I used the code:

Map<String,String> list = ontology.getNsPrefixMap();
System.out.println(list.toString());

The result is: {=http://www.w3.org/2002/07/owl#, xsd=http://www.w3.org/2001/XMLSchema#, rdfs=http://www.w3.org/2000/01/rdf-schema#, owl=http://www.w3.org/2002/07/owl#, rdf=http://www.w3.org/1999/02/22-rdf-syntax-ns#}.

I didn't receive the prefix for "http://james.miranda.br/Onto". Should it be declared anywhere?

Are there anything wrong with my code?

Community
  • 1
  • 1
James
  • 1,653
  • 2
  • 31
  • 60
  • In your ontology I don't see a prefix declaration for http://james.miranda.br/Onto. You have defined the ontology IRI, but that's not a namespace declaration for http://james.miranda.br/Onto. Namespace in RDFXML are defined by `xmlns:PREFIX_NAME=PREFIX_URI...` as you can see from the others like `rdfs`, `owl`, etc. – UninformedUser Sep 20 '16 at 09:45
  • @AKSW Thanks for your comment, but let me ask, should I replace the `rdf:about="http://james.miranda.br/Onto"` in each tag for anything? – James Sep 20 '16 at 12:09
  • I don't exactly know what you want to achieve. – UninformedUser Sep 20 '16 at 12:17
  • And I'm wondering why you write RDFXML manually, as it's mostly there for tools. It's much easier to use TURTLE instead as long as you only have RDF/RDFS data. – UninformedUser Sep 20 '16 at 12:18
  • @AKSW I created the ontology using Protégé and saved it as "RDF/XML". Saving it as TURTLE serialization is better? I'm trying to create some individuals and reasoning on them. I tried to use "OWL/XML" before, but its not work with Jena. – James Sep 20 '16 at 12:49
  • Ok, I see. What means better? Jena can read both formats, Turtle and RDF/XML. I don't know how Protege adds the prefixes, maybe there is an option to add the one you need. – UninformedUser Sep 21 '16 at 08:11
  • On the best/worst format, I'm new on SemWeb so I'm sorry for any mistake, but by my understanding, when I save my file using RDF rather than OWL, I lose the possibility of some inferences ([here](http://stackoverflow.com/a/1813585/3943162)). The same occurs when I save it in TURTLE serialization? Anyway, in this meanwhile I'll try to save it in TURTLE and also to include the base URI in Protégé. Thanks for all your help – James Sep 22 '16 at 03:17
  • 1
    OWL can be serialized in formats like RDF/XML, Turtle, N-Triples, OWL Functional syntax, etc. . This means that nothing is lost, otherwise it wouldn't be a valid serialization format and obviously not supported by an OWL ontology editor like Protege. – UninformedUser Sep 22 '16 at 14:11

1 Answers1

1

The documentation of getNsPrefixURI says "Get the URI bound to a specific prefix, null if there isn't one." i.e. prefix in, URI out.

"http://james.miranda.br/Onto" is not a legal prefix.

There is no namespace prefix for http://james.miranda.br/Onto.

This does not set a prefix:

<Ontology rdf:about="http://james.miranda.br/Onto">

We would expect to see:

xmlns:onto="http://james.miranda.br/Onto#"
AndyS
  • 16,345
  • 17
  • 21
  • Thanks for your response. After include it in my ontology, should I include the `onto` prefix in each tag? Are there any way to include it using Protégé? – James Sep 22 '16 at 03:06
  • The xmlns:onto can go along side the other xmlns:????. How that happens on protege, I don't know - I don't use it. – AndyS Sep 23 '16 at 13:15
  • I also don't know since I'm starting to use it just now. I'll accept the advice of @AKSW and save it in another serialization format (TURTLE). Thank you for your response anyway. – James Sep 23 '16 at 14:35