Search in sources :

Example 6 with ReleaseIdImpl

use of org.kie.util.maven.support.ReleaseIdImpl in project drools by kiegroup.

the class DroolsModelProvider method generateRulesFiles.

/**
 * This method depends on exec-model. Be aware in case of refactoring
 * @param packageDescr
 * @return
 */
protected List<GeneratedFile> generateRulesFiles(PackageDescr packageDescr) {
    ModelBuilderImpl<PackageSources> modelBuilder = new ModelBuilderImpl<>(PackageSources::dumpSources, new KnowledgeBuilderConfigurationImpl(getClass().getClassLoader()), new ReleaseIdImpl("dummy:dummy:0.0.0"), false);
    CompositeKnowledgeBuilder batch = modelBuilder.batch();
    batch.add(new DescrResource(packageDescr), ResourceType.DESCR);
    try {
        batch.build();
        if (modelBuilder.hasErrors()) {
            StringBuilder builder = new StringBuilder();
            for (DroolsError error : modelBuilder.getErrors().getErrors()) {
                logger.error(error.toString());
                builder.append(error.toString()).append(" ");
            }
            throw new KiePMMLException(builder.toString());
        }
    } catch (Exception e) {
        logger.error(e.getMessage());
        StringBuilder builder = new StringBuilder(e.getMessage()).append(" ");
        for (DroolsError error : modelBuilder.getErrors().getErrors()) {
            logger.error(error.toString());
            builder.append(error.toString()).append(" ");
        }
        throw new RuntimeException(builder.toString(), e);
    }
    return generateModels(modelBuilder).stream().map(f -> new GeneratedFile(GeneratedFile.Type.RULE, f.getPath(), new String(f.getData()))).collect(toList());
}
Also used : ModelBuilderImpl(org.drools.modelcompiler.builder.ModelBuilderImpl) KiePMMLDroolsModel(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel) DescrResource(org.drools.core.io.impl.DescrResource) EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE(org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Model(org.dmg.pmml.Model) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) DroolsCompilationDTO(org.kie.pmml.models.drools.dto.DroolsCompilationDTO) KiePMMLDescrFactory.getBaseDescr(org.kie.pmml.models.drools.commons.factories.KiePMMLDescrFactory.getBaseDescr) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) LoggerFactory(org.slf4j.LoggerFactory) StringUtils.getPkgUUID(org.drools.core.util.StringUtils.getPkgUUID) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ModelImplementationProvider(org.kie.pmml.compiler.api.provider.ModelImplementationProvider) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) Field(org.dmg.pmml.Field) Map(java.util.Map) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KiePMMLModelWithSources(org.kie.pmml.commons.model.KiePMMLModelWithSources) ModelBuilderImpl(org.drools.modelcompiler.builder.ModelBuilderImpl) CompilationDTO(org.kie.pmml.compiler.api.dto.CompilationDTO) PackageSources(org.drools.modelcompiler.builder.PackageSources) Logger(org.slf4j.Logger) PackageDescr(org.drools.drl.ast.descr.PackageDescr) IOException(java.io.IOException) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) KiePMMLDataDictionaryASTFactory(org.kie.pmml.models.drools.ast.factories.KiePMMLDataDictionaryASTFactory) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) DroolsError(org.drools.drl.parser.DroolsError) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DescrResource(org.drools.core.io.impl.DescrResource) IOException(java.io.IOException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) DroolsError(org.drools.drl.parser.DroolsError) PackageSources(org.drools.modelcompiler.builder.PackageSources) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 7 with ReleaseIdImpl

use of org.kie.util.maven.support.ReleaseIdImpl in project drools by kiegroup.

the class KieBuilderTest method testInvalidPomXmlGAV.

@Test
public void testInvalidPomXmlGAV() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    KieModuleModel kProj = createKieProject(namespace);
    ReleaseId releaseId = new ReleaseIdImpl("", "", "");
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML(kfs, releaseId);
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    KieBuilder kieBuilder = createKieBuilder(kfs);
    kieBuilder.buildAll();
    assertTrue(kieBuilder.getResults().hasMessages(Level.ERROR));
}
Also used : ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 8 with ReleaseIdImpl

use of org.kie.util.maven.support.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleRepoTest method removeStoreArtifactMapTest.

