API Jena

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 1078 Views - Last Post: 15 August 2011 - 01:04 AM Rate Topic: -----

#1 bpr25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 11

API Jena

Posted 08 August 2011 - 10:45 AM

Hi,

I have an interface with 4 labels, 4 textfields and some buttons like "Add", "modify, "next", etc. The user will enter name, first name, birth date, etc. After clicking the button "add", the information that he has entered will be added on rdf file. The "next" (in french "Suivant") button didn't work. When i click on "next" button, nothing appears on textfields. There is nothing in the textfields.
Can you tell me what's wrong?
Thanks.

My code:

import java.util.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.ontology.impl.*;
import com.hp.hpl.jena.util.*;
import java.io.*;
import java.awt.*; 	
import java.awt.event.*; 
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.XSD;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import org.apache.log4j.Logger;


public class FamilyModel extends Frame{	
	TextField[]tabTF=new TextField[4];
	Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant;	
	
	Button creerBouton(String S, int x, int y)
	{
		Button b=new Button(S);
		add(B)/>;			
		b.setBounds(x,y,120,30);
		return b;
	}
	
	void creerLabel(String etiquette, int x, int y)
	{
		Label la=new Label(etiquette);
		la.setBounds(x,y,120,25);
		add(la);
	}
	
		
	public FamilyModel ()
	{
			setLayout (null);
			setBackground (Color.pink);
			setBounds (100,200,600,450);
			addWindowListener(new FermerFenetre());	//close windows when click on X
			
			creerLabel("Prenom : ",10,50);
			creerLabel("Nom : ",10,100);
			creerLabel("Date de Naissance: ",10,145);
			creerLabel("Genre (H ou F): ",10,190);
						
			//TextFields
			for(int i=0;i<4;i++)
			{
				tabTF[i]=new TextField("");
				tabTF[i].setBackground(Color.white);
				add(tabTF[i]);
			}
			tabTF[0].setBounds(120,50,150,25);
			tabTF[1].setBounds(120,100,150,25);
			tabTF[2].setBounds(120,145, 100,25);
			tabTF[3].setBounds(120,190, 45,25);
			
			bAjout=creerBouton("Ajouter",20,250); //Add
			setVisible(true);
			
			bModifier=creerBouton("Modifier",138,250); //Modify
			setVisible(true);
			
			bSupprimer=creerBouton("Supprimer",250,250); //Remove
			setVisible(true);
			
			bPrecedent=creerBouton("Precedent",360,250); //back
			
			bSuivant=creerBouton("Suivant",450,250); //next
			setVisible(true);	
			
			
			
			traitement(this); 
			
	}
	
	
	
	void traitement(Frame fenetre)
	{
		
		bAjout.addActionListener(new ActionAjoutPersonne()); //Add
		bSuivant.addActionListener(new ActionSuivant()); //Next
	}
	
    //ActionAdd
	public class ActionAjoutPersonne extends Onto implements ActionListener
	{
		
		public void actionPerformed(ActionEvent evt)
		{
					
			Onto f;
						
			f = new Onto();
	
			
			for(int i=0;i<4;i++)
			{
				
							
				tabTF[i].getText();	
				Resource p1 = model.createResource(uriBase+tabTF[0].getText());
				p1.addProperty(RDF.type, f.personne);
				
				if (i==0) 
				{
					p1.addProperty(f.aPourPrenom, tabTF[i].getText());
					
				}
				if (i==1)
				{
					p1.addProperty(f.aPourNom, tabTF[i].getText());
				}
				if (i==2)
				{
					
					p1.addProperty(f.aDateNaiss, tabTF[i].getText());
				}
				if (i==3)
				{
					if (tabTF[i].getText().equals("F"))
					{
						p1.addProperty(f.aGenre, tabTF[i].getText());
						p1.addProperty(RDF.type, f.femme);
					}
					else 
						if (tabTF[i].getText().equals("H"))
						{
							p1.addProperty(f.aGenre, tabTF[i].getText());
							p1.addProperty(RDF.type, f.homme);
						}
				}
				
				StringWriter sw = new StringWriter();
				model.write(sw, "RDF/XML-ABBREV");
				String owlCode = sw.toString();
				File file = new File("d:/teste20.rdf");
				try{
					FileWriter fw = new FileWriter(file);
					fw.write(owlCode);
					fw.close();
				} catch(FileNotFoundException fnfe){
					fnfe.printStackTrace();} 
				catch(IOException ioe){
						ioe.printStackTrace();
				}	
				
			}
			
		}
        }
	
