Search code examples
javajenaowlprotege

get a property value of an individual jena api


I have my class Alert which contain as individual

Alert_1
Alert_2
Alert_3

and each individual has properties witch contains values for example

Alert_1 : 
      hasanalyser : analyser546
      hastime: 10
      hasdatainfo: difficult

I can now get all individuals but I can not get those (hasanalyser, hastime and hasdatainfo) values

Here is my code and it works. How I can get what I want please?

owlModel = ProtegeOWL.createJenaOWLModelFromURI("file:///D:/base_connaissance.owl");

OntModel model = owlModel.getOntModel();

OWLNamedClass theAlert = owlModel.getOWLNamedClass("Alert"); 
Collection CAlerte = theAlert.getInstances(); 

int nombreAlerte =CAlerte.size(); 
String[ ] list_alerte=new String[ nombreAlerte ];
OWLIndividual[ ] idorg=(OWLIndividual[ ]) CAlerte.toArray(new OWLIndividual[ 0 ]); 
for(int j=0;j< nombreAlerte;j++){
    list_alerte[ j ]=idorg[ j ].getName();
}   

System.out.println(" le nombres des alerte est:"+nombreAlerte);
OntModel inf1 = ModelFactory.createOntologyModel();
for(int k=0;k< nombreAlerte;k++){
    System.out.println(" \n"+list_alerte[k]);      
}

Here it display my

Alert_1
Alert_2
Alert_3

How to get their properties?


UPDATE:

Thanks for your answer, it doesn't work yet. I tried now to do like you said

    JenaOWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI("file:///D:/base_connaissance.owl");
    OntModel model = owlModel.getOntModel(); 

    ArrayList<Resource> results = new ArrayList<Resource>();

    ExtendedIterator  individuals = model.listIndividuals();
    while (individuals.hasNext()) {
        Resource individual = (Resource) individuals.next();
        results.add(individual);
    }

    for(int i = 0; i < results.size(); i++)
    {
      System.out.println("individual number " + i + " = " + results.get(i));//here it display my individual

      Individual ind = model.getIndividual(results.get(i).toString()); 
      Property hasTime = model.createProperty( "file:///D:/base_connaissance.owl#hasanalyser" );
      RDFNode time = ind.getPropertyValue( hasTime );
      System.out.println("property value of hasanalyser "+time);

At the end it displays all names of my individuals, and after each individual it display property value of hasanalyser NULL.

where is the problème please


Solution

  • IT WORK NOW , i can get now all properties of all individuals thanks lot , now what it doesnt work is HOW to add an individual and add properties to this individual and insert it into my "base_connaissance.owl" if you can help me i realy appreciate it here is the full code witch work perfectly .

    static JenaOWLModel owlModel ;
    public static void main(String[] args) {
    
        OntModel model;
        javax.swing.JDialog jDialog1 = new javax.swing.JDialog();       
        try{
            String ns="file:///D:/base_connaissance.owl#";
            owlModel = ProtegeOWL.createJenaOWLModelFromURI("file:///D:/base_connaissance.owl");//  crée  un modele owl  a partir du ficher owl charger
            model = owlModel.getOntModel();  
            JOptionPane.showMessageDialog(jDialog1,"chargement du fichier  effectuer avec succé","Information",JOptionPane.INFORMATION_MESSAGE);        
    
            ArrayList<Resource> results = new ArrayList<Resource>();            
            ExtendedIterator  individuals = model.listIndividuals();
            while (individuals.hasNext()) {
                Resource individual = (Resource) individuals.next();
                results.add(individual);
            }
            System.out.println("\n");
    
            for(int i = 0; i < results.size(); i++)
            {
            Individual ind = model.getIndividual(results.get(i).toString());
            System.out.println(""+ind);
            StmtIterator it = ind.listProperties();
    
            while ( it.hasNext()) {
                   Statement s = (Statement) it.next();    
    
                if (s.getObject().isLiteral()) {
    
    
                    System.out.println(""+s.getLiteral().getLexicalForm().toString()+" type = "+s.getPredicate().getLocalName());
    
                    }
    
    
                else    System.out.println(""+s.getObject().toString().substring(53)+" type = "+s.getPredicate().getLocalName());
    
    
                     }
            System.out.println("\n");
                }
    
    
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(jDialog1,
                    "error",
                    "Information",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }