Exemplo n.º 1
0
 private String interpretInterpretFunctionJoinParameter(FormulaElement child, Sprite sprite) {
   String parameterInterpretation = "";
   if (child != null) {
     if (child.getElementType() == ElementType.NUMBER) {
       Double number = Double.valueOf((String) child.interpretRecursive(sprite));
       if (number.isNaN()) {
         parameterInterpretation = "";
       } else {
         if (isInteger(number)) {
           parameterInterpretation += number.intValue();
         } else {
           parameterInterpretation += number;
         }
       }
     } else if (child.getElementType() == ElementType.STRING) {
       parameterInterpretation = child.value;
     } else {
       parameterInterpretation += child.interpretRecursive(sprite);
     }
   }
   return parameterInterpretation;
 }
Exemplo n.º 2
0
  private Object interpretFunctionLength(Object left, Sprite sprite) {
    if (leftChild == null) {
      return 0d;
    }
    if (leftChild.type == ElementType.NUMBER) {
      return (double) leftChild.value.length();
    }
    if (leftChild.type == ElementType.STRING) {
      return (double) leftChild.value.length();
    }
    if (leftChild.type == ElementType.USER_VARIABLE) {
      return (double) handleLengthUserVariableParameter(sprite);
    }
    if (leftChild.type == ElementType.USER_LIST) {
      DataContainer dataContainer =
          ProjectManager.getInstance().getSceneToPlay().getDataContainer();
      UserList userList = dataContainer.getUserList(leftChild.getValue(), sprite);
      if (userList == null) {
        return 0d;
      }
      if (userList.getList().size() == 0) {
        return 0d;
      }

      Object interpretedList = leftChild.interpretRecursive(sprite);
      if (interpretedList instanceof Double) {
        Double interpretedListDoubleValue = (Double) interpretedList;
        if (interpretedListDoubleValue.isNaN() || interpretedListDoubleValue.isInfinite()) {
          return 0d;
        }
        return (double) (String.valueOf(interpretedListDoubleValue.intValue())).length();
      }
      if (interpretedList instanceof String) {
        String interpretedListStringValue = (String) interpretedList;
        return (double) interpretedListStringValue.length();
      }
    }
    if (left instanceof Double && ((Double) left).isNaN()) {
      return 0d;
    }
    return (double) (String.valueOf(left)).length();
  }
Exemplo n.º 3
0
  public Object interpretRecursive(Sprite sprite) {

    Object returnValue = 0d;

    switch (type) {
      case BRACKET:
        returnValue = rightChild.interpretRecursive(sprite);
        break;
      case NUMBER:
        returnValue = value;
        break;
      case OPERATOR:
        Operators operator = Operators.getOperatorByValue(value);
        returnValue = interpretOperator(operator, sprite);
        break;
      case FUNCTION:
        Functions function = Functions.getFunctionByValue(value);
        returnValue = interpretFunction(function, sprite);
        break;
      case SENSOR:
        returnValue = interpretSensor(sprite);
        break;
      case USER_VARIABLE:
        returnValue = interpretUserVariable(sprite);
        break;
      case USER_LIST:
        returnValue = interpretUserList(sprite);
        break;
      case STRING:
        returnValue = value;
        break;
      case COLLISION_FORMULA:
        try {
          returnValue = interpretCollision(value);
        } catch (Exception exception) {
          returnValue = 0d;
          Log.e(getClass().getSimpleName(), Log.getStackTraceString(exception));
        }
    }
    return normalizeDegeneratedDoubleValues(returnValue);
  }
Exemplo n.º 4
0
  private Object interpretOperator(Operators operator, Sprite sprite) {

    if (leftChild != null) { // binary operator
      Object leftObject;
      Object rightObject;
      try {
        leftObject = leftChild.interpretRecursive(sprite);
      } catch (NumberFormatException numberFormatException) {
        leftObject = Double.NaN;
      }

      try {
        rightObject = rightChild.interpretRecursive(sprite);
      } catch (NumberFormatException numberFormatException) {
        rightObject = Double.NaN;
      }

      Double left;
      Double right;

      switch (operator) {
        case PLUS:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left + right;
        case MINUS:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left - right;
        case MULT:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left * right;
        case DIVIDE:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left / right;
        case POW:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return java.lang.Math.pow(left, right);
        case EQUAL:
          return interpretOperatorEqual(leftObject, rightObject);
        case NOT_EQUAL:
          return interpretOperatorEqual(leftObject, rightObject) == 1d ? 0d : 1d;
        case GREATER_THAN:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left.compareTo(right) > 0 ? 1d : 0d;
        case GREATER_OR_EQUAL:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left.compareTo(right) >= 0 ? 1d : 0d;
        case SMALLER_THAN:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left.compareTo(right) < 0 ? 1d : 0d;
        case SMALLER_OR_EQUAL:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left.compareTo(right) <= 0 ? 1d : 0d;
        case LOGICAL_AND:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return (left * right) != 0d ? 1d : 0d;
        case LOGICAL_OR:
          left = interpretOperator(leftObject);
          right = interpretOperator(rightObject);
          return left != 0d || right != 0d ? 1d : 0d;
      }
    } else { // unary operators
      Object rightObject;
      try {
        rightObject = rightChild.interpretRecursive(sprite);
      } catch (NumberFormatException numberFormatException) {
        rightObject = Double.NaN;
      }

      switch (operator) {
        case MINUS:
          Double result = interpretOperator(rightObject);
          return -result;
        case LOGICAL_NOT:
          return interpretOperator(rightObject) == 0d ? 1d : 0d;
      }
    }
    return 0d;
  }
