JOINTJS: Get children recursively of a parent - parent-child

How can i get all children recursively in jointjs.
I have done embedding in correct order.
I have done some homework in trying to find the answer, I have been using cell.getNeighbours().
But this does not help me in retrieving what i want.It only give the first child or first neighbours.
Since the embedding is done correct, I was thinking to use cell.getEmbeddedCells().
I am looking for help in creating a recursive function using cell.getEmbeddedCells()

getNeightbors() is for retrieving neighbors of an element in terms of links (both inbound and outbound) that are connected to that element. This has nothing to do with embeds. For retrieving all the embedded cells and their embedded cells recursively, you can do something like this:
var subtree = [];
function collectDeepEmbedded(cell) {
_.each(cell.getEmbeddedCells(), function(c) {
subtree.push(c);
collectDeepEmbedded(c);
})
}
collectDeepEmbedded(myCell);

Related

How can I return the last array inside this recursive method?

I'm coding a calculator that simplifies a string of operations to arraylists of positive and negative results.
Right now, im trying to deal with adding the parenthesees feature and I thought about creating arraylists inside others to represent the operations within them.
There is only 1 method that processes everything, so when I add "(" an arraylist must be created in the current position of the array, and signify in some way to the rest of the code that next time the method goes in, it must start in that inner array.
So i thought about doing a recursive method that returns the "deepest" arraylist that has no more arrays inside.
public ArrayList<Object> deepest(ArrayList<Object> al){
ArrayList<Object> input=new ArrayList<>(al);
for(Object o:input){
if(o instanceof ArrayList){
return deepest((ArrayList<Object>) o);
}
}
return input;
}
Is this actually returning the deepest array inside the "tree" ? My code is doing some unexpected stuff and I feel like this starts to escape my understanding.
Your approach would return a leftmost ArrayList, not the deepest (the very first it finds in the tree). For the deepest, you would need a more exhaustive search, a counter that keeps track of the current depth, and also calculation of the "deepest so far" item at the appropriate places (after visiting all elements of an ArrayList).

Paper js hittest doesn't work in Compound Path as I expect

In paper js I've created a compound path and now I want to implement drag and drop on it. The compound path has two children (two circles).
Here's the issue: when I attach a mouse event to the compound path (using hittest to get the whole path), and click on it, I get the reference only to the first child path. If I click on the second child path, the hittest returns undefined, while I'd like to get the compound path.
Any idea?
Thanks.
Francesco R.
I had the same problem, but there are few ways to make it work.
I assume you use Tool. If so, you can try to get the item not by hit testing, but using ToolEvent's item object
example:
mousedown: function (event) {
if (event.item) {
// event.item is what you need
}
},
If you know what item you'll be hit testing - for example you want to check if user clicked on a specific item, you can call hitTest() from this item
example:
mousedown: function (event) {
var hit = yourItem.hitTest(event.point, { tolerance: 10 });
if (hit) {
// now you can use hit.item or yourItem ('cos you know the item)
}
},
You can try hitTest options. There you can specify what class of items are you looking for:
options.class: Only hit test again a certain item class and its sub-classes: Group, Layer, Path, CompoundPath, Shape, Raster, PlacedSymbol, PointText, etc.
Tell me if that helps or provide some test case. You can create a paperscript sketch here and provide us the link you get in your adress bar after running your sketch.
EDIT: Actually I've created a sketch for you, showing those methods: look here

Navigating along rapid.xml nodes

This question slightly differs from check for variable number of sibling nodes & different siblings in Rapidxml. In most of the examples I have found on the web I see hard coded keys, for example:
xml_node<>* root = doc.first_node("rootnode");
Here "rootnode" is hard coded. My situation is slightly different, in the sense that parsing and tagging is already done by rapidxml. I need to know the names as read by the parser by iterating over nodes and their sibling without knowing the hard coded name and the depth of the node. I am looking for the suggestion/solution on some kind of recursive navigation along rapidxml tree.
Thank you.
According to RapidXml's documentation (see here) the node name parameter in first_node() method is optional. You can just omit that parameter and you will get the first child node, regardless of its name:
xml_node<>* root = doc.first_node();
Then you can just get node's name by calling its name() method.

