Created
August 7, 2023 14:36
-
-
Save mariofusco/ada6caebaf78557a4ca624ec2ea063a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testFromGlobalWithDuplicates() throws Exception { | |
String str = | |
"import java.util.concurrent.atomic.AtomicInteger;\n" + | |
"import " + NamedPerson.class.getCanonicalName() + ";\n" + | |
"global java.util.List list \n" + | |
"rule R when \n" + | |
" $i : AtomicInteger()\n" + | |
" $o : NamedPerson(age > $i.get()) from list\n" + | |
"then \n" + | |
" insert($o); \n" + | |
"end "; | |
KieSession ksession = getKieSession(str); | |
List<NamedPerson> strings = Arrays.asList(new NamedPerson("Mario", 1), new NamedPerson("Mario", 2)); | |
ksession.setGlobal("list", strings); | |
AtomicInteger i = new AtomicInteger(0); | |
FactHandle fh = ksession.insert(i); | |
assertThat(ksession.fireAllRules()).isEqualTo(2); | |
i.incrementAndGet(); | |
ksession.update(fh, i); | |
assertThat(ksession.fireAllRules()).isEqualTo(1); | |
} | |
public static class NamedPerson { | |
private final String name; | |
private final int age; | |
public NamedPerson(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
NamedPerson myPerson = (NamedPerson) o; | |
return Objects.equals(name, myPerson.name); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(name); | |
} | |
public int getAge() { | |
return age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment