0
votes

I have problems in add instances to list in RHS of rule.

There're two classes shown as below:

class Person {
    private java.lang.Integer age;
    private java.lang.String name;
}

class A {
    private java.util.List<Person> persons;
    private java.util.List<Person> selectedPersons;
}

In the following rule, I want to put person with age larger than 30 into selectedPersons.

rule "test"
when 
    $a:A()
    $p : Person(age > 30) from $a.persons
then
    $a.getSelectedPersons().add($p);
end

It works in eclipse, with Drools plugin; but it doesn't after deploying to KIE server. What I get is the instance reference only. Any ideas?

Also, I wonder why KIE server throw java.lang.NoSuchMethodError Exception when I add following constructor to Person class, while workbench can build and deploy the rule successfully:

public Person(Person p)
{
    this.name = p.name;
    this.age = p.age;
}
2
why not write a function to add into list, and call that function in drools?piyushj
What do you mean by "what I get is the instance reference only"? - No stack trace and full error message for the NSME?laune
@piyushj, could you please give an example of the function, and how to call it in drools?dehiker
@laune While I said "instance reference only", I mean in the drools result, there's only something like <selectedPersons> <Person reference="../../selectedPersons/Person"/> </selectedPersons>, but no name or agedehiker
@laune Also, what does NSME mean here?dehiker

2 Answers

1
votes

The class should look like this:

class A {
    private java.util.List<Person> persons;
    private java.util.List<Person> selectedPersons;

    //First create getter, setter for the lists.
    void addToList(Person item)
    {
        selectedPersons.add(item);
    }
}

And the rule should look like this:

rule "test"
when 
    $a:A()
    $p : Person(age > 30) from $a.persons
then
    $a.addToList($p);
end
0
votes
  1. "What I get is the instance reference only"

I should add constructor mentioned in the question. BTW, if there's a Date attribute in Person class, it should be copied or cloned, see here.

  1. "java.lang.NoSuchMethodError Exception when I add following constructor to Person class"

Hmm, after deleting the container and create it again, exception disappeared. I don't know if it's a problem of drools (Version 6.2), or a problem of process of setting up drools.