0

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;
}
dehiker
  • 454
  • 1
  • 8
  • 21

2 Answers2

1

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
piyushj
  • 1,546
  • 5
  • 21
  • 29
  • While validating, I got __The method put(Person) is undefined for the type List__. Any ideas? – dehiker Jun 13 '16 at 11:48
  • edited the answer, `put` is not a valid command for list, its `add` – piyushj Jun 13 '16 at 11:50
  • Hmm, I got the same result as that of `$a.getSelectedPersons().add($p);`, the reference only. – dehiker Jun 13 '16 at 12:09
  • If you are adding a list item, from another list......... you will get reference only, so please be specific which value you need to deduce – piyushj Jun 13 '16 at 12:12
  • Yes, I need to add an item from one list to another, so I try `add(new Person($p))` too, which sometimes gives me NSME, sometimes not, even I add the constructor mentioned in the question. – dehiker Jun 13 '16 at 12:45
0
  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.

Community
  • 1
  • 1
dehiker
  • 454
  • 1
  • 8
  • 21