[rules-users] Decision table multiple sheets...

Swindells, Thomas TSwindells at nds.com
Wed Dec 21 05:52:31 EST 2011


We have the following code to load multiple sheets from a single xls - it's more involved than I would like and I'd agree with the sentiment that drools should load all the sheets not just the first one.

It's brutely pulled from out codebase so I'll leave it as an exercise to the reader to tidy it up for your usage,

Thomas


protected File loadXLS(KnowledgeBuilder kbuilder, Map<String, String> compiledFiles, File xls)
                                                                                   throws KnowledgeBaseLoadException {
                        log.info("Loading spreadsheet from {}", xls.getAbsolutePath());
                        DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
                        dtconf.setInputType(DecisionTableInputType.XLS);
                        SpreadsheetCompiler compiler = new SpreadsheetCompiler();
                        Workbook w;
                        String sheetName = "";
                        FileInputStream in = null;
                        try {
                                w = Workbook.getWorkbook(xls);
                                for (Sheet sheet : w.getSheets()) {
                                        in = new FileInputStream(xls);
                                        sheetName = sheet.getName();
                                        log.info("Compiling sheet {} from {}", sheetName, xls.getAbsolutePath());
                                        compileSheet(kbuilder, compiledFiles, xls, compiler, sheetName, in);
                                        IOUtils.closeQuietly(in);
                                }
                        }
                        catch (DecisionTableParseException e) {
                                throw new KnowledgeBaseLoadException("Failed to parse spreadsheet " + sheetName, e);
                        }
                        //CHECKSTYLE:OFF
                        //Need to catch the DecisionTableParseException exception
                        catch (RuntimeException e) {
                                throw new KnowledgeBaseLoadException("Failed to import decision table " + sheetName, e);
                        }
                        //CHECKSTYLE:OFN
                        catch (BiffException e) {
                                throw new KnowledgeBaseLoadException("Failed to read spreadsheet: " + e.getMessage(), e);
                        }
                        catch (IOException e) {
                                throw new KnowledgeBaseLoadException("Failed to read spreadsheet: " + e.getMessage(), e);
                        }
                        finally {
                                IOUtils.closeQuietly(in);
                        }
}

        private void compileSheet(KnowledgeBuilder kbuilder, Map<String, String> compiledFiles, File xls,
                                  SpreadsheetCompiler compiler, String sheetName, FileInputStream in) {
                try {
                        String compiled = compiler.compile(in, sheetName);
                        log.debug("Sheet {} compiled to:\n{}", sheetName, compiled);
                        compiledFiles.put(xls.getName() + "#" + sheetName, compiled);
                        kbuilder.add(ResourceFactory.newReaderResource(new StringReader(compiled)), ResourceType.DRL);
                }
                catch (DecisionTableParseException dtpe) {
                        // TODO fix dirty hack ... multi sheet support should be moved into drools
                        if (dtpe.getMessage().equals("No RuleTable's were found in spreadsheet.")) {
                                log.info("No rule tables found in sheet {}", sheetName);
                        }
                        else {
                                throw dtpe;
                        }
                }
        }

> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org [mailto:rules-users-
> bounces at lists.jboss.org] On Behalf Of Wolfgang Laun
> Sent: 21 December 2011 10:38
> To: Rules Users List
> Subject: Re: [rules-users] Decision table multiple sheets...
>
> By definition (see the "Expert" manual) only sheet 1 is used for generating
> rules.
>
> For controlling rule firing you may consider using any of the rule attributes
> supported by spreadsheets, e.g., agenda-group.
>
> -W
>
> On 21/12/2011, Sumeet Karawal <sumeet.karawal at tcs.com> wrote:
> >
> > Hi,
> >
> > I am having my rules in a Decision Table (.xls)
> >
> > There I have 3 sheets for rules.
> >
> > Like in Sheet1 there are two Rule Tables... In Sheet2 there are 2 and
> > in
> > Sheet3 there is only 1.
> >
> > When I am creating the knowledgebase and firing rules, the rules that
> > are in the Sheet1 are getting fired. But the rules in other sheets are
> > not getting fired.
> >
> > When I convert them to String to check using this code :
> >
> > String str = DecisionTableFactory.loadFromInputStream
> > (ResourceFactory.newClassPathResource
> > ("com/sample/MemberDiscountRules.xls").getInputStream(), dtconf);
> > System.out.println(str);
> >
> > I can see the rules that are present in Sheet1 only.
> >
> > Please anybody help me on this. How can I get the rules in other two
> > sheets to get fired.
> > Also how can we fire the rules of only a particular Rule table in the
> > decision table.
> >
> > Thanks & Regards,
> > Sumeet
> >
> > Mailto: sumeet.karawal at tcs.com
> >
> >
> > =====-----=====-----=====
> > Notice: The information contained in this e-mail message and/or
> > attachments to it may contain confidential or privileged information.
> > If you are not the intended recipient, any dissemination, use, review,
> > distribution, printing or copying of the information contained in this
> > e-mail message and/or attachments to it are strictly prohibited. If
> > you have received this communication in error, please notify us by
> > reply e-mail or telephone and immediately and permanently delete the
> > message and any attachments. Thank you
> >
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users


**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************




More information about the rules-users mailing list