开发者

Let the user enter the filename

I created an interface that allows to add instances in an rdf file. I put the filepath in the readRDFfile parameter and the same filepath in Filewriter (in order to update the file when user add instances). But i'd like to allow user enter the name file he want to create when I execute the code. And FileWriter must take this file in parameter when user add instances.

My problem is that I don't know how to put the file that user has chosen and that was read in readRDFfile, in Filewriter parameter in order to be updated when he adds instances.

import java.util.*;
import java.util.List;
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;


public class FamilyModel extends Frame
{   
TextField[]tabTF=new TextField[4];
Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant, bValiderModif; //buttons Add, Remove, Previous, Next
OntModel model;
Onto onto;
int indice=0;
int p=0;
Resource p1; 


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,100,25);
    add(la);
}


public FamilyModel ()
{
        setLayout (null);
        setBackground (Color.pink);
        setBounds (100,200,900,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,45,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); 
        setVisible(true);

        bModifier=creerBouton("Modifier",138,250); 
        setVisible(true);

        //bSupprimer=creerBouton("Supprimer",250,250); 
        //setVisible(true);

        bPrecedent=creerBouton("Precedent",360,250); 

        bSuivant=creerBouton("Suivant",450,250); 

        bSupprimer=creerBouton("Supprimer",600,250);

        setVisible(true);   

        onto = new Onto();

         readRDFfile();



        traitement(this);   

}



void traitement(Frame fenetre)
{

    bAjout.addActionListener(new ActionAjoutPersonne());
    //bModifier.addActionListener(new ActionModifier());
    //bValiderModif.addActionListener(new ActionModif());
    bSuivant.addActionListener(new ActionSuivant());
    bPrecedent.addActionListener(new ActionPrecedent());
    bSupprimer.addActionListener(new ActionSupprimer());

}


//Button Add
public class ActionAjoutPersonne implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {


        p1=onto.model.createResource(onto.uriBase+"#"+tabTF[0].getText()); 
        p1.addProperty(onto.aPourPrenom, tabTF[0].getText());         
        p1.addProperty(onto.aPourNom, tabTF[1].getText());         
        p1.addProperty(onto.aDateNaiss, tabTF[2].getText()); 

        if (tabTF[3].getText().equals("F"))
        {
            p1.addProperty(onto.aGenre, tabTF[3].getText()); 
            p1.addProperty(RDF.type, onto.femme);

        }
        else if (tabTF[3].getText().equals("H"))
        {
            p1.addProperty(onto.aGenre, tabTF[3].getText());    
            p1.addProperty(RDF.type, onto.homme);
        }

        StringWriter sw = new StringWriter();
        onto.model.write(sw, "RDF/XML-ABBREV");
        String owlCode = sw.toString();
        File file = new File("d:/Onto.rdf");
        try{
            FileWriter fw = new FileWriter(file);
            fw.write(owlCode);
            fw.close();
        } catch(FileNotFoundException fnfe){
            fnfe.printStackTrace();} 
        catch(IOException ioe){
                ioe.printStackTrace();
        }  

    }
}


//Button Remove
public class ActionSupprimer implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {
        List<Statement> statementsToRemove = new ArrayList<Statement>();


        StmtIterator iter = onto.model.listStatements();
        while (iter.hasNext()) 
        {
            Statement stmt  = iter.nextStatement(); 

            Resource  subject = stmt.getSubject(); 
            Property  predicate = stmt.getPredicate();  
            RDFNode object  = stmt.getObject();  
            if(subject.toString().equals (onto.uriBase+"#"+tabTF[0].getText()))
            {
                statementsToRemove.add(stmt);
            }
       }


       for( Statement stmt : statementsToRemove) 
       {
            onto.model.remove(stmt);
       }

       StringWriter sw = new StringWriter();
       onto.model.write(sw, "RDF/XML-ABBREV");
       String owlCode = sw.toString();
       File file = new File("d:/Onto.rdf");
       try{
           FileWriter fw = new FileWriter(file);
           fw.write(owlCode);
           fw.close();
       } catch(FileNotFoundException fnfe){
           fnfe.printStackTrace();} 
       catch(IOException ioe){
               ioe.printStackTrace();
       }  
   }
}


//Read Onto.rdf
public void readRDFfile()
{
     String inputFile="D:/Onto.rdf";
     try
     {
     InputStream in =new  FileInputStream(inputFile);
      if (in == null) {  
      System.out.println("File not found");
     }  
      onto.model.read(in, null);

     }catch(Exception e) {
           System.out.println("model.read catched error: " + e);
     }
}

//Button Next
class ActionSuivant implements ActionListener
{
    public void actionPerformed(ActionEvent evt)
    {

        ++indice;
        ExtendedIterator instances = onto.personne.listInstances();
        Individual instance = null;
        Individual firstInstance = null;
        for (p = 0; p < indice && instances.hasNext(); p++) {
            instance = (Individual) instances.next();
            if (firstInstance == null) {
                firstInstance = instance;
            }
        }    
        if (p < indice) {
            indice = 1;
            instance = firstInstance;
        }
        tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
        tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
        tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
        tabTF[3].setText(instance.getPropertyValue(onto.aGenre).toString());
    }

} 

class ActionPrecedent implements ActionListener
{
    public void actionPerformed(ActionEvent evt)
    {      
      --indice; 
      //Instances de la Classe Personne          
        ExtendedIterator instances=onto.personne.listInstances();

        Individual instance = null;
                for(p = 0; p < indice && instances.hasNext(); p++)
                {
                   instance = (Individual) instances.next();

                }   
                 tabTF[0].setText(instance.getPropertyValue(onto.aPourPrenom).toString());
                 tabTF[1].setText(instance.getPropertyValue(onto.aPourNom).toString());
                 tabTF[2].setText(instance.getPropertyValue(onto.aDateNaiss).toString());
                 tabTF[3].setText(instance.getPropertyValue(onto.aGenre).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_MEM_MICRO_RULE_INF );
    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(uri开发者_开发问答Base+"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();  

}
}


If your app has a GUI, the standard way to select an input file would be to use a file chooser, for example JFileChooser if your app is Swing based, or FileDialog if you want to stick to AWT components.

Here is an exmaple for JFileChooser:

    int returnVal = fc.showOpenDialog(FileChooserDemo.this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        String filename = file.getName();
    } 

JFileChooser Tutorial: http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

If your app is command line based (which I gather it not the case from your code for handling button clicks), you could make the input be one of the command line arguments when you run the app, and you could read it out of args[] array passed into main().


There are many options and schools of thought regarding how to get data from the user. Most of this depends on who the user will be and how they are going to be interacting with this program. A couple I've listed.

  1. Command Line Interface This is my favorite because I'm in the terminal a lot and write a lot of bash scripts. Very simple and low development overhead, while still being extensible. You'll need to add code in your main method to retrieve options and values. I like using Apache Commons CLI even though it's not under active development, there is a good tutorial.
  2. Graphical User Interface Use Java Swing or a web application to create a UI around your app. This will inevitably take much longer than any other option but will be the most accessible for non-technical users.
  3. Standard In If you just want the program to pause and prompt the user on the console with no bells or whistles use this in main (if you using

    Console con = System.console();

    String file = con.readLine("File Name: ");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