I am facing a problem and I am kind of desesperate :
I am trying to transform a constraint OCL into a C# program. To do so, I define my ocl constraints in a CompleteOCL document, and I save it as Abstract Syntax : POC.ocl.oclas. Then I use Acceleo with the Pivot Meta-model ('http://www.eclipse.org/ocl/2015/Pivot').
However, common OCL operations (such as 'size') are defined in another model : the Library. So when I try to recover operations used on my OCL model, nothing happened, I can only recover the operation I defined in my ocl document.
When I opened POC.ocl.oclas, I have these 2 models :
POC.ocl model + Library model.
I defined these generation :
[comment encoding = UTF-8 /]
[module generate('http://www.eclipse.org/ocl/2015/Pivot','http://www.eclipse.org/ocl/2015/Library')]
[template public generateElement(aModel : Model)]
[comment #main/]
[file (aModel.name + 'xx', false, 'UTF-8')]
yo
[/file]
[/template]
And it only generate one file : "POC.oclxx", not "Library.oclxx"
That lead us to this question :
Is it possible in Acceleo to make a reference to another model (than the main one) ?
And if it is, how to do that ?
ANNEXE :
The code I wrote :
[comment getCode() opération/]
[template public getCode(operationCallExp : pivot::OperationCallExp) post (trim())]
[operationCallExp.ownedSource.getCode()/]
[operationCallExp.referredOperation.name/][operationCallExp.ownedArguments -> getArguments()/]
[/template]
In theory, [operationCallExp.referredOperation.name/] gives me the name of the operation. In reality, it gives me nothing, except when I defined the operation (and thus when the operation doesn't come from the OCL Library)
Thank you in advance !
The zipped projet : Archive_OCL_Acceleo
The POC folder contains POC metamodel (POC.ecore), OCL constraint on this metamodel (POC.ocl) and the Pivot model associate (POC.ocl.oclas). Files generated by Acceleo are in the files folder
The POC_Acceleo forlder contains the Acceleo transformation (generate.mtl)
From the *.oclas file extension, I take it that you are using the/my Pivot-based Eclipse OCL Abstract Syntax.
My first attempt at Java code generation from OCL used Acceleo, but I abandoned this for various reasons, not least of which is that the step from OCL AS to Java code is far too big to perform in a single M2T step. While Java (and no doubt C#) is deceptively similar to OCL making a simple text template-driven translation attractive, that approach is doomed to support only a modest language subset. Real code generation needs real analyses such as Common Subexpression Elimination and these introduce a conflict between preserved source and rewritten source, if you rewrite the source.
The current Eclipse OCL to Java Generator (my third attempt) uses an intermediate CG model where rewrites happen. It is intended to be retargetable to C (or C# or ...). I have many plans for a higher level of auto-generation in my next (fourth) attempt with a further Java (or C or C# or ...) intermediate model to separate the 'trivial' textual language serialization from the non-trivial language concept synthesis.
If you are interested in a serious rather than simplified example tool for C# generation, I strongly recommend you look at the Eclipse OCL CG. If you want to work collaboratively to making it better and are happy to make you contributions available under the EPL, then perhaps we can arrange something.
Are you using the latest code? I recall fixing a couple of bugs recently regarding missing 'cosmetic' AS model content.
Related
There may be some related questions, but I think my situation is peculiar enough to justify a question on its own.
I'm working on a historically grown huge Java project (far over one million LOC, due to other reasons we're still bound to Java 6 at the moment), where reflection is used to display data in tables - reflection is not used for dynamically changing the displayed data, but just for using some kind of short cuts in the code. A simplified part of the code looks like this.
TableColumns taco = new TableColumns(Bean.class);
taco.add(new TableColumn("myFirstMember"));
taco.add(new TableColumn("mySecondMember"));
...
List<Bean> dataList = getDataFromDB(myFilterSettings);
taco.displayTable(dataList);
So the values of the table cells of each row are stored in an instance of Bean. The values for the first cell comes from calling itemOfDataList.getMyFirstMember() (so here comes the reflection part of the code). The rendering of the table cells is done depending on the return type of the itemOfDataList.getMyFirstMember().
This way, it's easy to add new columns to the table, getting them rendered in a standard way without caring about any details.
Problem of this approach: when the getter name changes, the compiler doesn't notice and there will be an exception at runtime in case Bean.getMyFirstMember() was renamed to Bean.getMyFirstMemberChanged().
While reflection is used to determine which getter is called, the needed info is in fact available at compile time, there are no variables used for the column info.
My goal: having a validator that will check at compile time whether the needed getter methods in the Bean class do exist.
Possible solultions:
modifying the code (using more specific infos, writing an adapter, using annotations or whatever that can be checked at compile time by the compiler), I explicitely don't want a solution of this kind, due to the huge code basis. I just need to guarantee that the reflection won't fail at runtime.
writing a custom validator: I guess this shouldn't be too complex, but I have no real idea how to start, we use eclipse as ide, so it should be possible to write such a custom validator - any hints for a good starting point?
The validator should show a warning in eclipse if the parameter in the TableColumn(parameter) isn't final (should be a literal or constant). The validator should show an error in eclipse if the TableColumn is added to TableColumns and the corresponding Bean.getParameter() doesn't exist.
as we use SonarQube for quality checking, we could also implement a custom rule checking if the methods do exist - not completely sure if such a custom rule is possible (probably yes)
maybe other solutions that will give a fast feedback within eclipse that some tables won't render correctly after some getter methods were renamed
What I'm asking for:
what will be easier in this situation: writing a custom validator for eclipse or writing a custom rule for SonarQube?
hints where to start either approach
hints for other solultions
Thanks for your help.
Some alternatives:
You could migrate to more modern Java for this pattern, it is a prime candidate for method references. Then, your IDE of choice can automatically take care of the problem when you refactor/rename. This can be done bit-by-bit as the opportunity/necessity arises.
You could write your own custom annotations:
Which you can probably get SonarQube to scan for
Which could allow you to take advantage of javax.validation.* goodies, so your code may look/feel more like 'standard' Java EE code.
Annotations can be covered by a processor during the build step, various build tools have ways to hook this up -- and the processor can do more advanced/costly introspection so you can push the validation to compile-time as opposed to run-time.
There is TemplateLoader in Play 1.0 for generating templates in runtime.
Is there any solution to dynamically load template in Play 2.0? Or can I somehow convert it to scala code for using Eval?
For example: I want to store some templates in the database, so that certain users could edit them.
Play 2.0 already compiles your templates to object methods, so you don't have to 'dynamically load' them!
Consider this simple template called app/views/test.scala.html.
#(num:Long)
Your number is #num
It becomes a Scala method of views.html called test. Evaluate it with this code:
val msg : String = views.html.test(23).toString()
You don't have to use html views only. To use templates with strings, use the play.api.templates.Txt derived classes. This is a template called app/views/quick.scala.txt:
#(id:Long)Your id is #id
It becomes a method views.txt.quick and is used:
val msg2 : String = views.txt.quick(32).body
You can find out more in the documentation for the the play.api.templates package.
It seems the relevant code is in framework/src/play/src/main/scala/system/ApplicationProvider.scala in the Play-2.0 directory, particularly the ReloadableApplication class. I'm unsure how this compiling on the fly would suit you, since you don't want to do it when the template is requested (it is slow). Which means that storing in a database doesn't quite make sense: you don't want to store the template source code, but rather the compiled template object.
For arguments sake, if you just wrote the templates to the app/views directory, you could leave Play to compile them at its leisure. But then, beware, because they probably won't compile on a production system.
I have a large monolithic webapplication that I whish to break apart into smaller modules. As a first step I'd like to change the package hierarchy which currently looks like:
- com.companyname.project
- dao
- bean
- booking // possibly containing more sub packages
- core
... etc etc (there are a bunch of others as well)
- service
- booking // possibly containing more sub packages
- core
... etc etc
- logic
- bean
- booking // possibly containing more sub packages
- core
... etc etc
- service
- booking
- core
... etc etc
- web
- bean // same substructure as above...
- service // same substructure as above...
- taglib // same substructure as above...
- util
I would like it if the package structure used for instance booking as the package name below com.companyname.project
Now I am wondering if there is a tool that could for instance use a simple regex to do the restructuring for me.
E.g.
com.companyname.project.dao.bean.booking
would become:
com.companyname.project.booking.dao.bean
and
com.companyname.project.dao.service.booking
would become:
com.companyname.project.booking.dao.service
I could use Eclipse and drag and drop the packages, but I am looking for something that could do this with a minimal of my involvment, since it will be very repetetive.
You can use any of the Java IDE with refactoring compatibilities:
IntelliJ IDEA (The best refactoring support). It has open source community edition and closed source ultimate edition.
Eclipse (open source)
NetBeans (open source)
I don't recommend you using regexp search/replace since it is error prone.
Why not use Java itself? I believe in your current scenario, you could easily write a small Java program to do the exact refactorings you're talking about and ensure that nothing is missed along the way.
You could write a method that takes "pre" (i.e. "com.companyname.project.dao.bean.booking") and "post" (i.e. "com.companyname.project.booking.dao.bean") params and copies the "pre" structure appropriately into the "post" structure.
In this program, I envision a main method and a "convertPackage(pre,post)" method. You could then have an array of your "pre" and "post" structures and loop through converting them over to the new. It would be best to do this copying into some sort of temp directory and not muck with the original as an added safety measure.
Maybe my thinking is too simple on this, but I could see a program built to do this refactoring in 1/2 a day or less.
You'll get the added benefit of knowing exactly what is changing rather than the insecurity that comes with these automated tools that may do it right for you or may not...
I'm interested in an executed script allowing user input to modify the process and corresponding source.
What precedents exist to implement such a structure?
Yes, depending on what is meant.
Consider such projects as ObjectWeb ASM (see the the ASM 2.0 tutorial for a general rundown).
Trying to emit the-would-need-to-be-decompiled Java source code is another story: if this was the goal then perhaps the source should be edited, re-compiled, and somehow loaded in/over. (This is possible as well, consider tools like JRebel.)
Happy coding.
You should not be able to modify existing classes. But if you implement a ClassLoader then you can dynamically load classes from non-traditional sources: network, XML file, user input, random number generator, etc.
There are probably other, better ways.
Maybe the Java scripting API is what you're looking for:
http://docs.oracle.com/javase/6/docs/api/javax/script/package-summary.html
http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html
I wrote an app once that used reflection to allow tests to be driven by a text file. For instance, if you had a class like this:
class Tuner(String Channel) {
tune(){...
play(){...
stop(){...
}
You could execute methods via code like:
tuner=Channel 1
tune tuner
play tuner
stop tuner
It had some more capabilities (You could pass objects into other objects, etc), but mostly I used it to drive tests on a cable box where a full write/build/deploy in order to test took on the order of a half hour.
You could create a few reusable classes and tie them together with this test language to make some very complex and easy to create tests.
THAT is a DSL, not monkeying around with your loose-syntax language by eliminating parenthesis and adding underscores and dots in random locations to make it look like some strange semi-English.
I have a schema in xsd file. once in a while a new version of the schema is created, and I need to update my .ecore (and .genmodel).
How do I update them, without deleting them and re-generate them. I have made some manual modification to the ecore, and i want to keep this modifications.
Ido.
Use the Reload... action on the *.genmodel to update the *.ecore based on the new version of the *.xsd.
And don't change the .ecore directly. Using ecore: annotations in the schema. http://www.eclipse.org/modeling/emf/docs/overviews/XMLSchemaToEcoreMapping.pdf
I've never tried this, but the XSD FAQ says this:
JAXB produces a simple Java API given
an XML Schema and it does so using
essentially a black box design. EMF
produces an Ecore model given an XML
Schema and then uses template-based
generator technology to generate a
rich Java API (of hand written
quality). The XML Schema to Ecore
conversion can be tailored, the
templates used to generate the Java
API can be tailored, and the resulting
Java API can be tailored. The
generator supports merging
regeneration so that it will preserve
your hand written changes. In other
words, EMF is far richer and more
flexible, and supports a broader
subset of XML Schema (especially in
2.0, where wildcards and mixed content will be supported).
If I were you, I'd try some experiments to see how well this process works, and what the practical limitations are.
You can regenerate using the context menu options. To preserve your modifications:
If there is a method that has "Gen" added to the name -- e.g. setWhateverGen in addition to setWhatever -- new code will be generated to the "Gen" method. So leave the "Gen" method alone so that it can be overwritten, and then call it from the non-Gen method, which you can modify.
All the generated methods are annotated with #generated. If you add "NOT" -- #generated NOT -- it will not be overwritten.
All other content should be merged. Go ahead and experiment -- that's what version control is for....