Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A tree query language for in-memory trees of objects?

Tags:

java

tree

What options are there, preferably in Java?

I have seen JXPath that extends XPath to objects. Is there anything else?

edit: by tree query language, I mean a language that can create expressions that match objects (by whatever property) that have been organized into a tree.

Edit2 : Example:

Let's say I have a tree of these objects:

public interface Node {
  String getName();
  int getValue();
  String getSomeOtherAttribute();
  List<Node> getChildren();
  Node getParent();
}

Now imagine a hierarchy of these. What I am looking for is something that can query for instances in this tree. Such as "give me all the Node instances where the name is "bar", the value is less than 100 and the parent is "foo" and the parent's parent is "joe". All this in a nice concise language.

As I said, JXPath is one option. Looking for others. I have not found any.

BTW, I think a JXPath query would look something like "//joe/foo/bar[@value < 100]" ( or something like that)

someroot
    |
   joe
 / | \
c  d  foo
     / \
    f   bar,99   
like image 836
marathon Avatar asked Apr 04 '12 16:04

marathon


1 Answers

For tree structures that you create yourself you could apply the Visitor Pattern. Let your nodes accept a visitor and write all kinds of visitors that check for criteria and collect your objects.

Using Visitor also means your code will be type safe as opposed to a query inside a string. And if you rename your getter methods, IDE will rename them in all the Visitors and your code will still work. If your query is inside a string it can break.

Apart from JXPath there is JoQL which uses an SQL-like language for querying objects, but its not really made for tree type structures.

like image 165
Andrejs Avatar answered Nov 06 '22 02:11

Andrejs