Search in sources :

Example 6 with KnowledgeBuilderResult

use of org.kie.internal.builder.KnowledgeBuilderResult in project drools by kiegroup.

the class KnowledgeBuilderImpl method compileRulesLevel.

private void compileRulesLevel(PackageDescr packageDescr, PackageRegistry pkgRegistry, List<RuleDescr> rules) {
    boolean parallelRulesBuild = this.kBase == null && rules.size() > PARALLEL_RULES_BUILD_THRESHOLD;
    if (parallelRulesBuild) {
        Map<String, RuleBuildContext> ruleCxts = new ConcurrentHashMap<>();
        rules.stream().parallel().filter(ruleDescr -> filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())).forEach(ruleDescr -> {
            initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
            RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
            ruleCxts.put(ruleDescr.getName(), context);
            List<? extends KnowledgeBuilderResult> results = addRule(context);
            if (!results.isEmpty()) {
                synchronized (this.results) {
                    this.results.addAll(results);
                }
            }
        });
        for (RuleDescr ruleDescr : rules) {
            RuleBuildContext context = ruleCxts.get(ruleDescr.getName());
            if (context != null) {
                pkgRegistry.getPackage().addRule(context.getRule());
            }
        }
    } else {
        for (RuleDescr ruleDescr : rules) {
            if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
                initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
                RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
                this.results.addAll(addRule(context));
                pkgRegistry.getPackage().addRule(context.getRule());
            }
        }
    }
}
Also used : WindowDeclarationDescr(org.drools.compiler.lang.descr.WindowDeclarationDescr) Arrays(java.util.Arrays) BaseKnowledgeBuilderResultImpl(org.drools.compiler.compiler.BaseKnowledgeBuilderResultImpl) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DroolsError(org.drools.compiler.compiler.DroolsError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) GuidedScoreCardFactory(org.drools.compiler.compiler.GuidedScoreCardFactory) DroolsWarningWrapper(org.drools.compiler.compiler.DroolsWarningWrapper) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DuplicateRule(org.drools.compiler.compiler.DuplicateRule) Map(java.util.Map) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) KnowledgeBaseImpl.registerFunctionClassAndInnerClasses(org.drools.core.impl.KnowledgeBaseImpl.registerFunctionClassAndInnerClasses) ScoreCardFactory(org.drools.compiler.compiler.ScoreCardFactory) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) Set(java.util.Set) KieAssemblers(org.kie.api.internal.assembler.KieAssemblers) Reader(java.io.Reader) DialectError(org.drools.compiler.rule.builder.dialect.DialectError) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) ResourceConfiguration(org.kie.api.io.ResourceConfiguration) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) DroolsWarning(org.drools.compiler.compiler.DroolsWarning) PMMLCompiler(org.drools.compiler.compiler.PMMLCompiler) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) PMMLCompilerFactory(org.drools.compiler.compiler.PMMLCompilerFactory) GuidedRuleTemplateFactory(org.drools.compiler.compiler.GuidedRuleTemplateFactory) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) JaxbConfigurationImpl(org.drools.core.builder.conf.impl.JaxbConfigurationImpl) ChangeSet(org.kie.internal.ChangeSet) ResourceType(org.kie.api.io.ResourceType) Supplier(java.util.function.Supplier) DecisionTableFactory(org.drools.compiler.compiler.DecisionTableFactory) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ConfigurableSeverityResult(org.drools.compiler.compiler.ConfigurableSeverityResult) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) BPMN2ProcessFactory(org.drools.compiler.compiler.BPMN2ProcessFactory) EnumDeclarationDescr(org.drools.compiler.lang.descr.EnumDeclarationDescr) PackageBuilderResults(org.drools.compiler.compiler.PackageBuilderResults) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) ImportDeclaration(org.drools.core.rule.ImportDeclaration) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) FileOutputStream(java.io.FileOutputStream) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) IOException(java.io.IOException) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) ParserError(org.drools.compiler.compiler.ParserError) File(java.io.File) WindowDeclaration(org.drools.core.rule.WindowDeclaration) DeprecatedResourceTypeWarning(org.drools.compiler.compiler.DeprecatedResourceTypeWarning) ExecutionException(java.util.concurrent.ExecutionException) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) Dialect(org.drools.compiler.compiler.Dialect) DrlParser(org.drools.compiler.compiler.DrlParser) StringReader(java.io.StringReader) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) DroolsErrorWrapper(org.drools.compiler.compiler.DroolsErrorWrapper) DescrBuildWarning(org.drools.compiler.compiler.DescrBuildWarning) Array(java.lang.reflect.Array) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KieAssemblerService(org.kie.api.internal.assembler.KieAssemblerService) ResourceChange(org.kie.internal.builder.ResourceChange) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) Function(org.drools.core.rule.Function) GuidedRuleTemplateProvider(org.drools.compiler.compiler.GuidedRuleTemplateProvider) TypeDeclaration(org.drools.core.rule.TypeDeclaration) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) ResourceWithConfiguration(org.kie.api.io.ResourceWithConfiguration) LoggerFactory(org.slf4j.LoggerFactory) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) StringUtils.isEmpty(org.drools.core.util.StringUtils.isEmpty) Method(java.lang.reflect.Method) EntryPointDeclarationDescr(org.drools.compiler.lang.descr.EntryPointDeclarationDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) DroolsJaxbHelperProviderImpl(org.drools.compiler.runtime.pipeline.impl.DroolsJaxbHelperProviderImpl) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) ScoreCardConfiguration(org.kie.internal.builder.ScoreCardConfiguration) Collection(java.util.Collection) Process(org.kie.api.definition.process.Process) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) UUID(java.util.UUID) ExpanderException(org.drools.compiler.lang.ExpanderException) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) Resource(org.kie.api.io.Resource) List(java.util.List) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) DroolsStreamUtils(org.drools.core.util.DroolsStreamUtils) SAXException(org.xml.sax.SAXException) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) ResourceTypeDeclarationWarning(org.drools.compiler.compiler.ResourceTypeDeclarationWarning) ProcessLoadError(org.drools.compiler.compiler.ProcessLoadError) StringUtils(org.drools.core.util.StringUtils) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ReaderResource(org.drools.core.io.impl.ReaderResource) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) HashMap(java.util.HashMap) RuleBuilder(org.drools.compiler.rule.builder.RuleBuilder) Stack(java.util.Stack) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) HashSet(java.util.HashSet) IoUtils(org.drools.core.util.IoUtils) PMMLResource(org.drools.compiler.compiler.PMMLResource) ResultSeverity(org.kie.internal.builder.ResultSeverity) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) LinkedList(java.util.LinkedList) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) KieBase(org.kie.api.KieBase) KiePackage(org.kie.api.definition.KiePackage) RuleBuildError(org.drools.compiler.compiler.RuleBuildError) KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) InternalResource(org.drools.core.io.internal.InternalResource) GuidedDecisionTableProvider(org.drools.compiler.compiler.GuidedDecisionTableProvider) PatternDestinationDescr(org.drools.compiler.lang.descr.PatternDestinationDescr) PackageBuilderErrors(org.drools.compiler.compiler.PackageBuilderErrors) GlobalError(org.drools.compiler.compiler.GlobalError) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr) AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ServiceRegistry(org.kie.api.internal.utils.ServiceRegistry) GuidedDecisionTableFactory(org.drools.compiler.compiler.GuidedDecisionTableFactory) ProcessBuilderFactory(org.drools.compiler.compiler.ProcessBuilderFactory) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Collections(java.util.Collections) ResourceConversionResult(org.drools.compiler.compiler.ResourceConversionResult) InputStream(java.io.InputStream) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with KnowledgeBuilderResult

