[rules-users] Noob question: graph searching, query, root node for a given node
Mark Proctor
mproctor at codehaus.org
Wed Jun 18 14:57:29 EDT 2014
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();
List<Datum> list = new ArrayList<Datum>();
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 List<Datum> children;
public Datum(String description) {
this.description = description;
children = new ArrayList<Datum>();
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Datum> getChildren() {
return children;
}
@Override
public String toString() {
return "Datum{" +
"description='" + description + '\'' +
'}';
}
}
On 18 Jun 2014, at 18:44, Borris <borris at 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 at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20140618/e8aae2de/attachment-0001.html
More information about the rules-users
mailing list