Frage

How to update a model with jena API and SPARQL, eg. update value of a node . The SPARQL 1.1 Update description said that SPARQL 1.1 Update is an update language for RDF graphs. INSERT AND DELETE can not be usde for update model. Is there any method for update model,like updating a RDF graph?

War es hilfreich?

Lösung

You can use SPARQL Updates with Models, and OntModels are Models, so you can use SPARQL Update with OntModels. Here's a simple example that removes all rdfs:labels from an individual and adds a new one:

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.update.UpdateAction;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDFS;

public class OntModelUpdateExample {
    public static void main(String[] args) {
        String ns = "http://stackoverflow.com/q/23102507/1281433/";
        OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
        model.setNsPrefix( "", ns );

        Individual i = model.createIndividual( ns+"JDoe", OWL.Thing );
        i.addLabel( "John Doe", "en" );

        model.write( System.out, "TTL" );

        String rename = "" +
                "prefix : <"+ns+">\n" +
                "prefix rdfs: <"+RDFS.getURI()+">\n" +
                "delete { :JDoe rdfs:label ?label }\n" +
                "insert { :JDoe rdfs:label \"Jack Doe\"@en }\n" +
                "where { :JDoe rdfs:label ?label }";

        UpdateAction.parseExecute( rename, model );

        model.write( System.out, "TTL" );
    }
}

The model before and after are as follows:

@prefix :      <http://stackoverflow.com/q/23102507/1281433/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:JDoe   a           owl:Thing ;
        rdfs:label  "John Doe"@en .
@prefix :      <http://stackoverflow.com/q/23102507/1281433/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:JDoe   a           owl:Thing ;
        rdfs:label  "Jack Doe"@en .
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top