Пример #1
0
  /**
   * Try to get a constant factory
   *
   * @param a the first node
   * @param b the second node
   * @param s the node type set
   * @return the constant factory
   */
  public static final ConstantType getConstantType(
      final Node<?> a, final Node<?> b, final NodeTypeSet<?> s) {
    NodeType<?, ?> t;
    int i;

    if (a != null) {
      t = a.getType();
      if (t instanceof ConstantType) {
        return ((ConstantType) t);
      }
    }

    if (b != null) {
      t = b.getType();
      if (t instanceof ConstantType) {
        return ((ConstantType) t);
      }
    }

    if (s != null) {
      for (i = s.size(); (--i) >= 0; ) {
        t = s.get(i);
        if (t instanceof ConstantType) {
          return ((ConstantType) t);
        }
      }
    }

    return DEFAULT_CONSTANT_TYPE;
  }
Пример #2
0
  /**
   * Try to reduce this node by adhering the given allowed types
   *
   * @param allowed the allowd types
   * @return the reduced node, or this node if no reduction is possible
   */
  @Override
  @SuppressWarnings("unchecked")
  public RealFunction reduce(final NodeTypeSet<RealFunction> allowed) {
    RealFunction oa, a;
    NodeType<Tan, RealFunction> t;
    ConstantType q;

    t = ((NodeType) (this.getType()));
    oa = this.get(0);

    a = oa.reduce(t.getChildTypes(0));
    if ((a.hasConstantValue()) && (!(a.hasEffect()))) {
      q = ConstantType.getConstantType(a, null, allowed);
      if (q != null) {
        if ((allowed == null) || (allowed.containsType(q))) {
          return q.instantiate(compute(a.getConstantValue()));
        }
      }
    }

    if (a != oa) {
      return new Tan(new Node[] {a}, t);
    }

    return this; // super.reduce(allowed);
  }
Пример #3
0
  /**
   * Try to reduce this node by adhering the given allowed types
   *
   * @param allowed the allowd types
   * @return the reduced node, or this node if no reduction is possible
   */
  @Override
  @SuppressWarnings("unchecked")
  public RealFunction reduce(final NodeTypeSet<RealFunction> allowed) {
    RealFunction oa, ob, a, b;
    NodeType<Add, RealFunction> t;
    ConstantType q;

    t = ((NodeType) (this.getType()));
    oa = this.get(0);
    ob = this.get(1);

    a = oa.reduce(t.getChildTypes(0));
    b = ob.reduce(t.getChildTypes(1));

    if (a.hasConstantValue() && (!(a.hasEffect()))) {

      if (b.hasConstantValue() && (!(b.hasEffect()))) {
        q = ConstantType.getConstantType(a, b, allowed);
        if (q != null) {
          if ((allowed == null) || (allowed.containsType(q))) {
            return q.instantiate( //
                a.getConstantValue() + b.getConstantValue());
          }
        }
      }

      if (a.getConstantValue() == 0d) {
        if ((allowed == null) || (allowed.containsType(b.getType()))) {
          return b;
        }
      }
    }

    if ((b.hasConstantValue() && (!(b.hasEffect()))) && (b.getConstantValue() == 0d)) {
      if ((allowed == null) || (allowed.containsType(a.getType()))) {
        return a;
      }
    }

    if ((a != oa) || (b != ob)) {
      return new Add(new Node[] {a, b}, t);
    }

    return this; // super.reduce(allowed);
  }