Search in sources :

Example 21 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class Misc2Test method testFactTemplates.

@Test
public void testFactTemplates() {
    // DROOLS-600
    String drl = "package com.testfacttemplate;" + " rule \"test rule\" " + " dialect \"mvel\" " + " when " + " $test : TestFactTemplate( status == 1 ) " + " then " + " System.out.println( \"Hello World\" ); " + " end ";
    KnowledgePackageImpl kPackage = new KnowledgePackageImpl("com.testfacttemplate");
    FieldTemplate fieldTemplate = new FieldTemplateImpl("status", 0, Integer.class);
    FactTemplate factTemplate = new FactTemplateImpl(kPackage, "TestFactTemplate", new FieldTemplate[] { fieldTemplate });
    KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl(kPackage);
    StringReader rule = new StringReader(drl);
    try {
        ((KnowledgeBuilderImpl) kBuilder).addPackageFromDrl(rule);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) FactTemplate(org.drools.core.facttemplates.FactTemplate) Test(org.junit.Test)

Example 22 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
    MockDialectConfiguration mockConf = new MockDialectConfiguration();
    // cfg1.buildDialectRegistry().addDialect( "mock",
    // mockConf.getDialect() );
    cfg1.addDialect("mock", mockConf);
    cfg1.setDefaultDialect("mock");
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
    PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
    MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
    assertSame(pkg, mockDialect2.getPkg());
    assertNull(mockDialect2.getRuleDescr());
    RuleDescr ruleDescr = new RuleDescr("test rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
    ruleDescr.setLhs(new AndDescr());
    EvalDescr evalDescr = new EvalDescr();
    ruleDescr.getLhs().addDescr(evalDescr);
    PackageDescr pkgDescr = new PackageDescr("org.pkg1");
    pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
    FunctionImportDescr functionImportDescr = new FunctionImportDescr();
    functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
    pkgDescr.addFunctionImport(functionImportDescr);
    pkgDescr.addRule(ruleDescr);
    builder.addPackage(pkgDescr);
    assertSame(ruleDescr, mockDialect2.getRuleDescr());
    assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
    assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
    assertEquals("eval was built", evalDescr.getContent());
    assertEquals("consequence was built", ruleDescr.getConsequence());
    assertTrue(mockDialect2.isCompileAll());
    assertNotNull(pkg.getRule("test rule"));
    // make sure there were no other general errors.
    assertFalse(builder.hasErrors());
}
Also used : FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 23 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class TypeDeclarationTest method testAnnotationReDefinition.

@Test
public void testAnnotationReDefinition() {
    String str1 = "";
    str1 += "package org.kie \n" + "declare org.kie.EventA \n" + "    name : String \n" + "    duration : Long \n" + "end \n";
    String str2 = "";
    str2 += "package org.kie \n" + "declare org.kie.EventA \n" + "    @role (event) \n" + "    @duration (duration) \n" + "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    // No Warnings
    KnowledgeBuilderResults warnings = kbuilder.getResults(ResultSeverity.WARNING);
    Assert.assertEquals(0, warnings.size());
    // just 1 package was created
    Assert.assertEquals(1, kbuilder.getKnowledgePackages().size());
    // Get the Fact Type for org.kie.EventA
    FactType factType = ((KnowledgePackageImpl) kbuilder.getKnowledgePackages().iterator().next()).getFactType("org.kie.EventA");
    assertNotNull(factType);
    // 'name' field must still be there
    FactField field = factType.getField("name");
    assertNotNull(field);
    // 'duration' field must still be there
    field = factType.getField("duration");
    assertNotNull(field);
    // New Annotations must be there too
    TypeDeclaration typeDeclaration = ((KnowledgePackageImpl) kbuilder.getKnowledgePackages().iterator().next()).getTypeDeclaration("EventA");
    assertEquals(Role.Type.EVENT, typeDeclaration.getRole());
    assertEquals("duration", typeDeclaration.getDurationAttribute());
}
Also used : KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) FactField(org.kie.api.definition.type.FactField) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 24 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class KnowledgeBaseImpl method internalAddPackages.

