@Test public void testRuleCreation() { this.shell.eval("(import org.drools.Person)"); this.shell.eval("(defrule yyy => (printout t yy \" \" (eq 1 1) ) ) )"); Package pkg = shell.getStatefulSession().getRuleBase().getPackage("MAIN"); Rule rule = pkg.getRule("yyy"); assertEquals("yyy", rule.getName()); this.shell.eval( "(defrule xxx (Person (name ?name&bob) (age 30) ) (Person (name ?name) (age 35)) => (printout t xx \" \" (eq 1 1) ) )"); rule = pkg.getRule("xxx"); assertEquals("xxx", rule.getName()); assertEquals(2, pkg.getRules().length); assertTrue(pkg.getImports().containsKey("org.drools.Person")); WorkingMemory wm = shell.getStatefulSession(); wm.insert(new Person("bob", "cheddar", 30)); wm.insert(new Person("bob", "stilton", 35)); wm.fireAllRules(); assertEquals("yy truexx true", new String(this.baos.toByteArray())); }
public static void main(String[] args) throws Exception { ClipsShell shell = new ClipsShell(); ByteArrayOutputStream out = new ByteArrayOutputStream(); shell.addRouter("t", new PrintStream(out)); StringBuffer buf = new StringBuffer(); System.out.print("Drools>"); StringBuffer sessionLog = new StringBuffer(); while (true) { byte name[] = new byte[256]; System.in.read(name); String cmd = (new String(name)).trim(); if (cmd.equals("(exit)") || cmd.equals("(quit)")) { sessionLog.append(cmd); break; } buf.append(cmd); if (isBalancedBrackets(buf)) { String exp = buf.toString(); if (exp.startsWith("(save ")) { String file = getFileName(exp); System.out.println("Saving transcript to [" + file + "]"); writeFile(file, sessionLog); sessionLog = new StringBuffer(); System.out.print("Drools>"); } else { sessionLog.append(cmd + "\n"); if (exp.startsWith("(load ")) { String file = getFileName(exp); System.out.println("Loading transcript from [" + file + "]"); exp = loadFile(file); } shell.eval(exp); String output = new String(out.toByteArray()); if (output != null && output.trim().length() > 0) { System.out.println(output); } out.reset(); System.out.print("Drools>"); buf = new StringBuffer(); } } } System.out.println("Goodbye, and good luck !"); }
@Before public void setUp() { FunctionHandlers handlers = FunctionHandlers.getInstance(); handlers.registerFunction(new PlusFunction()); handlers.registerFunction(new MinusFunction()); handlers.registerFunction(new MultiplyFunction()); handlers.registerFunction(new ModifyFunction()); handlers.registerFunction(new CreateListFunction()); handlers.registerFunction(new PrintoutFunction()); handlers.registerFunction(new PrognFunction()); handlers.registerFunction(new IfFunction()); handlers.registerFunction(new LessThanFunction()); handlers.registerFunction(new LessThanOrEqFunction()); handlers.registerFunction(new MoreThanFunction()); handlers.registerFunction(new MoreThanOrEqFunction()); handlers.registerFunction(new EqFunction()); handlers.registerFunction(new SwitchFunction()); // handlers.registerFunction( new DeffunctionFunction() ); handlers.registerFunction(new ReturnFunction()); handlers.registerFunction(new RunFunction()); handlers.registerFunction(new BindFunction()); handlers.registerFunction(new NewFunction()); handlers.registerFunction(new SetFunction()); handlers.registerFunction(new GetFunction()); handlers.registerFunction(new CallFunction()); handlers.registerFunction(new AssertFunction()); this.shell = new ClipsShell(); this.baos = new ByteArrayOutputStream(); shell.addRouter("t", new PrintStream(baos)); }
@Before public void setUp() { this.shell = new ClipsShell(); this.baos = new ByteArrayOutputStream(); PrintStream p = new PrintStream(baos); shell.addRouter("t", p); }
@Test @Ignore public void testTemplateCreationWithJava() throws Exception { this.shell.eval("(deftemplate Person (slot name (type String) ) (slot age (type int) ) )"); this.shell.eval("(defrule yyy => (printout t yy \" \" (eq 1 1) ) ) )"); Package pkg = shell.getStatefulSession().getRuleBase().getPackage("MAIN"); Rule rule = pkg.getRule("yyy"); assertEquals("yyy", rule.getName()); this.shell.eval( "(defrule xxx (Person (name ?name&bob) (age 30) ) (Person (name ?name) (age 35)) => (printout t xx \" \" (eq 1 1) ) )"); rule = pkg.getRule("xxx"); assertEquals("xxx", rule.getName()); assertEquals(2, pkg.getRules().length); WorkingMemory wm = shell.getStatefulSession(); Class personClass = ((InternalRuleBase) this.shell.getStatefulSession().getRuleBase()) .getRootClassLoader() .loadClass("MAIN.Person"); Method nameMethod = personClass.getMethod("setName", new Class[] {String.class}); Method ageMethod = personClass.getMethod("setAge", new Class[] {int.class}); Object bob1 = personClass.newInstance(); nameMethod.invoke(bob1, "bob"); ageMethod.invoke(bob1, 30); Object bob2 = personClass.newInstance(); nameMethod.invoke(bob2, "bob"); ageMethod.invoke(bob2, 35); // Constructor constructor = personClass.getConstructor( new Class[] { // String.class,String.class, int.class} ); wm.insert(bob1); wm.insert(bob2); wm.fireAllRules(); assertEquals("yy truexx true", new String(this.baos.toByteArray())); }