使用 OWL API 检索 OWL class 限制
Retrieve OWL class restrictions using OWL API
我正在研究 ontology population.I 想为 ontology 中的每个 class 提取限制,例如 somevaluefrom、alvaluefrom、基数限制数据属性和对象 properties.I' 使用 OWL API.now 我正在使用 OWLClassExpression class 获取数据 属性 特定限制class.this 是我的代码。
private static JSONObject getclassaxioms(OWLClass cls,OWLOntology ontology){
Set<OWLClassAxiom> tempAx=ontology.getAxioms(cls);
JSONObject datapropertyrestrictions = new JSONObject();
JSONArray data_has_value = new JSONArray();
JSONArray data_max_cardinality = new JSONArray();
JSONArray data_all_values_from = new JSONArray();
JSONArray data_exact_cardinality = new JSONArray();
JSONArray data_min_cardinality = new JSONArray();
JSONArray data_some_values_from = new JSONArray();
for(OWLClassAxiom ax: tempAx){
for(OWLClassExpression nce:ax.getNestedClassExpressions()) {
if(nce.getClassExpressionType()==ClassExpressionType.DATA_HAS_VALUE) {
// System.out.println("\t\t\tDATA_HAS_VALUE:" + nce.getNNF());
String dpString = nce.toString();
String propertyname;
String enumm;
String datatype;
JSONObject hasdatavalueobj = new JSONObject();
if (dpString.contains("#")) {
propertyname = dpString.substring(
dpString.indexOf("#") + 1,
dpString.lastIndexOf(">"));
hasdatavalueobj.put("propertyname",propertyname);
}
if(dpString.contains("\"")){
enumm = dpString.substring(
dpString.indexOf("\"") + 1,
dpString.lastIndexOf("\""));
hasdatavalueobj.put("enumvalue",enumm);
}
if(dpString.contains("xsd")){
datatype = dpString.substring(
dpString.indexOf("xsd") + 4,
dpString.lastIndexOf(")"));
hasdatavalueobj.put("datatype",datatype);
}
data_has_value.add(hasdatavalueobj);
// System.out.println(hasdatavalueobj);
//System.out.println(nce.toString());
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_MAX_CARDINALITY) {
// System.out.println("\t\t\tDATA_MAX_CARDINALITY:" + nce.getNNF());
JSONObject hasdatavalueobj = getCardinality(nce.toString());
data_max_cardinality.add(hasdatavalueobj);
System.out.println(nce.toString());
System.out.println(hasdatavalueobj);
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_ALL_VALUES_FROM) {
//System.out.println("\t\t\tDATA_ALL_VALUES_FROM:" + nce.getNNF());
data_all_values_from.add(nce.getNNF().toString());
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_EXACT_CARDINALITY) {
//System.out.println("\t\t\tDATA_EXACT_CARDINALITY:" + nce.getNNF());
JSONObject hasdatavalueobj = getCardinality(nce.toString());
data_exact_cardinality.add(hasdatavalueobj);
System.out.println(nce.toString());
System.out.println(hasdatavalueobj);
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_MIN_CARDINALITY) {
//System.out.println("\t\t\tDATA_MIN_CARDINALITY:" + nce.getNNF());
JSONObject hasdatavalueobj = getCardinality(nce.toString());
data_min_cardinality.add(hasdatavalueobj);
System.out.println(nce.toString());
System.out.println(hasdatavalueobj);
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_SOME_VALUES_FROM) {
// System.out.println("\t\t\tDATA_SOME_VALUES_FROM:" + nce.getNNF());
data_some_values_from.add(nce.getNNF().toString());
System.out.println(nce.toString());
}
//if(nce.getClassExpressionType()==ClassExpressionType.)
}
}
datapropertyrestrictions.put("data_has_value",data_has_value);
datapropertyrestrictions.put("data_max_cardinality",data_max_cardinality);
datapropertyrestrictions.put("data_all_values_from",data_all_values_from);
datapropertyrestrictions.put("data_exact_cardinality",data_exact_cardinality);
datapropertyrestrictions.put("data_min_cardinality",data_min_cardinality);
datapropertyrestrictions.put("data_some_values_from",data_some_values_from);
return datapropertyrestrictions;
}
private static JSONObject getCardinality(String dpString){
String propertyname;
String cardinalityvalue;
String datatype;
JSONObject hasdatavalueobj = new JSONObject();
if (dpString.contains("#")) {
propertyname = dpString.substring(
dpString.indexOf("#") + 1,
dpString.lastIndexOf(">"));
hasdatavalueobj.put("propertyname",propertyname);
}
if(dpString.contains("(")){
cardinalityvalue = dpString.substring(
dpString.indexOf("(") + 1,
dpString.lastIndexOf("<") -1 );
hasdatavalueobj.put("cardinalityvalue",cardinalityvalue);
}
if(dpString.contains("DataRangeRestriction")) {
if (dpString.contains("xsd")) {
datatype = dpString.substring(
dpString.indexOf("xsd") + 4,
dpString.lastIndexOf(" "));
hasdatavalueobj.put("datatype", datatype);
}
}
else{
if (dpString.contains("xsd")) {
datatype = dpString.substring(
dpString.indexOf("xsd") + 4,
dpString.lastIndexOf(")"));
hasdatavalueobj.put("datatype", datatype);
}
}
return hasdatavalueobj;
}
我得到数据 属性 限制 IRI 并使用字符串 class 函数我记录这些限制 json array.i 认为这是最糟糕的 implementation.Please 谁能帮帮我?
您可以使用 OWLClassVisitor
实现访问嵌套的 class 表达式(代码中的 nce
),其方法中包含 if
分支的内容。这保证您不会忘记 class 表达式类型之一。
我正在研究 ontology population.I 想为 ontology 中的每个 class 提取限制,例如 somevaluefrom、alvaluefrom、基数限制数据属性和对象 properties.I' 使用 OWL API.now 我正在使用 OWLClassExpression class 获取数据 属性 特定限制class.this 是我的代码。
private static JSONObject getclassaxioms(OWLClass cls,OWLOntology ontology){
Set<OWLClassAxiom> tempAx=ontology.getAxioms(cls);
JSONObject datapropertyrestrictions = new JSONObject();
JSONArray data_has_value = new JSONArray();
JSONArray data_max_cardinality = new JSONArray();
JSONArray data_all_values_from = new JSONArray();
JSONArray data_exact_cardinality = new JSONArray();
JSONArray data_min_cardinality = new JSONArray();
JSONArray data_some_values_from = new JSONArray();
for(OWLClassAxiom ax: tempAx){
for(OWLClassExpression nce:ax.getNestedClassExpressions()) {
if(nce.getClassExpressionType()==ClassExpressionType.DATA_HAS_VALUE) {
// System.out.println("\t\t\tDATA_HAS_VALUE:" + nce.getNNF());
String dpString = nce.toString();
String propertyname;
String enumm;
String datatype;
JSONObject hasdatavalueobj = new JSONObject();
if (dpString.contains("#")) {
propertyname = dpString.substring(
dpString.indexOf("#") + 1,
dpString.lastIndexOf(">"));
hasdatavalueobj.put("propertyname",propertyname);
}
if(dpString.contains("\"")){
enumm = dpString.substring(
dpString.indexOf("\"") + 1,
dpString.lastIndexOf("\""));
hasdatavalueobj.put("enumvalue",enumm);
}
if(dpString.contains("xsd")){
datatype = dpString.substring(
dpString.indexOf("xsd") + 4,
dpString.lastIndexOf(")"));
hasdatavalueobj.put("datatype",datatype);
}
data_has_value.add(hasdatavalueobj);
// System.out.println(hasdatavalueobj);
//System.out.println(nce.toString());
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_MAX_CARDINALITY) {
// System.out.println("\t\t\tDATA_MAX_CARDINALITY:" + nce.getNNF());
JSONObject hasdatavalueobj = getCardinality(nce.toString());
data_max_cardinality.add(hasdatavalueobj);
System.out.println(nce.toString());
System.out.println(hasdatavalueobj);
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_ALL_VALUES_FROM) {
//System.out.println("\t\t\tDATA_ALL_VALUES_FROM:" + nce.getNNF());
data_all_values_from.add(nce.getNNF().toString());
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_EXACT_CARDINALITY) {
//System.out.println("\t\t\tDATA_EXACT_CARDINALITY:" + nce.getNNF());
JSONObject hasdatavalueobj = getCardinality(nce.toString());
data_exact_cardinality.add(hasdatavalueobj);
System.out.println(nce.toString());
System.out.println(hasdatavalueobj);
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_MIN_CARDINALITY) {
//System.out.println("\t\t\tDATA_MIN_CARDINALITY:" + nce.getNNF());
JSONObject hasdatavalueobj = getCardinality(nce.toString());
data_min_cardinality.add(hasdatavalueobj);
System.out.println(nce.toString());
System.out.println(hasdatavalueobj);
}
if(nce.getClassExpressionType()==ClassExpressionType.DATA_SOME_VALUES_FROM) {
// System.out.println("\t\t\tDATA_SOME_VALUES_FROM:" + nce.getNNF());
data_some_values_from.add(nce.getNNF().toString());
System.out.println(nce.toString());
}
//if(nce.getClassExpressionType()==ClassExpressionType.)
}
}
datapropertyrestrictions.put("data_has_value",data_has_value);
datapropertyrestrictions.put("data_max_cardinality",data_max_cardinality);
datapropertyrestrictions.put("data_all_values_from",data_all_values_from);
datapropertyrestrictions.put("data_exact_cardinality",data_exact_cardinality);
datapropertyrestrictions.put("data_min_cardinality",data_min_cardinality);
datapropertyrestrictions.put("data_some_values_from",data_some_values_from);
return datapropertyrestrictions;
}
private static JSONObject getCardinality(String dpString){
String propertyname;
String cardinalityvalue;
String datatype;
JSONObject hasdatavalueobj = new JSONObject();
if (dpString.contains("#")) {
propertyname = dpString.substring(
dpString.indexOf("#") + 1,
dpString.lastIndexOf(">"));
hasdatavalueobj.put("propertyname",propertyname);
}
if(dpString.contains("(")){
cardinalityvalue = dpString.substring(
dpString.indexOf("(") + 1,
dpString.lastIndexOf("<") -1 );
hasdatavalueobj.put("cardinalityvalue",cardinalityvalue);
}
if(dpString.contains("DataRangeRestriction")) {
if (dpString.contains("xsd")) {
datatype = dpString.substring(
dpString.indexOf("xsd") + 4,
dpString.lastIndexOf(" "));
hasdatavalueobj.put("datatype", datatype);
}
}
else{
if (dpString.contains("xsd")) {
datatype = dpString.substring(
dpString.indexOf("xsd") + 4,
dpString.lastIndexOf(")"));
hasdatavalueobj.put("datatype", datatype);
}
}
return hasdatavalueobj;
}
我得到数据 属性 限制 IRI 并使用字符串 class 函数我记录这些限制 json array.i 认为这是最糟糕的 implementation.Please 谁能帮帮我?
您可以使用 OWLClassVisitor
实现访问嵌套的 class 表达式(代码中的 nce
),其方法中包含 if
分支的内容。这保证您不会忘记 class 表达式类型之一。