Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [xpand] problem calling xpand from Java
[xpand] problem calling xpand from Java [message #559366] Thu, 16 September 2010 13:31 Go to next message
Arwin is currently offline ArwinFriend
Messages: 3
Registered: September 2010
Junior Member
I keep getting the following exception when calling Xpand from a Java program:

Exception in thread "main" EvaluationException : No Definition templates::Template::main for idl::Interface could be found!
	Internal error : element was null



However I am sure I pass an object of type Interface to Xpand AND the template containing a definition
main FOR Interface is found by Xpand.

My code:


          ResourceSet resourceSet = new ResourceSetImpl();
            URI uri = URI.createFileURI(InputFileName);
            Resource resource = resourceSet.getResource(uri, true);
            Model model = (Model) resource.getContents().get(0);

            // XPand
            String outputFolder = "output";
            OutputImpl output = new OutputImpl();
            Outlet outlet = new Outlet(outputFolder);
            outlet.setOverwrite(true);
            output.addOutlet(outlet);

            EmfMetaModel metaModel = new EmfMetaModel(IdlPackage.eINSTANCE);
            XpandExecutionContextImpl executionContext = new XpandExecutionContextImpl(output, null);
            executionContext.registerMetaModel(metaModel);

            XpandFacade facade = XpandFacade.create(executionContext);
            facade.evaluate("templates::Template::main", model.getInterfaces().get(0));



My template file Template.xpt:


«IMPORT org::xtext::example::idl::idl»

«EXTENSION templates::Extensions»


«DEFINE main FOR Interface-»

«REM»  TODO only for non-local interfaces!!  «ENDREM»
«EXPAND header FOR this»
«EXPAND proxy  FOR this»
«EXPAND stub   FOR this»

«ENDDEFINE»


«DEFINE header FOR Interface»
«FILE this.name + "_PS.h"-»
«EXPAND Header::header FOR this-»
«ENDFILE»
«ENDDEFINE»


«DEFINE proxy FOR Interface»
«FILE this.name + "_Proxy.c"-»
«EXPAND Proxy::proxy FOR this-»
«ENDFILE»
«ENDDEFINE»


«DEFINE stub FOR Interface»
«FILE this.name + "_Stub.c"-»
«EXPAND Stub::stub FOR this-»
«ENDFILE»
«ENDDEFINE»



I did some debugging and I stepped into:

XpandFacade.evaluate
-> XpandFacade.evaluate2
---> XpandExecutionContextImpl.findDefinition (public)
-----> XpandExecutionContextImpl.findDefinition (private) :


 private XpandDefinition findDefinition(final XpandDefinition[] definitions, final String name, final Type target,
            Type[] paramTypes, final XpandExecutionContext ctx) {
        if (paramTypes == null) {
            paramTypes = new Type[0];
        }
        final Set<Callable> features = new HashSet<Callable>();
        for (int i = 0; i < definitions.length; i++) {
            final XpandDefinition def = definitions[i];
            if (def.getParams().length == paramTypes.length) {
                final List<Type> defsParamTypes = new ArrayList<Type>();
                Type t = null;
                boolean complete = true;
                for (int j = 0; j < paramTypes.length && complete; j++) {
                    t = ctx.getTypeForName(def.getParams()[j].getType().getValue());
                    if (t == null) {
                        complete = false;
                    }
                    defsParamTypes.add(t);
                }
                t = ctx.getTypeForName(def.getTargetType());
                if (t == null) {
                    complete = false;
                }
                if (complete) {
                    features.add(new DefinitionOperationAdapter(def, def.getName(), t, defsParamTypes));
                }
            }
        }
        final DefinitionOperationAdapter defAdapter = (DefinitionOperationAdapter) PolymorphicResolver.getOperation(
                features, XpandUtil.getLastSegment(name), target, Arrays.asList(paramTypes));
        if (defAdapter != null)
            return defAdapter.def;
        return null;
    }


I notice the following:

- first loop is taken 4 times since 4 definitions were found in my template (which is ok) main, header, proxy and stub For Interface

- 2nd loop is always false, since paramTypes.length is 0 for all definitions

- ctx.getTypeForName returns false every time (for Interface). So "complete" is always false, and features.add is never called

- defAdapter is null, so finddefinition returns null always resulting in the exception

I don't know which of the observations above are expected or not, but I do know that findDefinition should not return null ......
Any idea what is going wrong here?



Re: [xpand] problem calling xpand from Java [message #559368 is a reply to message #559366] Thu, 16 September 2010 13:33 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13319
Registered: July 2009
Senior Member
Hi,

you call the generator with EmfMetaModel but the template uses JavaBeansMetamodel. The error message should hint you to it Wink

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [xpand] problem calling xpand from Java [message #559373 is a reply to message #559368] Thu, 16 September 2010 13:38 Go to previous messageGo to next message
Arwin is currently offline ArwinFriend
Messages: 3
Registered: September 2010
Junior Member
Thanks for the quick response!
So the quickest solution is to register a JavaBeansMetamodel to my executionContext?
Is that possible? So far I only found examples using EmfMetaModel.


Re: [xpand] problem calling xpand from Java [message #559378 is a reply to message #559373] Thu, 16 September 2010 13:45 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13319
Registered: July 2009
Senior Member
Hi,

yes or you change the import to «IMPORT idl»

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [xpand] problem calling xpand from Java [message #559386 is a reply to message #559378] Thu, 16 September 2010 13:56 Go to previous message
Arwin is currently offline ArwinFriend
Messages: 3
Registered: September 2010
Junior Member
I did register the JavaBeansMetaModel and it works perfectly now! Thanks! Smile

	        executionContext.registerMetaModel(new JavaBeansMetaModel());
Previous Topic:InstantiationException while assigning vaiables
Next Topic:[XPand/MWE] Error executing workflow - java.lang.NoSuchFieldError: ruleMemo
Goto Forum:
  


Current Time: Tue Jan 28 06:33:44 GMT 2020

Powered by FUDForum. Page generated in 0.01934 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top