ObjectDataCompiler 是用于将一个java 对象填充到template 内生成drl。
- 实际是转为String对象,父类为DataProviderCompiler
- 要注意对象值为空的处理,如果填充遇到null,会导致rule很奇怪。
测试java 代码如下
public class ObjectDataCompilerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectDataCompilerTest.class);
public static void main(String[] args) {
ObjectDataCompilerTest test=new ObjectDataCompilerTest();
try {
test.loadingFromDLRArrayCorrectnessCheck( );
test.TenTemplatesManyRules();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void loadingFromDLRArrayCorrectnessCheck() throws Exception {
final String[][] rows = new String[3][6];
rows[0] = new String[]{"tomato", "weight", "200", "1000", "6", "== Taste.GOOD || == Taste.EXCELENT"};
rows[1] = new String[]{"cucumber", "length", "20", "40", "15", "== Taste.EXCELENT"};
rows[2] = new String[]{"carrot", "weight", "0", "1000", "2", "== Taste.HORRIBLE"};
final ArrayDataProvider adp = new ArrayDataProvider(rows);
final DataProviderCompiler converter = new DataProviderCompiler();
try (InputStream resourceStream = KieServices.Factory.get().getResources().newClassPathResource("template_1.drl", getClass()).getInputStream()) {
final String drl = converter.compile(adp, resourceStream);
// prints rules generated from template
System.out.println(drl);
// assertEqualsIgnoreWhitespace(EXPECTED_RULES.toString(), drl);
// testCorrectnessCheck(drl);
}
}
public void TenTemplatesManyRules() throws IOException {
final KieServices kieServices = KieServices.Factory.get();
final ObjectDataCompiler converter = new ObjectDataCompiler();
try (InputStream resourceStream = kieServices.getResources().newClassPathResource("template_2.drl", getClass()).getInputStream()) {
final String drl = converter.compile(
generateParamSetCollection(5),
resourceStream);
// prints rules generated from template
System.out.println(drl);
//testManyRules(drl, 500);
}
}
private Collection<ParamSet> generateParamSetCollection(final int loops) {
final Collection<ParamSet> result = new ArrayList<>();
for (int i = 0; i < loops; i++) {
result.add(new ParamSet("tomato", "weight", 200, (1000 + i), 6, EnumSet.of(Taste.GOOD, Taste.EXCELENT)));
result.add(new ParamSet("tomato", "weight", 100, (500 + i), 6, EnumSet.of(Taste.BAD)));
result.add(new ParamSet("tomato", "length", 5, (10 + i), 6, EnumSet.of(Taste.AVERAGE)));
result.add(new ParamSet("tomato", "weight", 300, (4000 + i), 6, EnumSet.of(Taste.GOOD, Taste.EXCELENT)));
result.add(new ParamSet("tomato", "weight", 200, (6000 + i), 6, EnumSet.of(Taste.EXCELENT)));
result.add(new ParamSet("tomato", "length", 2, (4 + i), 6, EnumSet.of(Taste.AVERAGE, Taste.GOOD)));
result.add(new ParamSet("tomato", "weight", 100, (1000 + i), 6, EnumSet.of(Taste.GOOD)));
result.add(new ParamSet("tomato", "weight", 500, (1000 + i), 6, EnumSet.of(Taste.EXCELENT)));
result.add(new ParamSet("tomato", "length", 4, (15 + i), 6, EnumSet.of(Taste.AVERAGE, Taste.EXCELENT)));
result.add(new ParamSet("tomato", "weight", 200, (1000 + i), 6, EnumSet.of(Taste.GOOD, Taste.EXCELENT)));
}
return result;
}