Acceleo 文件标签

Acceleo file tags

我是 Acceleo 的新手,我正在尝试了解有关我可以使用的文件标签的一些信息。 我最常看到的格式是这样的:

[file (someElement.anotherElement.concat('SomeText.java'), false, 'UTF-8')]

第一个问题:如何先写一些文本,然后再写一些元素的文本?例如创建一个名为 "SomeTextElement1.java" 的文件。其中 Element1 是来自 .ecore 文件的 属性(或其他东西)

第二个问题:如何从 2 个不同的元素中获取值? 例如创建一个名为 "Element1-Element2.java" 的文件。其中 Element1 和 Element2 取自 .ecore 文件。

和第三个问题:如何将文件语句放入 [if] 语句中??? 例如

[if (condition) /]
[file (Element1.concat('.java'),false, 'UTF-8')]
[else if (condition)]
[file (Element1.concat('.java'),false, 'UTF-8')]
[if/]

我收到一条错误消息,指出文件标签未终止。但我不想在 if 语句中终止它,因为那样我将不得不两次编写相同的代码......我想要的只是在条件为真时更改文件名......这可能吗?

提前致谢。

您通常不希望为此使用外部 "if"。文件名本身是一个表达式,而不是一个简单的字符串,因此您可以通过以下方式实现您想要获得的内容:

[file (if (condition) then 'somename.java' else 'someothername.java' endif, false, 'UTF-8')]

请注意,在这种情况下,您将使用的 "if" 结构是 OCL "if",因此您必须正确使用 "if then else endif" 结构。如果你想要多个条件,这将变成

if (condition) then 'somename' else
    if (otherCondition) then 'someothername' else 'yetanothername' endif
endif

为了在一个条件下为同一类型生成两个不同的文件,您有多种选择:

您可以按照@Kellindil 在[file ()] 标签

中的建议进行操作

您也可以使用您的解决方案,但要像这样结束 [if..] 中的 [/file] 标记:

[if (condition)]
[file ('name1', ...)]
...
[/file]
[elseif (condition)]
[file ('name2', ...)]
...
[/file]
[/if]

最后,你还可以在拥有代码的模板上使用守卫:

[template public generate_stuff(p : your_type) ? (condition1)]
[file ('name1' ....)]
...
[/file]
[/template]

[comment Yep, the template name is exactly the same, only the condition change/]
[template public generate_stuff(p: your_type) ? (condition2)]
[file ('name2' ....)]
...
[/file]
[/template]

后面的解决方案可能是最好的,因为您可以清楚地 'cut' 您的代码并针对条件编写专门的实现。

(您也可以使用将 if 解决方案与多个模板混合的解决方案)

编辑>

最后一个解决方案可以产生相同的代码,没有问题

[template public generate_stuff(p : your_type) ? (condition1)]
[file ('name1' ....)]
[p.body_generation()/]
[/file]
[/template]

[comment Yep, the template name is exactly the same, only the condition change/]
[template public generate_stuff(p: your_type) ? (condition2)]
[file ('name2' ....)]
[p.body_generation()/]
[/file]
[/template]

[template public body_generation(p: your_type)]
...
[/template]

Acceleo 是一种非常冗长的语言,试图将所有内容放在一个语句中通常不是可读性的好主意。