0

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

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Boss Krikou
  • 21
  • 1
  • 3

2 Answers2

2

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);
    }
}
Boss Krikou
  • 21
  • 1
  • 3
1

I'm not clear from your code how you are using Jena models. The OntModel called model does not appear to be used, nor does the InfModel called inf1. OWLIndividual is not a Jena class, but a Protégé one. The question asks about using the Jena API, however, so the rest of this answer assumes that you can get the Individuals that you are interested in, not the OWLIndividuals.

Jena's Individuals have a method getPropertyValue that returns the value of a property for the individual. You should be able to do something like this:

Individual alert1 = ...;
String hasTimeURI = "...";
Property hasTime = model.createProperty( hasTimeURI );
RDFNode time = alert1.getPropertyValue( hasTime );

The additional code you posted is printing null probably because the IRI for the property is incorrect. File-based IRIs are not very good identifiers; URIs are supposed to be universal identifiers, and something that includes a file path almost certainly isn't going to be universal. Without seeing your data, we cannot know what the proper IRI for the property should be. However, if you do something like this, you should be able to get enough information to determine what the property IRI should be.

Individual ind = model.getIndividual(results.get(i).toString());
// Check whether ind is null.
System.out.println( "ind: "+ind );
// Iterate over all the statements that have ind as subject.
StmtIterator it = ind.ListProperties();
while ( it.hasNext() ) {
  System.out.println( it.next() );
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353