Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rules cannot iterate through facts? #92

Closed
brunoriscado opened this issue Sep 14, 2017 · 2 comments
Closed

Rules cannot iterate through facts? #92

brunoriscado opened this issue Sep 14, 2017 · 2 comments

Comments

@brunoriscado
Copy link

brunoriscado commented Sep 14, 2017

I understand one can apply multiple rules to any value on the facts map.

But what if I have a List of objects that I want to have evaluated by the same rule pipeline?

Imagine I have something like:

List<Person> people = new ArrayList<Person>();
people.add(new Person("Steve", 30));
people.add(new Person("Eddie", 42));
people.add(new Person("Mary", 32));

RuleBook ruleBook = ruleBookBuilder.addRule(RuleBuilder.create().withFactType(JsonObject.class)
                .when(person -> person.age < 40)
                .then(person -> person.getName())
                .build()).build();

        ruleBook.run(people);

-------OUTPUT------

Steve
Mary

It seems that if I want to put a List/Set of objects through a rule pipeline at the moment I always need to recreate a new Rulebook, is this right?

@Clayton7510
Copy link
Collaborator

Clayton7510 commented Sep 14, 2017

You do not need to create a new RuleBook each time you run it. RuleBooks can, however, change facts. So, if you rerun the same facts in a RuleBook multiple times, then you could certainly get a different state after subsequent runs.

Taking a modified version of your example:

   NameValueReferableMap<Integer> facts = new FactMap<>();

    facts.setValue("Steve", 30);
    facts.setValue("Eddie", 42);
    facts.setValue("Mary", 32);

    RuleBook ruleBook = RuleBookBuilder.create().addRule(rule -> rule.withFactType(Integer.class)
        .then(people -> people.entrySet().stream()
            .map(entry -> entry.getValue()) //map entries to people Facts
            .filter(person -> person.getValue() < 40) //filter out people Facts for those who are under 40
            .map(person -> person.getName()) //map people to their names
            .forEach(System.out::println))) //print the names of the people
        .build();

    ruleBook.run(facts);
    ruleBook.run(facts);

RuleBooks accept data as Facts, which can also be created or manipulated within a Rule. A Rule is provided all of the matching facts as an input. So, whereas the above example that I provided from your example does work, it's probably not the best use case for RuleBook. Something more akin to the MegaBank example, the POJO MegaBank Example or another scenario where there are different facts that determine whether or not an action should be taken is probably a better scenario.

Anyway, to your question about having to create a new RuleBook for each run: In the example I used above, the RuleBook I created is run twice from the same instance. In both cases the results are:
Steve
Mary

Thanks,

Clayton

@brunoriscado
Copy link
Author

Thanks will give this a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants