Search in sources :

Example 91 with InternalKieModule

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

the class DMNUpdateTest method test_as_kie_wb_common_services_backend_Builder2.

@Test
public void test_as_kie_wb_common_services_backend_Builder2() throws Exception {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId v100 = ks.newReleaseId("org.kie", "dmn-test", "1.0.0");
    final KieModule kieModule = KieHelper.createAndDeployJar(ks, v100, ks.getResources().newClassPathResource("v1_2/dmn-hotcold.dmn", this.getClass()));
    final KieContainer kieContainer = ks.newKieContainer(v100);
    final KieSession kieSession = kieContainer.newKieSession();
    final DMNRuntime runtime = kieSession.getKieRuntime(DMNRuntime.class);
    Assert.assertNotNull(runtime);
    assertThat(runtime.getModels(), hasSize(1));
    checkDMNHotColdDMN12WithNSScattered(runtime);
    // the below is performed by the WB at: https://github.com/kiegroup/kie-wb-common/blob/9e6b6da145e61ac8f5a9f7c0259d44aa9d090a2b/kie-wb-common-services/kie-wb-common-services-backend/src/main/java/org/kie/workbench/common/services/backend/builder/core/Builder.java#L592-L620
    final KieProject kieProject = new KieModuleKieProject((InternalKieModule) kieModule, null);
    final KieContainer kieContainer2 = new KieContainerImpl(kieProject, ks.getRepository(), v100);
    // exhibit the issue.
    final KieSession kieSession2 = kieContainer2.newKieSession();
    final DMNRuntime runtime2 = kieSession2.getKieRuntime(DMNRuntime.class);
    Assert.assertNotNull(runtime2);
    assertThat(runtime2.getModels(), hasSize(1));
    checkDMNHotColdDMN12WithNSScattered(runtime2);
}
Also used : KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 92 with InternalKieModule

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

the class DMNRuntimeUtil method createJarIgnoringErrors.

public static byte[] createJarIgnoringErrors(KieServices ks, ReleaseId releaseId, Resource... resources) {
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId);
    for (int i = 0; i < resources.length; i++) {
        if (resources[i] != null) {
            kfs.write(resources[i]);
        }
    }
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    ((InternalKieBuilder) kieBuilder).buildAll(o -> true);
    InternalKieModule kieModule = (InternalKieModule) ((InternalKieBuilder) kieBuilder).getKieModuleIgnoringErrors();
    byte[] jar = kieModule.getBytes();
    return jar;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 93 with InternalKieModule

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

the class GeneratedClassNamesTest method testKjarWithoutGeneratedClassNames.

@Test
public void testKjarWithoutGeneratedClassNames() {
    // Build a kjar without generated-class-names file (= simulating a build by old drools version)
    ProjectClassLoaderTestUtil.setEnableStoreFirst(false);
    String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person(name == \"Mario\")\n" + "then\n" + "  System.out.println(\"hello\");\n" + "end";
    KieServices ks = KieServices.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "kjar-test-" + UUID.randomUUID(), "1.0");
    KieBuilder kieBuilder = createKieBuilder(ks, getDefaultKieModuleModel(ks), releaseId, toKieFiles(new String[] { str }));
    final InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModule();
    byte[] kjar = kieModule.getBytes();
    Resource kjarResource = ks.getResources().newByteArrayResource(kjar);
    // cleanup
    ks.getRepository().removeKieModule(releaseId);
    // Load the kjar with the current drools version
    ProjectClassLoaderTestUtil.setEnableStoreFirst(true);
    ks.getRepository().addKieModule(kjarResource);
    KieContainer kcontainer = ks.newKieContainer(releaseId);
    KieModule kieModule2 = ((KieContainerImpl) kcontainer).getKieModuleForKBase("kbase");
    assertTrue(kieModule2 instanceof CanonicalKieModule);
    KieSession ksession = kcontainer.newKieSession();
    Set<String> generatedClassNames = ((CanonicalKieModule) kieModule2).getGeneratedClassNames();
    assertTrue(generatedClassNames.isEmpty());
    Person me = new Person("Mario", 40);
    ksession.insert(me);
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);
}
Also used : Resource(org.kie.api.io.Resource) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Person(org.drools.modelcompiler.domain.Person) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 94 with InternalKieModule

use of org.drools.compiler.kie.builder.impl.InternalKieModule 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 95 with InternalKieModule

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

the class CanonicalKieModule method findProcesses.

