views:

6

answers:

0

Hi,

I'm writing a code to add individuals to a native repository created in the code. I don't get any errors but when I query for individuals I don't get any records. Is there something I am missing here. I create a native repository, add an owl file 'sudoku.owl' to it and then try to add an individual for one of the classes in the ontology. Here is the code.

public class OWLRepositoryManager {

private Repository therepository;
private String baseURI;

public void createConnection(){

    File dataDir = new File("E:\\nativeStorage");
    String indexes = "spoc,posc,cosp";
    therepository = new SailRepository(new NativeStore(dataDir, indexes));
    try {
        therepository.initialize();
    } catch (RepositoryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public HashMap<String,String> populateClassURIMap() {

    try {
           RepositoryConnection con = therepository.getConnection();
           HashMap<String,String> classURIMap = new HashMap<String,String>();
           String className = null;
           String uri = null;
           try {
              String queryString = "SELECT C FROM {C} rdf:type {owl:Class}";
              TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SERQL, queryString);
              TupleQueryResult result = tupleQuery.evaluate();
              try {
                  while (result.hasNext()) {
                        BindingSet b = result.next();
                        Set<String> names = b.getBindingNames();
                        for (Object n : names) {
                            uri = b.getValue((String) n).stringValue();
                            className = uri.split("#")[1];
                            classURIMap.put(className,uri);
                            //System.out.println("uri:" + uri + " className:" + className);
                        }
                    }
                  baseURI = uri.split("#")[0];
              }
              finally {
                 result.close();
              }               
           }
           finally {
              con.close();
           }
           return classURIMap;
        }
        catch (OpenRDFException e) {
           // handle exception
        }
    return null;
}

public void createIndividual(URI individualName, URI className ){
    try {
        RepositoryConnection con = therepository.getConnection();
        try{
            System.out.println("adding individual");
            con.add(individualName,RDF.TYPE,className);
            con.commit();
        }finally{
            con.close();
        }
    }catch (RepositoryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

public URI getURI(String uriString, boolean append){

    ValueFactory f = therepository.getValueFactory();
    URI newURI = null;
    if(append)
        newURI = f.createURI(baseURI + "/" + uriString);
    else
        newURI = f.createURI(uriString);

    return newURI;      
}

public void addFile(String filepath,  RDFFormat format) {
    try {
        RepositoryConnection con = therepository.getConnection();
        try {
            con.add(new File(filepath),"", format);
        } finally {
            con.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
} 

public void testQuery(String qs){
    System.out.println("testQuery: " + qs);     
    try {
           RepositoryConnection con = therepository.getConnection();
            try {
              TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SERQL, qs);
              TupleQueryResult result = tupleQuery.evaluate();
              try {
                  while (result.hasNext()) {
                        BindingSet b = result.next();
                        Set<String> names = b.getBindingNames();
                        for (Object n : names) {
                            System.out.println(b.getValue((String)n));
                        }
                    }                     
              }
              finally {
                 result.close();
              }               
           }
           finally {
              con.close();
           }              
        }
        catch (OpenRDFException e) {
           // handle exception
        }   
}

public static void main(String [] args){
    OWLRepositoryManager m = new OWLRepositoryManager();
    m.createConnection();
    //m.addFile("E:\\MAMContext.owl",RDFFormat.RDFXML);

    m.addFile("E:\\masters\\project development\\ontologies\\sudoku.owl", RDFFormat.RDFXML);
    HashMap<String,String> uriclassMap = m.populateClassURIMap();

    String individualName = "column";
    String  classURIString = uriclassMap.get(individualName);
    URI individualURI = m.getURI("col99",true);
    URI classURI = m.getURI(classURIString,false);
    //System.out.println(classURI);
    //System.out.println(individualURI);

    m.createIndividual(individualURI,classURI);
    String qs = "SELECT C FROM {C} rdf:type {owl:NamedIndividual}";
    m.testQuery(qs);
}

}