<div dir="ltr"><br>&nbsp;&nbsp; Hi Dave,<br><br>&nbsp;&nbsp; Excellent!<br>&nbsp;&nbsp; I will try to explain the current situation and one possible solution, but you may have better ideas.<br><br>&nbsp;&nbsp; Functions in Drools are compiled as simple static methods in a generated java class. We use MVEL Templates to generate the code of the class and the static method.<br>
<br>&nbsp;&nbsp; Take a look at JavaFunctionBuilder.java class for the code generation call and at javaFunction.mvel for the code template.<br><br>&nbsp;&nbsp; Now, the problem with globals is that they are scoped to sessions, not rulebases, so you can not resolve them until runtime. You can not for instance, make them a static reference of the generated class and set it at rulebase compilation time.<br>
<br>&nbsp;&nbsp; So, my suggestion would be to:<br><br>1. at compile time, use JavaDialect.analyzeBlock() method to analyze and find out what are the globals that are used by the funcion method code.<br><br>2. modify the code generation to add parameters to that in the method call. So, if &quot;log&quot; is a global and if the function is declared like this:<br>
<br>function void someFunction( String param ) {<br>&nbsp;&nbsp;&nbsp; // ... code ...<br>&nbsp;&nbsp;&nbsp; log.something(...);<br>&nbsp;&nbsp;&nbsp; // ... code ...<br>} <br><br>&nbsp;&nbsp; you detect the use of &quot;log&quot; and add it as a parameter of the generated method:<br>
<br>...<br>public static void someFunction( Logger log, String Param ) {<br>&nbsp;&nbsp; ...<br>}<br>...<br><br>&nbsp;&nbsp; This way, at runtime we can inject the parameter into the call. You can look at JavaConsequenceBuilder.java and javaInvokers.mvel to see how we do kind-of the same thing for consequences.<br>
<br>3. Now the most interesting part. :)&nbsp; We use an ANTLR grammar for parsing Java code blocks. You need to change the parser to rewrite any function call the user is doing in his code to inject the log parameter transparently. I did the very same thing for modify blocks:<br>
<br>modify( $something ) {<br>&nbsp;&nbsp; ...<br>} <br><br>&nbsp;&nbsp; It is not hard once you get the hang of it. It is a bit of &quot;hand work&quot; though. Look at the JavaConsequenceBuilder.fixModifyBlocks() for what I did. Also, the ANTLR Java grammar is java.g. <br>
<br>&nbsp;&nbsp; Let me know if you have questions or if you have a better idea, and welcome aboard!<br><br>&nbsp;&nbsp; Cheers,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Edson<br><br><br><br><div class="gmail_quote">2008/10/13 David Sinclair <span dir="ltr">&lt;<a href="mailto:dsinclair@chariotsolutions.com">dsinclair@chariotsolutions.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div dir="ltr">Hi Edson,<br><br>My name is dave sinclair. I started using Drools in early August of this year, but have a lot of experience with rules engines. I have worked primarily with ArtEntrprise and some with PegaRules. I would love to help with this project and thought that this may be the area to jump in on.<br>

<br>I have the M2 code, and was reading it over the weekend. Mostly the core and some of the compilier. If you want to point me in the right direction on the global/functions I&#39;d be happy to have a look.<br><br>thanks<br>

<br>dave<div><div></div><div class="Wj3C7c"><br><br><div class="gmail_quote">On Mon, Oct 13, 2008 at 9:59 AM, Bagwell, Allen F <span dir="ltr">&lt;<a href="mailto:afbagwe@sandia.gov" target="_blank">afbagwe@sandia.gov</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">




