Пример #1
0
  public void testWithModelElementList() {
    final TestModelRoot root = TestModelRoot.TYPE.instantiate();
    final ModelElementList<TestModelElementA> list = root.getList1();
    final FunctionContext context = new ModelElementFunctionContext(root);

    TestModelElementA a;

    testForExpectedValue(context, "${ Avg( List1 ) }", new BigDecimal("0"));
    testForExpectedValue(context, "${ Avg( List1, 'Value1' ) }", new BigDecimal("0"));
    testForExpectedValue(context, "${ Avg( List1, 'Value2' ) }", new BigDecimal("0"));

    a = list.insert();
    a.setValue1("1");
    a.setValue2(2);

    testForExpectedValue(context, "${ Avg( List1 ) }", new BigDecimal("1"));
    testForExpectedValue(context, "${ Avg( List1, 'Value1' ) }", new BigDecimal("1"));
    testForExpectedValue(context, "${ Avg( List1, 'Value2' ) }", new BigDecimal("2"));

    a = list.insert();
    a.setValue1("2");
    a.setValue2(3);

    a = list.insert();
    a.setValue1("3.5");
    a.setValue2(4);

    testForExpectedValue(context, "${ Avg( List1 ) }", new BigDecimal("2.2"));
    testForExpectedValue(context, "${ Avg( List1, 'Value1' ) }", new BigDecimal("2.2"));
    testForExpectedValue(context, "${ Avg( List1, 'Value2' ) }", new BigDecimal("3"));

    testForExpectedError(
        context, "${ Avg( List1, 'abc' ) }", "Property TestModelElementA.abc could not be found.");
    testForExpectedError(
        context,
        "${ Avg( List1, 'Element1' ) }",
        "Property TestModelElementA.Element1 is not a value property.");
    testForExpectedError(
        context,
        "${ Avg( List2 ) }",
        "Element type TestModelElementB does not contain a value property.");

    final FunctionResult result =
        ExpressionLanguageParser.parse("${ Avg( List1, 'Value1' ) }").evaluate(context);

    assertEquals(new BigDecimal("2.2"), result.value());

    list.get(0).setValue1("2");
    list.get(1).setValue1("3.5");

    assertEquals(new BigDecimal("3.0"), result.value());

    a = list.insert();
    a.setValue1("5.2");

    assertEquals(new BigDecimal("3.6"), result.value());
  }
  protected final void init(final ActionSystemPartDef def) {
    this.functionContext = initFunctionContext();

    if (def != null) {
      this.id = def.getId().content();

      final Function labelFunction =
          FailSafeFunction.create(def.getLabel().content(), Literal.create(String.class));

      this.labelFunctionResult = labelFunction.evaluate(this.functionContext);

      this.labelFunctionResult.attach(
          new Listener() {
            @Override
            public void handle(final Event event) {
              broadcast(new LabelChangedEvent());
            }
          });

      final Function toolTipFunction =
          FailSafeFunction.create(def.getToolTip().content(), Literal.create(String.class));

      this.toolTipFunctionResult = toolTipFunction.evaluate(this.functionContext);

      this.toolTipFunctionResult.attach(
          new Listener() {
            @Override
            public void handle(final Event event) {
              broadcast(new ToolTipChangedEvent());
            }
          });

      this.description = def.getDescription().content();

      for (ImageReference image : def.getImages()) {
        final Function imageFunction =
            FailSafeFunction.create(image.getImage().content(), Literal.create(ImageData.class));
        final FunctionResult imageFunctionResult = imageFunction.evaluate(this.functionContext);
        final ImageData data = (ImageData) imageFunctionResult.value();

        if (data != null) {
          this.images.add(data);
        }

        imageFunctionResult.dispose();
      }

      for (ActionLocationHint locationHintDef : def.getLocationHints()) {
        final String locationHintText = locationHintDef.getReferenceEntityId().content();

        if (locationHintText != null) {
          final SapphireActionLocationHint locationHint;

          if (locationHintDef instanceof ActionLocationHintBefore) {
            locationHint = new SapphireActionLocationHintBefore(locationHintText);
          } else if (locationHintDef instanceof ActionLocationHintAfter) {
            locationHint = new SapphireActionLocationHintAfter(locationHintText);
          } else {
            throw new IllegalStateException();
          }

          this.locationHints.add(locationHint);
        }
      }
    }

    this.enabled = true;
    this.visible = true;
  }