How to traverse/rebuild UIComponent tree structure in Flex

I have some kind of UIComponent grouping which may look something like this with the classes "Group" and "Element".
Groups can have children and children may be elements or groups again, basically similar to a file system or the str+g group function in several graphics programs. The simplest form of a group is a group with only children which are also the most low level groups in the tree.
Edit:
The display hierarchy is already existant, i try to persist it to xml.
Group
- element
- Group
- element
- Group
-element
-element
- element
- element
I want to rebuild this structure in an xml-document for persistence.
I know how to build an xml document in Flex but not how to (recursively) traverse this n-tree correctly.
Update:
For getting only the child nodes one could make use of the following algorithm (pseudo code). But somehow i don't understand how to create the xml from this.
walkTree(group) {
children = node.getChildren
if(children != null) {
for(int i=0; i<children.length; i++) {
if(children[i].isGroup()) {
walkTree(group[i]);
} else {
trace(child);
}
}
}
}
As a starter, I'd suggest this : http://www.sephiroth.it/tutorials/flashPHP/E4X/
So, basically, what you are looking for seems like E4X in Actionscript 3
My suggestion would be to have your structure be data driven from the start, so the XML controls the draw of the screen. Then you would have the XML and wouldn't need to make it.
However, if you really want to do this, you'll need to loop through all the children at each level and add some sort of node that describes the child. However, if you don't have a view that is data driven, I don't see what good this will do you (and if you do, and it's not XML, you're better off writing data export from the data side, not the view side).

Deleting items in foreach

Should you be allowed to delete an item from the collection you are currently iterating in a foreach loop?
If so, what should be the correct behavior?
I can take quite a sophisticated Collection to support enumerators that track changes to the collection to keep position info correct. Even if it does some compromisation or assumptions need to be made. For that reason most libraries simply outlaw such a thing or mutter about unexpected behaviour in their docs.
Hence the safest approach is to loop. Collect references to things that need deleting and subsequently use the collected references to delete items from the original collection.
It really depends on the language. Some just hammer through an array and explode when you change that array. Some use arrays and don't explode. Some call iterators (which are wholly more robust) and carry on just fine.
Generally, modifying a collection in a foreach loop is a bad idea, because your intention is unknown to the program. Did you mean to loop through all items before the change, or do you want it to just go with the new configuration? What about the items that have already been looped through?
Instead, if you want to modify the collection, either make a predefined list of items to loop through, or use indexed looping.
Some collections such as hash tables and dictionaries have no notion of "position" and the order of iteration is generally not guaranteed. Therefore it would be quite difficult to allow deletion of items while iterating.
You have to understand the concept of the foreach first, and actually it depends on the programming language. But as a general answer you should avoid changing your collections inside foreach
Just use a standard for loop, iterate through the item collection backwards and you should have no problem deleting items as you go.
iterate in reverse direction and delete item one by one... That should proper solution.
No, you should not. The correct behaviour should be to signal that a potential concurrency problem has been encountered, however that is done in your language of choice (throw exception, return error code, raise() a signal).
If you modify a data structure while iterating over its elements, the iterator might no longer be valid, which means that you risk working on objects that are no longer part of the collection. If you want to filter elements based on some more complex notation, you could do something like this (in Java):
List<T> toFilter = ...;
List<T> shadow;
for ( T element : toFilter )
if ( keep(element) )
shadow.add(element);
/* If you'll work with toFilter in the same context as the filter */
toFilter = shadow;
/* Alternatively, if you want to modify toFilter in place, for instance if it's
* been given as a method parameter
*/
toFilter.clear();
toFilter.addAll(shadow);
The best way to remove an item from a collection you are iterating over it to use the iterator explitly. For example.
List<String> myList = ArrayList<String>();
Iterator<String> myIt = myList.iterator();
while (myIt.hasNext()) {
myIt.remove();
}

Resources