use of org.kie.internal.builder.KnowledgeBuilderResult in project drools by kiegroup.

the class KnowledgeBuilderImpl method getResultList.

private List<KnowledgeBuilderResult> getResultList(ResultSeverity... severities) {
    List<ResultSeverity> typesToFetch = Arrays.asList(severities);
    ArrayList<KnowledgeBuilderResult> problems = new ArrayList<KnowledgeBuilderResult>();
    for (KnowledgeBuilderResult problem : results) {
        if (typesToFetch.contains(problem.getSeverity())) {
            problems.add(problem);
        }
    }
    return problems;
}
Also used : ArrayList(java.util.ArrayList) ResultSeverity(org.kie.internal.builder.ResultSeverity) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 8 with KnowledgeBuilderResult

use of org.kie.internal.builder.KnowledgeBuilderResult in project drools by kiegroup.

the class PMML4Compiler method compile.

public String compile(InputStream source, ClassLoader classLoader) {
    this.results = new ArrayList<KnowledgeBuilderResult>();
    PMML pmml = loadModel(PMML, source);
    helper.setResolver(classLoader);
    if (getResults().isEmpty()) {
        return generateTheory(pmml);
    } else {
        return null;
    }
}
Also used : PMML(org.dmg.pmml.pmml_4_2.descr.PMML) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 9 with KnowledgeBuilderResult

use of org.kie.internal.builder.KnowledgeBuilderResult in project drools by kiegroup.

the class Misc2Test method testIsGetClash.

