Created
August 7, 2023 14:23
-
-
Save mariofusco/84fd85094ce6db650c0fabf157837d56 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 " + StringWrapper.class.getCanonicalName() + ";\n" + | |
"global java.util.List list \n" + | |
"rule R when \n" + | |
" $i : AtomicInteger()\n" + | |
" $o : StringWrapper(length > $i.get()) from list\n" + | |
"then \n" + | |
" insert($o); \n" + | |
"end "; | |
KieSession ksession = getKieSession(str); | |
List<StringWrapper> strings = Arrays.asList(new StringWrapper("a"), new StringWrapper("ab"), new StringWrapper("abc")); | |
ksession.setGlobal("list", strings); | |
AtomicInteger i = new AtomicInteger(0); | |
FactHandle fh = ksession.insert(i); | |
assertThat(ksession.fireAllRules()).isEqualTo(3); | |
i.incrementAndGet(); | |
ksession.update(fh, i); | |
assertThat(ksession.fireAllRules()).isEqualTo(2); | |
} | |
public static class StringWrapper { | |
private final String s; | |
public StringWrapper(String s) { | |
this.s = s; | |
} | |
@Override | |
public int hashCode() { | |
return 1; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
return obj instanceof StringWrapper; | |
} | |
public int length() { | |
return s.length(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment