使用OWL API检索OWL类限制

时间:2017-09-12 07:58:30

标签: java ontology owl-api restrictions

我正在研究有关本体的人口。我想提取本体中每个类的限制,例如somevalue,alvaluefrom,基数限制数据属性和对象属性。我正在使用OWL API。现在我使用OWLClassExpression类来获取特定类的数据属性限制。这是我的代码。

    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并使用字符串类函数我在json数组上记录这些限制。我认为这是最糟糕的实现。请问有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以使用nce实现访问嵌套类表达式(代码中的OWLClassVisitor),该实现在其方法中包含if个分支的内容。这可以保证您不会忘记其中一个类表达式类型。