	//ActionNext
	class ActionSuivant extends Onto implements ActionListener
	{
		public void actionPerformed(ActionEvent evt)
		{
					
			Onto s = new Onto();
			int indice=1;
			String []M= new String [10];
			
			//Read RDF File
			InputStream in = FileManager.get().open("d:\teste20.rdf");
			if (in == null) 
			{
			  throw new IllegalArgumentException("File: " + "d:\teste20.rdf" + " not found");
			}
			s.model.read(in, null);
				
			//Search instances of Personne Class			
			ExtendedIterator instances=s.personne.listInstances();
					
			indice++;
			for(int p=0;p<10;p++) //10 instances
			{
				while (instances.hasNext())
				{
				     Individual thisInstance = (Individual) instances.next();
				     M[p]= thisInstance.getLocalName();
				     
					if(p==indice)
			        {					
			          tabTF[0].setText((thisInstance.getPropertyResourceValue(s.aPourPrenom)).toString());
			          tabTF[1].setText((thisInstance.getPropertyResourceValue(s.aPourNom)).toString());
			          tabTF[2].setText((thisInstance.getPropertyResourceValue(s.aDateNaiss)).toString());
			        }
			     }
			 }
		}
	}
	
	//Clode window when clicked on X
	public class FermerFenetre extends WindowAdapter {

		public void windowClosing(WindowEvent evt)
		{
			if(evt.getWindow().getName().equals("frame0"))
			{
				System.exit(0);
			}
			else
			{
				evt.getWindow().dispose();
			}
		}
	}
	
	//ontology
	public class Onto 
	{
		OntClass personne, genre, homme, femme, feminin, masculin, evenement, deces, mariage, divorce;
		OntModel model;
		String uriBase;
		ObjectProperty aPourFils, aPourFille, aGenre;
		DatatypeProperty aPourNom, aPourPrenom, aDateNaiss;
		
		public Onto (){
		model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
		uriBase = "http://www.something.com/FAM";
		model.createOntology(uriBase);
		
		//Classes
		personne = model.createClass(uriBase+"#personne");
		femme = model.createClass(uriBase+"#femme");
		homme = model.createClass(uriBase+"#homme");
		genre = model.createClass(uriBase+"#genre");
		feminin = model.createClass(uriBase+"#feminin");
		masculin = model.createClass(uriBase+"#masculin");
		evenement = model.createClass(uriBase+"#evenement");
		deces = model.createClass(uriBase+"#deces");
		mariage = model.createClass(uriBase+"#mariage");
		divorce = model.createClass(uriBase+"#divorce");
		
		
		//Sub-classes
		genre.addSubClass(feminin);
		genre.addSubClass(masculin);
		personne.addSubClass(homme);
		personne.addSubClass(femme);
		evenement.addSubClass(deces);
		evenement.addSubClass(mariage);
		evenement.addSubClass(divorce);
		
		aPourFils = model.createObjectProperty(uriBase+"#aPourFils");
		aPourFils.setDomain(personne);
		aPourFils.setRange(homme);
		
		aPourFille = model.createObjectProperty(uriBase+"#aPourFille");
		aPourFille.setDomain(personne);
		aPourFille.setRange(femme);
		
		aGenre = model.createObjectProperty(uriBase+"#aGenre");
		aGenre.setDomain(personne);
		aGenre.setRange(genre);
		
		aPourNom = model.createDatatypeProperty(uriBase+"#aPourNom"); 
		aPourNom.setDomain(personne);
		aPourNom.setRange(XSD.xstring);
		
		aPourPrenom = model.createDatatypeProperty(uriBase+"#aPourPrenom"); 
		aPourPrenom.setDomain(personne);
		aPourPrenom.setRange(XSD.xstring);
		
		aDateNaiss = model.createDatatypeProperty(uriBase+"#aDateNaiss"); 
		aDateNaiss.setDomain(personne);
		aDateNaiss.setRange(XSD.xstring);
		}

	}
	
	public static void main(String args[]) 
	{
	
		
		new FamilyModel();
		
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: API Jena

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: API Jena

Posted 08 August 2011 - 05:37 PM

What is the used of doing

    for(int i = 0; i < 4; ++i) {
        tabTF[i].getText();        // text that will be stored where
        if(i == 0) {
          ... code
        }
        if(i == 1) {
          ... code
        }
        if(i == 2) {
          ... code
        }
        if(i == 3) {
          ... code
        }
    }


You simply do not need the loop

        p1.addProperty(f.aPourPrenom, tabTF[0].getText());         
        p1.addProperty(f.aPourPrenom, tabTF[1].getText());         
        p1.addProperty(f.aPourPrenom, tabTF[2].getText()); 

        if (tabTF[3].getText().equals("F"))
        {
           p1.addProperty(f.aGenre, "F");    // tabTF[3].getText() is "F" we know it
           p1.addProperty(RDF.type, f.femme);
        }
        else if (tabTF[3].getText().equals("H"))
        {
           p1.addProperty(f.aGenre, "H");    // tabTF[3].getText() est "H" on le sait deja
           p1.addProperty(RDF.type, f.homme);
        }


Was This Post Helpful? 1
  • +
  • -

#3 bpr25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 11

Re: API Jena

Posted 09 August 2011 - 04:04 AM

Hi pbl,
You're right, thanks :)

Button "Next" doesn't work, I don't know why. Can you tell me whats wrong?
Eclipse executes the code, I see the frame with the buttons but text fields are empty and when I click on the button Next (Next=Suivant in french)nothing happens. Text fields are empty. :(

//Button Next (Bouton Suivant)
    class ActionSuivant extends Onto implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto s = new Onto();
            int indice=1;
            String []M= new String [2];
            
            //Read RDF File
            String inputFileName = "D:\teste20.rdf";
            InputStream in = FileManager.get().open(inputFileName);
            if (in == null) 
            {
              throw new IllegalArgumentException("File: " + "d:\teste20.rdf" + " not found");
            }
            s.model.read(in, null);
                
            //Instances de la Classe Personne          
            ExtendedIterator instances=s.personne.listInstances();
                    
            indice++;
            for(int p=0;p<2;p++
            {
                while (instances.hasNext())
                {
                     Individual thisInstance = (Individual) instances.next();
                     M[p]= thisInstance.getLocalName();
                     
                    if(p==indice)
                    { 
                      tabTF[0].setText((thisInstance.getPropertyValue(s.aPourPrenom)).toString());
                      tabTF[1].setText((thisInstance.getPropertyValue(s.aPourNom)).toString());
                      tabTF[2].setText((thisInstance.getPropertyValue(s.aDateNaiss)).toString());
                    }
                 }
             }
        }
    }

Attached image(s)

  • Attached Image

This post has been edited by bpr25: 09 August 2011 - 04:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,472
  • Joined: 27-December 08

Re: API Jena

Posted 09 August 2011 - 06:48 AM

Your code shouldn't even compile. You are missing an end parenthesis here.
for(int p=0;p<2;p++


Was This Post Helpful? 0
  • +
  • -

#5 bpr25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 11

Re: API Jena

Posted 09 August 2011 - 06:59 AM

Hi, thanks. I did copie paste here, maybe I forgotten to copy this parenthesis or I removed it. But thanks :)
Can you explain me whats wrong in my code? As I explained, button Next (=Suivant in french) doesn't work :(
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,472
  • Joined: 27-December 08

Re: API Jena

Posted 09 August 2011 - 07:01 AM

Did you add the ActionListener to the button? Are you sure that these return non-empty Strings:
tabTF[0].setText((thisInstance.getPropertyValue(s.aPourPrenom)).toString());
tabTF[1].setText((thisInstance.getPropertyValue(s.aPourNom)).toString());                      tabTF[2].setText((thisInstance.getPropertyValue(s.aDateNaiss)).toString());


Was This Post Helpful? 0
  • +
  • -

#7 bpr25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 11

Re: API Jena

Posted 09 August 2011 - 07:09 AM

Yes, I'm sure :( teste20.rdf contains 2 instances of person, with 3 Datatypeproperties value for each instance.

Here's my code for Button Next:

//Button "Next"
    class ActionSuivant extends Onto implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto s = new Onto();
            int indice=1;
            String []M= new String [2];
            
            //Read RDF File
            String inputFileName = "D:\teste20.rdf";
            InputStream in = FileManager.get().open(inputFileName);
            if (in == null) 
            {
              throw new IllegalArgumentException("File: " + "d:\teste20.rdf" + " not found");
            }
            s.model.read(in, null);
                
            //Instances Classe Person         
            ExtendedIterator instances=s.personne.listInstances();
                    
            indice++;
            for(int p=0;p<2;p++) //10 instances
            {
                while (instances.hasNext())
                {
                     Individual thisInstance = (Individual) instances.next();
                     M[p]= thisInstance.getLocalName();
                     
                    if(p==indice)
                    { 
                      tabTF[0].setText((thisInstance.getPropertyValue(s.aPourPrenom)).toString());
                      tabTF[1].setText((thisInstance.getPropertyValue(s.aPourNom)).toString());
                      tabTF[2].setText((thisInstance.getPropertyValue(s.aDateNaiss)).toString());
                    }
                 }
             }
        }
    }
	


Complete code:

import java.util.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.ontology.impl.*;
import com.hp.hpl.jena.util.*;
import java.io.*;
import java.awt.*; 	
import java.awt.event.*; 
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.XSD;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import org.apache.log4j.Logger;



public class FamilyModel extends Frame
{	
	TextField[]tabTF=new TextField[4];
	Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant;
	
	
	Button creerBouton(String S, int x, int y)
	{
		Button b=new Button(S);
		add(B)/>;			
		b.setBounds(x,y,120,30);
		return b;
	}
	
