gpt4 book ai didi

java - 在 Java 中解析 OCL?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:38 25 4
gpt4 key购买 nike

我正在为一门类(class)编写 Java 程序,该程序采用元模型 UML 类图作为输入,并允许用户创建元模型中指定类型的图。然后,用户应该能够对该图建模的实例进行建模。

因此,我正在解析生成的表示 UML 的 XML 文件并提取所有类和关联。到目前为止,还不错。

但是还有一些限制,我需要了解这些限制并在用户违反它们时发出警告。但是,我不知道如何进行 OCL 解析。我调查了dresden OCL但我不确定这是否是我想要的,因为我需要在运行时解析 OCL,而不是导入模型并使用 eclipse 从 OCL 生成 java 代码。

因此,如果有人能指出一种解析 OCL 并提取其基本语法的方法,我将不胜感激。

最好的问候,若昂·费尔南德斯

最佳答案

Eclipse OCL project提供独立使用(只是 Eclipse 中的一个 java 程序)并且有一些 documentation and examples关于如何去做。

具体请参见以下链接:

一些 Jave API 用法示例,取自帮助,以揭示如何创建和评估不变量和查询:

OCL ocl = OCL.newInstance(new PivotEnvironmentFactory());
OCLHelper helper = ocl.createOCLHelper(EXTLibraryPackage.Literals.LIBRARY);
ExpressionInOCL invariant = helper.createInvariant(
"books->forAll(b1, b2 | b1 <> b2 implies b1.title <> b2.title)");
ExpressionInOCL query = helper.createQuery(
"books->collect(b : Book | b.category)->asSet()");

// create a Query to evaluate our query expression
Query queryEval = ocl.createQuery(query);
// create another to check our constraint
Query constraintEval = ocl.createQuery(invariant);

List<Library> libraries = getLibraries(); // hypothetical source of libraries
// only print the set of book categories for valid libraries
for (Library next : libraries) {
if (constraintEval.check(next)) {
// the OCL result type of our query expression is Set(BookCategory)
@SuppressWarnings("unchecked")
Set<BookCategory> categories = (Set<BookCategory>) queryEval.evaluate(next);

System.out.printf("%s: %s%n", next.getName(), categories);
}
}

关于java - 在 Java 中解析 OCL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19705929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com