<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    On 31/10/2010 05:35, Roger Smith wrote:
    <blockquote
      cite="mid:AANLkTinvbBFzGruVRV6-FLzvOgsweZnRqLWCjBj=+8Kr@mail.gmail.com"
      type="cite">
      <div><br>
      </div>
      We are working on a project to integrate Drools with Apache
      Hadoop, <a moz-do-not-send="true"
        href="http://hadoop.apache.org/">http://hadoop.apache.org/</a>,
      and run into some road blocks. We would very much appreciate any
      suggestions/help from this list.&nbsp;
      <div>
        <br>
      </div>
      <div>
        <p class="MsoNormal" style="margin-bottom: 12pt;"><span style="">We
            have an app where we call drools rule engine inside the
            reducer task of a Hadoop map reduce job. It throws
            a&nbsp;NullPointerException when we the rule package resource to
            the knowledge
            builder. The same code works fine when run&nbsp;as part of a
            stand alone app.&nbsp;<br>
            <br>
            Code:<br>
            <br>
            &nbsp;&nbsp;&nbsp; private static Map&lt;String,
            StatefulKnowledgeSession&gt;
            sessions =<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new
            HashMap&lt;String, StatefulKnowledgeSession&gt;();<br>
            &nbsp;&nbsp;&nbsp; private static final String RULE_PACK_DIR = "<a
              moz-do-not-send="true"
              href="file:///%5C%5Chome%5Cpranab%5CProjects%5Cgridx%5C">file:///home/roger/Projects/gridx/</a>";<br>
            &nbsp;&nbsp;&nbsp; private static final String RULE_PACK_EXT =
            ".drl";<br>
            &nbsp;&nbsp;&nbsp; <br>
            <br>
            &nbsp;&nbsp;&nbsp; public int process(String rulePackage, String dateTime,
            String type) throws TException {<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int rate = 0;<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StatefulKnowledgeSession session =
            sessions.get(rulePackage);<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (null == session){<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            KnowledgeBuilder kbuilder =
            KnowledgeBuilderFactory.newKnowledgeBuilder();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String
            rulePackPath = RULE_PACK_DIR + rulePackage + RULE_PACK_EXT;<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            kbuilder.add( ResourceFactory.newFileResource(rulePackPath
            ),
            ResourceType.DRL);<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (
            kbuilder.hasErrors() ) {<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            System.err.println( kbuilder.getErrors().toString() );<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            KnowledgeBase kbase =
            KnowledgeBaseFactory.newKnowledgeBase();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());<br>
            <br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session =
            kbase.newStatefulKnowledgeSession();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sessions.put(rulePackage,
            session);<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
            <br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ContractRule contractRule = new
            ContractRule();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contractRule.prepare(dateTime,
            type);;<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FactHandle ruleHandle =
            session.insert(contractRule);<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.fireAllRules();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("" +
            contractRule);<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rate = contractRule.getRate();<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session.retract(ruleHandle);<br>
            <br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return rate;<br>
            &nbsp;&nbsp;&nbsp; }</span></p>
        <p class="MsoNormal" style="margin-bottom: 12pt;"><span style=""><span
              style="font-size: 12pt; font-family: 'Times New
              Roman',serif;">This line throws the exception:<br>
              kbuilder.add( ResourceFactory.newFileResource(rulePackPath
              ), ResourceType.DRL);<br>
            </span></span></p>
      </div>
    </blockquote>
    <span style=""><span style="font-size: 12pt; font-family: 'Times New
        Roman',serif;">ResourceFactory.newFileResource just creates a
        file object but cleaning up the paths.<br>
        &nbsp;&nbsp;&nbsp; public FileSystemResource(File file) {<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( file == null ) {<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new IllegalArgumentException( "File must not
        be null" );<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.file = new File(
        StringUtils.cleanPath(file.getPath()) );<br>
        &nbsp;&nbsp;&nbsp; }<br>
        The code is borrowed from Spring and uses the sme cleanPath
        logic. Once the file is created it just creates the stream as
        so:<br>
        &nbsp;&nbsp;&nbsp; public InputStream getInputStream() throws IOException {<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.lastRead = getLastModified();<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new FileInputStream(this.file);<br>
        &nbsp;&nbsp;&nbsp; }<br>
        <br>
        So it's pretty basic, the stack trace with line numbers may
        help. But I suspect you'll need to do a debug and step into the
        drools code at the point where you get the nullpointer and see
        what's going on.<br>
        <br>
        Mark<br>
        <br>
      </span></span>
    <blockquote
      cite="mid:AANLkTinvbBFzGruVRV6-FLzvOgsweZnRqLWCjBj=+8Kr@mail.gmail.com"
      type="cite">
      <div>
        <p class="MsoNormal" style="margin-bottom: 12pt;"><span style=""><span
              style="font-size: 12pt; font-family: 'Times New
              Roman',serif;">
              <br>
              It works fine as a stand alone app, outside hadoop<br>
              <br>
              Roger Smith<br>
            </span></span></p>
        <div><span style=""><span style="font-size: 12pt; font-family:
              'Times New Roman',serif;"><br>
            </span></span></div>
        <span style="font-size: 12pt; font-family: &quot;Times New
          Roman&quot;,&quot;serif&quot;;"><br>
        </span>
        <div><br>
        </div>
        <div>&nbsp;</div>
      </div>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
rules-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>