	void creerLabel(String etiquette, int x, int y)
	{
		Label la=new Label(etiquette);
		la.setBounds(x,y,120,25);
		add(la);
	}
	
		
	public FamilyModel ()
	{
			setLayout (null);
			setBackground (Color.pink);
			setBounds (100,200,600,450);
					
			creerLabel("Prenom : ",10,50);
			creerLabel("Nom : ",10,100);
			creerLabel("Date de Naissance: ",10,145);
			creerLabel("Genre (H ou F): ",10,190);
						
			//TextFields
			for(int i=0;i<4;i++)
			{
				tabTF[i]=new TextField("");
				tabTF[i].setBackground(Color.white);
				add(tabTF[i]);
			}
			tabTF[0].setBounds(120,50,150,25);
			tabTF[1].setBounds(120,100,150,25);
			tabTF[2].setBounds(120,145, 100,25);
			tabTF[3].setBounds(120,190, 45,25);
			
			bAjout=creerBouton("Ajouter",20,250); //Add
			setVisible(true);
			
			bModifier=creerBouton("Modifier",138,250); //Modify
			setVisible(true);
			
			bSupprimer=creerBouton("Supprimer",250,250); //Remove
			setVisible(true);
			
			bPrecedent=creerBouton("Precedent",360,250); //back
			
			bSuivant=creerBouton("Suivant",450,250); //next
			setVisible(true);	
			
			
			
			traitement(this); 
			
	}
	
	
	
	void traitement(Frame fenetre)
	{
		
		bAjout.addActionListener(new ActionAjoutPersonne()); //Add
		bSuivant.addActionListener(new ActionSuivant()); //Next
	}
	
    
    //Button Add: It works
    public class ActionAjoutPersonne extends Onto implements ActionListener
    {
        
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto f;
                        
            f = new Onto();
    
            
            for(int i=0;i<4;i++)
            {
                
                            
                tabTF[i].getText();    
                Resource p1 = model.createResource(uriBase+tabTF[0].getText());
                p1.addProperty(RDF.type, f.personne);
                
                if (i==0) 
                {
                    p1.addProperty(f.aPourPrenom, tabTF[i].getText());
                    
                }
                if (i==1)
                {
                    p1.addProperty(f.aPourNom, tabTF[i].getText());
                }
                if (i==2)
                {
                    
                    p1.addProperty(f.aDateNaiss, tabTF[i].getText());
                }
                if (i==3)
                {
                    if (tabTF[i].getText().equals("F"))
                    {
                        p1.addProperty(f.aGenre, tabTF[i].getText());
                        p1.addProperty(RDF.type, f.femme);
                    }
                    else 
                        if (tabTF[i].getText().equals("H"))
                        {
                            p1.addProperty(f.aGenre, tabTF[i].getText());
                            p1.addProperty(RDF.type, f.homme);
                        }
                }
                
                StringWriter sw = new StringWriter();
                model.write(sw, "RDF/XML-ABBREV");
                String owlCode = sw.toString();
                File file = new File("d:/teste20.rdf");
                try{
                    FileWriter fw = new FileWriter(file);
                    fw.write(owlCode);
                    fw.close();
                } catch(FileNotFoundException fnfe){
                    fnfe.printStackTrace();} 
                catch(IOException ioe){
                        ioe.printStackTrace();
                }    
                
            }
            
        }
        }
    
    //Button "Next": Doesn't work
    class ActionSuivant extends Onto implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto s = new Onto();
            int indice=1;
            String []M= new String [2];
            
            //Read RDF File
            String inputFileName = "D:\teste20.rdf";
            InputStream in = FileManager.get().open(inputFileName);
            if (in == null) 
            {
              throw new IllegalArgumentException("File: " + "d:\teste20.rdf" + " not found");
            }
            s.model.read(in, null);
                
            //Instances Classe Person         
            ExtendedIterator instances=s.personne.listInstances();
                    
            indice++;
            for(int p=0;p<2;p++) //2 instances
            {
                while (instances.hasNext())
                {
                     Individual thisInstance = (Individual) instances.next();
                     M[p]= thisInstance.getLocalName();
                     
                    if(p==indice)
                    { 
                      tabTF[0].setText((thisInstance.getPropertyValue(s.aPourPrenom)).toString());
                      tabTF[1].setText((thisInstance.getPropertyValue(s.aPourNom)).toString());
                      tabTF[2].setText((thisInstance.getPropertyValue(s.aDateNaiss)).toString());
                    }
                 }
             }
        }
    }
	
	
	//ontology
	public class Onto 
	{
		OntClass personne, genre, homme, femme, feminin, masculin, evenement, deces, mariage, divorce;
		OntModel model;
		String uriBase;
		ObjectProperty aPourFils, aPourFille, aGenre;
		DatatypeProperty aPourNom, aPourPrenom, aDateNaiss;
		
		public Onto (){
		model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
		uriBase = "http://www.something.com/FAM";
		model.createOntology(uriBase);
		
		//Classes
		personne = model.createClass(uriBase+"#personne");
		femme = model.createClass(uriBase+"#femme");
		homme = model.createClass(uriBase+"#homme");
		genre = model.createClass(uriBase+"#genre");
		feminin = model.createClass(uriBase+"#feminin");
		masculin = model.createClass(uriBase+"#masculin");
		evenement = model.createClass(uriBase+"#evenement");
		deces = model.createClass(uriBase+"#deces");
		mariage = model.createClass(uriBase+"#mariage");
		divorce = model.createClass(uriBase+"#divorce");
		
		
		//Sub-classes
		genre.addSubClass(feminin);
		genre.addSubClass(masculin);
		personne.addSubClass(homme);
		personne.addSubClass(femme);
		evenement.addSubClass(deces);
		evenement.addSubClass(mariage);
		evenement.addSubClass(divorce);
		
		aPourFils = model.createObjectProperty(uriBase+"#aPourFils");
		aPourFils.setDomain(personne);
		aPourFils.setRange(homme);
		
		aPourFille = model.createObjectProperty(uriBase+"#aPourFille");
		aPourFille.setDomain(personne);
		aPourFille.setRange(femme);
		
		aGenre = model.createObjectProperty(uriBase+"#aGenre");
		aGenre.setDomain(personne);
		aGenre.setRange(genre);
		
		aPourNom = model.createDatatypeProperty(uriBase+"#aPourNom"); 
		aPourNom.setDomain(personne);
		aPourNom.setRange(XSD.xstring);
		
		aPourPrenom = model.createDatatypeProperty(uriBase+"#aPourPrenom"); 
		aPourPrenom.setDomain(personne);
		aPourPrenom.setRange(XSD.xstring);
		
		aDateNaiss = model.createDatatypeProperty(uriBase+"#aDateNaiss"); 
		aDateNaiss.setDomain(personne);
		aDateNaiss.setRange(XSD.xstring);
		}

	}
	
	public static void main(String args[]) 
	{
	
		
		new FamilyModel();
		
		
	}
}
	
	  
 


