Skip to content

Instantly share code, notes, and snippets.

@baldimir
Created May 21, 2018 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baldimir/6d27b37d157abd87b846e0947b1011cb to your computer and use it in GitHub Desktop.
Save baldimir/6d27b37d157abd87b846e0947b1011cb to your computer and use it in GitHub Desktop.
public static class StaticMethods {
public static String getString1(final String string) {
return string;
}
public static String getString2(final String string) {
return string;
}
}
public static class StaticMethods2 {
public static String getString3(final String string, final Integer integer) {
return string + integer;
}
}
@Test
public void testImportFunctions() {
final String drl = "package org.drools.compiler.integrationtests.drl;\n" +
"import function " + StaticMethods.class.getCanonicalName() + ".*;\n" +
"import function " + StaticMethods2.class.getCanonicalName() + ".getString3;\n" +
"import " + Cheese.class.getCanonicalName() + ";\n" +
"global java.util.List list;\n" +
"\n" +
"function String getString4( String string ) {\n" +
" return string;\n" +
"}\n" +
"\n" +
"rule \"test rule1\"\n" +
" salience 30\n" +
" when\n" +
" Cheese()\n" +
" then\n" +
" list.add( getString1( \"rule1\" ) );\n" +
"end \n" +
"\n" +
"rule \"test rule2\"\n" +
" salience 20\n" +
" when\n" +
" Cheese( type == ( getString2(\"stilton\") ) );\n" +
" then\n" +
" list.add( getString3( \"rule\", new Integer( 2 ) ) );\n" +
"end \n" +
"\n" +
"rule \"test rule3\"\n" +
" salience 10\n" +
" when\n" +
" Cheese( $type : type);\n" +
" eval( $type.equals( getString1( \"stilton\" ) ) );\n" +
" then\n" +
" list.add( getString2( \"rule3\" ) );\n" +
"end \n" +
"\n" +
"rule \"test rule4\"\n" +
" salience 0\n" +
" when\n" +
" Cheese();\n" +
" then\n" +
" list.add( getString4( \"rule4\" ) );\n" +
"end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("imports-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession();
try {
final Cheese cheese = new Cheese("stilton",
15);
session.insert(cheese);
List list = new ArrayList();
session.setGlobal("list", list);
final int fired = session.fireAllRules();
list = (List) session.getGlobal("list");
assertEquals(4, fired);
assertEquals(4, list.size());
assertEquals("rule1", list.get(0));
assertEquals("rule2", list.get(1));
assertEquals("rule3", list.get(2));
assertEquals("rule4", list.get(3));
} finally {
session.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment