Example #1
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;
  }
Example #2
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();
      }
    }
  }