-1

I have written a code in JESS. I am now running the code from Java. I have run to this problem that I want to have the engine.execute("") command in a for loop. My example code is :

for (int i = 0; i < 10; i++){
    for (int j = 0; j < 10; j++){
r.executeCommand("(answer(ident headers.get(i)(text patientData.get(j).get(i))");
    }
}

where answer is a deftemplate in JESS with two slots. I am reading a text file in Java and inserting the values in the slots of the deftemplate.

Thanks, Ali

  • you haven't actually asked a question, or provided any information about what error you're having. Please provide more information on exactly what the problem is. – Davis Broda Mar 26 '15 at 19:36
  • Well, for starters, you can't use variable names in string quotes, because that will print the literal name, not the value. You need to use the string concat operator to build a string containing variables. Ex: `"(answer(ident headers.get("+i+")(text patientData.get("+j+").get("+i+"))"` – Ryan J Mar 26 '15 at 19:37
  • sorry, I didnt add the problem, The whole phrase in the command will be treated as a string , while the headers.get() is a variable in Java. using the concat doesnt seem to work. – Alireza Khamesipour Mar 26 '15 at 20:12

1 Answers1

0

If answer is a deftemplate with slots ident and text, the following snippet looks like the construction of a fact:

(answer(ident headers.get(i)(text patientData.get(j).get(i))
  • A bare fact does not represent a Jess command that can be executed via Rete.executeCommand.
  • You shouldn't use the deprecated method executeCommand; use eval.
  • Your parentheses aren't balanced properly, 6 opening parentheses against 4 closing ones.
  • Assuming that headers is a List<String> in your Java application, you cannot simply throw a Java method call at Jess, which doesn't understand Java syntax in the first place and (presumably) doesn't know about the pojo header at all.
  • The same is true for the pojo patientData.
  • While you might pass objects from your Java program to the Jess engine, this is absolutely impossible for simple variables like i and j.

Considering all of this (most of which is easy to learn from the excellent Jess manual), using

r.eval( "(assert (answer (ident \"" +
        headers.get(i) +
        "\")(text \"" +
        patientData.get(j).get(i) +
        "\")))" );

might have a chance of succeeding and indeed insert a new fact into working memory.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thanks for the response Laune, I have inserted the facts now. Rules do fire in my .clp file, but I cannot assert deftemplates in the .clp file. Is there anything I have to do in addition for each rule and assert I want to make? Thank you very much Ali – Alireza Khamesipour Apr 01 '15 at 16:04
  • The assert function call as it is built in the eval call in my answer can also be used in a CLP file: `(assert (answer (ident "123")(text "some data"))` should give you a fact. – laune Apr 01 '15 at 16:20
  • Only one rule in the .clp file will be fired even though the facts should fire more rules. Do I have to add anything to make all the rules fire? Thanks – Alireza Khamesipour Apr 02 '15 at 14:39
  • Certainly not. - You might post a new question showing the rules you expect to fire and the data you insert. – laune Apr 02 '15 at 15:18