Sort Ecore classes topologically - sorting

Is there a way to perform topological sorting of the classes in an Ecore EPackage, i.e sort the list of classes so that classes that use another class as supertype are positioned after their superclass? I did something similar for another code generator using manual graph traversal, but surely someone must already have implemented this for Ecore.

MoDisco allows to define queries on metamodels. So you can write a query for an Ecore model that returns the EClasses in topological order. This shouldn't be a to difficult task with such a query implemented in Java. See the help for MoDisco for more information.

Related

How to build generic tree datastructure with ABAP OO

I want to build a tree datastructure in ABAP. My key requirement is the possibility to use generic objects as node for the tree.
I Java it would look something like this:
public class MyTree<? extends TreeNode> {
(...)
}
My current approach would be to define a class TreeNode which is the super class of all possible nodes inside the tree.
Is there a more elegant way to realize generic types with ABAP OO? Or are there maybe datastructures shipped with SAP that I don't know of?
Most operations on a SORTED TABLE have performance characteristics like a binary tree. I am pretty sure that the internal implementation of sorted tables is some flavor of binary trees (either that or skip-lists).
During my internship I recreated some typical data structures like linked lists or binary trees with ABAP objects and compared their performance to the equivalent native TABLE flavors - the native ones usually had the same complexity classes, but they always performed much faster. Unless you have some very special use-case which requires some exotic data structure, you should try to work with TABLE, SORTED TABLE or HASHED TABLE.

List implementation in ruby?

I'm a bit struggling with the understanding of the Array class in Ruby. I have seen on Google
that an Array class is actually more of a list, but I can't seem to find how it actually works.
I am really concerned with performance issues as I have to deal with large sorted lists, and
I don't want to step over the whole array to add a single element to it.
So I was wondering if there were any real and clear implementation of a list (as in caml for instance), and I am also looking for a good documentation about how Array's method are implemented, regarding optimization matters.
Thanks!
A Ruby array offers a full list interface:
push/<< for adding element at the end
each provides an iterator for list traversal
sort lets you sort the items with an optional block for a custom comparator
...
So there's no apparent need to have a special List class or Module - take Java for example, we end up using ArrayList if we need a List all the time because it gives us good performance and the additional benefit of accessing elements by their index. So Ruby (similar to other languages such as Python, PHP or Lua) tries to keep things simple with regards to collection types by offering just three types - Array, Hash and Set - therefore with a rich interface that makes it easy to emulate other collection types such as a List, Queue or Deque etc.
If you'd like to know details about the implementation, I would recommend simply downloading the Ruby sources and investigating the corresponding files (for MRI it's array.c in the top-level directory) .

Languages with native / syntactical / inline graph support?

The graph is arguably the most versatile and valuable data structure of all. I can store single variables, lists, hashes etc., and of course graphs, with it.
Given this, are there any languages that offer inline / native graph support and syntax? I can create variables, arrays, lists and hashes inline in Ruby, Python and Javascript, but if I want a graph, I have to either manage the representation myself with a matrix / list, or select a library, and use the graph through method calls.
Why on earth is this still the case in 2010? And, practically, are there any languages out there which offer inline graph support and syntax?
The main problem of what you are asking is that a more general solution is not the best one for a specific problem. It's just average for all of them but not a best one.
Ok, you can store a list in a graph assuming its degeneracy but why should you do something like that? And how would you store an hashmap inside a graph? Why would you need such a structure?
And do not forgot that graph implementation must be chosen accordingly to which operations you are going to do on it, otherwise it would be like using a hashtable to store a list of values or a list to store an ordered collection instead that a tree. You know that you can use an adjacency matrix, an edge list or adjacency lists.. every different implementation with it's own strenghts and weaknesses.
Then graphs can have really many properties compared to other collections of data, cyclic, acyclic, directed, undirected, bipartite, and so on.. and for any specific case you can implement them in a different way (assuming some hypothesis on the graph you need) so having them in native syntax would be overkill since you would need to configure them anyway (and language should provide many implementations/optimizations).
If everything is already made you remove the fun of developing :)
By the way just look for a language that allows you to write your own graph DSL and live with it!
Gremlin, a graph-based programming language: https://github.com/tinkerpop/gremlin/wiki
GrGen.NET (www.grgen.net) is a programming language for graph transformation plus an environment including a graphical debugger. You can define your graph model, the rewrite rules, and rule control with some nice special purpose languages and use the generated assemblies/C# code from any .NET language you like or from the supplied shell.
To understand why normal languages don't offer such a convenient/built-in interface to graphs, just take a look at the amount of code written for that project: the compiler alone is several man-years of work. That's a price tag too hefty for a feature/data structure only a minority of programmers ever need - so it's not included in general purpose programming languages.

Data structure and algorithms for a directed cyclic graph (F#)

I'm trying to analyse an application where the assembly references should be a directed-acyclic-graph, but aren't. There is also a related problem of sub-assemblies referencing different versions of one sub-sub-assembly (think Escher...)
What I want to do is analyse each assembly-subassembly pair and build up a picture of where things are wrong.
I need some guidance on what would be a good data structure for this. I'm not too sure that I can build up an immutable one, but I don't mind having it mutable internally then transformed to immutable at the end.
The other part of the question is what kind of algorithms I should use for filling the data structure, and also afterwards for 'analysing' the problems.
You can just use NDepend, it analyzes your assemblies and detects dependency cycles.
If you really want to do this yourself, I'd use QuickGraph to model the dependency graphs, it also includes graph algorithms, like topological sort.
I don't mind having it mutable internally then transformed to immutable at the end.
You may well find it easier to use immutable data structures throughout. In particular, you can easily represent a graph as a Map from source nodes to sets of destination nodes. For a topological sort, you want efficient access to the source nodes of a destination node so you may want to augment your graph with another Map going in the opposite direction.
I just implemented this in F# and the topological sort is just 12 lines of code... :-)
What you want to do is called "Topological sorting". Wikipedia has a good overview:
http://en.wikipedia.org/wiki/Topological_sort

Search algorithm for a sorted double linked list

As a learning excercise, I've just had an attempt at implementing my own 'merge sort' algorithm. I did this on an std::list, which apparently already had the functions sort() and merge() built in. However, I'm planning on moving this over to a linked list of my own making, so the implementation is not particuarly important.
The problem lies with the fact that a std::list doesnt have facilities for accessing random nodes, only accessing the front/back and stepping through. I was originally planning on somehow performing a simple binary search through this list, and finding my answer in a few steps.
The fact that there are already built in functions in an std::list for performing these kinds of ordering leads me to believe that there is an equally easy way to access the list in the way I want.
Anyway, thanks for your help in advance!
The way a linked list works is that you step through the items in the list one at a time. By definition there is no way to access a "random" element in the list. The Sort method you refer to actually creates a brand new list by going through each node one at a time and placing items at the correct location.
You'll need to store the data differently if you want to access it randomly. Perhaps an array of the elements you're storing.
Further information on linked lists: http://en.wikipedia.org/wiki/Linked_list
A merge sort doesn't require access to random elements, only to elements from one end of the list.

Resources