Search in sources :

Example 31 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieModuleRepoTest method testDeployTwoArtifactVersionsSameTime.

/**
 * TESTS ----------------------------------------------------------------------------------------------------------------------
 */
// simultaneous requests to deploy two new deployments (different versions)
// for an empty/new GA artifactMap
@Test(timeout = 5000)
public void testDeployTwoArtifactVersionsSameTime() throws Exception {
    final String groupId = "org";
    final String artifactId = "one";
    final String firstVersion = "1.0";
    final String secondVersion = "1.0-NEW-FEATURE";
    final CyclicBarrier storeOperationBarrier = new CyclicBarrier(2);
    final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
    final Thread firstThread = new Thread(getStoreArtifactRunnable(kieModuleRepo, groupId, artifactId, firstVersion, storeOperationBarrier, threadsFinishedBarrier));
    final Thread secondThread = new Thread(getStoreArtifactRunnable(kieModuleRepo, groupId, artifactId, secondVersion, storeOperationBarrier, threadsFinishedBarrier));
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    firstThread.setName("normal");
    executor.submit(firstThread);
    secondThread.setName("newFeature");
    executor.submit(secondThread);
    try {
        waitFor(threadsFinishedBarrier);
    } finally {
        executor.shutdownNow();
    }
    final String ga = groupId + ":" + artifactId;
    final Map<ComparableVersion, KieModule> artifactMap = kieModuleRepo.kieModules.get(ga);
    final ComparableVersion normalVersion = new ComparableVersion(firstVersion);
    final KieModule normalKieModule = artifactMap.get(normalVersion);
    final ComparableVersion newFeatureVersion = new ComparableVersion(secondVersion);
    final KieModule newFeatureKieModule = artifactMap.get(newFeatureVersion);
    assertNotNull("Race condition occurred: normal KieModule disappeared from KieModuleRepo!", normalKieModule);
    assertNotNull("Race condition occurred: new feature KieModule disappeared from KieModuleRepo!", newFeatureKieModule);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ComparableVersion(org.kie.api.builder.ReleaseIdComparator.ComparableVersion) KieModule(org.kie.api.builder.KieModule) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 32 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieModuleRepoTest method testOldKieModulesLRUCache.

@Test
public void testOldKieModulesLRUCache() throws Exception {
    // setup
    setFinalField(maxSizeGaCacheField, null, 2);
    setFinalField(maxSizeGaVersionsCacheField, null, 4);
    final ReleaseIdImpl[] releaseIds = new ReleaseIdImpl[9];
    for (int i = 0; i < releaseIds.length; ++i) {
        final String artifactId = Character.toString((char) ('A' + i / 2));
        releaseIds[i] = new ReleaseIdImpl("org", artifactId, "1." + i);
    }
    // store
    for (int i = 0; i < releaseIds.length; ++i) {
        final InternalKieModule kieModule = mock(InternalKieModule.class);
        when(kieModule.getReleaseId()).thenReturn(releaseIds[i]);
        when(kieModule.getCreationTimestamp()).thenReturn(10l);
        kieModuleRepo.store(kieModule);
        // in order to trigger storage to oldKieModules
        kieModuleRepo.store(kieModule);
    }
    int maxSameGAModules = 0;
    int maxGAs = 0;
    for (final Map<ComparableVersion, KieModule> artifactMap : kieModuleRepo.kieModules.values()) {
        maxGAs++;
        if (artifactMap.size() > maxSameGAModules) {
            maxSameGAModules = artifactMap.size();
        }
    }
    assertTrue("The maximum of artifacts per GA should not grow past " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + ": " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + " < " + maxSameGAModules, KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE >= maxSameGAModules);
    assertTrue("The number of GAs not grow past " + KieModuleRepo.MAX_SIZE_GA_CACHE + ": " + KieModuleRepo.MAX_SIZE_GA_CACHE + " > " + maxGAs, KieModuleRepo.MAX_SIZE_GA_CACHE >= maxGAs);
    final int oldKieModulesSize = kieModuleRepo.oldKieModules.size();
    final int maxOldKieModules = KieModuleRepo.MAX_SIZE_GA_CACHE * KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE;
    assertTrue("KieModuleRepo old KieModules map is not limited in it's growth: " + oldKieModulesSize + " > " + maxOldKieModules, oldKieModulesSize <= maxOldKieModules);
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) ComparableVersion(org.kie.api.builder.ReleaseIdComparator.ComparableVersion) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 33 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieBuilderImpl method init.

private PomModel init() {
    KieServices ks = KieServices.Factory.get();
    results = new ResultsImpl();
    // if pomXML is null it will generate a default, using default ReleaseId
    // if pomXml is invalid, it assign pomModel to null
    PomModel pomModel = getPomModel();
    // if kModuleModelXML is null or invalid it will generate a default kModule, with a default kbase name
    buildKieModuleModel();
    if (pomModel != null) {
        // creates ReleaseId from build pom
        // If the pom was generated, it will be the same as teh default ReleaseId
        releaseId = pomModel.getReleaseId();
        // add all the pom dependencies to this builder ... not sure this is a good idea (?)
        KieRepositoryImpl repository = (KieRepositoryImpl) ks.getRepository();
        for (AFReleaseId dep : pomModel.getDependencies(DependencyFilter.COMPILE_FILTER)) {
            KieModule depModule = repository.getKieModule(adapt(dep), pomModel);
            if (depModule != null) {
                addKieDependency(depModule);
            }
        }
    } else {
        // if the pomModel is null it means that the provided pom.xml is invalid so use the default releaseId
        releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    }
    return pomModel;
}
Also used : AFReleaseId(org.appformer.maven.support.AFReleaseId) PomModel(org.appformer.maven.support.PomModel) KieServices(org.kie.api.KieServices) KieModule(org.kie.api.builder.KieModule)

Example 34 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieBuilderImpl method buildAll.

public KieBuilder buildAll(BiFunction<InternalKieModule, ClassLoader, KieModuleKieProject> kprojectSupplier, Predicate<String> classFilter) {
    PomModel pomModel = init();
    // kModuleModel will be null if a provided pom.xml or kmodule.xml is invalid
    if (!isBuilt() && kModuleModel != null) {
        trgMfs = new MemoryFileSystem();
        writePomAndKModule();
        addKBasesFilesToTrg();
        markSource();
        MemoryKieModule memoryKieModule = new MemoryKieModule(adapt(releaseId), kModuleModel, trgMfs);
        if (kieDependencies != null && !kieDependencies.isEmpty()) {
            for (KieModule kieModule : kieDependencies) {
                memoryKieModule.addKieDependency((InternalKieModule) kieModule);
            }
        }
        if (pomModel != null) {
            memoryKieModule.setPomModel(pomModel);
        }
        KieModuleKieProject kProject = kprojectSupplier.apply(memoryKieModule, classLoader);
        for (ReleaseId unresolvedDep : memoryKieModule.getUnresolvedDependencies()) {
            results.addMessage(Level.ERROR, "pom.xml", "Unresolved dependency " + unresolvedDep);
        }
        List<String> pmmlFileNames = getPmmlFileNames(srcMfs);
        if (pmmlFileNames != null && !pmmlFileNames.isEmpty()) {
            buildPMMLPojos(pmmlFileNames, kProject);
        }
        compileJavaClasses(kProject.getClassLoader(), classFilter);
        buildKieProject(results, kProject, trgMfs);
        kModule = kProject.getInternalKieModule();
    }
    return this;
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) PomModel(org.appformer.maven.support.PomModel) AFReleaseId(org.appformer.maven.support.AFReleaseId) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule)

