我有一个EMF元模型,如下所述:
class Application
- runsOn: Host [0..*]
interface Host
class VirtualMachine implements Host
- runsOn: Host [0..*]
class PhysicalMachine implements Host
我想使用OCL回答的问题是:查找给定PhysicalMachine
实际运行的所有Application
(直接或传递)。请注意,实例模型中可能出现以下所有路径:
- Application runsOn PhysicalServer
- Application runsOn VirtualMachine runsOn PhysicalServer
- Application runsOn VirtualMachine runsOn VirtualMachine runsOn PhysicalServer
- ...
我想这可能会以某种方式使用OCL的closure
运算符来表达,但我无法弄清楚正确的语法。
答案 0 :(得分:1)
经过大量的修补,我想我已经找到了答案。
self.runsOn->asSet()
->closure(host: Host |
if host.oclIsKindOf(VirtualMachine) then
host.oclAsType(VirtualMachine).runsOn
else
host->asSet()
endif
)
这似乎有效。如果上述查询错误或效率低,请纠正我。
修改:感谢Ed Willink的评论,我已将上述查询简化为:
self.runsOn->closure(host: Host | host->selectByKind(VirtualMachine).runsOn)