yes you are right.. did you try removing the insert cmd from the second code snippet?<br>just to have the same scenario?<br>The first code should fail as the second. With the message Unexpected global [test2]<br><br><div class="gmail_quote">
On Tue, Jun 22, 2010 at 8:40 AM, Samuli Saarinen <span dir="ltr">&lt;<a href="mailto:samuli.saarinen@remion.com">samuli.saarinen@remion.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Actually I am trying to set an unexistent variable (test2) in both<br>
snippets. But for some reason only the second one produces the exception.<br>
<br>
Samuli<br>
<div><div></div><div class="h5"><br>
Salaboy wrote:<br>
&gt; Well, in the second code snippet you are trying to set an unexistent<br>
&gt; variable called test2. For setting a global variable you need to<br>
&gt; define it first in the drl file. Greetings!<br>
&gt;<br>
&gt; - CTO @ <a href="http://www.plugtree.com" target="_blank">http://www.plugtree.com</a><br>
&gt; - MyJourney @ <a href="http://salaboy.wordpress.com" target="_blank">http://salaboy.wordpress.com</a><br>
&gt; - Co-Founder @ <a href="http://www.jbug.com.ar" target="_blank">http://www.jbug.com.ar</a><br>
&gt; - Mauricio &quot;Salaboy&quot; Salatino -<br>
&gt;<br>
&gt; On Jun 22, 2010, at 4:32, Samuli Saarinen &lt;<a href="mailto:samuli.saarinen@remion.com">samuli.saarinen@remion.com</a>&gt;<br>
&gt; wrote:<br>
&gt;<br>
&gt;&gt; Hello,<br>
&gt;&gt;<br>
&gt;&gt; I&#39;m new to drools and have problems with globals or with the different<br>
&gt;&gt; behaviour of drools depending on how the globals are specified.<br>
&gt;&gt;<br>
&gt;&gt; I have the following simple rule:<br>
&gt;&gt;<br>
&gt;&gt; global String test<br>
&gt;&gt;<br>
&gt;&gt; rule &quot;Global&quot;<br>
&gt;&gt;   when<br>
&gt;&gt;   then<br>
&gt;&gt;     System.out.println(test);<br>
&gt;&gt; end<br>
&gt;&gt;<br>
&gt;&gt; If i run the rule with:<br>
&gt;&gt;<br>
&gt;&gt; KnowledgeBase kb = getKnowledgeBase(&quot;global.drl&quot;);<br>
&gt;&gt; StatelessKnowledgeSession ses = kb.newStatelessKnowledgeSession();<br>
&gt;&gt; ses.setGlobal(&quot;test2&quot;, &quot;foo&quot;);<br>
&gt;&gt; ses.execute(&quot;&quot;);<br>
&gt;&gt;<br>
&gt;&gt; I get the expected result of null printed<br>
&gt;&gt;<br>
&gt;&gt; but if I use commands:<br>
&gt;&gt;<br>
&gt;&gt; KnowledgeBase kb = getKnowledgeBase(&quot;global.drl&quot;);<br>
&gt;&gt; StatelessKnowledgeSession ses = kb.newStatelessKnowledgeSession();<br>
&gt;&gt; List&lt;Command&lt;?&gt;&gt; cmds = new ArrayList&lt;Command&lt;?&gt;&gt;();<br>
&gt;&gt; cmds.add(CommandFactory.newSetGlobal(&quot;test2&quot;, &quot;bar&quot;));<br>
&gt;&gt; cmds.add(CommandFactory.newInsert(&quot;&quot;));<br>
&gt;&gt; ses.execute(CommandFactory.newBatchExecution(cmds));<br>
&gt;&gt;<br>
&gt;&gt; I get java.lang.RuntimeException: Unexpected global [test2]<br>
&gt;&gt;<br>
&gt;&gt; Why is the behaviour different and which one is correct?<br>
&gt;&gt;<br>
&gt;&gt; Regards,<br>
&gt;&gt;<br>
&gt;&gt; Samuli<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Remion Oy           Etävalvontajärjestelmät liiketoiminnan<br>
&gt;&gt; Samuli Saarinen           tehostamiseen<br>
&gt;&gt; gsm +358 (0)50 3560075<br>
&gt;&gt; fax +358 (0)3 2125064       <a href="http://www.remion.com" target="_blank">www.remion.com</a><br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; rules-users mailing list<br>
&gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;<br>
</div></div>&gt; --<br>
&gt; STOP Virus, STOP SPAM, SAVE Bandwidth!<br>
&gt; <a href="http://www.safentrix.com/adlink?cid=0" target="_blank">http://www.safentrix.com/adlink?cid=0</a><br>
<div><div></div><div class="h5">&gt; -<br>
<br>
<br>
<br>
--<br>
Remion Oy           Etävalvontajärjestelmät liiketoiminnan<br>
Samuli Saarinen           tehostamiseen<br>
gsm +358 (0)50 3560075<br>
fax +358 (0)3 2125064       <a href="http://www.remion.com" target="_blank">www.remion.com</a><br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">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>
</div></div></blockquote></div><br><br clear="all"><br>-- <br> - CTO @ <a href="http://www.plugtree.com">http://www.plugtree.com</a>  <br> - MyJourney @ <a href="http://salaboy.wordpress.com">http://salaboy.wordpress.com</a><br>
 - Co-Founder @ <a href="http://www.jbug.com.ar">http://www.jbug.com.ar</a><br> <br> - Salatino &quot;Salaboy&quot; Mauricio -<br>