Search in sources :

Example 51 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class AccumulateTemplateTest method testInvokerGenerationSinglePattern.

@Test
public void testInvokerGenerationSinglePattern() {
    final String className = "accumulate0";
    final String[] declarationTypes = new String[] { "String", "int" };
    final Declaration[] declarations = new Declaration[] { new Declaration("name", store.getReader(Person.class, "name"), null), new Declaration("age", store.getReader(Person.class, "age"), null) };
    final Declaration[] inner = new Declaration[] { new Declaration("cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("price", store.getReader(Cheese.class, "price"), null) };
    final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
    final List globalTypes = Arrays.asList(new String[] { "String", "String" });
    final Map map = new HashMap();
    map.put("className", StringUtils.ucFirst(className));
    map.put("instanceName", className);
    map.put("package", "org.drools");
    map.put("ruleClassName", "Rule0");
    map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
    map.put("declarations", declarations);
    map.put("declarationTypes", declarationTypes);
    map.put("globals", globals);
    map.put("globalTypes", globalTypes);
    map.put("innerDeclarations", inner);
    map.put("attributes", new Attribute[] { new Attribute("int", "x") });
    map.put("initCode", "x = 0;");
    map.put("actionCode", "x += 1;");
    map.put("reverseCode", "");
    map.put("resultCode", "x + 10");
    map.put("supportsReverse", "false");
    map.put("resultType", Integer.class);
    map.put("hashCode", new Integer(10));
    map.put("isMultiPattern", Boolean.FALSE);
    TemplateRegistry registry = getInvokerTemplateRegistry();
    Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInvoker"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) PatternExtractor(org.drools.core.spi.PatternExtractor) Cheese(org.drools.compiler.Cheese) TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) List(java.util.List) Declaration(org.drools.core.rule.Declaration) Person(org.drools.compiler.Person) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 52 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class AsmGeneratorTest method testOr.

@Test
public void testOr() {
    String s = "package org.kie.test\n" + "import org.drools.compiler.Person\n" + "import org.drools.compiler.Cheese\n" + "global java.util.List list\n" + "rule r1 when\n" + "    s1 : String( this == 's1' )\n" + "    Cheese( $type : type == \"stilton\", $price : price ) or\n" + "    ( Cheese( $type : type == \"brie\", $price : price ) and Person( name == \"bob\", likes == $type ) )\n" + "then\n" + "    // *2 are missed out on purpose to make sure we only resolved required declarations\n" + "    list.add( \"test3\"+$type +\":\"+ new Integer( $price ) ); \n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(s.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("s1");
    ksession.insert(new Person("bob", "brie"));
    ksession.insert(new Cheese("stilton"));
    ksession.insert(new Cheese("brie"));
    ksession.insert(new Person("s2"));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains("test3brie:0"));
    assertTrue(list.contains("test3stilton:0"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 53 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class JavaConsequenceBuilderPRAlwaysTest method testFixModifyBlocks.

@Test
public void testFixModifyBlocks() throws Exception {
    String consequence = " System.out.println(\"this is a test\");\n " + " Cheese c1 = $cheese;\n" + " try { \r\n" + "     modify( c1 ) { setPrice( 10 ), \n" + "                    setOldPrice( age ) }\n " + "     Cheese c4 = $cheese;\n" + "     try { \n" + "         modify( c4 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } catch (java.lang.Exception e) {\n" + "         modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } finally {\n " + "         Cheese c3 = $cheese;\n" + "         modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "    }\n" + " } catch (java.lang.Exception e) {\n" + "     Cheese c2 = $cheese;\n" + "     modify( c2 ) { setPrice( 10 ), setOldPrice( age ) }\n " + " } finally {\n " + "     Cheese c3 = $cheese;\n" + "     modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "}\n" + " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " System.out.println(\"we are done\");\n ";
    setupTest("", new HashMap<String, Object>());
    ruleDescr.setConsequence(consequence);
    JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
    Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
    declrCls.put("$cheese", Cheese.class);
    JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(declrCls, null));
    BoundIdentifiers bindings = new BoundIdentifiers(new HashMap(), null);
    bindings.getDeclrClasses().put("$cheese", Cheese.class);
    bindings.getDeclrClasses().put("age", int.class);
    // Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
    List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
    setContainerBlockInputs(context, descrs, analysis.getBlockDescrs(), consequence, bindings, new HashMap(), 0);
    analysis.setBoundIdentifiers(bindings);
    context.getKnowledgeBuilder().getTypeDeclaration(Cheese.class).setPropertyReactive(true);
    String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations(context.getRule()), descrs);
    String expected = " System.out.println(\"this is a test\");\r\n" + "  Cheese c1 = $cheese;\r\n" + " try { \r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); \r\n" + "__obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      Cheese c4 = $cheese;\r\n" + "     try { \r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c4 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      } catch (java.lang.Exception e) {\r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      } finally {\r\n" + "          Cheese c3 = $cheese;\r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c3 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "     }\r\n" + " } catch (java.lang.Exception e) {\r\n" + "     Cheese c2 = $cheese;\r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c2 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "  } finally {\r\n" + "      Cheese c3 = $cheese;\r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c3 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + " }\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "  System.out.println(\"we are done\");\r\n" + " \r\n" + "";
    assertNotNull(context.getErrors().toString(), fixed);
    assertEqualsIgnoreSpaces(expected, fixed);
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr) Test(org.junit.Test)

Example 54 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class JavaConsequenceBuilderTest method testFixModifyBlocks.

@Test
public void testFixModifyBlocks() throws Exception {
    String consequence = " System.out.println(\"this is a test\");\n " + " Cheese c1 = $cheese;\n" + " try { \r\n" + "     modify( c1 ) { setPrice( 10 ), \n" + "                    setOldPrice( age ) }\n " + "     Cheese c4 = $cheese;\n" + "     try { \n" + "         modify( c4 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } catch (java.lang.Exception e) {\n" + "         modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } finally {\n " + "         Cheese c3 = $cheese;\n" + "         modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "    }\n" + " } catch (java.lang.Exception e) {\n" + "     Cheese c2 = $cheese;\n" + "     modify( c2 ) { setPrice( 10 ), setOldPrice( age ) }\n " + " } finally {\n " + "     Cheese c3 = $cheese;\n" + "     modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "}\n" + " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " System.out.println(\"we are done\");\n ";
    setupTest("", new HashMap<String, Object>());
    ruleDescr.setConsequence(consequence);
    JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
    Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
    declrCls.put("$cheese", Cheese.class);
    JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(declrCls, null));
    BoundIdentifiers bindings = new BoundIdentifiers(new HashMap(), null);
    bindings.getDeclrClasses().put("$cheese", Cheese.class);
    bindings.getDeclrClasses().put("age", int.class);
    // Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
    List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
    setContainerBlockInputs(context, descrs, analysis.getBlockDescrs(), consequence, bindings, new HashMap(), 0);
    analysis.setBoundIdentifiers(bindings);
    context.getKnowledgeBuilder().getTypeDeclaration(Cheese.class).setPropertyReactive(true);
    String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations(context.getRule()), descrs);
    String expected = " System.out.println(\"this is a test\");\r\n" + "  Cheese c1 = $cheese;\r\n" + " try { \r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); \r\n" + "__obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      Cheese c4 = $cheese;\r\n" + "     try { \r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c4 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      } catch (java.lang.Exception e) {\r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "      } finally {\r\n" + "          Cheese c3 = $cheese;\r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c3 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "     }\r\n" + " } catch (java.lang.Exception e) {\r\n" + "     Cheese c2 = $cheese;\r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c2 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "  } finally {\r\n" + "      Cheese c3 = $cheese;\r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c3 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + " }\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" + "  System.out.println(\"we are done\");\r\n" + " \r\n" + "";
    assertNotNull(context.getErrors().toString(), fixed);
    assertEqualsIgnoreSpaces(expected, fixed);
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr) Test(org.junit.Test)

Example 55 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class DescrBuilderTest method testConditionalBranch.

@Test
public void testConditionalBranch() {
    String expected = "packageorg.drools.compiler\n" + "rule \"test\"\n" + "when\n" + "    Cheese( type == \"stilton\" )  \n" + "    if ( price < 10 ) do[c1] \n" + "    Cheese( type == \"cheddar\" )  \n" + "then\n" + "// do something\n" + "then[c1]\n" + "// do something else\n" + "end";
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("test").lhs().pattern("Cheese").constraint("type == \"stilton\"").end().conditionalBranch().condition().constraint("price < 10").end().consequence().name("c1").end().end().pattern("Cheese").constraint("type == \"cheddar\"").end().end().rhs("// do something").namedRhs("c1", "// do something else").end().getDescr();
    assertEquals(1, pkg.getRules().size());
    String drl = new DrlDumper().dump(pkg);
    Assertions.assertThat(expected).isEqualToIgnoringWhitespace(drl);
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = createKnowledgeSession(kbase);
    Cheese stilton = new Cheese("stilton", 5);
    Cheese cheddar = new Cheese("cheddar", 7);
    Cheese brie = new Cheese("brie", 5);
    ksession.insert(stilton);
    ksession.insert(cheddar);
    ksession.insert(brie);
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

Cheese (org.drools.compiler.Cheese)222 Test (org.junit.Test)204 KieSession (org.kie.api.runtime.KieSession)195 ArrayList (java.util.ArrayList)152 KieBase (org.kie.api.KieBase)138 List (java.util.List)100 Person (org.drools.compiler.Person)92 FactHandle (org.kie.api.runtime.rule.FactHandle)53 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)36 KiePackage (org.kie.api.definition.KiePackage)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 Cheesery (org.drools.compiler.Cheesery)14 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)14 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)13 HashMap (java.util.HashMap)10 Command (org.kie.api.command.Command)8 ExecutionResults (org.kie.api.runtime.ExecutionResults)8 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)7