Exemplo n.º 1
0
  @Override
  public boolean canBeSimplified() {
    if (this.numerator != null && this.denominator != null) {
      if (this.numerator instanceof Number && this.denominator instanceof Number) {
        Number numeratorConverted = (Number) numerator;
        Number denominatorConverted = (Number) denominator;
        if (Operation.isInteger(numeratorConverted.getValue())
            && Operation.isInteger(denominatorConverted.getValue())) {
          ExpressionPart gcd =
              new GreatestCommonDivisor(numeratorConverted, denominatorConverted).simplyfy();
          if (gcd instanceof Number && ((Number) gcd).getValue() > 1) {
            return true;
          }
        } else {
          return true;
        }
      }

      if (denominator instanceof Number) {
        if (((Number) denominator).getValue() == 1) {
          return true;
        }
      }

      if (numerator.canBeSimplified() || denominator.canBeSimplified()) return true;
    }
    return false;
  }
Exemplo n.º 2
0
 /**
  * Evaluates this binary add arithmetic function.
  *
  * @return the result of the evaluation.
  * @throws EvaluationException if the function is not evaluable or if an arithmetic error occurs
  *     during the evaluation.
  * @see pddl4j.exp.fexp.OpExp#isEvaluable()
  */
 public Number evaluate() throws EvaluationException {
   if (!this.isGround())
     throw new EvaluationException("arithmetic function " + this.toString() + " not ground");
   try {
     Number arg1 = null;
     if (this.getArg1().getTermID().equals(TermID.ARITHMETIC_FUNCTION)) {
       OpExp func = (OpExp) this.getArg1();
       arg1 = func.evaluate();
     } else if (this.getArg1().getTermID().equals(TermID.NUMBER)) {
       arg1 = (Number) this.getArg1();
     } else {
       throw new EvaluationException(
           "arithmetic function "
               + this.toString()
               + ": argument "
               + this.getArg1()
               + " is not evaluable");
     }
     Number arg2 = null;
     if (this.getArg2().getTermID().equals(TermID.ARITHMETIC_FUNCTION)) {
       OpExp func = (OpExp) this.getArg2();
       arg2 = func.evaluate();
     } else if (this.getArg2().getTermID().equals(TermID.NUMBER)) {
       arg2 = (Number) this.getArg2();
     } else {
       throw new EvaluationException(
           "arithmetic function "
               + this.toString()
               + ": argument "
               + this.getArg2()
               + " is not evaluable");
     }
     return new Number(arg1.getValue() + arg2.getValue());
   } catch (ArithmeticException e) {
     throw new EvaluationException(
         "arithmetic function " + this.toString() + ": " + e.getMessage(), e);
   }
 }
Exemplo n.º 3
0
  @Override
  public ExpressionPart simplyfy() {
    ExpressionPart tmpNumerator = numerator;
    ExpressionPart tmpDenominator = denominator;

    if (tmpNumerator.canBeSimplified()) {
      tmpNumerator = tmpNumerator.simplyfy();
    }
    if (tmpDenominator.canBeSimplified()) {
      tmpDenominator = tmpDenominator.simplyfy();
    }
    if (tmpNumerator instanceof Number && tmpDenominator instanceof Number) {
      Number numeratorConverted = (Number) tmpNumerator;
      Number denominatorConverted = (Number) tmpDenominator;

      if (Operation.isInteger(numeratorConverted.getValue())
          && Operation.isInteger(denominatorConverted.getValue())) {
        ExpressionPart gcd =
            new GreatestCommonDivisor(numeratorConverted, denominatorConverted).simplyfy();

        if (gcd instanceof Number && ((Number) gcd).getValue() > 1) {
          tmpNumerator = new Division(numeratorConverted, gcd).simplyfy();
          tmpDenominator = new Division(denominatorConverted, gcd).simplyfy();
        }
      } else {
        while (!(Operation.isInteger(numeratorConverted.getValue())
            && Operation.isInteger(denominatorConverted.getValue()))) {
          numeratorConverted = new Number(numeratorConverted.getValue() * 10);
          denominatorConverted = new Number(denominatorConverted.getValue() * 10);
        }
        return new Fraction(numeratorConverted, denominatorConverted).simplyfy();
      }
    }

    if (tmpNumerator instanceof Fraction && tmpDenominator instanceof Fraction) {
      Fraction tmpNumeratorConverted = (Fraction) tmpNumerator;
      Fraction tmpDenominatorConverted = (Fraction) tmpDenominator;
      return new Fraction(
          new Multiplication(tmpNumeratorConverted.numerator, tmpDenominatorConverted.denominator),
          new Multiplication(tmpNumeratorConverted.denominator, tmpDenominatorConverted.numerator));
    }

    if (tmpNumerator instanceof Fraction) {
      Fraction tmpNumeratorConverted = (Fraction) tmpNumerator;
      return new Fraction(
          tmpNumeratorConverted.numerator,
          new Multiplication(tmpNumeratorConverted.denominator, tmpDenominator));
    }

    if (tmpDenominator instanceof Fraction) {
      Fraction tmpDenominatorConverted = (Fraction) tmpDenominator;
      return new Fraction(
          new Multiplication(tmpDenominatorConverted.denominator, tmpNumerator),
          tmpDenominatorConverted.numerator);
    }

    if (tmpDenominator instanceof Number) {
      if (((Number) tmpDenominator).getValue() == 1) {
        return tmpNumerator;
      }
    }

    Fraction retValue = new Fraction(tmpNumerator, tmpDenominator);
    /*retValue.setSign(this.isPositive);
    if(tmpNumerator instanceof Number){
    	if(((Number)tmpNumerator).getValue() < 0){
    		retValue.setSign(false);
    		tmpNumerator = new Number(Math.abs(((Number)tmpNumerator).getValue()));
    		retValue.setSignChecked(true);
    	}
    }*/

    return retValue;
  }