@Test
public void testIsGetClash() {
    // DROOLS-18
    String str = "import org.drools.compiler.integrationtests.Misc2Test.Foo3;\n" + "" + "global java.util.List list;" + "\n" + "" + "rule \"Init\" when\n" + "   $x : Foo3( x == true, y == true, z == \"ok\", isZ() == true ) \n" + "then\n" + "   list.add( \"ok\" ); \n" + "end\n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    assertEquals(2, kbuilder.getResults(ResultSeverity.WARNING).size());
    for (KnowledgeBuilderResult res : kbuilder.getResults(ResultSeverity.WARNING)) {
        System.out.println(res.getMessage());
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) Test(org.junit.Test)

Example 10 with KnowledgeBuilderResult

use of org.kie.internal.builder.KnowledgeBuilderResult in project drools by kiegroup.

the class DroolsJaxbHelperProviderImpl method addXsdModel.

public static String[] addXsdModel(Resource resource, KnowledgeBuilderImpl kBuilder, Options xjcOpts, String systemId) throws IOException {
    InputSource source = new InputSource(new CachingRewindableReader(resource.getReader()));
    source.setSystemId(systemId.trim().startsWith(".") ? systemId : "." + systemId);
    xjcOpts.addGrammar(source);
    try {
        xjcOpts.parseArguments(new String[] { "-npa" });
    } catch (BadCommandLineException e) {
        throw new IllegalArgumentException("Unable to parse arguments", e);
    }
    ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
    Model model = ModelLoader.load(xjcOpts, new JCodeModel(), errorReceiver);
    model.generateCode(xjcOpts, errorReceiver);
    MapVfsCodeWriter codeWriter = new MapVfsCodeWriter();
    model.codeModel.build(xjcOpts.createCodeWriter(codeWriter));
    MemoryResourceReader src = new MemoryResourceReader();
    boolean useProjectClassLoader = kBuilder.getRootClassLoader() instanceof ProjectClassLoader;
    List<String> classNames = new ArrayList<String>();
    List<String> srcNames = new ArrayList<String>();
    for (Entry<String, byte[]> entry : codeWriter.getMap().entrySet()) {
        String name = entry.getKey();
        int dotPos = name.lastIndexOf('.');
        String pkgName = name.substring(0, dotPos);
        if (!name.endsWith("package-info.java")) {
            classNames.add(pkgName);
        }
        dotPos = pkgName.lastIndexOf('.');
        if (dotPos != -1) {
            pkgName = pkgName.substring(0, dotPos);
        }
        PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
        if (pkgReg == null) {
            kBuilder.addPackage(new PackageDescr(pkgName));
            pkgReg = kBuilder.getPackageRegistry(pkgName);
        }
        if (useProjectClassLoader) {
            String srcName = convertToResource(entry.getKey());
            src.add(srcName, entry.getValue());
            srcNames.add(srcName);
        } else {
            JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
            dialect.addSrc(convertToResource(entry.getKey()), entry.getValue());
        }
    }
    if (useProjectClassLoader) {
        ProjectJavaCompiler compiler = new ProjectJavaCompiler(kBuilder.getBuilderConfiguration());
        List<KnowledgeBuilderResult> results = compiler.compileAll((ProjectClassLoader) kBuilder.getRootClassLoader(), srcNames, src);
        for (String className : classNames) {
            Class<?> clazz = null;
            try {
                clazz = Class.forName(className, true, kBuilder.getRootClassLoader());
            } catch (ClassNotFoundException e) {
                continue;
            }
            String pkgName = className.substring(0, className.lastIndexOf('.'));
            PackageRegistry pkgReg = kBuilder.getPackageRegistry(pkgName);
            pkgReg.getPackage().addTypeDeclaration(TypeDeclaration.createTypeDeclarationForBean(clazz));
        }
        kBuilder.updateResults(results);
    } else {
        kBuilder.compileAll();
        kBuilder.updateResults();
    }
    return classNames.toArray(new String[classNames.size()]);
}
Also used : InputSource(org.xml.sax.InputSource) ArrayList(java.util.ArrayList) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) MemoryResourceReader(org.drools.compiler.commons.jci.readers.MemoryResourceReader) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) BadCommandLineException(com.sun.tools.xjc.BadCommandLineException) ProjectJavaCompiler(org.drools.compiler.compiler.ProjectJavaCompiler) JCodeModel(com.sun.codemodel.JCodeModel) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) Model(com.sun.tools.xjc.model.Model) JCodeModel(com.sun.codemodel.JCodeModel) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Aggregations

KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)12 ArrayList (java.util.ArrayList)6 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)3 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)3 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 PMMLResource (org.drools.compiler.compiler.PMMLResource)2 ProjectClassLoader (org.drools.core.common.ProjectClassLoader)2 PMML (org.kie.dmg.pmml.pmml_4_2.descr.PMML)2 ResultSeverity (org.kie.internal.builder.ResultSeverity)2 JCodeModel (com.sun.codemodel.JCodeModel)1 BadCommandLineException (com.sun.tools.xjc.BadCommandLineException)1 ErrorReceiver (com.sun.tools.xjc.ErrorReceiver)1 Model (com.sun.tools.xjc.model.Model)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1