I have xtext file similar to the xtext describe in here:
(www.ebpml.org/blog2/index.php/2010/11/05/mde-xtext-and-json)
JSONDataSource: root = Object ;
Object: '{' firstObject=TerminalObject (',' objects+=TerminalObject)* '}';
Array: '['firstItem=ObjectValue (',' items+=ObjectValue)* ']';
EmptyObject: isEmpty?= '{}' ;
EmptyArray: isEmpty?= '[]' ;
ObjectValue: value=STRING | Object | Array | EmptyObject | EmptyArray ;
TerminalObject: element=STRING ':' (content = ObjectValue) ;
So after generating the code I have all the objects generated in java.
now in I want to have more methods in those generated java files.
what is the best way to do so?
thanks :)
Well, there are no easy ways to customize these classes. You can do basically two things:
Create your own EMF (Ecore) model, and create your grammar in a way that reuses your Ecore model. This way, you can define new operations, and create their implementations manually.
You could also set up a postprocessor for the Ecore model generator that fills out the contents. See http://christiandietrich.wordpress.com/2011/07/22/customizing-xtext-metamodel-inference-using-xtend2/ for details - but be vary, as the model inference cannot directly output custom code; there are some EMF genmodel-annotation based way to create the bodies of generated code (see the final part of http://jevopisdeveloperblog.blogspot.hu/2011/03/implement-tostring-with-xtexts.html for details).
Alltogether, customizing the generated EMF models is quite hard, and I do not recommend doing it. Although it seems unnecessary to introduce new helper classes, technologically it is much easier to do.
Related
I've applied the guidance on programmatic usage of M2Doc (also with this help) to successfully generate a document via the API, which was previously prepared by using the M2Doc GUI (configured .docx plus a .genconf file). It seems to also work with a configured .docx, but without a .genconf file.
Now I would like to go a step further and ease the user interface in our application. The user should come with a .docx, include the {m:...} fields there, especially for variable definition, and then in our Eclipse application just assign model elements to the list of variables. Finally press "generate". The rest I would like to handle via the M2Doc API:
Get list of variables from the .docx
Tell M2Doc the variable objects (and their types and other required information, if that is separately necessary)
Provide M2Doc with sufficient information to handle AQL expressions like projectmodel::PJDiagram.allInstances() in the Word fields
I tried to analyse the M2Doc source code for this, but have some questions to achieve the goal:
The parse/generate API does not create any config information into the .docx or .genconf files, right? What would be the API to at least generate the .docx config information?
The source code mentions "if you are using a Generation" - what is meant with that? The use of a .genconf file (which seems to be optional for the generate API)?
Where can I get the list of variables from, which M2Doc found in a .docx (during parse?), so that I can present it to the user for Object (Model Element) assignment?
Do I have to tell M2Doc the types of the variables, and in which resource file they are located, besides handing over the variable objects? My guess is no, as using a blank .docx file without any M2Doc information stored also worked for the variables themselves (not for any additional AQL expressions using other types, or .oclAsType() type castings).
How can I provide M2Doc with the types information for the AQL expressions mentioned above, which I normally tell it via the nsURI configuration? I handed over the complete resourceSet of my application, but that doesn't seem to be enough.
Any help would be very much appreciated!
To give you an impression of my code so far, see below - note that it's actually Javascript instead of Java, as our application has a built-in JS-Java interface.
//=================== PARSING OF THE DOCUMENT ==============================
var templateURIString = "file:///.../templateReqs.docx";
var templateURI = URI.createURI(templateURIString);
// canNOT be empty, as we get nullpointer exceptions otherwise
var options = {"TemplateURI":templateURIString};
var exceptions = new java.util.ArrayList();
var resourceSetForModels = ...; //here our application's resource set for the whole model is used, instead of M2Doc "createResourceSetForModels" - works for the moment, but not sure if some services linking is not working
var queryEnvironment = m2doc.M2DocUtils.getQueryEnvironment(resourceSetForModels, templateURI, options);
var classProvider = m2doc.M2DocPlugin.getClassProvider();
// empty Monitor for the moment
var monitor = new BasicMonitor();
var template = m2doc.M2DocUtils.parse(resourceSetForModels.getURIConverter(), templateURI, queryEnvironment, classProvider, monitor);
// =================== GENERATION OF THE DOCUMENT ==============================
var outputURIString = "file:///.../templateReqs.autogenerated.docx";
var outputURI = URI.createURI(outputURIString);
variables["myVar1"] = ...; // assigment of objects...
m2doc.M2DocUtils.generate(template, queryEnvironment, variables, resourceSetForModels, outputURI, monitor);
Thanks!
No the API used to parse an generate don't modifies the template file nor the .genconf file. To modify the configuration of the template you will need to use the
TemplateCustomProperties class. That will allow you to register your metamodels and service classes. This instormation is then used to configure the IQueryEnvironment, so you might also want to directly configure the IQueryEnvironment in your code.
The generation in this context referes to the .genconf file. Note The genconf file is also an EMF model, so you can also craft one in memory to launch you generation if it's easier for you. But yes the use of a .genconf file is optional like in your code example.
To the list of variables in the template you can use the class TemplateCustomProperties:
TemplateCustomProperties.getVariables() will list the variables that are declared with their type
TemplateCustomProperties.getMissingVariables() to list varaibles that are used in the template but not declared
You can also find le list of used metamodels (EPackage nsURIs) and imported services classes.
The type of variables is not needed at generation time, it's only needed if you want to validate your template. At generation time you need to pass a map from the variable name to its value as you did in your example. The value of a variable can be a any object from your model (an EObject), a String, an Integer, ... If you want to use something like oclIsKindOf(pkg::MyEClass) you will need to register the nsURI of pkg first see the next point.
The code you provided should let you use something like projectmodel::PJDiagram.allInstances(). This service needs a ResourceSetRootEObjectProvider() that is initialized in M2DocUtils.getQueryEnvironment(). But you need to declare the nsURI of your metamodel in your template (see TemplateCustomProperties). This will register it in the IQueryEnvironment. You can also register it yourself using IQueryEnvironment.registerEPackage().
This should help you finding the missing parts in the configuration of the AQL environment. Your code seems good and should work when you add the configuration part.
I want to add methods to my datamodel so I need a way to specify them inside my tdd data file(s).
For example having a tdd data file containing two scalars :
a: 1
b: 1
I would like to add a method area which multiplies them.
Is this even possible and if so how do I achieve this?
So let's say you have MyUtils that has a foo() and a bar() methods, and you want to access those in the templates.
You can add an arbitrary Java objects to the model using the eval data loader in data, like myUtils: eval('new com.example.MyUtils()'). Then you can issue myUtils.foo() in the templates. But, you wanted to add the methods at top level. That's also possible. Both in eval and in a custom DataLoader (whichever you want to use) you have access to engine, the fmpp.Engine object. And then you can pull this trick:
// Note: In case you are using eval, use Java 1.2 syntax (no generics).
TemplateHashModel myUtilsModel = (TemplateHashModel) engine.wrap(new MyUtils());
Map<String, TemplateModel> myUtilsMethodModels = new HashMap<>();
myUtilsMethodModels.put("foo", myUtilsModel.get("foo"));
myUtilsMethodModels.put("bar", myUtilsModel.get("bar"));
return myUtilsMethodModels;
Then you add that Map to data without a name. (If you add a Map to data without a name, its keys become top-level variables.)
Certainly it can be polished to be nicer, like find methods you want automatically, etc. Plus I did not try this above (so typos are possible). But this is the basic idea. (I guess it would be practical if FMPP had a data loader that loads the static methods of a class... But, right now it doesn't have that.)
I have this idea, where I want to use lua to create my levels, and the maps inside those levels. I want to simply and explicitly be able to manipulate data and add new levels as buy ins in the app store. How can I use lua to create maps and levels inside those maps? Does lua support OOP so I can make a base Map "class" and a base Level "class" or do I need to hardcode everything? I know for a fact that angry birds uses lua, so can I?
Any directions or samples are much appreciated. Thanks.
Lua does not support classes directly (although there are some libraries which offer class-like functionality), instead it uses data structures called tables. Tables are very versatile because they can contain strings, numbers, functions and other tables.
The big benefit of Lua is that it is scripted, so it can be downloaded as a file and run while the compiled game is running. Here's a possible example of level stored in a .lua file:
return {
name = 'Example level',
score = 80,
map = {
height = 8,
width = 8,
data = 'sssssssssoooooossoooooossoooooossoooooossoooooossbooooesssssssss'
},
ondraw = function (self)
drawmap(self.map)
drawscore(self.score)
end
}
Another feature which you may find useful are metatables. They sort-of allow inheritance to be used with tables. You can read more about implementing classes with metatables here: http://lua-users.org/wiki/SimpleLuaClasses
I have a UML model having OpaqueActions containing text conform to an XText Grammar/MetaModel. I am turning the UML model into text by means of an ACCELEO transformation. I'd like to invoke from the ACCELEO script a Java service which takes as input the text in the opaque actions within the model and provides as output the root element of the related model so that I can use it seamlessly from ACCELEO.
To this end I need to define a Java class with a method which: takes as parameter a String, invokes XTEXT, parses the text and, if it is correct, produces a related EMF model. Suppose the text is OCL (It isn't but I guess the procedure is the same), how would you do that?
You could try to load the OpaqueActions as the content of a resource in the resource set that holds the currently processed model. That will return the AST for that string.
I have seen the [DebuggerNonUserCode] and [ExcludeFromCodeCoverage] attributes in resources and other SO questions about exlcuding code from coverage statistics, and wanted to know if it was possible to automatically add this attribute to the classes in the code generated by the Entity Framework using .NET 4.0.
Also would it need to be class level or could it be on the diagram.Designer.cs level, needing one attribute for all code generated by that diagram?
Since partial classes (which Entity Framework creates) merge attributes, extended functionality in other partial classes are also excluded if the attribute is class level in the template, it will have to be applied at the method level.
The best way that I've found to do this is using T4 (as recommended in #Craig Stuntz's answer) to:
include: using System.Diagnostics.CodeAnalysis; at the top of the file
Then apply [ExcludeFromCodeCoverage] to getters, setters and Factory methods by searching for:
#>get
#>set
Template_FactoryMethodComment
and placing them in the appropriate place.
This was made a lot easier using Tangible's T4 editor Extension for VS.
This is my first attempt and it seems to work, "your milage may vary", so complete a test run to make sure everything's working as necessary.