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();
}
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);
}
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);
}
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());
}
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);
}
}
Aggregations