gpt4 book ai didi

java - ATL 转换 - 空输出

转载 作者:行者123 更新时间:2023-12-01 13:11:21 30 4
gpt4 key购买 nike

我正在尝试将XText与EMF的ATL模型结合起来进行模型转换。我正在读取我的 DSL,将其转储到 EMF 的 XMI 资源中,然后将其放入 ATL api 中:ATL 不会给我任何错误并且运行正确:

Number of instructions executed: 38

无论我做什么,我的 OutModel (palData) 都保持为空 (null)。如果我从这里获取所有文件(hdl.ecore、pal.ecore、hdl.xmi)并将它们放入示例 ATL 项目中,我会得到正确的输出。

那么我需要将任何神奇参数扔到 EMFVM 启动器上吗?

我的触发ATL的代码:

// create stuff
EMFModelFactory emfFactory = new EMFModelFactory();
XMIResourceFactoryImpl xmiFactory = new XMIResourceFactoryImpl();

EMFExtractor extractor = new EMFExtractor();
EMFInjector emfInjector = new EMFInjector();
ResourceSet resourceSet = new ResourceSetImpl();

// load model
EMFReferenceModel hdlMetaModel = (EMFReferenceModel) emfFactory.newReferenceModel();
emfInjector.inject(hdlMetaModel, resourceSet.getResource(URI.createFileURI("org.xtext.hal/model/generated/Hdl.ecore"), true));
EMFModel hdlData = (EMFModel) emfFactory.newModel(hdlMetaModel);

EMFReferenceModel palMetaModel = (EMFReferenceModel) emfFactory.newReferenceModel();
emfInjector.inject(palMetaModel, resourceSet.getResource(URI.createFileURI("org.xtext.hal/model/Pal.ecore"), true));
EMFModel palData = (EMFModel) emfFactory.newModel(palMetaModel);
palData.setIsTarget(true);

// load xtext content, convert to xmi
Resource xmiResource = xmiFactory.createResource(URI.createURI("org.xtext.hal/model/generated/Hdl.xmi"));
xmiResource.getContents().addAll(hdlModel.getContents());
emfInjector.inject(hdlData, xmiResource);

// ATL transformation
InputStream asm = new FileInputStream("org.xtext.hal/model/Pal.asm");
EMFVMLauncher launcher = new EMFVMLauncher();

HashMap<String,Object> options = new HashMap<String,Object>();
options.put("showSummary", "true");
options.put("step", "true");

launcher.initialize(Collections.<String, Object> emptyMap());
launcher.addInModel(hdlData, "IN", "hdl");
launcher.addOutModel(palData, "OUT", "pal");
launcher.launch(ILauncher.DEBUG_MODE, new NullProgressMonitor(), options, asm);

// get output
Resource t_palData = palData.getResource();
t_palData.setURI(URI.createURI("palData.xmi")); // Exception in thread "main" java.lang.NullPointerException
t_palData.save(null);

我在这里 ATL:

-- @path hdl=/org.xtext.hal/model/generated/Hdl.ecore
-- @path pal=/org.xtext.hal/model/Pal.ecore

module HDL2PAL;
create OUT : pal from IN : hdl;

rule Foobar
{
from
s : hdl!Model
to
t : pal!AddressSpace (
name <- s.name
)
}

HDL.ecore(输入元模型):

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="hdl" nsURI="http://www.xtext.org/hal/Hdl" nsPrefix="hdl">
<eClassifiers xsi:type="ecore:EClass" name="Model">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
</ecore:EPackage>

PAL.ecore(输出元模型):

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="pal" nsURI="http://www.xtext.org/hal/Pal" nsPrefix="pal">
<eClassifiers xsi:type="ecore:EClass" name="AddressSpace">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
</ecore:EPackage>

输入的模型数据:

<?xml version="1.0" encoding="ASCII"?>
<hdl:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:hdl="http://www.xtext.org/hal/Hdl" name="bar"/>

ATL 示例项目的输出:

<?xml version="1.0" encoding="ISO-8859-1"?>
<pal:AddressSpace xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:pal="http://www.xtext.org/hal/Pal" name="bar"/>

ATL 汇编器的输出:

