Simple code for load an ontology and add instances but errors.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple code for load an ontology and add instances but errors.

Reza Moradi rad
Hi everyone,
I wrote a code for load an ontology and fetch a predefined class and add an instance to this class.
After that i need to save the new version of ontology, here is the whole of code:
but some errors occurred, do you know where they come from?
 
 
import edu.stanford.smi.protege.exception.OntologyLoadException;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.semanticweb.owlapi.model.*;
import com.hp.hpl.jena.util.FileUtils;
import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
 
public class NewClass {
    public static void main(String[] args){
        try {
            String base1 = "
file:///c:/tmp/SAOnew.owl";
            String ontologyURI = "
file:///c:/tmp/Ontology/SAO.owl";
            JenaOWLModel Model = ProtegeOWL.createJenaOWLModelFromURI(ontologyURI);
            // Fetch a predefined class
            OWLNamedClass MyClass = owlModel.getOWLNamedClass("MyClass");
          
  // Add an instance to a specific class
34:       OWLIndividual myTestOWLIndivid = MyClass.createOWLIndividual("myIndividual");
            // save the new version of ontology
            Collection errors = new ArrayList();
            owlModel.save(new File("owlModel.owl").toURI(), FileUtils.langXMLAbbrev, errors);
            System.out.println("File saved with " + errors.size() + " errors.");
                 } catch (OntologyLoadException ex) {
                 Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
       }
      
    }
 
Exception in thread "main" java.lang.NullPointerException
        at NewClass.main(NewClass.java:34)
Java Result: 1

--
By : Reza Moradi Rad

_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03
Reply | Threaded
Open this post in threaded view
|

Re: Simple code for load an ontology and add instances but errors.

Olivier Dameron
On 10/05/2010 04:17 PM, Reza Moradi rad wrote:

> OWLNamedClass MyClass = owlModel.getOWLNamedClass("MyClass");
>    // Add an instance to a specific class
> 34:   OWLIndividual myTestOWLIndivid =
> MyClass.createOWLIndividual("myIndividual");
> // save the new version of ontology
>              Collection errors = new ArrayList();
>              owlModel.save(new File("owlModel.owl").toURI(),
> FileUtils.langXMLAbbrev, errors);
>              System.out.println("File saved with " + errors.size() + "
> errors.");
>                   } catch (OntologyLoadException ex) {
>
>   Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
>          }
>         }
>
>      }
> Exception in thread "main" java.lang.NullPointerException
> at NewClass.main(NewClass.java:34)
> Java Result: 1

MyClass is an empty object (i.e. the system could not find a class named
MyClass. Make sure that there is no typo (names are case-sensitive) and
maybe also include the complete URI

I hope this helps
olivier
_______________________________________________
protege-owl mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-owl

Instructions for unsubscribing: http://protege.stanford.edu/doc/faq.html#01a.03