use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class RuleBuilderTest method testBuildMetaAttributes.
@Test
public void testBuildMetaAttributes() throws Exception {
// creates mock objects
final RuleBuildContext context = mock(RuleBuildContext.class);
final RuleImpl rule = mock(RuleImpl.class);
// creates input object
final RuleDescr ruleDescr = new RuleDescr("my rule");
ruleDescr.addAnnotation("ruleId", "123");
ruleDescr.addAnnotation("author", "Bob Doe");
ruleDescr.addAnnotation("text", "\"It's a quoted\\\" string\"");
// creates expected results
// defining expectations on the mock object
when(context.getRule()).thenReturn(rule);
when(context.getRuleDescr()).thenReturn(ruleDescr);
when(context.getKnowledgeBuilder()).thenReturn(new KnowledgeBuilderImpl());
// calling the build method
RuleBuilder.buildMetaAttributes(context);
// check expectations
verify(rule).addMetaAttribute("ruleId", 123);
verify(rule).addMetaAttribute("author", "Bob Doe");
verify(rule).addMetaAttribute("text", "It's a quoted\" string");
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class RuleBuilderTest method testInvalidDialect.
@Test
public void testInvalidDialect() throws Exception {
final PackageDescr pkgDescr = new PackageDescr("org.drools");
final RuleDescr ruleDescr = new RuleDescr("Test Rule");
ruleDescr.addAttribute(new AttributeDescr("dialect", "mvl"));
ruleDescr.setConsequence("");
pkgDescr.addRule(ruleDescr);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(pkgDescr);
assertFalse(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class ReteooBuilderPerformanceTest method generatePackage.
private static InternalKnowledgePackage generatePackage(int ruleCount) throws DroolsParserException {
StringReader reader = new StringReader(generateRules(ruleCount));
System.out.println("Generating packages");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
try {
pkgBuilder.addPackageFromDrl(reader);
} catch (IOException e) {
fail("Unable to parse rules\n" + e.getMessage());
}
if (pkgBuilder.hasErrors()) {
fail(pkgBuilder.getErrors().toString());
}
return pkgBuilder.getPackage("org.kie.test");
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class CanonicalKieModule method findProcesses.
private List<Process> findProcesses(InternalKieModule kieModule, KieBaseModelImpl kBaseModel) {
List<Process> processes = new ArrayList<>();
Collection<KiePackage> pkgs = kieModule.getKnowledgePackagesForKieBase(kBaseModel.getName());
if (pkgs == null) {
List<Resource> processResources = kieModule.getFileNames().stream().filter(fileName -> {
ResourceType resourceType = determineResourceType(fileName);
return resourceType == ResourceType.DRF || resourceType == ResourceType.BPMN2;
}).map(fileName -> {
final Resource processResource = kieModule.getResource(fileName);
processResource.setResourceType(determineResourceType(fileName));
return processResource;
}).collect(toList());
if (!processResources.isEmpty()) {
KnowledgeBuilderImpl kbuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder(createBuilderConfiguration(kBaseModel, moduleClassLoader));
for (Resource processResource : processResources) {
kbuilder.add(processResource, processResource.getResourceType());
}
for (InternalKnowledgePackage knowledgePackage : kbuilder.getPackages()) {
processes.addAll(knowledgePackage.getProcesses());
}
}
} else {
for (KiePackage pkg : pkgs) {
processes.addAll(pkg.getProcesses());
}
}
return processes;
}
use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.
the class MVELAccumulateBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(new PackageDescr("pkg1"));
InternalKnowledgePackage pkg = pkgBuilder.getPackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final AccumulateDescr accDescr = new AccumulateDescr();
final PatternDescr inputPattern = new PatternDescr("org.drools.mvel.compiler.Cheese", "$cheese");
accDescr.setInputPattern(inputPattern);
accDescr.setInitCode("total = 0;");
accDescr.setActionCode("total += $cheese.price;");
accDescr.setReverseCode("total -= $cheese.price;");
accDescr.setResultCode("new Integer(total)");
final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
final Accumulate acc = (Accumulate) builder.build(context, accDescr);
((MVELCompileable) acc.getAccumulators()[0]).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
sink.setLeftTupleSource(source);
final Cheese cheddar1 = new Cheese("cheddar", 10);
final Cheese cheddar2 = new Cheese("cheddar", 8);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(new InitialFactImpl());
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
final InternalFactHandle f2 = (InternalFactHandle) ksession.insert(cheddar2);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
Object wmContext = acc.createWorkingMemoryContext();
AccumulateNode.AccumulateContext accContext = new AccumulateNode.AccumulateContext();
Object funcContext = acc.createFunctionContext();
funcContext = acc.init(wmContext, accContext, funcContext, tuple, ksession);
accContext.setFunctionContext(funcContext);
Object value1 = acc.accumulate(wmContext, accContext, tuple, f1, ksession);
acc.accumulate(wmContext, accContext, tuple, f2, ksession);
assertEquals(new Integer(18), acc.getResult(wmContext, accContext, tuple, ksession));
LeftTuple match = new FromNodeLeftTuple();
match.setContextObject(value1);
acc.tryReverse(wmContext, accContext, tuple, f1, null, match, ksession);
assertEquals(new Integer(8), acc.getResult(wmContext, accContext, tuple, ksession));
}
Aggregations