main:0  getasm
stack: HDL2PAL : ASMModule
locals: self=HDL2PAL : ASMModule
main:1 push OclParametrizedType
stack: HDL2PAL : ASMModule, 'OclParametrizedType'
locals: self=HDL2PAL : ASMModule
main:2 push #native
stack: HDL2PAL : ASMModule, 'OclParametrizedType', '#native'
locals: self=HDL2PAL : ASMModule
main:3 new
stack: HDL2PAL : ASMModule, <unnamed>(null)
locals: self=HDL2PAL : ASMModule
main:4 dup
stack: HDL2PAL : ASMModule, <unnamed>(null), <unnamed>(null)
locals: self=HDL2PAL : ASMModule
main:5 push Collection
stack: HDL2PAL : ASMModule, <unnamed>(null), <unnamed>(null), 'Collection'
locals: self=HDL2PAL : ASMModule
main:6 pcall J.setName(S):V
locals: self=HDL2PAL : ASMModule Calling <unnamed>(null).setName('Collection')
stack: HDL2PAL : ASMModule, Collection(null)
locals: self=HDL2PAL : ASMModule
main:7 dup
stack: HDL2PAL : ASMModule, Collection(null), Collection(null)
locals: self=HDL2PAL : ASMModule
main:8 push OclSimpleType
stack: HDL2PAL : ASMModule, Collection(null), Collection(null), 'OclSimpleType'
locals: self=HDL2PAL : ASMModule
main:9 push #native
stack: HDL2PAL : ASMModule, Collection(null), Collection(null), 'OclSimpleType', '#native'
locals: self=HDL2PAL : ASMModule
main:10 new
stack: HDL2PAL : ASMModule, Collection(null), Collection(null), <unnamed>
locals: self=HDL2PAL : ASMModule
main:11 dup
stack: HDL2PAL : ASMModule, Collection(null), Collection(null), <unnamed>, <unnamed>
locals: self=HDL2PAL : ASMModule
main:12 push OclAny
stack: HDL2PAL : ASMModule, Collection(null), Collection(null), <unnamed>, <unnamed>, 'OclAny'
locals: self=HDL2PAL : ASMModule
main:13 pcall J.setName(S):V
locals: self=HDL2PAL : ASMModule Calling <unnamed>.setName('OclAny')
stack: HDL2PAL : ASMModule, Collection(null), Collection(null), OclAny
locals: self=HDL2PAL : ASMModule
main:14 pcall J.setElementType(J):V
locals: self=HDL2PAL : ASMModule Calling Collection(null).setElementType(OclAny)
stack: HDL2PAL : ASMModule, Collection(OclAny)
locals: self=HDL2PAL : ASMModule
main:15 set col
stack:
locals: self=HDL2PAL : ASMModule
main:16 getasm
stack: HDL2PAL : ASMModule
locals: self=HDL2PAL : ASMModule
main:17 push TransientLinkSet
stack: HDL2PAL : ASMModule, 'TransientLinkSet'
locals: self=HDL2PAL : ASMModule
main:18 push #native
stack: HDL2PAL : ASMModule, 'TransientLinkSet', '#native'
locals: self=HDL2PAL : ASMModule
main:19 new
stack: HDL2PAL : ASMModule, TransientLinkSet {}
locals: self=HDL2PAL : ASMModule
main:20 set links
stack:
locals: self=HDL2PAL : ASMModule
main:21 getasm
stack: HDL2PAL : ASMModule
locals: self=HDL2PAL : ASMModule
main:22 pcall A.__matcher__():V
locals: self=HDL2PAL : ASMModule Calling HDL2PAL : ASMModule.__matcher__()


__matcher__:0 getasm
stack: HDL2PAL : ASMModule
locals: self=HDL2PAL : ASMModule
__matcher__:1 pcall A.__matchFoobar():V
locals: self=HDL2PAL : ASMModule Calling HDL2PAL : ASMModule.__matchFoobar()
__matchFoobar:0 push Model
stack: 'Model'
locals: self=HDL2PAL : ASMModule
__matchFoobar:1 push hdl
stack: 'Model', 'hdl'
locals: self=HDL2PAL : ASMModule
__matchFoobar:2 findme
stack: hdl!Model
locals: self=HDL2PAL : ASMModule
__matchFoobar:3 push IN
stack: hdl!Model, 'IN'
locals: self=HDL2PAL : ASMModule
__matchFoobar:4 call MMOF!Classifier;.allInstancesFrom(S):QJ
locals: self=HDL2PAL : ASMModule Calling hdl!Model.allInstancesFrom('IN')
stack: OrderedSet {}
locals: self=HDL2PAL : ASMModule
__matchFoobar:5 iterate
stack:
locals:
stack:
locals:
stack:
locals: self=HDL2PAL : ASMModule


main:23 getasm
stack: HDL2PAL : ASMModule
locals: self=HDL2PAL : ASMModule
main:24 pcall A.__exec__():V
locals: self=HDL2PAL : ASMModule Calling HDL2PAL : ASMModule.__exec__()
__exec__:0 getasm
stack: HDL2PAL : ASMModule
locals: self=HDL2PAL : ASMModule
__exec__:1 get links
stack: TransientLinkSet {}
locals: self=HDL2PAL : ASMModule
__exec__:2 push Foobar
stack: TransientLinkSet {}, 'Foobar'
locals: self=HDL2PAL : ASMModule
__exec__:3 call NTransientLinkSet;.getLinksByRule(S):QNTransientLink;
locals: self=HDL2PAL : ASMModule Calling TransientLinkSet {}.getLinksByRule('Foobar')
stack: []
locals: self=HDL2PAL : ASMModule
__exec__:4 iterate
stack:
locals:
stack:
locals:

最佳答案

您将在加载模型后从文件加载 HDL 元模型:

EMFReferenceModel hdlMetaModel = (EMFReferenceModel) emfFactory.newReferenceModel();emfInjector.inject(hdlMetaModel, resourceSet.getResource(URI.createFileURI("org.xtext.hal/model/generated/Hdl.ecore"), true));

现有的“hdlModel”将包含链接到其他 EClass 实例的 EObject,而不是您加载到“hdlMetaModel”中的实例。因此,如果 ATL 尝试确定“hdl!Model”的所有实例,它将找不到任何内容:真实实例引用“hdl!Model”的另一个版本。解决方案是获取 xText 加载的 HDL 元模型的预加载版本。

附注关于 ATL 的大多数支持讨论都在 the ATL forum 上进行。 。您可能想在那里继续讨论。

关于java - ATL 转换 - 空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22841706/

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