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

Go To StackoverFlow.com

9

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   
2012-04-04 16:51
by marathon
Please explain what you want to achieve with this query language. There are a number of options and it would help to know what your requirements are - Michael Slade 2012-04-04 18:33
As Michael Slade says, your use case will determine what will work better for you. For example, JSoup has a lot of tools for working with HTML, but assumes everything it's working with is HTML and is unsuitable for most XML (and trees can be expressed in a lot of other ways, too) - bdares 2012-04-05 02:24
Interesting problem, but why do you need a "language" for that? Just write a tree traversal algo, which checks based on the conditions specified. A query language will do the same for you, will just abstract it out. Curious to know, what is the use case? This will work for DS like List. Not sure about a tree structure: http://code.google.com/p/sbql4j - zengr 2012-04-05 03:11
I need a language for this, because users want to be able to write their own queries. If you have externalized expressions that get passed to a traversal algo, then basically you have a query language - marathon 2012-04-05 03:17
Ok, I am not sure about your detailed use case, but giving that at the code level might be a pain to support. Why not provide an web API for that tree and use eBay's http://ql.io to make queries. ql.io translates those queries to a web service call - zengr 2012-04-05 03:22


1

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.

2012-04-05 16:09
by Andrejs
yeah, the visitor pattern is very nice for trees. Very easy to do DFS and BFS traversals using that - marathon 2012-04-05 16:50
indeed. it also gives you type safety (updated answer - Andrejs 2012-04-05 18:17
Ads