Search code examples
javaswitch-statementowl-api

switch (unknown variable type)


I want to do some treatments depending on superClassII type. So, I defined :

ClassExpressionType superClassIIType = superClassII.getClassExpressionType();

If (superClassIIType = "ObjectMincardinality"or"ObjectExactCardinality"`so,

I do the same treatment for superClassII

else we do nothing.

Here is a part of my source code:

switch(superClassIIType){
    case OBJECT_EXACT_CARDINALITY:
    case OBJECT_MIN_CARDINALITY:
        if ((((superClassIIType)superClassII).getProperty().equals(
            ((OWLObjectMinCardinality)superClass).getProperty() ){

            int superClassIICardinality =((superClassIIType)superClassII).getCardinality();
            ...
        }
        break;
    default:
        break;
}

But, it seems that superClassIIType is not accepted. I tried to do what I need by a simple if ...else where there the if and else blocks contain both the same treatment(it works but I think it is naive).

Could you please tell me where is the problem and if implement what I need using another alternative))


Solution

  • This is best done with a visitor, although it will require you to duplicate the code. If you are using OWLAPI 5, implement OWLClassExpressionVisitor - here are default methods for everything so you'll have to implement just the two methods for the types you wish to use.

        OWLClassExpressionVisitor visitor = new OWLClassExpressionVisitor() {
            public void visit(OWLObjectMinCardinality c) {
                int superClassIICardinality = c.getCardinality();
            }
    
            @Override
            public void visit(OWLObjectExactCardinality ce) {
                int superClassIICardinality = c.getCardinality();
            }
        };
        superClassII.accept(visitor);