0
votes

Hi I am new to clips rule engine and have to do following in clips in mobile app (Android/Kotlin). we are using CLIPS4android library as wrapper of JNI.

How to get output from class file, when the rule gets executed by invoking the clips.run()

class RulesTest(filepath: String?) { private val clips: Environment fun stop() { clips.destroy() }

/**
 * Example of how to use "assert".
 * @throws CLIPSError
 */
@Throws(CLIPSError::class)
fun assertItems(products: List<Item>) {
    for (product in products) {
        Log.d(tag, "(Product " + "(productId " + product.ProductId + ")" + "(uomid " + product.UOMId + " )" + "(quantity " + product.Quantity + " ))")
        var InsertItem: String
        InsertItem = "(Product " + "(productId " + product.ProductId + ")" + "(uomid " + product.UOMId + " )" + "(quantity " + product.Quantity + " ))"
        clips.assertString(InsertItem)
    }
}

fun run() {
    clips.eval("(facts)");
    clips.run()
}

companion object {
    const val tag = "CLIPSProductRulesTest"
}

init {
    clips = Environment()
    clips.load(filepath)
    Log.d(tag, "Loading .clp...\n\n")
    clips.reset()
}

}

1
It's not clear what you mean by "output from class file".Gary Riley
@GaryRiley : Actually we are running the clips in Android using CLIPS4android library, See above this is the class which i am usingNishaL
I am able to run the rules using Clips.run() function. But the rule output is logged in logcat only. There is no function to get the output.NishaL

1 Answers

0
votes

In the 0.4 version of CLIPSJNI, you can define an implementation of the Router class to capture the output and then you can do whatever your want with the output in the print method of the implementation.

import CLIPSJNI.*;

class Example
  {  
   static class CaptureRouter implements Router
     {
      public int getPriority()
       {
        return 10;
       }

      public String getName()
        {
         return "grab";
        }

      public boolean query(
        String logName)
        {    
         if (logName.equals("wwarning") ||
             logName.equals("werror") ||
             logName.equals("wtrace") ||
             logName.equals("wdialog") ||
             logName.equals("wprompt") ||
             logName.equals("wdisplay") ||
             logName.equals("stdout"))
           { return true; }  

         return false;
        }

      public void print(
         String routerName,
         String printString)
         {
          System.out.print(printString);
         }

      public int getchar(
        String routerName)
        {
         return -1;
        }

      public int ungetchar(
        String routerName,
        int theChar)
        {
         return -1;
        }

      public boolean exit(
        int exitCode)
        {     
         return true; 
        }  

     }   

   public static void main(String args[])
     {  
      Environment clips;

      clips = new Environment();
      clips.addRouter(new CaptureRouter());

      clips.build("(defrule hello => (printout t \"Hello World!\" crlf))");
      clips.reset();
      clips.run();
     }  
  }