Thanks
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: API Jena

Posted 09 August 2011 - 10:30 AM

You are missing the addActionListener()


public class FamilyModel extends Frame{	
	TextField[]tabTF=new TextField[4];
	Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant;	
	
	Button creerBouton(String S, int x, int y)
	{
		Button b=new Button(S);
		add(B)/>;			
		b.setBounds(x,y,120,30);
		return b;
	}
	
	void creerLabel(String etiquette, int x, int y)
	{
		Label la=new Label(etiquette);
		la.setBounds(x,y,120,25);
		add(la);
	}
	
		
	public FamilyModel ()
	{
			setLayout (null);
			setBackground (Color.pink);
			setBounds (100,200,600,450);
			addWindowListener(new FermerFenetre());	//close windows when click on X
			
			creerLabel("Prenom : ",10,50);
			creerLabel("Nom : ",10,100);
			creerLabel("Date de Naissance: ",10,145);
			creerLabel("Genre (H ou F): ",10,190);
						
			//TextFields
			for(int i=0;i<4;i++)
			{
				tabTF[i]=new TextField("");
				tabTF[i].setBackground(Color.white);
				add(tabTF[i]);
			}
			tabTF[0].setBounds(120,50,150,25);
			tabTF[1].setBounds(120,100,150,25);
			tabTF[2].setBounds(120,145, 100,25);
			tabTF[3].setBounds(120,190, 45,25);
			
			bAjout=creerBouton("Ajouter",20,250); //Add
			setVisible(true);                   // do that only once at the end of the constructor
			// you miss
			bSuivant.addActionListener(new ActionAjoutPersonne());
			
			bModifier=creerBouton("Modifier",138,250); //Modify
			setVisible(true);   // you already made your frame setVisible(true) 3 lines upper
			
			bSupprimer=creerBouton("Supprimer",250,250); //Remove
			setVisible(true);    // you already made your frame setVisible(true) 
			
			bPrecedent=creerBouton("Precedent",360,250); //back
			
			bSuivant=creerBouton("Suivant",450,250); //next
			setVisible(true);	   // you already made your frame setVisible(true)
			// you miss
			bSuivant.addActionListener(new ActionSuivant());
			
			
			traitement(this); 
			setVisible(true);     // une fois c'est bien assez
			
	}
	
	
    //ActionAdd  no need to extend Onto... you create one inside
	public class ActionAjoutPersonne /*  extends Onto */ implements ActionListener
	{
		
	
	//ActionNext   ce n'est pas un Onto
	class ActionSuivant /*  extends Onto */   implements ActionListener
	{
		public void actionPerformed(ActionEvent evt)
		{
					
			Onto s = new Onto();   // vous le creez ici
			int indice=1;
			String []M= new String [10];
			
			//Read RDF File
			InputStream in = FileManager.get().open("d:\teste20.rdf");
			if (in == null) 
			{
			  throw new IllegalArgumentException("File: " + "d:\teste20.rdf" + " not found");
			}
			s.model.read(in, null);
				
			//Search instances of Personne Class			
			ExtendedIterator instances=s.personne.listInstances();
		}
	}
	
}



And your ActionListener DO NOT extends Onto
Was This Post Helpful? 0
  • +
  • -

#9 bpr25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 11

Re: API Jena

Posted 09 August 2011 - 10:42 AM

Hi pbl,
Thanks for your answer.
I have all addActionListener in method "void traitement(Frame fenetre)", see below please. Here is my complete code:

import java.util.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.ontology.impl.*;
import com.hp.hpl.jena.util.*;
import java.io.*;
import java.awt.*; 	
import java.awt.event.*; 
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.XSD;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import org.apache.log4j.Logger;



public class FamilyModel extends Frame
{	
	TextField[]tabTF=new TextField[4];
	Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant;
	
	
	Button creerBouton(String S, int x, int y)
	{
		Button b=new Button(S);
		add(B)/>;			
		b.setBounds(x,y,120,30);
		return b;
	}
	