private List<Process> findProcesses(InternalKieModule kieModule, KieBaseModelImpl kBaseModel) {
    List<Process> processes = new ArrayList<>();
    Collection<KiePackage> pkgs = kieModule.getKnowledgePackagesForKieBase(kBaseModel.getName());
    if (pkgs == null) {
        List<Resource> processResources = kieModule.getFileNames().stream().filter(fileName -> {
            ResourceType resourceType = determineResourceType(fileName);
            return resourceType == ResourceType.DRF || resourceType == ResourceType.BPMN2;
        }).map(fileName -> {
            final Resource processResource = kieModule.getResource(fileName);
            processResource.setResourceType(determineResourceType(fileName));
            return processResource;
        }).collect(toList());
        if (!processResources.isEmpty()) {
            KnowledgeBuilderImpl kbuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder(createBuilderConfiguration(kBaseModel, moduleClassLoader));
            for (Resource processResource : processResources) {
                kbuilder.add(processResource, processResource.getResourceType());
            }
            for (InternalKnowledgePackage knowledgePackage : kbuilder.getPackages()) {
                processes.addAll(knowledgePackage.getProcesses());
            }
        }
    } else {
        for (KiePackage pkg : pkgs) {
            processes.addAll(pkg.getProcesses());
        }
    }
    return processes;
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KiePath(org.kie.memorycompiler.resources.KiePath) Arrays(java.util.Arrays) ResourceChange(org.kie.internal.builder.ResourceChange) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ChangeType(org.kie.internal.builder.ChangeType) KieBaseUpdatersContext(org.drools.compiler.kie.builder.impl.KieBaseUpdatersContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) CoreComponentFactory(org.drools.core.reteoo.CoreComponentFactory) StringUtil.fileNameToClass(org.drools.modelcompiler.util.StringUtil.fileNameToClass) ReleaseId(org.kie.api.builder.ReleaseId) DependencyFilter(org.kie.util.maven.support.DependencyFilter) Collections.singletonList(java.util.Collections.singletonList) ModelComponent.areEqualInModel(org.drools.model.impl.ModelComponent.areEqualInModel) Map(java.util.Map) KieBaseUpdaterImplContext(org.drools.compiler.kie.builder.impl.KieBaseUpdaterImplContext) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl) Predicate(java.util.function.Predicate) KieProject(org.drools.compiler.kie.builder.impl.KieProject) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Process(org.kie.api.definition.process.Process) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KieBaseUpdaters(org.drools.compiler.kie.builder.impl.KieBaseUpdaters) Set(java.util.Set) Results(org.kie.api.builder.Results) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) Collectors(java.util.stream.Collectors) KieBaseUpdaterOptions(org.drools.compiler.kie.builder.impl.KieBaseUpdaterOptions) Drools(org.drools.core.util.Drools) CanonicalKieBaseUpdater(org.drools.modelcompiler.builder.CanonicalKieBaseUpdater) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceConfiguration(org.kie.api.io.ResourceConfiguration) UncheckedIOException(java.io.UncheckedIOException) Resource(org.kie.api.io.Resource) List(java.util.List) Stream(java.util.stream.Stream) BuildContext(org.drools.compiler.kie.builder.impl.BuildContext) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) StringUtils(org.drools.core.util.StringUtils) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) PomModel(org.kie.util.maven.support.PomModel) ModelSourceClass.getProjectModelClassNameNameWithReleaseId(org.drools.modelcompiler.builder.ModelSourceClass.getProjectModelClassNameNameWithReleaseId) HashMap(java.util.HashMap) ResourceType(org.kie.api.io.ResourceType) AlphaNetworkCompilerOption(org.kie.internal.builder.conf.AlphaNetworkCompilerOption) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) IoUtils(org.drools.core.util.IoUtils) ResourceProvider(org.drools.wiring.api.ResourceProvider) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieBaseUpdater(org.drools.compiler.kie.builder.impl.KieBaseUpdater) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) NamedModelItem(org.drools.model.NamedModelItem) KieServices(org.kie.api.KieServices) Message(org.kie.api.builder.Message) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Model(org.drools.model.Model) Iterator(java.util.Iterator) InternalResource(org.drools.core.io.internal.InternalResource) KieService(org.kie.api.internal.utils.KieService) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) AbstractKieModule.checkStreamMode(org.drools.compiler.kie.builder.impl.AbstractKieModule.checkStreamMode) IOException(java.io.IOException) DroolsAssemblerContext(org.drools.compiler.builder.DroolsAssemblerContext) ResultsImpl(org.drools.compiler.kie.builder.impl.ResultsImpl) GeneratedFact(org.drools.core.factmodel.GeneratedFact) File(java.io.File) ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Collectors.toList(java.util.stream.Collectors.toList) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Comparator(java.util.Comparator) ResourceType.determineResourceType(org.kie.api.io.ResourceType.determineResourceType) Collections(java.util.Collections) InputStream(java.io.InputStream) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ArrayList(java.util.ArrayList) Resource(org.kie.api.io.Resource) InternalResource(org.drools.core.io.internal.InternalResource) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) ResourceType.determineResourceType(org.kie.api.io.ResourceType.determineResourceType) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)201 ReleaseId (org.kie.api.builder.ReleaseId)125 KieServices (org.kie.api.KieServices)120 Test (org.junit.Test)86 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)72 File (java.io.File)62 ArrayList (java.util.ArrayList)57 KieContainer (org.kie.api.runtime.KieContainer)57 KieBuilder (org.kie.api.builder.KieBuilder)55 KieFileSystem (org.kie.api.builder.KieFileSystem)54 FileOutputStream (java.io.FileOutputStream)53 KieMavenRepository (org.kie.scanner.KieMavenRepository)51 KieSession (org.kie.api.runtime.KieSession)43 HashMap (java.util.HashMap)28 Before (org.junit.Before)28 KieModule (org.kie.api.builder.KieModule)24 DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)23 KieScanner (org.kie.api.builder.KieScanner)20 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)18 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)17