简体   繁体   中英

Retrieve OWL class restrictions using OWL API

I'm working on research regarding ontology population.I want to extract restrictions for each and every class in ontology such as somevaluefrom, alvaluefrom, cardinality restrictions on data properties and object properties.I'm Using the OWL API.now I'm using OWLClassExpression class to get data property restrictions for a particular class.this is my code.

    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;
}

I get the data property restriction IRI and using string class functions i record those restrictions on a json array.i think this is an worst implementation.Please anyone can help me?

You can visit the nested class expressions ( nce in your code) with an OWLClassVisitor implementation that has the contents of your if branches in its methods. This guarantees that you don't forget one of the class expression types.

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