Unable to filter list based on the node type

  • 14
  • 3

Hi everybody,

I'm trying to write a mapping inside a loop-macro, but I wasn't able to achieve what I wanted.
I have a list of rootElements with different nodes instances implementing interfaces (so I can tell what they are all about). At some point in my logic I want to do something like:

node.rootElements.ofType<node<ILinkNode>>

But, unfortunately, all the nodes are returned without taking into account the type, can anybody point out the way of achieving this filtering?

Regards
Peter.
Hello Peter,

I think you can try something like this:
node.rootElements.where({~it => it.isInstanceOf(ILinkNode; })

Here "where" is an operation from j.m.collections language (you should import it),
{~it => it.isInstanceOf(ILinkNode; } is a closure and ".isInstanceOf" is a node operation.

Also you can filter this list in a simple for loop using ".isInstanceOf" check.

Best regards,
Ilya

I already tried that but doesn't return any node. I guess is because each item in the list is of type node<ILinkNode> and not just ILinkNode.
I finally ended up doing something like:

node.rootElements.where({~it => it.concept.isSubConceptOf(ILinkNode); }).ofType<node<ILinkNode>>;

Not really sure if there is another shortcut to do it. Let me know if you think there is one so I can improve my code.

Thanks for your help!

Peter,
 Our node.isInstanceOf(concept)
operation checks exactly what you need - that the node is instance of a given concept. This operation differs from baseLanguage's "instanceof" operation.
So, IMO, Ilya is right and the code he provided should work.

Regards,
Mihail