Question

Let consider the following ontology, where PERSON, NAME, SURNAME and IDENTIFICATION are classes. NAME and SURNAME are subclass of IDENTIFICATION. hasSurname and hasName are functional ObjectProperty. hasValue is a DataTypeProperty with domain IDENTIFICATION and Range string.

enter image description here

Let do an automatic instanciation of this model using jena with 2 persons, person1 is "Henry Ford" and person2 is "Harrison Ford".

To deal with this repetition a want to automatically instanciate the ontology according to the following schema, where the individual name1 is used both for person1 and person2.

String   NS    = .... // the name space
OntModel model = .... // Jena model to use   

// creating all the individuals with random uri

Individual person1  = model.createIndividual(NS + "PERSON" + Math.random());
Individual name1    = model.createIndividual(NS + "NAME" + Math.random());
Individual surname1 = model.createIndividual(NS + "SURNAME" + Math.random());
Individual person2  = model.createIndividual(NS + "PERSON" + Math.random());
Individual name2    = model.createIndividual(NS + "NAME" + Math.random());
Individual surname2 = model.createIndividual(NS + "SURNAME" + Math.random());

// asserting that ...
// person1 _hasName_ (name1 _hasValue_ "Ford")     
// person1 _hasSurname (surname1 _hasValue_ "Henry")

name1.addProperty(model.getOntProperty(NS + "hasValue"), Resourcefactory.createLiteral("Ford"));
surname1.addProperty(model.getOntProperty(NS + "hasValue"), Resourcefactory.createLiteral("Henry"));
model.add(person1, model.getOntProperty(NS + "hasName"), name1);
model.add(person1, model.getOntProperty(NS + "hasSurname"), surname1);


name1 = null; // loosing reference to name1

// asserting that ...
// person2 _hasName_ name1
// person2 _hasSurname (surname2 _hasValue_ "Harrison")

How to find individual of class NAME which property hasvalue is "Ford" to correctly finish the instanciation ?

Thanks for any reply.

Was it helpful?

Solution

Conceptually, why do surname1 and surname2 have to be distinct individuals? The use of Math.random() looks very odd there. If you think about your stated problem, you have two individuals, Henry Ford and Harrison Ford, and in your domain model you want the name and surname to be separate individuals (I assume you have a good reason for this, it's an odd way of approaching this particular problem). So the question I have for your modelling approach is: is there one surname Ford that is shared by both persons, or are there two surnames which happen to have the same hasValue literal?

Personally, I would approach this by exploiting the common structure in the graph:

ns:ford 
  a ns:Surname ;
  ns:hasValue "Ford".

ns:harrison
  a ns:FirstName ;
  ns:hasValue "Harrison".

ns:henry
  a ns:FirstName ;
  ns:hasValue "Henry".

ns:person1
  a ns:Person ;
  ns:firstName ns:henry ;
  ns:surname ns:ford .

ns:person2
  a ns:Person ;
  ns:firstName ns:harrison ;
  ns:surname ns:ford .

But, I don't know what problem you're trying to solve so I can't really say whether this is the right approach for you. It would make it easy, for example, to find all the people that have the same surname.

Generally speaking, if you don't want to share nodes in a graph, so you do want the two different uses of Ford to be distinct, you could use blank nodes to save messing about with random numbers:

ns:person1
  a ns:Person ;
  [a ns:SurName ; ns:hasValue "Ford"] ;
  [a ns:FirstName ; ns:hasValue "Henry"].

So my main answer to you question is to be clear about the graph you're trying to build. Then it will be a lot clearer what Jena calls you need to make.

To answer your specific question, if you want to locate a particular resource based on a pattern of its properties, I'd suggest using a SPARQL query. You can do it with a pattern of Jena API calls, but SPARQL queries are much more compact once you get past a small initial size of pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top