Search in sources :

Example 1 with KnowledgeBuilderResult

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

the class KnowledgeBuilderImpl method removeObjectsGeneratedFromResource.

public ResourceRemovalResult removeObjectsGeneratedFromResource(Resource resource) {
    boolean modified = false;
    if (pkgRegistryMap != null) {
        for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
            modified = packageRegistry.removeObjectsGeneratedFromResource(resource) || modified;
        }
    }
    if (results != null) {
        Iterator<KnowledgeBuilderResult> i = results.iterator();
        while (i.hasNext()) {
            if (resource.equals(i.next().getResource())) {
                i.remove();
            }
        }
    }
    if (processBuilder != null && processBuilder.getErrors() != null) {
        Iterator<? extends KnowledgeBuilderResult> i = processBuilder.getErrors().iterator();
        while (i.hasNext()) {
            if (resource.equals(i.next().getResource())) {
                i.remove();
            }
        }
    }
    if (results.size() == 0) {
        // TODO Error attribution might be bugged
        for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
            packageRegistry.getPackage().resetErrors();
        }
    }
    Collection<String> removedTypes = typeBuilder.removeTypesGeneratedFromResource(resource);
    for (List<PackageDescr> pkgDescrs : packages.values()) {
        for (PackageDescr pkgDescr : pkgDescrs) {
            pkgDescr.removeObjectsGeneratedFromResource(resource);
        }
    }
    if (kBase != null) {
        modified = kBase.removeObjectsGeneratedFromResource(resource) || modified;
    }
    return new ResourceRemovalResult(modified, removedTypes);
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 2 with KnowledgeBuilderResult

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

the class KnowledgeBuilderImpl method resetProblemType.

private void resetProblemType(ResultSeverity problemType) {
    List<KnowledgeBuilderResult> toBeDeleted = new ArrayList<KnowledgeBuilderResult>();
    for (KnowledgeBuilderResult problem : results) {
        if (problemType != null && problemType.equals(problem.getSeverity())) {
            toBeDeleted.add(problem);
        }
    }
    this.results.removeAll(toBeDeleted);
}
Also used : ArrayList(java.util.ArrayList) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 3 with KnowledgeBuilderResult

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

the class AbstractKieProject method buildKnowledgePackages.

public KnowledgeBuilder buildKnowledgePackages(KieBaseModelImpl kBaseModel, ResultsImpl messages) {
    InternalKieModule kModule = getKieModuleForKBase(kBaseModel.getName());
    KnowledgeBuilder kbuilder = createKnowledgeBuilder(kBaseModel, kModule);
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    Set<Asset> assets = new HashSet<>();
    boolean allIncludesAreValid = true;
    for (String include : getTransitiveIncludes(kBaseModel)) {
        if (StringUtils.isEmpty(include)) {
            continue;
        }
        InternalKieModule includeModule = getKieModuleForKBase(include);
        if (includeModule == null) {
            String text = "Unable to build KieBase, could not find include: " + include;
            log.error(text);
            messages.addMessage(Message.Level.ERROR, KieModuleModelImpl.KMODULE_SRC_PATH, text).setKieBaseName(kBaseModel.getName());
            allIncludesAreValid = false;
            continue;
        }
        addFiles(assets, getKieBaseModel(include), includeModule);
    }
    if (!allIncludesAreValid) {
        return null;
    }
    addFiles(assets, kBaseModel, kModule);
    if (assets.isEmpty()) {
        if (kModule instanceof FileKieModule) {
            log.warn("No files found for KieBase " + kBaseModel.getName() + ", searching folder " + kModule.getFile());
        } else {
            log.warn("No files found for KieBase " + kBaseModel.getName());
        }
    } else {
        for (Asset asset : assets) {
            asset.kmodule.addResourceToCompiler(ckbuilder, kBaseModel, asset.name);
        }
    }
    ckbuilder.build();
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            messages.addMessage(error).setKieBaseName(kBaseModel.getName());
        }
        log.error("Unable to build KieBaseModel:" + kBaseModel.getName() + "\n" + kbuilder.getErrors().toString());
    }
    if (kbuilder.hasResults(ResultSeverity.WARNING)) {
        for (KnowledgeBuilderResult warn : kbuilder.getResults(ResultSeverity.WARNING)) {
            messages.addMessage(warn).setKieBaseName(kBaseModel.getName());
        }
        log.warn("Warning : " + kBaseModel.getName() + "\n" + kbuilder.getResults(ResultSeverity.WARNING).toString());
    }
    // cache KnowledgeBuilder and results
    kModule.cacheKnowledgeBuilderForKieBase(kBaseModel.getName(), kbuilder);
    kModule.cacheResultsForKieBase(kBaseModel.getName(), messages);
    return kbuilder;
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) HashSet(java.util.HashSet) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 4 with KnowledgeBuilderResult

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

the class ProjectJavaCompiler method compileAll.

public List<KnowledgeBuilderResult> compileAll(ProjectClassLoader projectClassLoader, List<String> classList, MemoryResourceReader src) {
    List<KnowledgeBuilderResult> results = new ArrayList<KnowledgeBuilderResult>();
    if (classList.isEmpty()) {
        return results;
    }
    final String[] classes = new String[classList.size()];
    classList.toArray(classes);
    CompilationResult result = compiler.compile(classes, src, new ProjectResourceStore(projectClassLoader), projectClassLoader);
    if (result.getErrors().length > 0) {
        Map<String, ErrorHandler> errorHandlerMap = new HashMap<String, ErrorHandler>();
        for (int i = 0; i < result.getErrors().length; i++) {
            final CompilationProblem err = result.getErrors()[i];
            ErrorHandler handler = errorHandlerMap.get(err.getFileName());
            if (handler == null) {
                handler = new SrcErrorHandler("Src compile error");
                errorHandlerMap.put(err.getFileName(), handler);
            }
            handler.addError(err);
        }
        for (ErrorHandler handler : errorHandlerMap.values()) {
            if (handler.isInError()) {
                results.add(handler.getError());
            }
        }
    }
    return results;
}
Also used : SrcErrorHandler(org.drools.compiler.builder.impl.errors.SrcErrorHandler) ErrorHandler(org.drools.compiler.builder.impl.errors.ErrorHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) SrcErrorHandler(org.drools.compiler.builder.impl.errors.SrcErrorHandler) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 5 with KnowledgeBuilderResult

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

