Search in sources :

Example 1 with CompilationProblemAdapter

use of org.drools.compiler.kie.builder.impl.CompilationProblemAdapter 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 = new CompilationProblemAdapter(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.kie.internal.jci.CompilationProblem) SrcErrorHandler(org.drools.compiler.builder.impl.errors.SrcErrorHandler) CompilationResult(org.kie.memorycompiler.CompilationResult) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) CompilationProblemAdapter(org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)

Example 2 with CompilationProblemAdapter

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

the class CanonicalModelKieProject method writeProjectOutput.

@Override
public void writeProjectOutput(MemoryFileSystem trgMfs, BuildContext buildContext) {
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    ModelWriter modelWriter = new ModelWriter();
    Collection<String> modelFiles = new HashSet<>();
    Collection<String> sourceFiles = new HashSet<>();
    Map<String, List<String>> modelsByKBase = new HashMap<>();
    for (Map.Entry<String, ModelBuilderImpl> modelBuilder : modelBuilders.entrySet()) {
        ModelWriter.Result result = modelWriter.writeModel(srcMfs, modelBuilder.getValue().getPackageSources());
        modelFiles.addAll(result.getModelFiles());
        sourceFiles.addAll(result.getSourceFiles());
        List<String> modelFilesForKieBase = new ArrayList<>();
        modelFilesForKieBase.addAll(result.getModelFiles());
        modelFilesForKieBase.addAll(((CanonicalModelBuildContext) buildContext).getNotOwnedModelFiles(modelBuilders, modelBuilder.getKey()));
        modelsByKBase.put(modelBuilder.getKey(), modelFilesForKieBase);
    }
    InternalKieModule kieModule = getInternalKieModule();
    ModelSourceClass modelSourceClass = new ModelSourceClass(kieModule.getReleaseId(), kieModule.getKieModuleModel().getKieBaseModels(), modelsByKBase, hasDynamicClassLoader());
    String projectSourcePath = modelWriter.getBasePath().asString() + "/" + modelSourceClass.getName();
    srcMfs.write(projectSourcePath, modelSourceClass.generate().getBytes());
    sourceFiles.add(projectSourcePath);
    Set<KiePath> origFileNames = new HashSet<>(trgMfs.getFilePaths());
    String[] sources = sourceFiles.toArray(new String[sourceFiles.size()]);
    if (sources.length != 0) {
        CompilationResult res = getCompiler().compile(sources, srcMfs, trgMfs, getClassLoader());
        Stream.of(res.getErrors()).collect(groupingBy(CompilationProblem::getFileName)).forEach((name, errors) -> {
            errors.forEach(m -> buildContext.getMessages().addMessage(new CompilationProblemAdapter(m)));
            File srcFile = srcMfs.getFile(name);
            if (srcFile instanceof MemoryFile) {
                String src = new String(srcMfs.getFileContents((MemoryFile) srcFile));
                buildContext.getMessages().addMessage(Message.Level.ERROR, name, "Java source of " + name + " in error:\n" + src);
            }
        });
        for (CompilationProblem problem : res.getWarnings()) {
            buildContext.getMessages().addMessage(new CompilationProblemAdapter(problem));
        }
    }
    if (ProjectClassLoader.isEnableStoreFirst()) {
        Set<KiePath> generatedClassPaths = new HashSet<>(trgMfs.getFilePaths());
        generatedClassPaths.removeAll(origFileNames);
        Set<String> generatedClassNames = generatedClassPaths.stream().map(KiePath::asString).map(ClassUtils::convertResourceToClassName).collect(Collectors.toSet());
        modelWriter.writeGeneratedClassNamesFile(generatedClassNames, trgMfs, getInternalKieModule().getReleaseId());
    }
    modelWriter.writeModelFile(modelFiles, trgMfs, getInternalKieModule().getReleaseId());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompilationProblem(org.kie.memorycompiler.CompilationProblem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) KiePath(org.kie.memorycompiler.resources.KiePath) CompilationResult(org.kie.memorycompiler.CompilationResult) HashMap(java.util.HashMap) Map(java.util.Map) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) CompilationProblemAdapter(org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)

Example 3 with CompilationProblemAdapter

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

the class JavaDialect method compileAll.

/**
 * This actually triggers the compiling of all the resources.
 * Errors are mapped back to the element that originally generated the semantic
 * code.
 */
public void compileAll() {
    if (this.generatedClassList.isEmpty()) {
        this.errorHandlers.clear();
        return;
    }
    final String[] classes = new String[this.generatedClassList.size()];
    this.generatedClassList.toArray(classes);
    File dumpDir = this.configuration.getPackageBuilderConfiguration().getDumpDir();
    if (dumpDir != null) {
        dumpResources(classes, dumpDir);
    }
    final CompilationResult result = this.compiler.compile(classes, this.src, this.packageStoreWrapper, rootClassLoader);
    // this will sort out the errors based on what class/file they happened in
    if (result.getErrors().length > 0) {
        for (int i = 0; i < result.getErrors().length; i++) {
            final CompilationProblem err = new CompilationProblemAdapter(result.getErrors()[i]);
            final ErrorHandler handler = this.errorHandlers.get(err.getFileName());
            handler.addError(err);
        }
        final Collection errors = this.errorHandlers.values();
        for (Object error : errors) {
            final ErrorHandler handler = (ErrorHandler) error;
            if (handler.isInError()) {
                this.results.add(handler.getError());
            }
        }
    }
    // We've compiled everthing, so clear it for the next set of additions
    this.generatedClassList.clear();
    this.errorHandlers.clear();
}
Also used : RuleErrorHandler(org.drools.compiler.builder.impl.errors.RuleErrorHandler) SrcErrorHandler(org.drools.compiler.builder.impl.errors.SrcErrorHandler) FunctionErrorHandler(org.drools.compiler.builder.impl.errors.FunctionErrorHandler) ErrorHandler(org.drools.compiler.builder.impl.errors.ErrorHandler) RuleInvokerErrorHandler(org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler) Collection(java.util.Collection) CompilationResult(org.kie.memorycompiler.CompilationResult) CompilationProblem(org.kie.internal.jci.CompilationProblem) File(java.io.File) CompilationProblemAdapter(org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)

Example 4 with CompilationProblemAdapter

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

the class JavaParserCompiler method compileAll.

public static Map<String, Class<?>> compileAll(KnowledgeBuilderImpl kbuilder, ClassLoader classLoader, List<GeneratedClassWithPackage> classes) {
    if (classes == null || classes.isEmpty()) {
        return Collections.emptyMap();
    }
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    MemoryFileSystem trgMfs = new MemoryFileSystem();
    String[] resources = writeModel(classes, srcMfs);
    CompilationResult resultCompilation = createDefaultCompiler().compile(resources, srcMfs, trgMfs, classLoader);
    CompilationProblem[] errors = resultCompilation.getErrors();
    if (errors.length != 0) {
        classes.forEach(c -> logger.error(c.toString()));
        for (CompilationProblem error : errors) {
            kbuilder.addBuilderResult(new CompilationProblemErrorResult(new CompilationProblemAdapter(error)));
        }
        return Collections.emptyMap();
    }
    InternalClassLoader internalClassLoader = AccessController.doPrivileged((PrivilegedAction<InternalClassLoader>) () -> new InternalClassLoader(classLoader, trgMfs));
    return loadClasses(getClassNames(classLoader, trgMfs), internalClassLoader);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CompilationProblemErrorResult(org.drools.modelcompiler.builder.errors.CompilationProblemErrorResult) CompilationResult(org.kie.memorycompiler.CompilationResult) CompilationProblem(org.kie.memorycompiler.CompilationProblem) CompilationProblemAdapter(org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)

Aggregations

CompilationProblemAdapter (org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)4 CompilationResult (org.kie.memorycompiler.CompilationResult)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ErrorHandler (org.drools.compiler.builder.impl.errors.ErrorHandler)2 SrcErrorHandler (org.drools.compiler.builder.impl.errors.SrcErrorHandler)2 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)2 CompilationProblem (org.kie.internal.jci.CompilationProblem)2 CompilationProblem (org.kie.memorycompiler.CompilationProblem)2 File (java.io.File)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 FunctionErrorHandler (org.drools.compiler.builder.impl.errors.FunctionErrorHandler)1 RuleErrorHandler (org.drools.compiler.builder.impl.errors.RuleErrorHandler)1 RuleInvokerErrorHandler (org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler)1 File (org.drools.compiler.compiler.io.File)1 MemoryFile (org.drools.compiler.compiler.io.memory.MemoryFile)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1