Reasonning with Interface

33 views
Skip to first unread message

Jonathan MERCIER

unread,
Jan 27, 2015, 7:39:59 AM1/27/15
to drools...@googlegroups.com
Dear,

Drools require to follow Java convention as example a fields name has a method getName(). With complex data model is interesting to put inside Interface a standardized way to communicate.

Interface Human extends Walkable, Speakable {
    String getName();
}

Interface Walkable{
    int speed;
    int[3] getPosition(); // x,y,z
    void move( int speed );
}

class Person implements Human {
    String name;
    int[3] position;
 ....
}

and reasonning by contract with Interface

rule "Human  walk"
  when
    $h : Human( speed > 0)
  then
   System.out.println( $h.getName() + " " + "walk" );
end


this do not work because drools try to find the name fields in Human definition intead of the Human instance.


Someone could say, how doing this in drools ?

I do not want to have all Object with a deep inheritance to do this.

regards
  

Mark Proctor

unread,
Jan 27, 2015, 9:29:30 AM1/27/15
to drools...@googlegroups.com
I’m not really understanding what you mean in the last 3 sentence.

Drools expects JavaBeans, so those interfaces will need to obey JavaBean specs for setters and getters. If it obeys the spec, it should work.

Mark

regards
  

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/c53a98b9-8aa4-425a-96c8-bc1eb5c103d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Davide Sottara

unread,
Jan 27, 2015, 10:38:15 AM1/27/15
to drools...@googlegroups.com
I agree with Mark, this use case should work. if you are getting errors, could you provide the stack trace and, possibly, a reproducer?

Jonathan MERCIER

unread,
Jan 28, 2015, 5:48:17 AM1/28/15
to drools...@googlegroups.com
I though using interface worked before. Firslty I though I done a mistake somewhere but I see nothing abnormal and when I use the class instead of the Knowledge interface that build.

My code:

public interface Fact {
public Observation getObservation();
public String getName();
public String getSource();
public void setObservation(final Observation o);
public void setName(final String n);
public void setSource(final String s);
}

public interface Knowledge extends Fact{
public Constraint getConstraint();
public List<Knowledge> subKnowledges();
public Knowledge partOf();
public int getGroup();
public Conclusion getConclusion();
public void setConstraint(final Constraint c );
public void setSubKnowledges(final List<Knowledge> lk);
public void setPartOf(final Knowledge k );
public void setConclusion(final Conclusion c );
public void setGroup(final int g );
public void add(final Knowledge k);
}

basic rules:
rule "Result infer to a knowledge not yet checked"
when
$k: Knowledge( observation == Observation.UNKNOWN )
$r: Result( name == $k.name, observation != Observation.UNKNOWN )
then
$k.observation = $r.observation;
update( $k );
System.out.println($k.name +" "+ $k.observation + " " + $k.conclusion );
System.out.println($r.name +" "+ $r.observation );
//kieSession.getAgenda().getAgendaGroup( "conclusion" )
end

Stack trace:

java.lang.RuntimeException: Error while creating KieBase[Message [id=1, level=ERROR, path=fr/cea/ig/labgem/brools/biology/knowledge_reasoner.drl, line=48, column=0
   text=Rule Compilation error observation cannot be resolved or is not a field
observation cannot be resolved or is not a field
name cannot be resolved or is not a field
observation cannot be resolved or is not a field
conclusion cannot be resolved or is not a field
name cannot be resolved or is not a field
observation cannot be resolved or is not a field]]
	at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:365)
	at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:514)
	at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:485)



Jonathan MERCIER

unread,
Jan 28, 2015, 8:00:03 AM1/28/15
to drools...@googlegroups.com
using method defined in Interface works:

rule "Result infer to a knowledge not yet checked"
when
        $k: Knowledge( this.getObservation() == Observation.UNKNOWN )
$r: Result( this.getName() == $k.getName(), observation != Observation.UNKNOWN )
then
$k.setObservation( $r.getObservation() );
update( $k );
//System.out.println($k.name +" "+ $k.observation + " " + $k.conclusion );
//System.out.println($r.name +" "+ $r.observation );
//kieSession.getAgenda().getAgendaGroup( "conclusion" )
end


As you can see I do not use fields naming but the method name, this works but I hope that is not an hided eval.

Jonathan MERCIER

unread,
Jan 28, 2015, 8:44:55 AM1/28/15
to drools...@googlegroups.com
sorry guy now that work but I do not see which change fix it, my code is close than the old one.

Thanks you a lot to have answered on my question.

Regards

Mario Fusco

unread,
Jan 28, 2015, 8:47:04 AM1/28/15
to drools...@googlegroups.com
Hi,

which version are you using? I believe this problem has been already reported here https://issues.jboss.org/browse/DROOLS-509 and fixed as demonstrated by this test case https://github.com/droolsjbpm/drools/blob/master/drools-compiler/src/test/java/org/drools/compiler/integrationtests/Misc2Test.java#L6241

Note there that the interfaces I0 and I1 are defined as it follows

public interface I0 {
    String getValue();
}
public interface I1 extends I0 { }

and the pattern in the rule does 'I1( value == \"x\" )' that looks pretty identical to your test case.

Is your test case more complex than this? If so can you provide a complete reproducer of the problem?

Thanks,
Mario


--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.

Jonathan MERCIER

unread,
Jan 30, 2015, 5:53:14 AM1/30/15
to drools...@googlegroups.com
Yes mario that is exactly this patrtern
Reply all
Reply to author
Forward
0 new messages