JESS: Initialize variables inside a rule

View: New views
2 Messages — Rating Filter:   Alert me  

JESS: Initialize variables inside a rule

by Peter Santos-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear All,
 
When executing the code below I receive the following error:
 
-------------------------------------------------------------------------------------------
 
f-0   (MAIN::Exerc_Funcao (Fun_Processo "operador1") (Fun_Emp "operador2") (Treinado "nao"))
For a total of 1 facts in module MAIN.
Exception in thread "main" Jess reported an error in routine Context.getReturn
    while executing (+ ?Humano)
    while executing (store Newuser (+ ?Humano)).
  Message: No such variable Humano.
  Program text: ( store Newuser ( + ?Humano ) )  at line 1.
    at jess.Context.getVariable(Unknown Source)
    at jess.Variable.resolveValue(Unknown Source)
    at jess.dl.call(Unknown Source)
    at jess.ab.a(Unknown Source)
    at jess.Funcall.execute(Unknown Source)
    at jess.FuncallValue.resolveValue(Unknown Source)
    at jess.fk.call(Unknown Source)
    at jess.ab.a(Unknown Source)
    at jess.Funcall.execute(Unknown Source)
    at jess.Jesp.a(Unknown Source)
    at jess.Jesp.parseExpression(Unknown Source)
    at jess.Jesp.promptAndParseOneExpression(Unknown Source)
    at jess.Jesp.parse(Unknown Source)
    at jess.Rete.eval(Unknown Source)
    at jess.Rete.executeCommand(Unknown Source)
    at acidentes.main(acidentes.java:35)
 
-------------------------------------------------------------------------------------------
import jess.*;
public class acidentes {

public static void main(String[] unused) throws JessException {
 
  Rete engine = new Rete();

  Deftemplate a = new Deftemplate("Exerc_Funcao", "Exerc_Funcao", engine);
  a.addSlot("Fun_Processo", Funcall.NIL, "STRING");
  a.addSlot("Fun_Emp", Funcall.NIL, "STRING");
  a.addSlot("Treinado", Funcall.NIL, "STRING");
  engine.addDeftemplate(a);

  String[] dados = {"operador1", "operador2", "nao"};

  Fact g = new Fact("Exerc_Funcao", engine);
  
      g.setSlotValue("Fun_Processo", new Value(dados[0], RU.STRING));
      g.setSlotValue("Fun_Emp",new Value(dados[1], RU.STRING));
      g.setSlotValue("Treinado",new Value(dados[2], RU.STRING));
     

      engine.assertFact(g); 
      engine.executeCommand("(facts)");

  String rules ="(defrule Verifica_Funcao";
   rules +="(Exerc_Funcao {Treinado == nao})";
   rules +="=>";
   rules +="(bind ?Humano (+ ?Humano 3)))";  
    
  
   engine.executeCommand(rules);
   engine.executeCommand("(run)"); 
   engine.executeCommand("(store Newuser(+ ?Humano))"); 
   
   Value Newvalue = engine.fetch("Newuser");
   int results = 0;


  if(Newvalue != null){
    results = Newvalue.intValue(engine.getGlobalContext());
   }

 
 System.out.println(results);

  }
}
 
According to the error the variable does not exists.
 
Could you please help me?
 
Regards,

Re: JESS: Initialize variables inside a rule

by Ernest Friedman-Hill :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On May 13, 2008, at 12:10 AM, Peter Santos wrote:

>
>   String rules ="(defrule Verifica_Funcao";
>    rules +="(Exerc_Funcao {Treinado == nao})";
>    rules +="=>";
>    rules +="(bind ?Humano (+ ?Humano 3)))";
>
>
>    engine.executeCommand(rules);
>    engine.executeCommand("(run)");
>    engine.executeCommand("(store Newuser(+ ?Humano))");
>
>


The variable ?Humano is a local variable; it exists only on the right  
hand side of rule Verifica_Funcao. To define global variables --  
variables that are visible anywhere in a program -- you can use the  
"defglobal" construct:


    engine.executeCommand("(defglobal ?*Humano* = 0)");

    String rules ="(defrule Verifica_Funcao";
    rules +="(Exerc_Funcao {Treinado == nao})";
    rules +="=>";
    rules +="(bind ?*Humano* (+ ?*Humano* 3)))";

    engine.executeCommand(rules);
    engine.executeCommand("(run)");
    engine.executeCommand("(store Newuser(+ ?Humano))");


There are a few other problems with this code. First, the rule will  
never match the fact you've created because nao is an RU,SYMBOL, but  
you've populated your fact with RU.STRING data. Either use RU.SYMBOL  
for the slot data, or use Treinado == "nao", with quotes.

Finally, in the expression (store Newuser (+ ?Humano)), the "+"  
function isn't doing anything; it's just inefficient and messy. Just  
use (store Newuser ?Humano) or, of course, (store Newuser ?*Humano*) .



---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 ejfried@...
Livermore, CA 94550                 http://www.jessrules.com




--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users you@...'
in the BODY of a message to majordomo@..., NOT to the list
(use your own address!) List problems? Notify owner-jess-users@....
--------------------------------------------------------------------