如何使用jena API和SPARQL更新模型,例如。更新节点的值。 SPARQL 1.1 Update说明SPARQL 1.1 Update是RDF图的更新语言。 INSERT和DELETE不能用于更新模型。有更新模型的方法,比如更新RDF图吗?
答案 0 :(得分:2)
您可以将SPARQL更新与模型一起使用,而OntModel是模型,因此您可以将SPARQL更新与OntModels一起使用。这是一个简单的示例,它从个人中删除所有rdfs:标签并添加一个新标签:
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" );
}
}
之前和之后的模型如下:
@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 .