예제 #1
0
 private static void appendAllAtomicExpressions(
     Vector<Expression> buildVector, Expression current) {
   if (current.getChilds() == null) {
     buildVector.add(current);
   } else {
     for (Expression e : current.getChilds()) {
       appendAllAtomicExpressions(buildVector, e);
     }
   }
 }
예제 #2
0
  /**
   * Evaluate expression considering some declared variables
   *
   * @param variables
   * @return
   * @throws TypeMismatchException
   */
  public Expression evaluate(final Hashtable<String, ?> variables) throws TypeMismatchException {
    try {
      Expression resolvedExpression =
          transform(
              new ExpressionTransformer() {
                @Override
                public Expression performTransformation(Expression e) throws TransformException {
                  if (e instanceof BindingValueAsExpression) {
                    BindingValueAsExpression bv = (BindingValueAsExpression) e;
                    if (bv.isSimpleVariable() && variables.get(bv.toString()) != null) {
                      return Constant.makeConstant(variables.get(bv.toString()));
                    }
                  }
                  return e;
                }
              });
      return resolvedExpression.evaluate();
    } catch (TransformException e) {
      e.printStackTrace();
      return null;
    }

    /*return evaluate(new EvaluationContext(new ExpressionParser.DefaultConstantFactory(), new VariableFactory() {
    	@Override
    	public Expression makeVariable(Word value, Bindable bindable) {
    		Object valueObject = variables.get(value.getValue());
    		if (valueObject == null) {
    			return ObjectSymbolicConstant.NULL;
    		}
    		if (valueObject instanceof String) {
    			return new Constant.StringConstant((String) valueObject);
    		} else if (valueObject instanceof Enum) {
    			return new Constant.EnumConstant(((Enum) valueObject).name());
    		} else if (valueObject instanceof Integer) {
    			return new Constant.IntegerConstant((Integer) valueObject);
    		} else if (valueObject instanceof Long) {
    			return new Constant.IntegerConstant((Long) valueObject);
    		} else if (valueObject instanceof Short) {
    			return new Constant.IntegerConstant((Short) valueObject);
    		} else if (valueObject instanceof Float) {
    			return new Constant.FloatConstant((Float) valueObject);
    		} else if (valueObject instanceof Double) {
    			return new Constant.FloatConstant((Double) valueObject);
    		} else if (valueObject instanceof Boolean) {
    			return (Boolean) valueObject ? Constant.BooleanConstant.TRUE : Constant.BooleanConstant.FALSE;
    		}
    		// TODO Handle others
    		// return new Variable(value.getValue());
    		return new Constant.StringConstant(value.getValue());
    	}
    }, new ExpressionParser.DefaultFunctionFactory()), bindable);*/

  }
예제 #3
0
  public static Vector<Variable> extractVariables(String anExpression)
      throws ParseException, TypeMismatchException {
    final Hashtable<String, Variable> returnedHash = new Hashtable<String, Variable>();
    try {
      Expression e = ExpressionParser.parse(anExpression);
      e.transform(
          new ExpressionTransformer() {
            @Override
            public Expression performTransformation(Expression e) throws TransformException {
              if (e instanceof BindingValueAsExpression) {
                String variableName = ((BindingValueAsExpression) e).toString();
                Variable returned = returnedHash.get(variableName);
                if (returned == null) {
                  returned = new Variable(variableName);
                  returnedHash.put(variableName, returned);
                }
                return returned;
              }
              return e;
            }
          });
    } catch (ParseException e1) {
      throw e1;
    } catch (TransformException e) {
      e.printStackTrace();
    }

    /*DefaultExpressionParser parser = new DefaultExpressionParser();
    Expression expression = parser.parse(anExpression, bindable);
    expression.evaluate(new EvaluationContext(new ExpressionParser.DefaultConstantFactory(), new VariableFactory() {
    	@Override
    	public Expression makeVariable(Word value, Bindable bindable) {
    		Variable returned = returnedHash.get(value.getValue());
    		if (returned == null) {
    			returned = new Variable(value.getValue());
    			returnedHash.put(value.getValue(), returned);
    		}
    		return returned;
    	}
    }, new ExpressionParser.DefaultFunctionFactory()), bindable);*/

    Vector<Variable> returned = new Vector<Variable>();
    for (String v : returnedHash.keySet()) {
      returned.add(returnedHash.get(v));
    }
    return returned;
  }
