3

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?

3
  • AFAIK you can only update a graph with combining INSERT and DELETE queries. Apr 16, 2014 at 7:20
  • Thank you , Is there any query method for update ontology model?
    – Hello K
    Apr 16, 2014 at 7:26
  • 2
    A model is an RDF graph. You can use SPARQL Update.
    – AndyS
    Apr 16, 2014 at 10:53

1 Answer 1

4

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 .
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.