Example 35 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieRepositoryImpl method addKieModule.

public KieModule addKieModule(Resource resource, Resource... dependencies) {
    log.info("Adding KieModule from resource: " + resource);
    KieModule kModule = getKieModule(resource);
    if (dependencies != null && dependencies.length > 0) {
        for (Resource depRes : dependencies) {
            InternalKieModule depKModule = (InternalKieModule) getKieModule(depRes);
            ((InternalKieModule) kModule).addKieDependency(depKModule);
            log.info("Adding KieModule dependency from resource: " + resource);
        }
    }
    addKieModule(kModule);
    return kModule;
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) Resource(org.kie.api.io.Resource) KieModule(org.kie.api.builder.KieModule) KieBuilderImpl.setDefaultsforEmptyKieModule(org.drools.compiler.kie.builder.impl.KieBuilderImpl.setDefaultsforEmptyKieModule)

Aggregations

KieModule (org.kie.api.builder.KieModule)124 KieServices (org.kie.api.KieServices)104 Test (org.junit.Test)94 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)84 ReleaseId (org.kie.api.builder.ReleaseId)84 KieContainer (org.kie.api.runtime.KieContainer)75 KieSession (org.kie.api.runtime.KieSession)64 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)53 Message (org.drools.compiler.Message)25 KieFileSystem (org.kie.api.builder.KieFileSystem)23 Resource (org.kie.api.io.Resource)20 ArrayList (java.util.ArrayList)15 KieModuleModel (org.kie.api.builder.model.KieModuleModel)15 KieBuilder (org.kie.api.builder.KieBuilder)13 KieRepository (org.kie.api.builder.KieRepository)10 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)7 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 Results (org.kie.api.builder.Results)5