Sparql query doesn't upadate when insert some

2020-03-04 08:34发布

问题:

I'm trying to insert data through my java code to the owl file which is loaded into Fuseki server. Update query doesn't give any error message. But owl file doesn't update.I'm using jena library and implemented using java code. What is the wrong in my code?

    public boolean addLecturerTriples(String fName, String lName,
    String id, String module) {
    try{
    ArrayList<String> subject = new ArrayList<String>();
    ArrayList<String> predicate = new ArrayList<String>();
    ArrayList<String> object = new ArrayList<String>();

    subject.add("<http://people.brunel.ac.uk/~csstnns/university.owl#"+fName+">");
    predicate.add("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>");
    object.add("<http://people.brunel.ac.uk/~csstnns/university.owl#Lecturer>");

    for(int i = 0; i < subject.size(); i++){
        String qry = "INSERT DATA"+
                "{"+
                subject.get(i)+"\n"+
                predicate.get(i)+"\n"+
                object.get(i)+"\n"+
                "}";

        UpdateRequest update  = UpdateFactory.create(qry);
        UpdateProcessor qexec = UpdateExecutionFactory.createRemote(update, "http://localhost:3030/ds/update");
        qexec.execute();
    }
    }catch(Exception e){
        return false;
    }
    return true;
}

回答1:

It would help if you have provided a minimal complete example i.e. you had included your Fuseki configuration and the details of how your OWL file is loaded into Fuseki.

However I will assume you have not used any specific configuration and just launching Fuseki like so:

java -jar fuseki-server-VER.jar --update --loc /path/to/db /ds

So what you've done here is launch Fuseki with updates enabled and using the location /path/to/db as the on-disk TDB database location and the URL /ds for your dataset

The you open your browser and click through Control Panel > /ds and then use the Upload file function to upload your OWL file. When you upload a file it is read into Fuseki and copied into the dataset, in this example your dataset is the on disk TDB database located at /path/to/db.

It is important to understand that no reference to the original file is kept since Fuseki has simply copied the data from the file to the dataset.

You then use the SPARQL Update form to add some data (or in your case you do this via Java code). The update is applied to the dataset which to reiterate is in this example the on disk TDB database located at /path/to/db which has no reference to the original file. Therefore your original file will not change.

Using SPARQL Update to update the original file

If Fuseki is not essential then you could just load your file into local memory and run the update there instead:

Model m = ModelFactory.createDefaultModel();
m.read("example.owl", "RDF/XML");

// Prepare your update...

// Create an UpdateExecution on the local model
UpdateProcessor processor = UpdateExecutionFactory.create(update, GraphStoreFactory.create(m));
processor.execute();

// Save the updated model 
updated.write(new FileOutputStream("example.owl"), "RDF/XML");

However if you want to/must stick with using Fuseki you can update your original file by retrieving the modified graph from Fuseki and writing it back out to your file e.g.

DatasetAccessor accessor = DatasetAccessorFactory.createHTTP("http://localhost:3030/ds/data");

// Download the updated model
Model updated = accessor.getModel();

// Save the updated model over the original file
updated.write(new FileOutputStream("example.owl"), "RDF/XML");

This example assumes that you have loaded the OWL file into the default graph, if not use the getModel("http://graph") overload to load the relevant named graph