private void internalAddPackages(List<InternalKnowledgePackage> clonedPkgs) {
    for (InternalWorkingMemory wm : getWorkingMemories()) {
        wm.flushPropagations();
    }
    // we need to merge all byte[] first, so that the root classloader can resolve classes
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        newPkg.checkValidity();
        this.eventSupport.fireBeforePackageAdded(newPkg);
        if (newPkg.hasTraitRegistry()) {
            getTraitRegistry().merge(newPkg.getTraitRegistry());
        }
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        if (pkg == null) {
            pkg = new KnowledgePackageImpl(newPkg.getName());
            // @TODO we really should have a single root cache
            pkg.setClassFieldAccessorCache(this.classFieldAccessorCache);
            pkgs.put(pkg.getName(), pkg);
        }
        // first merge anything related to classloader re-wiring
        pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader, true);
    }
    processAllTypesDeclaration(clonedPkgs);
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        // Add functions
        JavaDialectRuntimeData runtime = ((JavaDialectRuntimeData) newPkg.getDialectRuntimeRegistry().getDialectData("java"));
        for (Function function : newPkg.getFunctions().values()) {
            String functionClassName = function.getClassName();
            try {
                registerFunctionClassAndInnerClasses(functionClassName, runtime, this::registerAndLoadTypeDefinition);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("Unable to compile function '" + function.getName() + "'", e);
            }
        }
    }
    // now iterate again, this time onBeforeExecute will handle any wiring or cloader re-creating that needs to be done as part of the merge
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        // this needs to go here, as functions will set a java dialect to dirty
        if (newPkg.getFunctions() != null) {
            for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
                pkg.addFunction(entry.getValue());
            }
        }
        pkg.getDialectRuntimeRegistry().onBeforeExecute();
        // with the classloader recreated for all byte[] classes, we should now merge and wire any new accessors
        pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
    }
    for (InternalKnowledgePackage newPkg : clonedPkgs) {
        InternalKnowledgePackage pkg = this.pkgs.get(newPkg.getName());
        // now merge the new package into the existing one
        mergePackage(pkg, newPkg);
        // add the window declarations to the kbase
        for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
            this.reteooBuilder.addNamedWindow(window);
        }
        // add entry points to the kbase
        for (String id : newPkg.getEntryPointIds()) {
            this.reteooBuilder.addEntryPoint(id);
        }
        // add the rules to the RuleBase
        for (Rule r : newPkg.getRules()) {
            RuleImpl rule = (RuleImpl) r;
            checkMultithreadedEvaluation(rule);
            internalAddRule(rule);
        }
        // add the flows to the RuleBase
        if (newPkg.getRuleFlows() != null) {
            final Map<String, Process> flows = newPkg.getRuleFlows();
            for (Process process : flows.values()) {
                internalAddProcess(process);
            }
        }
        if (!newPkg.getResourceTypePackages().isEmpty()) {
            KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
            for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
                ResourceType rt = rtkKpg.getResourceType();
                KieWeaverService factory = weavers.getWeavers().get(rt);
                factory.weave(this, newPkg, rtkKpg);
            }
        }
        ruleUnitRegistry.add(newPkg.getRuleUnitRegistry());
        this.eventSupport.fireAfterPackageAdded(newPkg);
    }
    if (config.isMultithreadEvaluation() && !hasMultiplePartitions()) {
        disableMultithreadEvaluation("The rete network cannot be partitioned: disabling multithread evaluation");
    }
}
Also used : KieWeaverService(org.kie.api.internal.weaver.KieWeaverService) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) KieWeavers(org.kie.api.internal.weaver.KieWeavers) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Function(org.drools.core.rule.Function) WindowDeclaration(org.drools.core.rule.WindowDeclaration) Rule(org.kie.api.definition.rule.Rule) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 25 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class TraitFactory method getClassFieldAccessorStore.

protected ClassFieldAccessorStore getClassFieldAccessorStore() {
    InternalKnowledgePackage traitPackage = kBase.getPackagesMap().get(pack);
    if (traitPackage == null) {
        traitPackage = new KnowledgePackageImpl(pack);
        traitPackage.setClassFieldAccessorCache(kBase.getClassFieldAccessorCache());
        kBase.getPackagesMap().put(pack, traitPackage);
    }
    return traitPackage.getClassFieldAccessorStore();
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)41 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)26 Test (org.junit.Test)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)18 ClassObjectType (org.drools.core.base.ClassObjectType)13 Pattern (org.drools.core.rule.Pattern)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)9 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)9 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)8 WorkingMemory (org.drools.core.WorkingMemory)7 Consequence (org.drools.core.spi.Consequence)7 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)7 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)6 Declaration (org.drools.core.rule.Declaration)6 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)6 IOException (java.io.IOException)5 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)5 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)5