<div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial">Edson,</font></span></div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial">Thanks for the tip. I figured I&#39;d need to use a workaround 
like this.</font></span></div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial">Unfortunately I&#39;m under a series of tight development and 
test deadlines all the way into early summer. Otherwise, I&#39;d have a look. 
Hopefully someone else out there can assist.</font></span></div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial">Thanks,</font></span></div>
<div dir="ltr" align="left"><span><font color="#0000ff" size="2" face="Arial">-A</font></span></div><br>
<div dir="ltr" align="left" lang="en-us">
<hr>
<font size="2" face="Tahoma"><b>From:</b> <a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a> 
[mailto:<a href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>] <b>On Behalf Of </b>Edson 
Tirelli<br><b>Sent:</b> Friday, October 10, 2008 5:46 AM<br><b>To:</b> Rules 
Users List<br><b>Subject:</b> Re: [rules-users] No globals in 
functions?<br></font><br></div>
<div></div>
<div dir="ltr"><br>&nbsp;&nbsp; Allen,<br><br>&nbsp;&nbsp; There is a technical 
explanation behind that and we never had the time to find a way to overcome this 
limitation. What you can do, although not ideal, is to send the global as a 
parameter:<br><br>funcion void foo( Logger log, String cond ) 
<br>{<br>...<br>}<br><br>rule XYZ<br>when<br>then<br>&nbsp;&nbsp;&nbsp; foo( 
log, someString );<br>end<br><br>&nbsp;&nbsp; If you or anyone would like to 
help improving this, let us know and we can discuss ways into doing it. 
<br><br>&nbsp;&nbsp; []s<br>&nbsp;&nbsp; Edson<br><br>
<div class="gmail_quote">2008/10/9 Bagwell, Allen F <span dir="ltr">&lt;<a href="mailto:afbagwe@sandia.gov" target="_blank">afbagwe@sandia.gov</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
  <div><font size="3" face="Arial, sans-serif">
  <div>&nbsp;</div>
  <div><font size="2">There&#39;s probably an easy explanation for this. I was 
  wondering about why functions inside of rule files can&#39;t access 
  globals?</font></div>
  <div><font size="2"></font>&nbsp;</div>
  <div><font size="2">For example, I have a log4j logger that I pass into my rule 
  files via a global.&nbsp; The logger should never be a part of working memory. 
  It&#39;s just there to capture valuable feedback.</font></div>
  <div><font size="2"></font>&nbsp;</div>
  <div><font size="2">But I can&#39;t do this:</font></div>
  <div><font size="2"></font>&nbsp;</div>
  <div><font size="2">global Logger log;</font></div>
  <div><font size="2"></font>&nbsp;</div>
  <div><font size="2">function void foo(String cond)</font></div>
  <div><font size="2">{</font></div>
  <div><font size="2">&nbsp;&nbsp; if (cond == &quot;error&quot;)</font></div>
  <div><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; log.error(&quot;I saw an 
  error&quot;);</font></div>
  <div><font size="2">}</font></div>
  <div><font size="2"></font>&nbsp;</div>
  <div><font size="2">Because the compiler says that in the function it can&#39;t 
  resolve &#39;log&#39;.</font></div>
  <div><font size="2"></font>&nbsp;</div>
  <div><font size="2">-A</font></div>
  <div><font size="2"></font>&nbsp;</div>
  <div><font color="#008080" size="2" face="Tahoma, sans-serif">Allen F. 
  Bagwell</font></div>
  <div><font color="#008080" size="1" face="Tahoma, sans-serif">e-mail:&nbsp; <a href="mailto:afbagwe@sandia.gov" target="_blank">afbagwe@sandia.gov</a></font></div>
  <div><font color="#008080" size="1" face="Tahoma, sans-serif">phone:&nbsp; 
  505/284-4517</font></div>
  <div><font color="#008080" size="1" face="Tahoma, sans-serif">fax:&nbsp; 505/ 
  844-7886</font></div>
  <div><font color="#008080" size="1" face="Tahoma, sans-serif"></font>&nbsp;</div>
  <div style="margin-top: 5pt; margin-bottom: 5pt;"><font color="#008080" size="1" face="Tahoma, sans-serif">There is no monument dedicated 
  to the memory of a committee. -- Lester J. Pourciau</font></div>
  <div><font size="1" face="Tahoma, sans-serif"></font>&nbsp;</div>
  <div><font size="1" face="Tahoma, sans-serif"></font>&nbsp;</div>
  <div><font size="2"></font>&nbsp;</div></font></div><br>_______________________________________________<br>rules-users 
  mailing list<br><a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>

<br></blockquote></div><br><br clear="all"><br>-- <br>&nbsp;Edson Tirelli<br>&nbsp;JBoss Drools Core 
Development<br>&nbsp;JBoss, a division of Red Hat @ <a href="http://www.jboss.com" target="_blank">www.jboss.com</a><br></div></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br></div></div></div>
</blockquote></div><br><br clear="all"><br>-- <br> &nbsp;Edson Tirelli<br> &nbsp;JBoss Drools Core Development<br> &nbsp;JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>
</div>