Search in sources :

Example 6 with GlobalDescr

use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.

the class XmlPackageReaderTest method testParseGlobal.

@Test
public void testParseGlobal() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseGlobal.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 7 with GlobalDescr

use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.

the class XmlPackageReaderTest method testParseFunction.

@Test
public void testParseFunction() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFunction.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 8 with GlobalDescr

use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.

the class XmlPackageReaderTest method testParseQuery.

@Test
public void testParseQuery() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseQuery.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
    final QueryDescr queryDescr = (QueryDescr) packageDescr.getRules().get(0);
    assertEquals("my query", queryDescr.getName());
    final AndDescr lhs = queryDescr.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    final PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Foo", patternDescr.getObjectType());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) QueryDescr(org.drools.compiler.lang.descr.QueryDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 9 with GlobalDescr

use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.

the class XmlPackageReaderTest method testParseRhs.

@Test
public void testParseRhs() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseRhs.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
    final RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
    assertEquals("my rule", ruleDescr.getName());
    final String consequence = (String) ruleDescr.getConsequence();
    assertNotNull(consequence);
    assertEquals("System.out.println( \"hello\" );", consequence.trim());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 10 with GlobalDescr

use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testSerializable.

@Test
public void testSerializable() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
    // check that packageDescr is serializable
    final PackageDescr back = (PackageDescr) SerializationHelper.serializeObject(packageDescr);
    assertNotNull(back);
    assertEquals("p1", back.getName());
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage newPkg = SerializationHelper.serializeObject(pkg);
    final RuleImpl newRule = newPkg.getRule("rule-1");
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    // It's been serialised so we have to simulate the re-wiring process
    newPkg.getDialectRuntimeRegistry().onAdd(kBase.getRootClassLoader());
    newPkg.getDialectRuntimeRegistry().onBeforeExecute();
    kBase.getGlobals().put("map", Map.class);
    final KieSession workingMemory = kBase.newKieSession();
    final HashMap map = new HashMap();
    workingMemory.setGlobal("map", map);
    final LeftTupleImpl tuple = new MockTuple(new HashMap());
    tuple.setLeftTupleSink(new RuleTerminalNode(1, new CompositeObjectSinkAdapterTest.MockBetaNode(), newRule, newRule.getLhs(), 0, new BuildContext(kBase)));
    final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
    final DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.compiler.lang.descr.AndDescr) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)16 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)13 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)12 Test (org.junit.Test)12 AndDescr (org.drools.compiler.lang.descr.AndDescr)10 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)7 List (java.util.List)6 Cheese (org.drools.compiler.Cheese)6 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)6 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)6 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)5 InputStreamReader (java.io.InputStreamReader)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)4 FunctionDescr (org.drools.compiler.lang.descr.FunctionDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 HashMap (java.util.HashMap)2 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)2 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2