0

I have created an OWL ontology using Protégé, describing a patient database system. I am now attempting to develop a Java code using Apache Jena to read the OWL file I created, then perform a number of operations on it. My primary goal is to get my code to be able to find a specific Individual by name (Patient name for example) and then access a specific Object Property for that individual, and output its value. For example, A patient "John" has an object property "Treated_By" which corresponds to another individual "Amy" (Amy is an individual of type doctor). However, I have been unable to figure out which Jena method is used to retrieve Object property values from a certain individual.

Here is my code (Please ignore comments, they are fragments of previous attempts for this task):

public class Main {

public static void main(String[] args) {
    OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    String fileName = "C:/Users/Ahmed Medhat/Documents/assignment1ontv3.0.owl";

    try {
        InputStream inputStream = new FileInputStream(fileName);
        model.read(inputStream, "RDF/XML");
        //model.read(inputStream, "OWL/XML");
        inputStream.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Patient Name: ");
    String patientName = sc.next();

    ExtendedIterator<Individual> itI = model.listIndividuals();

    while (itI.hasNext()) {
        Individual i = itI.next();
        String localName = i.getLocalName();
        //System.out.println(patientName);
        //System.out.println(localName);
        if(localName.equals(patientName))
        {
            //System.out.println("Conditional Code Accessed.");
            OntClass Class = i.getOntClass();
            System.out.println("Patient Disease is: " + Class.listDeclaredProperties());
        }
        System.out.println("Failed.");
    }
}



}

1 Answers1

0

Try this (replace the property URI accordingly):

final Property p = model.createObjectProperty("http://example.org/Treated_by");
final RDFNode object = i.getPropertyValue(p);
  • Thank you for your answer, This method worked fine but I am facing two issues with it. The first is that the output is always the IRI of the property, and I need it to output its friendly name instead. The second issue is that when there are multiple instances under the same property name (For example, a patient taking multiple medications), This method only outputs the first property, and ignores the rest. – Ahmed Medhat Dec 09 '21 at 01:10
  • @AhmedMedhat 1) the "friendly name" has to be inside the data, so does your property have an `rdfs:label` in your ontology. That is the best practice. Or you use the local name which not always is human-readable. 2) call `listPropertyValues` instead. I'd also point to the [documentation](https://jena.apache.org/documentation/ontology/#the-generic-ontology-type-ontresource) and [Javadoc](https://jena.apache.org/documentation/javadoc/jena/) for future reference. – UninformedUser Dec 09 '21 at 07:41
  • 1
    Thanks! Using the `rdfs:label` worked perfectly for retrieving the friendly name, and so did `listPropertyValues` for multiple instances, all I had to do was simple Node conversions and then outputting each Node value in the Iterator seperately. – Ahmed Medhat Dec 09 '21 at 18:20