dehiker
dehiker

Reputation: 454

How to add items to the list?

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;
}

Upvotes: 0

Views: 2463

Answers (2)

dehiker
dehiker

Reputation: 454

  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.

Upvotes: 0

piyushj
piyushj

Reputation: 1552

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

Upvotes: 1

Related Questions