[rules-users] Noob question: graph searching, query, root node for a given node
I'm after a quick bit of help on how to do something that I think should be easy but I can't work out how. I have a Java-side class that essentially is something like declare Datum description: String broader: List Datum narrower: List Datum end These are arranged in a graph. Root nodes have no items in their broader list. I want a query that takes a datum and yields the root nodes for that datum. I think this should look something like query rootDatumsFor( Datum datum, Datum result ) not Datum() from $datum.broader or rootDatumsFor( $datum.broader, $result ) end but I then get confused. When there are no broader items, I am unclear how to assign $datum to $result. And I'm not convinced I'm doing the recursion correctly either. If an experienced Drools author could spend a minute and show me how to do this sensibly, I would be very grateful. -- View this message in context: http://drools.46999.n3.nabble.com/Noob-question-graph-searching-query-root-node-for-a-given-node-tp4030091.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Noob question: graph searching, query, root node for a given node
I want a query that takes a datum and yields the root nodes for that datum. Do you mean root node or nodes? Surely there can only be one root? Or do you mean you are looking for the leafs? Mark On 18 Jun 2014, at 13:31, Borris bor...@chaos.org.uk wrote: I'm after a quick bit of help on how to do something that I think should be easy but I can't work out how. I have a Java-side class that essentially is something like declare Datum description: String broader: List Datum narrower: List Datum end These are arranged in a graph. Root nodes have no items in their broader list. I want a query that takes a datum and yields the root nodes for that datum. I think this should look something like query rootDatumsFor( Datum datum, Datum result ) not Datum() from $datum.broader or rootDatumsFor( $datum.broader, $result ) end but I then get confused. When there are no broader items, I am unclear how to assign $datum to $result. And I'm not convinced I'm doing the recursion correctly either. If an experienced Drools author could spend a minute and show me how to do this sensibly, I would be very grateful. -- View this message in context: http://drools.46999.n3.nabble.com/Noob-question-graph-searching-query-root-node-for-a-given-node-tp4030091.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Noob question: graph searching, query, root node for a given node
Good question. I'm supporting a graph rather than a tree, so theoretically there could be more than one node that has no parents. But in my particular use case I am constraining the data so that there is never more than one root node. So how to find the root node (singular) from an arbitrary node in the graph is my goal. -- View this message in context: http://drools.46999.n3.nabble.com/Noob-question-graph-searching-query-root-node-for-a-given-node-tp4030091p4030096.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Noob question: graph searching, query, root node for a given node
Terms like root and parent are used with trees. You might call these nodes nodes with indegree 0. -W On 18/06/2014, Borris bor...@chaos.org.uk wrote: Good question. I'm supporting a graph rather than a tree, so theoretically there could be more than one node that has no parents. But in my particular use case I am constraining the data so that there is never more than one root node. So how to find the root node (singular) from an arbitrary node in the graph is my goal. -- View this message in context: http://drools.46999.n3.nabble.com/Noob-question-graph-searching-query-root-node-for-a-given-node-tp4030091p4030096.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Noob question: graph searching, query, root node for a given node
I guess it's probably useful for me to add that there are multiple such graphs and key part of the operation is which graph a datum belongs to. -- View this message in context: http://drools.46999.n3.nabble.com/Noob-question-graph-searching-query-root-node-for-a-given-node-tp4030091p4030097.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Noob question: graph searching, query, root node for a given node
It looks like your graph is not in the WM, so you can just iterate the references. Here is an example of how to do this. @Test public void testGraphIterationToFindLeafs() { String drl = import + Datum.class.getCanonicalName() + ;\n + import java.util.List;\n + query findLeafs(Datum datum, List results)\n + ( eval( datum.getChildren().size() == 0 ) and \n + eval( results.add ( datum ) ) )\n + or \n + ( eval( datum.getChildren().size() != 0 ) and \n + child : Datum( ) from datum.children and\n + findLeafs( child, results; ) ) \n + end\n; System.out.println( drl ); KnowledgeBase knowledgeBase = loadKnowledgeBaseFromString( drl ); StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession(); ListDatum list = new ArrayListDatum(); Datum d1 = new Datum(d1); Datum d2 = new Datum(d2); Datum d3 = new Datum(d3); Datum d4 = new Datum(d4); Datum d5 = new Datum(d5); Datum d6 = new Datum(d6); Datum d7 = new Datum(d7); d1.getChildren().add( d2 ); d1.getChildren().add( d3 ); d3.getChildren().add( d4 ); d3.getChildren().add( d5 ) ; d5.getChildren().add( d6 ) ; d6.getChildren().add( d7 ) ; ksession.getQueryResults(findLeafs, d1, list); System.out.println( list ); } public static class Datum { private String description; private ListDatum children; public Datum(String description) { this.description = description; children = new ArrayListDatum(); } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public ListDatum getChildren() { return children; } @Override public String toString() { return Datum{ + description=' + description + '\'' + '}'; } } On 18 Jun 2014, at 18:44, Borris bor...@chaos.org.uk wrote: Good question. I'm supporting a graph rather than a tree, so theoretically there could be more than one node that has no parents. But in my particular use case I am constraining the data so that there is never more than one root node. So how to find the root node (singular) from an arbitrary node in the graph is my goal. -- View this message in context: http://drools.46999.n3.nabble.com/Noob-question-graph-searching-query-root-node-for-a-given-node-tp4030091p4030096.html Sent from the Drools: User forum mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users