the class PatternBuilder method getFieldReadAccessor.

public static InternalReadAccessor getFieldReadAccessor(final RuleBuildContext context, final BaseDescr descr, final Pattern pattern, final ObjectType objectType, String fieldName, final AcceptsReadAccessor target, final boolean reportError) {
    // reportError is needed as some times failure to build accessor is not a failure, just an indication that building is not possible so try something else.
    InternalReadAccessor reader;
    if (ValueType.FACTTEMPLATE_TYPE.equals(objectType.getValueType())) {
        // @todo use accessor cache
        final FactTemplate factTemplate = ((FactTemplateObjectType) objectType).getFactTemplate();
        reader = new FactTemplateFieldExtractor(factTemplate, factTemplate.getFieldTemplateIndex(fieldName));
        if (target != null) {
            target.setReadAccessor(reader);
        }
        return reader;
    }
    boolean isGetter = getterRegexp.matcher(fieldName).matches();
    if (isGetter) {
        fieldName = fieldName.substring(3, fieldName.indexOf('(')).trim();
    }
    if (isGetter || identifierRegexp.matcher(fieldName).matches()) {
        Declaration decl = context.getDeclarationResolver().getDeclarations(context.getRule()).get(fieldName);
        if (decl != null && decl.getExtractor() instanceof ClassFieldReader && "this".equals(((ClassFieldReader) decl.getExtractor()).getFieldName())) {
            return decl.getExtractor();
        }
        try {
            reader = context.getPkg().getClassFieldAccessorStore().getReader(objectType.getClassName(), fieldName, target);
        } catch (final Exception e) {
            if (reportError && context.isTypesafe()) {
                DialectUtil.copyErrorLocation(e, descr);
                registerDescrBuildError(context, descr, e, "Unable to create Field Extractor for '" + fieldName + "'" + e.getMessage());
            }
            // if there was an error, set the reader back to null
            reader = null;
        } finally {
            if (reportError) {
                Collection<KnowledgeBuilderResult> results = context.getPkg().getClassFieldAccessorStore().getWiringResults(objectType.getClassType(), fieldName);
                if (!results.isEmpty()) {
                    for (KnowledgeBuilderResult res : results) {
                        if (res.getSeverity() == ResultSeverity.ERROR) {
                            context.addError(new DroolsErrorWrapper(res));
                        } else {
                            context.addWarning(new DroolsWarningWrapper(res));
                        }
                    }
                }
            }
        }
    } else {
        // we need MVEL extractor for expressions
        Dialect dialect = context.getDialect();
        try {
            MVELDialect mvelDialect = (MVELDialect) context.getDialect("mvel");
            context.setDialect(mvelDialect);
            final AnalysisResult analysis = context.getDialect().analyzeExpression(context, descr, fieldName, new BoundIdentifiers(pattern, context, Collections.EMPTY_MAP, objectType.getClassType()));
            if (analysis == null) {
                // something bad happened
                if (reportError) {
                    registerDescrBuildError(context, descr, "Unable to analyze expression '" + fieldName + "'");
                }
                return null;
            }
            final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
            if (!usedIdentifiers.getDeclrClasses().isEmpty()) {
                if (reportError && descr instanceof BindingDescr) {
                    registerDescrBuildError(context, descr, "Variables can not be used inside bindings. Variable " + usedIdentifiers.getDeclrClasses().keySet() + " is being used in binding '" + fieldName + "'");
                }
                return null;
            }
            reader = context.getPkg().getClassFieldAccessorStore().getMVELReader(context.getPkg().getName(), objectType.getClassName(), fieldName, context.isTypesafe(), ((MVELAnalysisResult) analysis).getReturnType());
            MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
            ((MVELCompileable) reader).compile(data, context.getRule());
            data.addCompileable((MVELCompileable) reader);
        } catch (final Exception e) {
            int dotPos = fieldName.indexOf('.');
            String varName = dotPos > 0 ? fieldName.substring(0, dotPos).trim() : fieldName;
            if (context.getKnowledgeBuilder().getGlobals().containsKey(varName)) {
                return null;
            }
            if (reportError) {
                DialectUtil.copyErrorLocation(e, descr);
                registerDescrBuildError(context, descr, e, "Unable to create reader for '" + fieldName + "':" + e.getMessage());
            }
            // if there was an error, set the reader back to null
            reader = null;
        } finally {
            context.setDialect(dialect);
        }
    }
    return reader;
}
Also used : FactTemplateFieldExtractor(org.drools.core.facttemplates.FactTemplateFieldExtractor) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) MVELCompileable(org.drools.core.base.mvel.MVELCompileable) DroolsErrorWrapper(org.drools.compiler.compiler.DroolsErrorWrapper) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) ClassFieldReader(org.drools.core.base.ClassFieldReader) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Dialect(org.drools.compiler.compiler.Dialect) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) DroolsWarningWrapper(org.drools.compiler.compiler.DroolsWarningWrapper) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) FactTemplate(org.drools.core.facttemplates.FactTemplate) 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