// remove request followed by a store request on a high load system
// * remove does not completely finish before store starts
@Test(timeout = 5000)
public void removeStoreArtifactMapTest() throws Exception {
    // actual test
    final ReleaseIdImpl releaseId = new ReleaseIdImpl("org", "redeploy", "2.0");
    final InternalKieModule originalKieModule = mock(InternalKieModule.class);
    when(originalKieModule.getReleaseId()).thenReturn(releaseId);
    when(originalKieModule.getCreationTimestamp()).thenReturn(0l);
    final InternalKieModule redeployKieModule = mock(InternalKieModule.class);
    when(redeployKieModule.getReleaseId()).thenReturn(releaseId);
    when(redeployKieModule.getCreationTimestamp()).thenReturn(1l);
    // 1. initial deploy ("long ago")
    kieModuleRepo.store(originalKieModule);
    final CyclicBarrier storeRemoveOperationBarrier = new CyclicBarrier(2);
    final CyclicBarrier operationsSerializationBarrier = new CyclicBarrier(2);
    final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
    final Runnable removeRunnable = () -> {
        waitFor(storeRemoveOperationBarrier);
        kieModuleRepo.remove(releaseId);
        waitFor(operationsSerializationBarrier);
        waitFor(threadsFinishedBarrier);
    };
    final Runnable redeployRunnable = () -> {
        waitFor(storeRemoveOperationBarrier);
        waitFor(operationsSerializationBarrier);
        kieModuleRepo.store(redeployKieModule);
        waitFor(threadsFinishedBarrier);
    };
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    try {
        // 2. remove and redploy
        executor.submit(removeRunnable);
        executor.submit(redeployRunnable);
        waitFor(threadsFinishedBarrier);
    } finally {
        executor.shutdownNow();
    }
    final String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();
    final Map<ComparableVersion, KieModule> artifactMap = kieModuleRepo.kieModules.get(ga);
    assertNotNull("Artifact Map for GA '" + ga + "' not in KieModuleRepo!", artifactMap);
    // never gets this far, but this is a good check
    final KieModule redeployedKieModule = artifactMap.get(new ComparableVersion(releaseId.getVersion()));
    assertNotNull("Redeployed module has disappeared from KieModuleRepo!", redeployedKieModule);
    assertEquals("Original module retrieved instead of redeployed module!", 1l, redeployKieModule.getCreationTimestamp());
}
Also used : ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) ExecutorService(java.util.concurrent.ExecutorService) ComparableVersion(org.kie.api.builder.ReleaseIdComparator.ComparableVersion) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 9 with ReleaseIdImpl

use of org.kie.util.maven.support.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleRepoTest method storingNewProjectVersionsCausesOldVersionEvictionFromKieModuleRepoTest.

@Test
public void storingNewProjectVersionsCausesOldVersionEvictionFromKieModuleRepoTest() throws Exception {
    // setup
    // to test oldKieModules caching
    setCacheSize(maxSizeGaCacheField, null, 2);
    setCacheSize(maxSizeGaVersionsCacheField, null, 3);
    final ReleaseIdImpl[] releaseIds = new ReleaseIdImpl[7];
    for (int i = 0; i < releaseIds.length; ++i) {
        releaseIds[i] = new ReleaseIdImpl("org", "test", "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 numKieModules = countKieModules(kieModuleRepo.kieModules);
    assertEquals("KieModuleRepo cache should not grow past " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + ": ", KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE, numKieModules);
    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);
    // store
    for (int o = 0; o < 2; ++o) {
        // loop 2 times in order to trigger storage to oldKieModules
        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);
        }
    }
    numKieModules = countKieModules(kieModuleRepo.kieModules);
    assertEquals("KieModuleRepo cache should not grow past " + KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE + ": ", KieModuleRepo.MAX_SIZE_GA_VERSIONS_CACHE, numKieModules);
    oldKieModulesSize = kieModuleRepo.oldKieModules.size();
    assertTrue("KieModuleRepo old KieModules map is not limited in it's growth: " + oldKieModulesSize + " > " + maxOldKieModules, oldKieModulesSize <= maxOldKieModules);
}
Also used : ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 10 with ReleaseIdImpl

use of org.kie.util.maven.support.ReleaseIdImpl in project drools by kiegroup.

the class KieModuleRepoTest method testOldKieModulesLRUCache.

@Test
public void testOldKieModulesLRUCache() throws Exception {
    // setup
    setCacheSize(maxSizeGaCacheField, null, 2);
    setCacheSize(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.kie.util.maven.support.ReleaseIdImpl) ComparableVersion(org.kie.api.builder.ReleaseIdComparator.ComparableVersion) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Aggregations

ReleaseIdImpl (org.kie.util.maven.support.ReleaseIdImpl)16 Test (org.junit.Test)9 ReleaseId (org.kie.api.builder.ReleaseId)8 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)5 KieModule (org.kie.api.builder.KieModule)3 ArrayList (java.util.ArrayList)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 ExecutorService (java.util.concurrent.ExecutorService)2 KieBuilder (org.kie.api.builder.KieBuilder)2 KieFileSystem (org.kie.api.builder.KieFileSystem)2 ComparableVersion (org.kie.api.builder.ReleaseIdComparator.ComparableVersion)2 File (java.io.File)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Field (org.dmg.pmml.Field)1