我是Acceleo的新手,我正在尝试找出一些关于我可以使用的文件标记的东西。我最常看到的格式是这样的:
[file (someElement.anotherElement.concat('SomeText.java'), false, 'UTF-8')]第一个问题:我怎样才能先写一些课文,然后再写一些元素的课文?例如创建一个名为"SomeTextElement1.java“的文件。其中Element1是.ecore文件中的一个属性(或其他属性))
第二个问题:我如何从两个不同的元素中取值?例如,创建一个名为"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语句中终止它,因为那样的话我必须写两次相同的代码.如果条件为真,我只想更改文件名。这可能吗?
提前谢谢。
发布于 2017-06-08 08:02:35
你通常不想有外部的“如果”是为了这个。文件名本身是一个表达式,而不是一个简单的字符串,因此您可以通过以下方法实现您想要得到的结果:
[file (if (condition) then 'somename.java' else 'someothername.java' endif, false, 'UTF-8')]请注意,在本例中,您将使用的"if“结构是OCL "if",因此您必须正确地使用"if then”结构。如果您想要多个条件,这将成为
if (condition) then 'somename' else
if (otherCondition) then 'someothername' else 'yetanothername' endif
endif发布于 2017-06-08 12:15:46
为了在一个条件下为同一类型生成两个不同的文件,您有多个选项:
您可以按照@Kellindil在[file ()]标记中的建议进行操作。
您也可以使用您的解决方案,但是通过在[/file]中结束[if..]标记,如下所示:
[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]这个稍后的解决方案可能是最好的,因为您可以清楚地“削减”您的代码,并编写关于该条件的专用实现。
(还可以使用将if解决方案与多个模板混合的解决方案)
EDIT>
最后一个解决方案可以生成相同的代码,没有任何问题。
[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是一种非常冗长的语言,试图把所有的东西都放在一条语句上,对于可读性来说通常不是一个好主意。
https://stackoverflow.com/questions/44422337
复制相似问题