我无法删除耶拿中的数据类型属性值

发布于 2024-11-29 09:33:55 字数 9835 浏览 1 评论 0原文

我试图通过我在 java 中创建的接口删除实例的数据类型属性的值,但它不起作用。它给了我一个

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException.

我不明白为什么。你能解释一下出了什么问题吗?

这是我的按钮删除代码:

//Button Remove
public class ActionRemove implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {

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

                onto.model.remove(stmt);

            }

    }
}
}

我的完整代码:

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;




public class FamilyModel extends Frame
{   
TextField[]tabTF=new TextField[4];
Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant, bRemove; //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);
        addWindowListener(new FermerFenetre());

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

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

        setVisible(true);   

        onto = new Onto();

         readRDFfile();

        traitement(this);   

}



void traitement(Frame fenetre)
{

    bAjout.addActionListener(new ActionAjoutPersonne());
    bModifier.addActionListener(new ActionRemove());
    bSuivant.addActionListener(new ActionSuivant());
    bPrecedent.addActionListener(new ActionPrecedent());
    bRemove.addActionListener(new ActionRemove());

}


//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 ActionRemove implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {

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

                onto.model.remove(stmt);

            }

    }
  }
}


//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," ");

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

} 

//Button Previous
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());

          }
} 


//Close window when X is pressed   
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_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();      
}
}

I'm trying to remove values of datatype properties of an instance through an interface which I created in java but it does not work. It gives me an

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException.

I don't understand why. Can you please explain me what's wrong?

Here is my code for button remove:

//Button Remove
public class ActionRemove implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {

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

                onto.model.remove(stmt);

            }

    }
}
}

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;




public class FamilyModel extends Frame
{   
TextField[]tabTF=new TextField[4];
Button bAjout, bModifier, bSupprimer, bPrecedent, bSuivant, bRemove; //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);
        addWindowListener(new FermerFenetre());

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

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

        setVisible(true);   

        onto = new Onto();

         readRDFfile();

        traitement(this);   

}



void traitement(Frame fenetre)
{

    bAjout.addActionListener(new ActionAjoutPersonne());
    bModifier.addActionListener(new ActionRemove());
    bSuivant.addActionListener(new ActionSuivant());
    bPrecedent.addActionListener(new ActionPrecedent());
    bRemove.addActionListener(new ActionRemove());

}


//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 ActionRemove implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {

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

                onto.model.remove(stmt);

            }

    }
  }
}


//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," ");

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

} 

//Button Previous
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());

          }
} 


//Close window when X is pressed   
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_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();      
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2024-12-06 09:33:55

您已从集合中删除了一个项目,然后继续在其上使用以前的迭代器。

大多数java集合迭代器都具有“快速失败”属性。一旦他们检测到底层集合已更改,他们将抛出 ConcurrentModificationException 。

您的解决方案是执行两步删除过程:

  1. 迭代并查找需要删除的节点
  2. 迭代要删除的节点列表(在第一步中找到)并实际从原始集合中删除它们。

代码(假设Java 5):

//Button Remove
public class ActionRemove implements ActionListener
{

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

        // step 1
        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);
            }
       }

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

还有另一种方法(参见http ://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CopyOnWriteArrayList.html 类),但由于您的集合位于 Jena api 内部,因此您无法使用它;)。

You have removed an item from a collection and then continued to use a previous iterator on it.

Most of the java collection iterators have the "fail fast" property. Once they detect that the underlying collection has changed they will throw a ConcurrentModificationException.

Your solution is to do a two step removal process:

  1. iterate and find the nodes that you need to remove
  2. iterate the list of nodes you want removed (found in first step) and actually remove them from the original collection.

Code (assuming Java 5):

//Button Remove
public class ActionRemove implements ActionListener
{

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

        // step 1
        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);
            }
       }

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

There is another way (see http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CopyOnWriteArrayList.html class) but since your collection is inside the Jena api you can't use it ;).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文