Eval.jrag 5.06 KB
Newer Older
René Schöne's avatar
René Schöne committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
aspect eval {


  uncache Clause.checkUsing(Request request, Resource resource);
  syn boolean Clause.checkUsing(Request request, Resource resource) = checkUsing(simpleAssignment(request, resource));

  syn double Expression.evalAsDouble();

  eq LiteralExpression.evalAsDouble() = getValue();
  eq ParenthesizedExpression.evalAsDouble() = getExpression().evalAsDouble();

  eq AddExpression.evalAsDouble() = getLeft().evalAsDouble() + getRight().evalAsDouble();
  eq SubExpression.evalAsDouble() = getLeft().evalAsDouble() - getRight().evalAsDouble();
  eq MultExpression.evalAsDouble() = getLeft().evalAsDouble() * getRight().evalAsDouble();
  eq DivExpression.evalAsDouble() = getLeft().evalAsDouble() / getRight().evalAsDouble();
  eq PowerExpression.evalAsDouble() = Math.pow(getLeft().evalAsDouble(), getRight().evalAsDouble());

  syn double Designator.evalAsDouble();
  eq QualifiedNameDesignator.evalAsDouble() {
    throw new RuntimeException("This attribute should not be called because a QualifiedNameDesignator is a temporary node!");
    }
  eq SoftwareDesignator.evalAsDouble() {
    throw new RuntimeException("TODO implement ParentResourceDesignator.evalAsDouble()");
  }
  eq PropertyResourceDesignator.evalAsDouble() {
    if (inRequiringClause()) {
      // TODO
    }
    throw new RuntimeException("TODO implement PropertyResourceDesignator.evalAsDouble()");
  }
  eq MetaParameterDesignator.evalAsDouble() {
    throw new RuntimeException("TODO implement MetaParameterDesignator.evalAsDouble()");
  }

  uncache Clause.evalUsing(Request request, Resource target);
  syn double Clause.evalUsing(Request request, Resource target) = evalUsing(simpleAssignment(request, target));

  // eval using for assignments
  uncache Clause.evalUsing(Assignment assignment);
  syn double Clause.evalUsing(Assignment assignment) = getExpression().evalUsing(assignment);

  uncache Expression.evalUsing(Assignment assignment);
  syn double Expression.evalUsing(Assignment assignment);

  eq LiteralExpression.evalUsing(Assignment assignment) = getValue();
  eq ParenthesizedExpression.evalUsing(Assignment assignment) = getExpression().evalUsing(assignment);

  eq AddExpression.evalUsing(Assignment assignment) = getLeft().evalUsing(assignment) + getRight().evalUsing(assignment);
  eq SubExpression.evalUsing(Assignment assignment) = getLeft().evalUsing(assignment) - getRight().evalUsing(assignment);
  eq MultExpression.evalUsing(Assignment assignment) = getLeft().evalUsing(assignment) * getRight().evalUsing(assignment);
  eq DivExpression.evalUsing(Assignment assignment) = getLeft().evalUsing(assignment) / getRight().evalUsing(assignment);
  eq PowerExpression.evalUsing(Assignment assignment) = Math.pow(getLeft().evalUsing(assignment), getRight().evalUsing(assignment));

  uncache Designator.evalUsing(Assignment assignment);
  syn double Designator.evalUsing(Assignment assignment);
  eq QualifiedNameDesignator.evalUsing(Assignment assignment) {
    throw new RuntimeException("This attribute should not be called because a QualifiedNameDesignator is a temporary node!");
  }
  eq SoftwareDesignator.evalUsing(Assignment assignment) {
    Assignment providingAssignment;
    if (this.hasInstanceRef()) {
      // referencing a required component
      providingAssignment = assignment.mappedAssignment(this.getInstanceRef().getRef());
    } else {
      // use given implementation for resolving
      providingAssignment = assignment;
    }
    if (providingAssignment == null) {
      MquatWriteSettings settings = new MquatWriteSettings("");
70
71
      logger.error("Could not evaluate {} in {}",
        this.print(settings), containingClause().print(settings));
René Schöne's avatar
René Schöne committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      return 0;
    }
    for (Clause clause : providingAssignment.getImplementation().getClauseList()) {
      if (clause.isProvidingClause()) {
        if (clause.getDesignator().isSoftwareDesignator()) {
          SoftwareDesignator sd = clause.getDesignator().asSoftwareDesignator();
          if (!sd.hasInstanceRef()) {
            if (sd.getPropertyRef().getRef().equals(this.getPropertyRef().getRef())) {
              return clause.getExpression().evalUsing(providingAssignment);
            }
            // found another provision clause with a different property
          }
        }
      }
    }
    throw new RuntimeException("this should not be happening!");
  }
  eq PropertyResourceDesignator.evalUsing(Assignment assignment) {
    Resource resource = assignment.mappedResource(this.getInstanceRef().getRef());
    return resource.getCurrentValueByProperty(this.getPropertyRef().getRef());
  }
  eq MetaParameterDesignator.evalUsing(Assignment assignment) {
    LiteralExpression litExp = assignment.getRequest().getMetaParameterExpression(getMetaParameterRef().getRef());
    if (litExp != null) {
      // TODO could also using evalAsDouble here
      return litExp.evalUsing(assignment);
    }
    logger.error("evalUsing: Request did not have assignment for meta {}, returning 0", getMetaParameterRef().name());
    return 0;
  }

  rewrite QualifiedNameDesignator {
    to Designator {
      return containingClause().resolveQualifiedName(this.getQualifiedName());
    }
  }

}