	void creerLabel(String etiquette, int x, int y)
	{
		Label la=new Label(etiquette);
		la.setBounds(x,y,120,25);
		add(la);
	}
	
		
	public FamilyModel ()
	{
			setLayout (null);
			setBackground (Color.pink);
			setBounds (100,200,600,450);
					
			creerLabel("Prenom : ",10,50);
			creerLabel("Nom : ",10,100);
			creerLabel("Date de Naissance: ",10,145);
			creerLabel("Genre (H ou F): ",10,190);
						
			//TextFields
			for(int i=0;i<4;i++)
			{
				tabTF[i]=new TextField("");
				tabTF[i].setBackground(Color.white);
				add(tabTF[i]);
			}
			tabTF[0].setBounds(120,50,150,25);
			tabTF[1].setBounds(120,100,150,25);
			tabTF[2].setBounds(120,145, 100,25);
			tabTF[3].setBounds(120,190, 45,25);
			
			bAjout=creerBouton("Ajouter",20,250); //Add
			setVisible(true);
			
			bModifier=creerBouton("Modifier",138,250); //Modify
			setVisible(true);
			
			bSupprimer=creerBouton("Supprimer",250,250); //Remove
			setVisible(true);
			
			bPrecedent=creerBouton("Precedent",360,250); //back
			
			bSuivant=creerBouton("Suivant",450,250); //next
			setVisible(true);	
			
			
			
			traitement(this); 
			
	}
	
	
	
	void traitement(Frame fenetre)
	{
		
		bAjout.addActionListener(new ActionAjoutPersonne()); //Add
		bSuivant.addActionListener(new ActionSuivant()); //Next
	}
	
    
    //Button Add: It works
    public class ActionAjoutPersonne extends Onto implements ActionListener
    {
        
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto f;
                        
            f = new Onto();
    
            
            for(int i=0;i<4;i++)
            {
                
                            
                tabTF[i].getText();    
                Resource p1 = model.createResource(uriBase+tabTF[0].getText());
                p1.addProperty(RDF.type, f.personne);
                
                if (i==0) 
                {
                    p1.addProperty(f.aPourPrenom, tabTF[i].getText());
                    
                }
                if (i==1)
                {
                    p1.addProperty(f.aPourNom, tabTF[i].getText());
                }
                if (i==2)
                {
                    
                    p1.addProperty(f.aDateNaiss, tabTF[i].getText());
                }
                if (i==3)
                {
                    if (tabTF[i].getText().equals("F"))
                    {
                        p1.addProperty(f.aGenre, tabTF[i].getText());
                        p1.addProperty(RDF.type, f.femme);
                    }
                    else 
                        if (tabTF[i].getText().equals("H"))
                        {
                            p1.addProperty(f.aGenre, tabTF[i].getText());
                            p1.addProperty(RDF.type, f.homme);
                        }
                }
                
                StringWriter sw = new StringWriter();
                model.write(sw, "RDF/XML-ABBREV");
                String owlCode = sw.toString();
                File file = new File("d:/teste20.rdf");
                try{
                    FileWriter fw = new FileWriter(file);
                    fw.write(owlCode);
                    fw.close();
                } catch(FileNotFoundException fnfe){
                    fnfe.printStackTrace();} 
                catch(IOException ioe){
                        ioe.printStackTrace();
                }    
                
            }
            
        }
        }
    
    //Button "Next": Doesn't work
    class ActionSuivant extends Onto implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto s = new Onto();
            int indice=1;
            String []M= new String [2];
            
            //Read RDF File
            String inputFileName = "D:\teste20.rdf";
            InputStream in = FileManager.get().open(inputFileName);
            if (in == null) 
            {
              throw new IllegalArgumentException("File: " + "d:\teste20.rdf" + " not found");
            }
            s.model.read(in, null);
                
            //Instances Classe Person         
            ExtendedIterator instances=s.personne.listInstances();
                    
            indice++;
            for(int p=0;p<2;p++) //2 instances
            {
                while (instances.hasNext())
                {
                     Individual thisInstance = (Individual) instances.next();
                     M[p]= thisInstance.getLocalName();
                     
                    if(p==indice)
                    { 
                      tabTF[0].setText((thisInstance.getPropertyValue(s.aPourPrenom)).toString());
                      tabTF[1].setText((thisInstance.getPropertyValue(s.aPourNom)).toString());
                      tabTF[2].setText((thisInstance.getPropertyValue(s.aDateNaiss)).toString());
                    }
                 }
             }
        }
    }
	
	
	//ontology
	public class Onto 
	{
		OntClass personne, genre, homme, femme, feminin, masculin, evenement, deces, mariage, divorce;
		OntModel model;
		String uriBase;
		ObjectProperty aPourFils, aPourFille, aGenre;
		DatatypeProperty aPourNom, aPourPrenom, aDateNaiss;
		
		public Onto (){
		model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
		uriBase = "http://www.something.com/FAM";
		model.createOntology(uriBase);
		
		//Classes
		personne = model.createClass(uriBase+"#personne");
		femme = model.createClass(uriBase+"#femme");
		homme = model.createClass(uriBase+"#homme");
		genre = model.createClass(uriBase+"#genre");
		feminin = model.createClass(uriBase+"#feminin");
		masculin = model.createClass(uriBase+"#masculin");
		evenement = model.createClass(uriBase+"#evenement");
		deces = model.createClass(uriBase+"#deces");
		mariage = model.createClass(uriBase+"#mariage");
		divorce = model.createClass(uriBase+"#divorce");
		
		
		//Sub-classes
		genre.addSubClass(feminin);
		genre.addSubClass(masculin);
		personne.addSubClass(homme);
		personne.addSubClass(femme);
		evenement.addSubClass(deces);
		evenement.addSubClass(mariage);
		evenement.addSubClass(divorce);
		
		aPourFils = model.createObjectProperty(uriBase+"#aPourFils");
		aPourFils.setDomain(personne);
		aPourFils.setRange(homme);
		
		aPourFille = model.createObjectProperty(uriBase+"#aPourFille");
		aPourFille.setDomain(personne);
		aPourFille.setRange(femme);
		
		aGenre = model.createObjectProperty(uriBase+"#aGenre");
		aGenre.setDomain(personne);
		aGenre.setRange(genre);
		
		aPourNom = model.createDatatypeProperty(uriBase+"#aPourNom"); 
		aPourNom.setDomain(personne);
		aPourNom.setRange(XSD.xstring);
		
		aPourPrenom = model.createDatatypeProperty(uriBase+"#aPourPrenom"); 
		aPourPrenom.setDomain(personne);
		aPourPrenom.setRange(XSD.xstring);
		
		aDateNaiss = model.createDatatypeProperty(uriBase+"#aDateNaiss"); 
		aDateNaiss.setDomain(personne);
		aDateNaiss.setRange(XSD.xstring);
		}

	}
	
	public static void main(String args[]) 
	{
	
		
		new FamilyModel();
		
		
	}
}
	
	  
 


Attached Image

The button Add (=Ajouter in french) works perfectly. But I have a problem with the button "Next" (bouton "Suivant" in french). This button doesn't work. When I execute the code, the text fields are empty and when I click on the button Next nothing happens.
When Eclipse executes the code, I expected to see in textfields the information regarding the instance1 of Person Class, and after clicking "Next", I expected to see the information regarding the instance2, and so on.

Thanks

This post has been edited by bpr25: 09 August 2011 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,472
  • Joined: 27-December 08

Re: API Jena

Posted 09 August 2011 - 11:11 AM

