简体   繁体   中英

Accessing Object Property for Individual using Apache Jena

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.");
    }
}



}

Try this (replace the property URI accordingly):

final Property p = model.createObjectProperty("http://example.org/Treated_by");
final RDFNode object = i.getPropertyValue(p);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM