Search in sources :

Example 21 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class TypeDeclarationMergingTest method testOverrideFromDeeperHierarchyParentInterface.

@Test
public void testOverrideFromDeeperHierarchyParentInterface() {
    // inherits role from and typesafe from the other
    String str = "" + "package org.drools.compiler.test \n" + "global java.util.List list \n" + "declare " + IA.class.getCanonicalName() + "\n" + "    @typesafe(true)\n" + "    @role(event)\n" + "end\n" + "declare " + IB.class.getCanonicalName() + "\n" + "    @role(fact)\n" + "end\n" + "declare " + DImpl.class.getCanonicalName() + "\n" + "end\n";
    KnowledgeBuilderImpl builder = getPackageBuilder(str);
    TypeDeclaration tdecl = builder.getTypeDeclaration(DImpl.class);
    assertEquals(true, tdecl.isTypesafe());
    assertEquals(Role.Type.FACT, tdecl.getRole());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Example 22 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class TypeDeclarationMergingTest method testOverrideFromMixedHierarchyParentInterface.

@Test
public void testOverrideFromMixedHierarchyParentInterface() {
    // inherits role from and typesafe from the other
    String str = "" + "package org.drools.compiler.test \n" + "global java.util.List list \n" + "declare " + IA.class.getCanonicalName() + "\n" + "    @typesafe(true)\n" + "    @role(event)\n" + "end\n" + "declare " + CImpl.class.getCanonicalName() + "\n" + "    @role(fact)\n" + "end\n" + "declare " + DImpl.class.getCanonicalName() + "\n" + "end\n";
    KnowledgeBuilderImpl builder = getPackageBuilder(str);
    TypeDeclaration tdecl = builder.getTypeDeclaration(DImpl.class);
    assertEquals(true, tdecl.isTypesafe());
    assertEquals(Role.Type.FACT, tdecl.getRole());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Example 23 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class ConsequenceOffsetTest method testConsequenceOffset.

@Test
public void testConsequenceOffset() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(ConsequenceOffsetTest.class.getResourceAsStream("test_consequenceOffset.drl")), ResourceType.DRL);
    assertFalse(kbuilder.hasErrors());
    int offset = -1;
    assertEquals(false, kbuilder.hasErrors());
    for (RuleDescr rule : ((KnowledgeBuilderImpl) kbuilder).getPackageDescrs("com.sample").get(0).getRules()) {
        if (rule.getName().equals("test")) {
            offset = rule.getConsequenceOffset();
        }
    }
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(ConsequenceOffsetTest.class.getResourceAsStream("test_consequenceOffset2.drl")), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newInputStreamResource(ConsequenceOffsetTest.class.getResourceAsStream("test_consequenceOffset.drl")), ResourceType.DRL);
    assertFalse(kbuilder.hasErrors());
    for (RuleDescr rule : ((KnowledgeBuilderImpl) kbuilder).getPackageDescrs("com.sample").get(0).getRules()) {
        if (rule.getName().equals("test")) {
            assertEquals(offset, rule.getConsequenceOffset());
            return;
        }
    }
    fail();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Test(org.junit.Test)

Example 24 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class AnnotationsTest method testCollectAnnotationsParsingAndBuilding.

@Test
public void testCollectAnnotationsParsingAndBuilding() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(new PackageDescr("org.drools"));
    String ruleDrl = "package org.drools.compiler; " + " " + "dialect 'mvel' " + " " + "import java.util.Collection; " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + "    when " + "        Collection() from collect ( " + "            String() @Annot " + "        ) " + "    then " + "end " + "";
    final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
    // just checking there is no parsing errors
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackage(pkgDescr.getName()).getRule("test collect with annotation");
    List<? extends RuleConditionElement> nested = ((Pattern) rule.getLhs().getChildren().get(0)).getSource().getNestedElements();
    assertEquals(1, nested.size());
    Map<String, AnnotationDefinition> annotations = ((Pattern) nested.get(0)).getAnnotations();
    assertEquals(1, annotations.size());
    assertNotNull(annotations.keySet().iterator().next());
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 25 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class AnnotationsTest method testAnnotationOnLHSAndMerging.

@Test
public void testAnnotationOnLHSAndMerging() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    String ruleDrl = "package org.drools.compiler; " + " " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + "    when " + "       ( and @Annot " + "         String() " + "         Integer() ) " + "    then " + "end " + "";
    final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Aggregations

KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)51 Test (org.junit.Test)33 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)20 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)20 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 Pattern (org.drools.core.rule.Pattern)11 TypeDeclaration (org.drools.core.rule.TypeDeclaration)11 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)10 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)9 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 HashMap (java.util.HashMap)6 Cheese (org.drools.compiler.Cheese)6 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)6 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)6 ClassObjectType (org.drools.core.base.ClassObjectType)6