Search in sources :

Example 31 with KnowledgeBuilderImpl

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

the class DialectUtil method findClassByName.

public static Class<?> findClassByName(RuleBuildContext context, String className) {
    if (className == null) {
        return null;
    }
    String namespace = context.getRuleDescr().getNamespace();
    KnowledgeBuilderImpl packageBuilder = context.getKnowledgeBuilder();
    Class<?> clazz = null;
    try {
        clazz = Class.forName(className.indexOf('.') < 0 ? namespace + "." + className : className, false, packageBuilder.getRootClassLoader());
    } catch (ClassNotFoundException e) {
    }
    if (clazz != null) {
        return clazz;
    }
    Set<String> imports = new HashSet<String>();
    List<PackageDescr> pkgDescrs = packageBuilder.getPackageDescrs(namespace);
    if (pkgDescrs == null) {
        return null;
    }
    for (PackageDescr pkgDescr : pkgDescrs) {
        for (ImportDescr importDescr : pkgDescr.getImports()) {
            imports.add(importDescr.getTarget());
        }
    }
    return findClass(className, imports, packageBuilder.getRootClassLoader());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) HashSet(java.util.HashSet)

Example 32 with KnowledgeBuilderImpl

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

the class PatternBuilder method getTypeDeclaration.

private TypeDeclaration getTypeDeclaration(RuleBuildContext context, Class<?> userProvidedClass) {
    String packageName = ClassUtils.getPackage(userProvidedClass);
    KnowledgeBuilderImpl kbuilder = context.getKnowledgeBuilder();
    PackageRegistry pkgr = kbuilder.getPackageRegistry(packageName);
    TypeDeclaration typeDeclaration = pkgr != null ? pkgr.getPackage().getTypeDeclaration(userProvidedClass) : null;
    if (typeDeclaration == null && kbuilder.getKnowledgeBase() != null) {
        // check if the type declaration is contained only in the already existing kbase (possible during incremental compilation)
        InternalKnowledgePackage pkg = kbuilder.getKnowledgeBase().getPackage(packageName);
        typeDeclaration = pkg != null ? pkg.getTypeDeclaration(userProvidedClass) : null;
    }
    if (typeDeclaration == null) {
        typeDeclaration = context.getPkg().getTypeDeclaration(userProvidedClass);
    }
    return typeDeclaration;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 33 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl 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 34 with KnowledgeBuilderImpl

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

the class KieBuilderSetImpl method buildChanges.

private IncrementalResults buildChanges(Collection<String> filesToBuild) {
    Map<String, Collection<KnowledgeBuilderError>> currentErrors = new HashMap<String, Collection<KnowledgeBuilderError>>();
    InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModuleIgnoringErrors();
    for (KieBaseModel kBaseModel : kieModule.getKieModuleModel().getKieBaseModels().values()) {
        KnowledgeBuilder kBuilder = kieModule.getKnowledgeBuilderForKieBase(kBaseModel.getName());
        if (kBuilder == null) {
            continue;
        }
        CompositeKnowledgeBuilder ckbuilder = kBuilder.batch();
        KnowledgeBuilderImpl.ResourceRemovalResult removalResult = new KnowledgeBuilderImpl.ResourceRemovalResult();
        KnowledgeBuilderImpl pkgBuilder = ((KnowledgeBuilderImpl) kBuilder);
        Set<String> wrongResources = resourcesWithErrors.get(kBaseModel.getName());
        for (String resourceName : wrongResources) {
            removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(resourceName)));
            removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, resourceName));
        }
        for (String file : filesToBuild) {
            if (wrongResources.contains(file)) {
                removalResult.mergeModified(true);
            } else {
                // remove the objects generated by the old Resource
                removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(file)));
                // add the modified Resource
                removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, file));
            }
        }
        if (removalResult.isModified()) {
            if (!removalResult.getRemovedTypes().isEmpty()) {
                ProjectClassLoader projectClassLoader = (ProjectClassLoader) ((KnowledgeBuilderImpl) kBuilder).getRootClassLoader();
                projectClassLoader.reinitTypes();
                for (String removedType : removalResult.getRemovedTypes()) {
                    projectClassLoader.undefineClass(removedType);
                }
            }
            ckbuilder.build();
            resourcesWithErrors.put(kBaseModel.getName(), findResourcesWithErrors(kBuilder));
            if (kBuilder.hasErrors()) {
                currentErrors.put(kBaseModel.getName(), kBuilder.getErrors());
                kBuilder.undo();
            } else {
                KieServices.Factory.get().getRepository().addKieModule(kieModule);
                kieBuilder.updateKieModuleMetaInfo();
            }
        }
    }
    IncrementalResultsImpl results = getIncrementalResults(currentErrors);
    previousErrors = currentErrors;
    return results;
}
Also used : ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) HashMap(java.util.HashMap) KieBaseModel(org.kie.api.builder.model.KieBaseModel) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Collection(java.util.Collection)

Example 35 with KnowledgeBuilderImpl

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

the class CompilerPerfProfileTest method build.

private void build(String msg, String resource, boolean showResults) throws DroolsParserException, IOException {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    long start = System.currentTimeMillis();
    builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream(resource)));
    InternalKnowledgePackage pkg = builder.getPackage("org.drools.compiler.test");
    assertFalse(builder.hasErrors());
    assertNotNull(pkg);
    if (showResults) {
        System.out.print("Time taken for " + msg + " : " + (System.currentTimeMillis() - start));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)51 Test (org.junit.Test)33 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)20 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)20 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 Pattern (org.drools.core.rule.Pattern)11 TypeDeclaration (org.drools.core.rule.TypeDeclaration)11 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)10 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)9 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 HashMap (java.util.HashMap)6 Cheese (org.drools.compiler.Cheese)6 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)6 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)6 ClassObjectType (org.drools.core.base.ClassObjectType)6