MiForum 医学信息学论坛's Archiver

南京猿人 发表于 2008-1-3 03:22

如何编程查询owl

假设有一个owl,里面有一些statement,类似于数据库的记录.
查询的方法,首先是在owl编译器里面书写,测试SPARQL,类似于SQL.
然后,编程.

[code]public static void addWMRestriction(OntClass newClass,
Individual newCuiInstance) {
try {
// newClass will be a kind of resource
String newCuiName = newCuiInstance.toString();
// http://www.phinformatics.org/Assets/Ontology/umls.owl#C0007932
String queryString = "PREFIX umls:"
+ "PREFIX owl:"
+ "SELECT ?subject"
+ "WHERE {?subject owl:onProperty umls:correspondsTo."
+ "?subject owl:hasValue <" + newCuiName + ">" + "}";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, WM);
ResultSet rs4 = qe.execSelect();
// link sty in owl
if (rs4.hasNext() == false) {
System.out.println("Check restriction availability: false");
HasValueRestriction valRestriction = null;
valRestriction = WM.createHasValueRestriction(null,
UMLSModel.correspondsTo, newCuiInstance);
newClass.addProperty(RDFS.subClassOf, valRestriction);
}
} catch (Exception ex) {
String ErrorStr = "Exception of addWMRestriction: "
+ ex.getMessage();
System.out.println(ErrorStr);
Min_ExceptionTable.insertToExceptionTable(ErrorStr);
}

}[/code]

[[i] 本帖最后由 南京猿人 于 2008-1-3 03:28 编辑 [/i]]

南京猿人 发表于 2008-1-3 03:30

把测试过的SPARQL放入String,Execute query(String),得到一个resultset,然后处理resultset,和SQL一样。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.