Rdf 让用户输入文件名

Rdf 让用户输入文件名,rdf,java,awt,jena,Rdf,Java,Awt,Jena,我创建了一个允许在文件中添加实例的接口。我将文件路径放在readRDFfile参数中,并将相同的文件路径放在Filewriter中(以便在用户添加实例时更新文件)。但我希望允许用户在我执行代码时输入他想要创建的名称文件。当用户添加实例时,FileWriter必须在参数中包含此文件 import java.util.*; import java.util.List; import com.hp.hpl.jena.rdf.model.*; import com.hp.hpl.jena.ontolog

我创建了一个允许在文件中添加实例的接口。我将文件路径放在
readRDFfile
参数中,并将相同的文件路径放在
Filewriter
中(以便在用户添加实例时更新文件)。但我希望允许用户在我执行代码时输入他想要创建的名称文件。当用户添加实例时,FileWriter必须在参数中包含此文件

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(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();  

}
}
我的问题是,我不知道如何将用户选择并在ReadrFFILE中读取的文件放入Filewriter参数中,以便在添加实例时进行更新

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(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();  

}
}
import java.util.*;
导入java.util.List;
导入com.hp.hpl.jena.rdf.model.*;
导入com.hp.hpl.jena.ontology.*;
导入com.hp.hpl.jena.ontology.impl.*;
导入com.hp.hpl.jena.util.*;
导入java.io.*;
导入java.awt.*;
导入java.awt.event.*;
导入com.hp.hpl.jena.词汇表.RDF;
导入com.hp.hpl.jena.词汇表.XSD;
导入com.hp.hpl.jena.util.iterator.ExtendedIterator;
公共类FamilyModel扩展框架
{   
TextField[]tabTF=新的TextField[4];
按钮bAjout、b修改器、b提供者、b最近、b向导、b验证修改;//按钮添加、删除、上一个、下一个
OntModel模型;
登上;
int-indice=0;
int p=0;
资源p1;
按钮过滤器按钮(字符串S、整数x、整数y)
{
按钮b=新按钮;
添加(b);
b、 立根(x,y,120,30);
返回b;
}
void creerLabel(字符串,整数x,整数y)
{
标签la=新标签(礼仪);
la.立根(x,y,100,25);
添加(la);
}
公共家庭模式()
{
setLayout(空);
挫折背景(颜色:粉红色);
立根(100200900450);
creerLabel(“Prenom:”,10,50);
筒子架标签(“名称:”,10100);
creerLabel(“诞生日期:”,10145);
creerLabel(“流派(H OUF):”,10190);
//文本字段

对于(int i=0;i来说,关于如何从用户那里获取数据,有许多选择和思想流派。其中大部分取决于用户是谁以及他们将如何与该程序交互。我列出了几个

  • 命令行界面这是我最喜欢的界面,因为我经常在终端中编写大量bash脚本。非常简单,开发开销低,同时仍然可以扩展。您需要在主方法中添加代码来检索选项和值。我喜欢使用,即使它不在活动开发中,也有很好的教程
  • 图形用户界面使用Java Swing或web应用程序围绕您的应用程序创建UI。这将不可避免地比任何其他选项花费更长的时间,但对于非技术用户来说是最容易访问的
  • 标准输入如果您只想让程序在控制台上暂停并提示用户,而不需要铃声或口哨,请在主屏幕上使用此选项(如果您使用
    Console con=System.Console();

    String file=con.readLine(“文件名:”);


  • 如果您的应用程序具有GUI,选择输入文件的标准方法是使用文件选择器,例如,如果您的应用程序基于Swing,或者如果您希望坚持使用AWT组件

    以下是JFileChooser的exmaple:

        int returnVal = fc.showOpenDialog(FileChooserDemo.this);
    
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String filename = file.getName();
        } 
    
    JFileChooser教程:

    如果您的应用程序是基于命令行的(我从处理按钮单击的代码中收集到的情况并非如此),则可以在运行应用程序时将输入作为命令行参数之一,并且可以从传递到main()中的args[]数组中读取