Search in sources :

Example 51 with KnowledgeBuilderImpl

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

the class ConsequenceOffsetTest method testConsequenceOffset.

@Test
public void testConsequenceOffset() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(ConsequenceOffsetTest.class.getResourceAsStream("test_consequenceOffset.drl")), ResourceType.DRL);
    assertFalse(kbuilder.hasErrors());
    int offset = -1;
    assertFalse(kbuilder.hasErrors());
    for (final 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 (final 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.drl.ast.descr.RuleDescr) Test(org.junit.Test)

Example 52 with KnowledgeBuilderImpl

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

the class DescrBuilderTest method compilePkgDescr.

private KiePackage compilePkgDescr(PackageDescr pkg, String pkgName) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newDescrResource(pkg), ResourceType.DESCR);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    if (pkgName == null) {
        Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
        assertEquals(1, kpkgs.size());
        return kpkgs.iterator().next();
    }
    return ((KnowledgeBuilderImpl) kbuilder).getPackage(pkgName);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl)

Example 53 with KnowledgeBuilderImpl

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

the class DescrBuilderTest method testDumperFromPkg.

@Test
public void testDumperFromPkg() {
    // DROOLS-109
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    System.out.println(drl);
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.err.println(knowledgeBuilder.getErrors());
    assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession knowledgeSession = kbase.newKieSession();
    KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
    org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
    RuleImpl r = ((RuleImpl) rule);
    assertEquals(2, r.getLhs().getChildren().size());
    Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
    RuleConditionElement arg1 = iter.next();
    assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
    assertEquals(2, ((GroupElement) arg1).getChildren().size());
    RuleConditionElement arg2 = iter.next();
    assertTrue(arg2 instanceof Pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleConditionElement(org.drools.core.rule.RuleConditionElement) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 54 with KnowledgeBuilderImpl

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

the class ReteooBuilderTest method checkRuleBase.

private void checkRuleBase(final String name) throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream("test_" + name + ".drl")));
    InternalKnowledgePackage pkg = builder.getPackage("org.drools.mvel.compiler.test");
    final InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackage(pkg);
    if (this.writeTree) {
        writeRuleBase(kBase, name);
    }
    final XStream xstream = createNonTrustingXStream();
    final InternalKnowledgeBase goodKBase = (InternalKnowledgeBase) xstream.fromXML(getClass().getResourceAsStream(name));
    nodesEquals(goodKBase.getRete(), kBase.getRete());
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createNonTrustingXStream(org.kie.utll.xml.XStreamUtils.createNonTrustingXStream) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 55 with KnowledgeBuilderImpl

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

the class RuleBuilderTest method testBuildBigDecimalLiteralConstraint.

@Test
public void testBuildBigDecimalLiteralConstraint() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    AndDescr andDescr = new AndDescr();
    PatternDescr patDescr = new PatternDescr("java.math.BigDecimal", "$bd");
    ExprConstraintDescr fcd = new ExprConstraintDescr("this == 10");
    patDescr.addConstraint(fcd);
    andDescr.addDescr(patDescr);
    ruleDescr.setLhs(andDescr);
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackages()[0].getRule("Test Rule");
    final GroupElement and = rule.getLhs();
    final Pattern pat = (Pattern) and.getChildren().get(0);
    if (pat.getConstraints().get(0) instanceof MVELConstraint) {
        final MVELConstraint fc = (MVELConstraint) pat.getConstraints().get(0);
        assertTrue("Wrong class. Expected java.math.BigDecimal. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigDecimal);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.drl.ast.descr.PatternDescr) MVELConstraint(org.drools.mvel.MVELConstraint) AndDescr(org.drools.drl.ast.descr.AndDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.drl.ast.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)178 Test (org.junit.Test)122 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)56 PackageDescr (org.drools.drl.ast.descr.PackageDescr)50 RuleDescr (org.drools.drl.ast.descr.RuleDescr)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)30 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)29 Pattern (org.drools.core.rule.Pattern)28 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)24 StringReader (java.io.StringReader)23 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)23 HashMap (java.util.HashMap)20 TypeDeclaration (org.drools.core.rule.TypeDeclaration)20 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 AndDescr (org.drools.drl.ast.descr.AndDescr)18 PatternDescr (org.drools.drl.ast.descr.PatternDescr)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)17 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)16