Look at the actionPerformed() method. At the start of the loop, indice == 2. Since p will never be 2, the if statement if(p == indice) will never execute. Thus, your TextFields will never be populated by the next button.
int indice=1;
...
indice++;
182	            for(int p=0;p<2;p++) //2 instances
183	            {
184	                while (instances.hasNext())
185	                {
186	                     Individual thisInstance = (Individual) instances.next();
187	                     M[p]= thisInstance.getLocalName();
188	                      
189	                    if(p==indice)


Was This Post Helpful? 0
  • +
  • -

#11 bpr25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 11

Re: API Jena

Posted 09 August 2011 - 12:31 PM

View Postmacosxnerd101, on 09 August 2011 - 11:11 AM, said:

Look at the actionPerformed() method. At the start of the loop, indice == 2. Since p will never be 2, the if statement if(p == indice) will never execute. Thus, your TextFields will never be populated by the next button.
int indice=1;
...
indice++;
182	            for(int p=0;p<2;p++) //2 instances
183	            {
184	                while (instances.hasNext())
185	                {
186	                     Individual thisInstance = (Individual) instances.next();
187	                     M[p]= thisInstance.getLocalName();
188	                      
189	                    if(p==indice)



Hi macosxnerd101,

Thank you very much, I corrected the error.
But button Next still does not work. Nothing happens when I click Next, the textfields are empty. I created the String M[], but now I wondered if it's useful. Maybe my code can not retrieve the values of data properties? But I don't know why, can you explain me? Is something missing or wrong?

Thanks


//Button Next
    class ActionSuivant extends Onto implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto s = new Onto();
            int indice=-1;
            String []M= new String [2];
            
            String inputFile="d:\teste20.rdf";
           try{
           InputStream in =new  FileInputStream(inputFile);
            if (in == null) {  
            System.out.println("File not found");
           }  
            s.model.read(in," ");
  
           }catch(Exception e){}
            
                       
            //Instances Classe Personne      
            ExtendedIterator instances=s.personne.listInstances();
                    
            indice++;
            for(int p=0;p<2;p++) //2 instances
            {
                while (instances.hasNext())
                {
                     Individual thisInstance = (Individual) instances.next();
                     M[p]= thisInstance.getLocalName();
                     
                    if(p==indice)
                    { 
                      tabTF[0].setText((thisInstance.getPropertyValue(s.aPourPrenom)).toString());
                      tabTF[1].setText((thisInstance.getPropertyValue(s.aPourNom)).toString());
                      tabTF[2].setText((thisInstance.getPropertyValue(s.aDateNaiss)).toString());
                    }
                 }
             }
        }
    }

This post has been edited by bpr25: 09 August 2011 - 12:32 PM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,472
  • Joined: 27-December 08

Re: API Jena

Posted 09 August 2011 - 12:52 PM

Do a System.out.println() on each parameter you pass to the JTextField setText() method calls in the if statement in your ActionListener. See what's being printed. Is it some empty Strings?
Was This Post Helpful? 0
  • +
  • -

#13 bpr25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-August 11

Re: API Jena

Posted 09 August 2011 - 01:11 PM

I did a System.out.Println on each parameter.

  //Button Next
    class ActionSuivant extends Onto implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
                    
            Onto s = new Onto();
            int indice=-1;
            String []M= new String [2];
            
            String inputFile="d:\teste20.rdf";
           try{
           InputStream in =new  FileInputStream(inputFile);
            if (in == null) {  
            System.out.println("File not found");
           }  
            s.model.read(in," ");
  
           }catch(Exception e){}
//Instances Classe Personne      
            ExtendedIterator instances=s.personne.listInstances();
                    
            indice++;
            for(int p=0;p<2;p++) //2 instances
            {
                while (instances.hasNext())
                {
                     Individual thisInstance = (Individual) instances.next();
                     thisInstance.getLocalName();
                     
                    if(p==indice)
                    { 
                      System.out.println("Value1: " +(thisInstance.getPropertyValue(s.aPourPrenom)).toString());
                      System.out.println("Value2: " +(thisInstance.getPropertyValue(s.aPourNom)).toString());
                      System.out.println("Value3: " +(thisInstance.getPropertyValue(s.aDateNaiss)).toString());
                    }
                 }
             }
        }


Nothing is printed. Value 1: Value 2: Value 3: do not even appear. So, I think the loop is never executed? :( What do you think?

This post has been edited by bpr25: 09 August 2011 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9026
  • View blog
  • Posts: 33,472
  • Joined: 27-December 08

Re: API Jena

Posted 09 August 2011 - 01:16 PM

You could add a println() statement in your loop before the if statement to confirm, but I think that's a fair assessment that the while loop isn't being executed.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,124
  • Joined: 06-March 08

Re: API Jena

Posted 09 August 2011 - 01:24 PM

For the 3rd time your ActionListener should NOT extends Onto
That does not make sense

When you do new ActionSuivant() why would you want to create a Onto object ?
Where do you store this Onto object ?
You can argue that this Onto object is never used but not sure of the side effects the creation of it. And I am afraid it shows a really misconception on how it should work.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3