Exemplo n.º 5
0
  private Object interpretFunction(Functions function, Sprite sprite) {
    Object left = null;
    Object right = null;

    Double doubleValueOfLeftChild = null;
    Double doubleValueOfRightChild = null;

    if (leftChild != null) {
      left = leftChild.interpretRecursive(sprite);
      if (left instanceof String) {
        try {
          doubleValueOfLeftChild = Double.valueOf((String) left);
        } catch (NumberFormatException numberFormatException) {
          Log.d(getClass().getSimpleName(), "Couldn't parse String", numberFormatException);
        }
      } else {
        doubleValueOfLeftChild = (Double) left;
      }
    }

    if (rightChild != null) {
      right = rightChild.interpretRecursive(sprite);
      if (right instanceof String) {
        try {
          doubleValueOfRightChild = Double.valueOf((String) right);
        } catch (NumberFormatException numberFormatException) {
          Log.d(getClass().getSimpleName(), "Couldn't parse String", numberFormatException);
        }
      } else {
        doubleValueOfRightChild = (Double) right;
      }
    }

    switch (function) {
      case SIN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.sin(Math.toRadians(doubleValueOfLeftChild));
      case COS:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.cos(Math.toRadians(doubleValueOfLeftChild));
      case TAN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.tan(Math.toRadians(doubleValueOfLeftChild));
      case LN:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.log(doubleValueOfLeftChild);
      case LOG:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.log10(doubleValueOfLeftChild);
      case SQRT:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.sqrt(doubleValueOfLeftChild);
      case RAND:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : interpretFunctionRand(doubleValueOfLeftChild, doubleValueOfRightChild);
      case ABS:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.abs(doubleValueOfLeftChild);
      case ROUND:
        return doubleValueOfLeftChild == null
            ? 0d
            : (double) java.lang.Math.round(doubleValueOfLeftChild);
      case PI:
        return java.lang.Math.PI;
      case MOD:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : interpretFunctionMod(doubleValueOfLeftChild, doubleValueOfRightChild);
      case ARCSIN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.toDegrees(Math.asin(doubleValueOfLeftChild));
      case ARCCOS:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.toDegrees(Math.acos(doubleValueOfLeftChild));
      case ARCTAN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.toDegrees(Math.atan(doubleValueOfLeftChild));
      case EXP:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.exp(doubleValueOfLeftChild);
      case POWER:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : java.lang.Math.pow(doubleValueOfLeftChild, doubleValueOfRightChild);
      case FLOOR:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.floor(doubleValueOfLeftChild);
      case CEIL:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.ceil(doubleValueOfLeftChild);
      case MAX:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : java.lang.Math.max(doubleValueOfLeftChild, doubleValueOfRightChild);
      case MIN:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : java.lang.Math.min(doubleValueOfLeftChild, doubleValueOfRightChild);
      case TRUE:
        return 1d;
      case FALSE:
        return 0d;
      case LETTER:
        return interpretFunctionLetter(right, left);
      case LENGTH:
        return interpretFunctionLength(left, sprite);
      case JOIN:
        return interpretFunctionJoin(sprite);
      case ARDUINODIGITAL:
        Arduino arduinoDigital =
            ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE)
                .getDevice(BluetoothDevice.ARDUINO);
        if (arduinoDigital != null && left != null) {
          if (doubleValueOfLeftChild < 0 || doubleValueOfLeftChild > 13) {
            return 0d;
          }
          return arduinoDigital.getDigitalArduinoPin(doubleValueOfLeftChild.intValue());
        }
        break;
      case ARDUINOANALOG:
        Arduino arduinoAnalog =
            ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE)
                .getDevice(BluetoothDevice.ARDUINO);
        if (arduinoAnalog != null && left != null) {
          if (doubleValueOfLeftChild < 0 || doubleValueOfLeftChild > 5) {
            return 0d;
          }
          return arduinoAnalog.getAnalogArduinoPin(doubleValueOfLeftChild.intValue());
        }
        break;
      case RASPIDIGITAL:
        RPiSocketConnection connection = RaspberryPiService.getInstance().connection;
        int pin = doubleValueOfLeftChild.intValue();
        try {
          return connection.getPin(pin) ? 1d : 0d;
        } catch (Exception e) {
          Log.e(getClass().getSimpleName(), "RPi: exception during getPin: " + e);
        }
        break;
      case MULTI_FINGER_TOUCHED:
        return TouchUtil.isFingerTouching(doubleValueOfLeftChild.intValue()) ? 1d : 0d;
      case MULTI_FINGER_X:
        return Double.valueOf(TouchUtil.getX(doubleValueOfLeftChild.intValue()));
      case MULTI_FINGER_Y:
        return Double.valueOf(TouchUtil.getY(doubleValueOfLeftChild.intValue()));
      case LIST_ITEM:
        return interpretFunctionListItem(left, sprite);
      case CONTAINS:
        return interpretFunctionContains(right, sprite);
      case NUMBER_OF_ITEMS:
        return interpretFunctionNumberOfItems(left, sprite);
    }
    return 0d;
  }