【发布时间】:2021-12-08 21:15:55
【问题描述】:
我使用 Protégé 创建了一个 OWL 本体,描述了一个患者数据库系统。我现在正在尝试使用 Apache Jena 开发 Java 代码来读取我创建的 OWL 文件,然后对其执行一些操作。我的主要目标是让我的代码能够按名称(例如患者姓名)找到特定个体,然后访问该个体的特定对象属性,并输出其值。例如,患者“John”具有对应于另一个个体“Amy”的对象属性“Treated_By”(Amy 是类型为医生的个体)。但是,我一直无法弄清楚使用哪种 Jena 方法从某个人那里检索 Object 属性值。
这是我的代码(请忽略 cmets,它们是此任务之前尝试的片段):
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.");
}
}
}
【问题讨论】: