Search in sources :

Example 56 with KnowledgeBuilderImpl

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");
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 57 with KnowledgeBuilderImpl

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());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 58 with KnowledgeBuilderImpl

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");
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 59 with KnowledgeBuilderImpl

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;
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KiePath(org.kie.memorycompiler.resources.KiePath) Arrays(java.util.Arrays) ResourceChange(org.kie.internal.builder.ResourceChange) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ChangeType(org.kie.internal.builder.ChangeType) KieBaseUpdatersContext(org.drools.compiler.kie.builder.impl.KieBaseUpdatersContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) CoreComponentFactory(org.drools.core.reteoo.CoreComponentFactory) StringUtil.fileNameToClass(org.drools.modelcompiler.util.StringUtil.fileNameToClass) ReleaseId(org.kie.api.builder.ReleaseId) DependencyFilter(org.kie.util.maven.support.DependencyFilter) Collections.singletonList(java.util.Collections.singletonList) ModelComponent.areEqualInModel(org.drools.model.impl.ModelComponent.areEqualInModel) Map(java.util.Map) KieBaseUpdaterImplContext(org.drools.compiler.kie.builder.impl.KieBaseUpdaterImplContext) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl) Predicate(java.util.function.Predicate) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Process(org.kie.api.definition.process.Process) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KieBaseUpdaters(org.drools.compiler.kie.builder.impl.KieBaseUpdaters) Set(java.util.Set) Results(org.kie.api.builder.Results) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) Collectors(java.util.stream.Collectors) KieBaseUpdaterOptions(org.drools.compiler.kie.builder.impl.KieBaseUpdaterOptions) Drools(org.drools.core.util.Drools) CanonicalKieBaseUpdater(org.drools.modelcompiler.builder.CanonicalKieBaseUpdater) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceConfiguration(org.kie.api.io.ResourceConfiguration) UncheckedIOException(java.io.UncheckedIOException) Resource(org.kie.api.io.Resource) List(java.util.List) Stream(java.util.stream.Stream) BuildContext(org.drools.compiler.kie.builder.impl.BuildContext) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) StringUtils(org.drools.core.util.StringUtils) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) PomModel(org.kie.util.maven.support.PomModel) ModelSourceClass.getProjectModelClassNameNameWithReleaseId(org.drools.modelcompiler.builder.ModelSourceClass.getProjectModelClassNameNameWithReleaseId) HashMap(java.util.HashMap) ResourceType(org.kie.api.io.ResourceType) AlphaNetworkCompilerOption(org.kie.internal.builder.conf.AlphaNetworkCompilerOption) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) IoUtils(org.drools.core.util.IoUtils) ResourceProvider(org.drools.wiring.api.ResourceProvider) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieBaseUpdater(org.drools.compiler.kie.builder.impl.KieBaseUpdater) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) NamedModelItem(org.drools.model.NamedModelItem) KieServices(org.kie.api.KieServices) Message(org.kie.api.builder.Message) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Model(org.drools.model.Model) Iterator(java.util.Iterator) InternalResource(org.drools.core.io.internal.InternalResource) KieService(org.kie.api.internal.utils.KieService) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) AbstractKieModule.checkStreamMode(org.drools.compiler.kie.builder.impl.AbstractKieModule.checkStreamMode) IOException(java.io.IOException) DroolsAssemblerContext(org.drools.compiler.builder.DroolsAssemblerContext) ResultsImpl(org.drools.compiler.kie.builder.impl.ResultsImpl) GeneratedFact(org.drools.core.factmodel.GeneratedFact) File(java.io.File) ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Collectors.toList(java.util.stream.Collectors.toList) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Comparator(java.util.Comparator) ResourceType.determineResourceType(org.kie.api.io.ResourceType.determineResourceType) Collections(java.util.Collections) InputStream(java.io.InputStream) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ArrayList(java.util.ArrayList) Resource(org.kie.api.io.Resource) InternalResource(org.drools.core.io.internal.InternalResource) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) ResourceType.determineResourceType(org.kie.api.io.ResourceType.determineResourceType) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 60 with KnowledgeBuilderImpl

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));
}
Also used : MVELCompileable(org.drools.mvel.expr.MVELCompileable) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) AccumulateNode(org.drools.core.reteoo.AccumulateNode) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) Cheese(org.drools.mvel.compiler.Cheese) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) LeftTuple(org.drools.core.reteoo.LeftTuple) FromNodeLeftTuple(org.drools.core.reteoo.FromNodeLeftTuple) Accumulate(org.drools.core.rule.Accumulate) MVELAccumulateBuilder(org.drools.mvel.builder.MVELAccumulateBuilder) FromNodeLeftTuple(org.drools.core.reteoo.FromNodeLeftTuple) MockTupleSource(org.drools.core.reteoo.MockTupleSource) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELDialect(org.drools.mvel.builder.MVELDialect) InitialFactImpl(org.drools.core.reteoo.InitialFactImpl) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) 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