예제 #4
0
  public static Vector<Expression> extractPrimitives(String anExpression)
      throws ParseException, TypeMismatchException {
    final Hashtable<String, Expression> returnedHash = new Hashtable<String, Expression>();

    try {
      Expression e = ExpressionParser.parse(anExpression);
      e.transform(
          new ExpressionTransformer() {
            @Override
            public Expression performTransformation(Expression e) throws TransformException {
              if (e instanceof BindingValueAsExpression) {
                String variableName = ((BindingValueAsExpression) e).toString();
                Expression returned = returnedHash.get(variableName);
                if (returned == null) {
                  returned = new Variable(variableName);
                  returnedHash.put(variableName, returned);
                }
                return returned;
              }
              return e;
            }
          });
    } catch (org.openflexo.antar.expr.parser.ParseException e1) {
      e1.printStackTrace();
    } catch (TransformException e) {
      e.printStackTrace();
    }

    /*DefaultExpressionParser parser = new DefaultExpressionParser();
    Expression expression = parser.parse(anExpression, bindable);
    expression.evaluate(new EvaluationContext(new ExpressionParser.DefaultConstantFactory(), new VariableFactory() {
    	@Override
    	public Expression makeVariable(Word value, Bindable bindable) {
    		Expression returned = returnedHash.get(value.getValue());
    		if (returned == null) {
    			returned = new Variable(value.getValue());
    			returnedHash.put(value.getValue(), returned);
    		}
    		return returned;
    	}
    }, new FunctionFactory() {
    	@Override
    	public Expression makeFunction(String functionName, List<Expression> args, Bindable bindable) {
    		StringBuffer key = new StringBuffer();
    		key.append(functionName + "(");
    		for (int i = 0; i < args.size(); i++) {
    			key.append((i > 0 ? "," : "") + "arg" + i);
    		}
    		key.append(")");
    		Expression returned = returnedHash.get(key);
    		if (returned == null) {
    			returned = new Function(functionName, args);
    			returnedHash.put(key.toString(), returned);
    		}
    		return returned;
    	}
    }), bindable);
    */

    Vector<Expression> returned = new Vector<Expression>();
    for (String v : returnedHash.keySet()) {
      returned.add(returnedHash.get(v));
    }
    return returned;
  }
예제 #5
0
  protected void updateDependancies() {
    if (getOwner() instanceof FIBComponent) {

      if (binding == null) {
        return;
      }

      Vector<Expression> primitives;
      try {
        primitives = Expression.extractPrimitives(binding.getStringRepresentation());

        FIBComponent component = (FIBComponent) getOwner();
        FIBComponent rootComponent = component.getRootComponent();
        Iterator<FIBComponent> subComponents = rootComponent.subComponentIterator();
        while (subComponents.hasNext()) {
          FIBComponent next = subComponents.next();
          if (next != getOwner()) {
            if (next instanceof FIBWidget
                && ((FIBWidget) next).getData() != null
                && ((FIBWidget) next).getData().isSet()) {
              String data = ((FIBWidget) next).getData().toString();
              if (data != null) {
                for (Expression p : primitives) {
                  String primitiveValue = null;
                  if (p instanceof Variable) {
                    primitiveValue = ((Variable) p).getName();
                  }
                  if (p instanceof Function) {
                    primitiveValue = ((Function) p).getName();
                  }
                  if (primitiveValue != null && primitiveValue.startsWith(data)) {
                    try {
                      component.declareDependantOf(next);
                    } catch (DependancyLoopException e) {
                      logger.warning(
                          "DependancyLoopException raised while declaring dependancy (data lookup)"
                              + "in the context of binding: "
                              + binding.getStringRepresentation()
                              + " primitive: "
                              + primitiveValue
                              + " component: "
                              + component
                              + " dependancy: "
                              + next
                              + " data: "
                              + data
                              + " message: "
                              + e.getMessage());
                    }
                  }
                }
              }
            }
            if (next.getName() != null) {
              for (Expression p : primitives) {
                String primitiveValue = null;
                if (p instanceof Variable) {
                  primitiveValue = ((Variable) p).getName();
                }
                if (p instanceof Function) {
                  primitiveValue = ((Function) p).getName();
                }
                if (primitiveValue != null
                    && StringUtils.isNotEmpty(next.getName())
                    && primitiveValue.startsWith(next.getName())) {
                  try {
                    component.declareDependantOf(next);
                  } catch (DependancyLoopException e) {
                    logger.warning(
                        "DependancyLoopException raised while declaring dependancy (name lookup)"
                            + "in the context of binding: "
                            + binding.getStringRepresentation()
                            + " primitive: "
                            + primitiveValue
                            + " component: "
                            + component
                            + " dependancy: "
                            + next
                            + " message: "
                            + e.getMessage());
                  }
                }
              }
            }
          }
        }

      } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (TypeMismatchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }