dimanche 20 décembre 2015

Or switch cases with reflection discretization

In class DataTuple.java. I have this attributes

//Ints position
public int pacmanPosition;
public int blinkyDist = -1;
public int inkyDist = -1;
public int pinkyDist = -1;
public int sueDist = -1;

//Enum Move {UP, DOWN, LEFT, RIGHT, NEUTRAL}
public MOVE blinkyDir;
public MOVE inkyDir;
public MOVE pinkyDir;
public MOVE sueDir;

//booleans
public boolean isBlinkyEdible = false;
public boolean isInkyEdible = false;
public boolean isPinkyEdible = false;
public boolean isSueEdible = false;

Here is the function discretize. it's like or cases. 4 cases. 1 for MOVE's, 2 and 3 for ints, 4 for booleans.

public String discretize(String string) {
    // TODO Auto-generated method stub
    Field field = this.getClass().getDeclaredField(string);
    field.setAccessible(true);

    switch (string) {
        case "DirectionChosen":
        case "blinkyDir":
        case "inkyDir":
        case "pinkyDir":
        case "sueDir":
            MOVE direction = (MOVE) field.get(this);
            return direction.toString();
            break;
        case "pacmanPosition":
            //Here i don't need use the reflection
            return discretizePosition(this.pacmanPosition).toString();
        case "blinkyDist":
        case "inkyDist":
        case "pinkyDist":
        case "sueDist":
            int distance =  (int) field.get(this);
            return discretizeDistance(distance).toString();
            break;
        case "isBlinkyEdible":
        case "isInkyEdible":
        case "isPinkyEdible":
        case "isSueEdible":
            String bool = (String) field.get(this);
            return bool;    
            break;

        default:
        break;
    }
}

discretizePosition and discretizeDistance return a DiscreteTag. Param: double

public enum DiscreteTag {
    VERY_LOW, LOW, MEDIUM, HIGH, VERY_HIGH, NONE;

    public static DiscreteTag DiscretizeDouble(double aux) {
        if (aux < 0.1)
            return DiscreteTag.VERY_LOW;
        else if (aux <= 0.3)
            return DiscreteTag.LOW;
        else if (aux <= 0.5)
            return DiscreteTag.MEDIUM;
        else if (aux <= 0.7)
            return DiscreteTag.HIGH;
        else
            return DiscreteTag.VERY_HIGH;
    }
}

in class decTree.java. Here I want to put the string attribute with the string name of the attribute i want. Example:

String attribute = "DirectionChosen" 
DataTuple tuple = ...; //value of tuple.DirectionChosen = MOVE.UP 

Here i want to return a string. In this case is "UP" it's equal to another string like "UP" or other string i want to pass

if(tuple.discretize(attribute)=="UP") {
  //...
}

The poblem is the reflection. It doesn't work

Field field = this.getClass().getDeclaredField(string);
field.setAccessible(true);

case "sueDist":
        int distance =  (int) field.get(this);
        return discretizeDistance(distance).toString();
        break;





Aucun commentaire:

Enregistrer un commentaire