Reputation: 81
Can you please help me in building the code in setting a Resource to a Class
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.ReadWrite;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.tdb.TDBFactory;
public class ModelMain {
String dbName = "DataBase";
String ns1 = "http://text.book/Someone#";
String ns;
Dataset ds;
OntModel m;
// created a model and stored in database public StoryModel(String Name){
ns = ns1 + Name;
ds = TDBFactory.createDataset(dbName);
m = ModelFactory.createOntologyModel();
}
// Assigning resource to class
public void initModel() {
m.createClass(ns + "Thing");
m.createClass(ns + "Object");
saveModel();
}
//read and write model public void saveModel() {
ds.begin(ReadWrite.WRITE);
m.write(System.out, "RDF/XML-ABBREV");
}
// creating a resource
public Resource createResource(String resourceName, String clsName) {
String resourceuri = ns + resourceName;
String classuri = ns + className;
Resource classr = m.getResource(classuri);
Individual i = m.createIndividual(resourceuri, classr);
return i;
}
// Assigning type to resource
public static boolean setType(Resource resource, String typeName)
{
//how to assign type to a resource
}
}
Upvotes: 0
Views: 140
Reputation: 2823
If you have an OntResource (which Individual is), then you may potentially consider use of the setPropertyValue(Property, RDFNode) method to set a type. Note that this differs only from @AndyS's answer in that it removes any other RDF.type
properties that already exist in the graph before adding the new triple.
Starting with the model:
:a rdf:type :Cat .
:a rdf:type :DomesticAnimal .
The following code (assuming a
is an Individual
):
a.setPropertyValue(RDF.type, TheTypeAsResource);
Will result in the model:
a: rdf:type :theType .
This, naturally, will only serve your purpose if you do not intend to add an additional type to the resource, and instead, intend to set a specific type for that resource to have.
Upvotes: 1
Reputation: 16630
You need to add a statement to the data:
model.add(resource, RDF.type, TheTypeAsAResource) ;
Upvotes: 1