ObjectDataCompiler compiler = new ObjectDataCompiler(); String generatedDRL = compiler.compile(ruleAttributes, new FileInputStream(REGULATION_TEMPLATE_FILE));
/** * Compile templates, substituting from a collection of maps or objects * into the given template. * * @param objs objs the collection of maps or objects * @param templateStream the template as a stream * @return the expanded rules as a string */ public String compile(final Collection<?> objs, final InputStream templateStream) { TemplateContainer tc = new DefaultTemplateContainer(templateStream); closeStream(templateStream); return compile(new ObjectDataProvider(tc, objs), new TemplateDataListener(tc)); } }
/** * Compile templates, substituting from a collection of maps or objects * into the given template. * * @param objs the collection of maps or objects * @param template the template resource pathname * @return the expanded rules as a string */ public String compile(final Collection<?> objs, final String template) { final InputStream templateStream = this.getClass().getResourceAsStream(template); return compile(objs, templateStream); }
/** * Add rule by instantiating a template * @param rule containing template * @return instantiated string */ private String instantiateTemplate(InstanceORM rule) { // create Drools template compiler ObjectDataCompiler compiler = new ObjectDataCompiler(); // get parameters Collection<Map<String, String>> paramsMap = new ArrayList<>(); Map<String, String> fields = rule.getFieldsAsMap(); // populate fields from string Collection collection = Collections.singletonList(fields); InputStream template = new ByteArrayInputStream(rule.getRule().getBytes()); // return compiled rule return compiler.compile(collection, template); }
public static void main(String[] args) throws SQLException { InputStream template = RuleTemplateArrayDemo.class.getResourceAsStream("/com/template/template.drt"); List<Person> personList = new ArrayList<>(); personList.add(new Person(1,"Tom")); personList.add(new Person(2,"Lucy")); ObjectDataCompiler dataCompiler = new ObjectDataCompiler(); String drl = dataCompiler.compile(personList, template); KieSession kieSession = getKieSessionByDrl(drl); Person person = new Person(1,""); kieSession.insert(person); kieSession.fireAllRules(); System.out.println("Person's username is " + person.getUsername()); // sql /*Connection connection = null; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select id, username from user "); ResultSetGenerator generator = new ResultSetGenerator(); String drl = generator.compile(resultSet, template);*/ }