Search in sources :

Example 1 with DrlParser

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

the class NestingTest method testNesting.

@Test
public void testNesting() throws Exception {
    final Person p = new Person();
    p.setName("Michael");
    final Address add1 = new Address();
    add1.setStreet("High");
    final Address add2 = new Address();
    add2.setStreet("Low");
    final List l = new ArrayList();
    l.add(add1);
    l.add(add2);
    p.setAddresses(l);
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr desc = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nested_fields.drl")));
    final List packageAttrs = desc.getAttributes();
    assertEquals(1, desc.getRules().size());
    assertEquals(1, packageAttrs.size());
    final RuleDescr rule = desc.getRules().get(0);
    final Map<String, AttributeDescr> ruleAttrs = rule.getAttributes();
    assertEquals(1, ruleAttrs.size());
    assertEquals("mvel", ruleAttrs.get("dialect").getValue());
    assertEquals("dialect", ruleAttrs.get("dialect").getName());
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(desc));
    KieSession session = createKnowledgeSession(kbase);
    session.insert(p);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}
Also used : Address(org.drools.compiler.Address) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Person(org.drools.compiler.Person) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Example 2 with DrlParser

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

the class DRLIncompleteCodeTest method testIncompleteCode5.

@Test
public void testIncompleteCode5() throws DroolsParserException, RecognitionException {
    String input = "package a.b.c import a.b.c.*" + " rule MyRule when Class ( property memberOf collection ) then end " + " query MyQuery Class ( property memberOf collection ) end ";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr descr = parser.parse(true, input);
    assertNotNull(descr);
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 3 with DrlParser

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

the class DRLIncompleteCodeTest method testIncompleteCode13.

@Test
public void testIncompleteCode13() throws DroolsParserException, RecognitionException {
    String input = "package com.sample " + "import com.sample.DroolsTest.Message; " + "rule \"Hello World\"" + "  when " + "  then" + "     \\\" " + "end ";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr descr = parser.parse(true, input);
    assertNotNull(descr);
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 4 with DrlParser

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

the class DRLIncompleteCodeTest method testIncompleteCode2.

@Test
public void testIncompleteCode2() throws DroolsParserException, RecognitionException {
    String input = "rule MyRule when Class ( property memberOf collection ";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr descr = parser.parse(true, input);
    assertNotNull(descr);
    assertEquals(Location.LOCATION_LHS_INSIDE_CONDITION_END, getLastIntegerValue(parser.getEditorSentences().get(0).getContent()));
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 5 with DrlParser

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

the class DRLIncompleteCodeTest method testIncompleteCode8.

@Test
public void testIncompleteCode8() throws DroolsParserException, RecognitionException {
    String input = "package a.b.c import a.1111.c.*" + " rule MyRule when Class ( property memberOf collection ) then end " + " query MyQuery Class ( property memberOf collection ) end ";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr descr = parser.parse(true, input);
    System.out.println(parser.getErrors());
    assertEquals("a.b.c", descr.getNamespace());
    // FIXME: assertEquals(2, descr.getRules().size());
    assertEquals(true, parser.hasErrors());
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Aggregations

DrlParser (org.drools.compiler.compiler.DrlParser)44 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)43 Test (org.junit.Test)32 InputStreamReader (java.io.InputStreamReader)10 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)9 StringReader (java.io.StringReader)8 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)6 ArrayList (java.util.ArrayList)5 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 ParserError (org.drools.compiler.compiler.ParserError)5 DrlDumper (org.drools.compiler.lang.DrlDumper)4 Reader (java.io.Reader)3 List (java.util.List)3 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Ignore (org.junit.Ignore)3 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)2 MVELConsequence (org.drools.core.base.mvel.MVELConsequence)2