How to force Acceleo to iterate over a collection in the same order on every template execution? - eclipse-emf

I'm using an EMF model based on Modisco KDM metamodel. At some point of my Acceleo template I need to iterate over a collection, e.g.:
[for (e: AbstractCodeElement | action.codeElement) separator(', ')][e.generateCode() /]
The action.codeElement is a collection and modisco's kdm.ecore metamodel defines it as non-ordered.
Every time I run my generator, the output is generated on a different order. Cleary the serialized model xmi enforces a specific order, and every model editor (emf default editor, modisco editor) I open the model shows the same order always (matching the order the elements were serialized to the xmi file).
Since I cannot change the kdm.ecore metamodel to make the set ordered, would there be a workaround to get Acceleo to always iterate on the same order?
Thanks in advance

I'm afraid you can't.
Try and cast it to a sequence:
action.codeElement->asSequence()
but I don't think anything guarantees that the sequence you get will always be sorted in the same order.
If the metamodel is made that way, there should be a reason, so either you can contact the metamodel authors to check this reason, or you should sort the result of action.codeElement with some stable criterion:
action.codeElement->sortedBy( some OCL expression)

I don't know of a clean way. I solved the problem by altering the name attribute of a child element so that it was sortable alphabetically in the way I wanted.
I wanted Slots in the same order every time, so I changed the name of each of their "value" child.
The names looked like: "01_id", "02_username", "03_city", ... "10_instructions", "11_contact". I didn't have to change what the "value" elements held, just their name, which I wasn't using for anything anyways. Hope this helps.
[for (s : Slot | instanceSpecification.slot->select(definingFeature.name = 'column')->sortedBy(value->asSequence()->first().name)]
... do work here ...
[/for]

Related

Which component of MV should transform raw data into user friendly format?

There is a structure that holds raw data (consider int) and a gui that should visualize this data. Gui part consists of a few models and and a few standard views (table and tree). The value should be presented either as a string or as a picture depend on user choice. There are some additional difficulties: in some cases the text is different for different elements - i.e., for the same column 0 normally means "Ok", but for a particular element it should be printed as "Yes", also depend on a user choice some columns should not print text for 0 values at all, living a cell empty and don't clutter the table; next, sometimes cell should show two values - e.g., current value and requested - something like "Yes -> No". This also applies to the case when user wants to see icons.
I see two options:
The model forms string text itself and store it somewhere (in the model item may be). The model also forms composite icon from base icons if it is needed. In that scenario I can use standard delegate and just use standard roles reimplementing QAbstractItemModel::data() method.
Reimplement delegates' paint() method (actually I need to reimplement delegates anyway because I need custom editors like QComboBox). The delegate can get any information it needs through the model and custom roles in paint() method and form the string or complex icon itself. As a suboption, I suppose, it's possible for a model to compose needed information itself, like returning std::tuple<int /*value*/, int /*requested value*/, int /*source element type*/, bool /*text or icon*/> in data() method for Qt::DisplayRole.
Update: I want add one thought about second option. Presumably delegates were introduced to handle the work of representing data. One of the consequences of that is the flexibility delegates can give. If you want to see a raw number in some column, you can just leave the default delegate, if you want some additional info presented in a cell you can set a special delegate for that specific column. And it seems you don't need even touch the model or something else.

Forms - Managing OneToMany collection elements

Let's imagine these 2 entities:
ShoppingCart
creationDate
Item
name
shoppingCart # ManyToOne
I'm managing a ShoppingCart in a form with a CollectionType of Items
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder
// ...
->add('items', CollectionType::class, array(
'entry_type' => ItemFormType::class,
// ...
}
I'd like my users to be able to add/remove items, which seems to me like a quite common need.
If I understand well, here is what I have to do:
Define the reverse OneToMany side of the ShoppingCart relation to Item
Make sure this relation have the cascade={"persist"} and orphanRemoval=true options
Initialize this items Collection into the constructor
Set the allow_add and allow_delete form option to true
Set the by_reference form option to false
Make sure andItem() & removeItem() functions are implemented into ShoppingCart
Set/unset the owning side relation to the ShoppingCart in addition to add/remove the Item from the ShoppingCart items collection
Keep the collection indexes within javascript so Doctrine can keep track of references
Am I forgetting something ?
I find it a big pain in the arse for me compared to the need and to other frameworks/ORMs
Is there a way to do it simpler ?
As I understand, your question is "why is it so complicated", isn't it?
Well, it's not. You've listed things multiple layers in your project. To exaggerate the point a little bit - why didn't you wrote that in order to create this form you need to install Symfony or even turn on your computer. ;-)
Anyway, most of these things are not required, others should be done before anyway.
Let's start with your OneToMany relation (Doctrine level).
ShoppingCart is your aggregate root (DDD term, but it doesn't matter if you follow DDD idea), which means you probably will do most of work on this object which will handle Items inside. If so, then you should have done inversed side of the relation, which covers these points (I think you went into details so much to make your list longer ;-), they all should be in one list item), but it takes a minute to do this stuff:
Define the reverse OneToMany side of the ShoppingCart relation to Item
Make sure this relation have the cascade={"persist"} and orphanRemoval=true options
Initialize this items Collection into the constructor
Make sure andItem() & removeItem() functions are implemented into ShoppingCart
Set/unset the owning side relation to the ShoppingCart in addition to add/remove the Item from the ShoppingCart items collection
This has nothing to do with forms. It's about how you want your entities to work with Doctrine.
Following two are not required:
Set the allow_add and allow_delete form option to true
Set the by_reference form option to false
They are about enabling/disabling features. You want them, so you need to use them. That's obvious and still not so much to do.
Last one:
Keep the collection indexes within javascript so Doctrine can keep track of references
This is the one that I actually don't understand. But what you need (in your case, because you're using allow_add/delete feature), is to provide JavaScript code that will create HTML for new row or delete existing one. But that's also just about copying prototype code. Also there are ready examples in Symfony's doc.
Let me answer myself in a simpler manner:
No I am not forgetting anything
No we can't do simpler
Some reasons are explained in #dragoste answer

How to add multiple nodes to relations

How to add multiple node to relations here is my query
MATCH (n:Customer{name:"motoM"})-[:RECENT {default:TRUE}]-(l:Location{name:"Adugodi"}) return l how to write a query to add one more "location" node to the relation "recent" if location node is not found and setting default to true to newly created realtion
What about this?
MATCH (n:Customer{name:"motoM"})-[:RECENT {default:TRUE}]-(l:Location{name:"Adugodi"})
MERGE (n)-[:RECENT]->(l2:Location)
ON CREATE SET l2.default = true
RETURN l, l2
The direction needs to be specified so I made it up, but it might need to go the other way.
Well, I don't know if I understood what you were looking for, but this might help you :)
Try with this query:
MATCH (n:Customer{name:"motoM"})-[r:RECENT {default:TRUE}]-(:Location{name:"Adugodi"})
CREATE (l2:Location{name:"Wherever You need"})
With r,n,l,l2
Set r.default = false
With n,l2
CREATE (n)-[r2:RECENT{default:TRUE}]->(l2)
I'm using Withto make the query easier to read, but you can do it in a single query.
In fact, I think your problem is your Graph model.
You should probably do something like a Customer node, related to Location nodes with a "VISITED" relation, and when you create your VISITED relation, you set date property to timestamp. Then, when you get your relations, you can simply compare timestamps to get the closest one, and you know which one is the one your need. Also, if you need a default property, set it on the node, it'll be easier to match.
Tell me if you need a code example for match, create and set data with this graph model.

Custom Date Aggregate Function

I want to sort my Store models by their opening times. Store models contains is_open function which controls Store's opening time ranges and produces a boolean if it's open or not. The problem is I don't want to sort my queryset manually because of efficiency problem. I thought if I write a custom annotate function then I can filter the query more efficiently.
So I googled and found that I can extend Django's aggregate class. From what I understood, I have to use pre-defined sql functions like MAX, AVG etc. The thing is I want to check that today's date is in a given list of time intervals. So anyone can help me that which sql name should I use ?
Edit
I'd like to put the code here but it's really a spaghetti one. One pages long code only generates time intervals and checks the suitable one.
I want to avoid :
alg= lambda r: (not (s.is_open() and s.reachable))
sorted(stores,key=alg)
and replace with :
Store.objects.annotate(is_open = CheckOpen(datetime.today())).order_by('is_open')
But I'm totally lost at how to write CheckOpen...
have a look at the docs for extra

Is there a table which holds all possible states of a determined work Item type in TFS?

I'm developing a Time Tracking system in TFS so we can control how much time is spent in each task. I'm doing it by checking changes in work items states, and recording the time between states.
I'm using WCF and TFS2010 alert subscription.
Then I noticed the State column in the WorkItem table holds a string, instead of an ID pointing to a State.
With that in mind, I noticed I would have to parse each state and check if it corresponds to some string. And then, some day, someone might want to change the State name. Then we're doomed.
But before I hardcore (or put in some random config.xml)... let me ask, is there a table which holds all possible states of a determined work Item type in TFS?
The states of work item types are stored in the process template files. You can export the work item type to an xml file using witadmin.exe and see the allowed values of the "State" in there.
Programmatically, you can use the Microsoft.TeamFoundation.WorkItemTracking.Client namespace to get the WorkItemType object of your work item type, look for the FieldDefinition object of the "State" in the FieldDefinitions property, then get the possible states from the AllowedValues property of FieldDefinition class.