开发者

How to increment a Resource?

开发者_JAVA百科

I have a function that adds instances (and their properties values) of class Person in a RDF file when I click on the Add button. I can add the first instance, but when I try to add the second, it replaces the first instance by the second.

I think it is because I do not increment Resource P1 = model.createResource(personURI+"Name"), after adding the first instance.

How can I increment a Resource in java from P1 to P2 to P3, etc?

public class ActionAjoutPersonne implements ActionListener
{

    public void actionPerformed(ActionEvent evt)
    {

        Onto f = new Onto();

        Resource p1 = f.model.createResource(f.uriBase+tabTF[0].getText());


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

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

        StringWriter sw = new StringWriter();
        f.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();
        }    
    }
}


I assume you're using Jena?

The createResource(uri) method will overwrite an existing resource if you call it again with the same URI argument.

So you have to make sure to assign a different URI for each person.

You can achieve this using some sort of auto-incrementing counter, or by making the person's name a part of the URI (using URLEncoder.encode(name, "utf-8") for example).


I'm a little unclear what your precise end goal here is, A.R., but I can see two possibilities:

  • you want a series of files on disk, with different file names, each containing the RDF fragment from one user's form
  • you want one file on disk, but which contains multiple RDF subjects, each one representing a different user's form details

In the first case, you would need to change the output file each time. Currently you have:

File file = new File("d:/teste20.rdf");

so the file name is fixed to 'teste20.rdf'. You would have to change that to use a different file name each time. There are various ways to achieve that: one way would be to have a private static int field on your class which you increment each time:

File file = new File("d:/teste_" + fileCounter++ + ".rdf");

There are other ways, which you can see from this question.

If, however, you want the second case: multiple RDF resources in one file, then it's a little hard to diagnose the problem without seeing more of the code. However, if the model in:

Resource p1 = f.model.createResource(...)

is not the same model each time (I can't tell from your code how that model is being created), the each time the listener runs, you create a model containing only the new data from the user's form, and then write that out in place. In that case, you have two choices: either don't keep creating a new model every time (but that means the model will eventually get quite large, and would also reset every time your application restarts), or read the existing contents of d:/teste20.rdf before you write out the contents of the model (not tested, but should work):

    File file = new File("d:/teste20.rdf");
    f.model.read( new FileReader( file ) );
    f.model.write( new FileWriter( file ), "RDF/XML-ABBREV" );

If you're not creating a new model each time, an assuming that the expression f.uriBase+tabTF[0].getText() gives you a unique URI string for each user (otherwise the problem is non-unique subject URI's, as Richard suggested), then we would need to see more of the code to get to the root cause.


You are just rewriting the file four times. I suggest you to use some structured programming practices to avoid this kind of mistakes in the future.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