Search in sources :

Example 6 with KieModuleMetaInfo

use of org.drools.core.rule.KieModuleMetaInfo in project drools by kiegroup.

the class MarshallingKieMetaInfoBuilder method generateKieModuleMetaInfo.

public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
    Map<String, TypeMetaInfo> typeInfos = new HashMap<>();
    Map<String, Set<String>> rulesPerPackage = new HashMap<>();
    KieModuleModel kieModuleModel = kModule.getKieModuleModel();
    for (String kieBaseName : kieModuleModel.getKieBaseModels().keySet()) {
        KnowledgeBuilder kBuilder = kModule.getKnowledgeBuilderForKieBase(kieBaseName);
        KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
        KieModuleCache.CompilationData.Builder _compData = createCompilationData();
        for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
            PackageRegistry pkgRegistry = ((InternalKnowledgeBuilder) kBuilder).getPackageRegistry(kPkg.getName());
            JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
            List<String> types = new ArrayList<>();
            for (FactType factType : kPkg.getFactTypes()) {
                Class<?> typeClass = ((ClassDefinition) factType).getDefinedClass();
                TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(typeClass);
                if (typeDeclaration != null) {
                    typeInfos.put(typeClass.getName(), new TypeMetaInfo(typeDeclaration));
                }
                String className = factType.getName();
                String internalName = className.replace('.', '/') + ".class";
                if (trgMfs != null) {
                    byte[] bytes = runtimeData.getBytecode(internalName);
                    if (bytes != null) {
                        trgMfs.write(internalName, bytes, true);
                    }
                }
                types.add(internalName);
            }
            Set<String> rules = rulesPerPackage.get(kPkg.getName());
            if (rules == null) {
                rules = new HashSet<>();
            }
            for (Rule rule : kPkg.getRules()) {
                rules.add(rule.getName());
            }
            if (!rules.isEmpty()) {
                rulesPerPackage.put(kPkg.getName(), rules);
            }
            addToCompilationData(_compData, runtimeData, types);
        }
        _kmoduleCacheBuilder.addCompilationData(_compData.build());
        if (trgMfs != null) {
            writeCompilationDataToTrg(_kmoduleCacheBuilder.build(), kieBaseName, trgMfs);
        }
    }
    return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ClassDefinition(org.drools.core.factmodel.ClassDefinition) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) FactType(org.kie.api.definition.type.FactType) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo) Rule(org.kie.api.definition.rule.Rule) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 7 with KieModuleMetaInfo

use of org.drools.core.rule.KieModuleMetaInfo in project drools by kiegroup.

the class KieMetaInfoBuilder method writeKieModuleMetaInfo.

public void writeKieModuleMetaInfo(ResourceStore trgMfs) {
    if (hasMvel()) {
        KieModuleMetaInfo info = generateKieModuleMetaInfo(trgMfs);
        trgMfs.write(KieModuleModelImpl.KMODULE_INFO_JAR_PATH, info.marshallMetaInfos().getBytes(IoUtils.UTF8_CHARSET), true);
    }
}
Also used : KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo)

Example 8 with KieModuleMetaInfo

use of org.drools.core.rule.KieModuleMetaInfo in project drools by kiegroup.

the class KieModuleMetaDataImpl method indexMetaInfo.

private void indexMetaInfo(byte[] bytes) {
    KieModuleMetaInfo info = KieModuleMetaInfo.unmarshallMetaInfos(new String(bytes, UTF8_CHARSET));
    typeMetaInfos.putAll(info.getTypeMetaInfos());
    rulesByPackage.putAll(info.getRulesByPackage());
}
Also used : KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo)

Example 9 with KieModuleMetaInfo

use of org.drools.core.rule.KieModuleMetaInfo in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method getResourcesFromADroolsPRJ.

@Test
// https://issues.redhat.com/browse/AF-2892
@Ignore
public void getResourcesFromADroolsPRJ() throws Exception {
    /**
     * If the test fail check if the Drools core classes used, KieModuleMetaInfo and TypeMetaInfo implements Serializable
     */
    String alternateSettingsAbsPath = TestUtilMaven.getSettingsFile();
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = TestUtil.createAndCopyToDirectory(tmpRoot, "dummy", ResourcesConstants.KJAR_2_SINGLE_RESOURCES);
    final AFCompiler compiler = KieMavenCompilerFactory.getCompiler(EnumSet.of(KieDecorator.STORE_KIE_OBJECTS, KieDecorator.STORE_BUILD_CLASSPATH, KieDecorator.ENABLE_INCREMENTAL_BUILD));
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(Paths.get(tmp.toUri()));
    CompilationRequest req = new DefaultCompilationRequest(mavenRepoPath, info, new String[] { MavenCLIArgs.COMPILE, MavenCLIArgs.ALTERNATE_USER_SETTINGS + alternateSettingsAbsPath }, Boolean.FALSE);
    KieCompilationResponse res = (KieCompilationResponse) compiler.compile(req);
    TestUtil.saveMavenLogIfCompilationResponseNotSuccessfull(tmp, res, this.getClass(), testName);
    if (!res.isSuccessful()) {
        List<String> msgs = res.getMavenOutput();
        for (String msg : msgs) {
            logger.info(msg);
        }
    }
    assertThat(res.isSuccessful()).isTrue();
    Optional<KieModuleMetaInfo> metaDataOptional = res.getKieModuleMetaInfo();
    assertThat(metaDataOptional).isPresent();
    KieModuleMetaInfo kieModuleMetaInfo = metaDataOptional.get();
    assertThat(kieModuleMetaInfo).isNotNull();
    Optional<KieModule> kieModuleOptional = res.getKieModule();
    assertThat(kieModuleOptional).isPresent();
    KieModule kModule = kieModuleOptional.get();
    assertThat(res.getDependenciesAsURI()).hasSize(4);
    KieModuleMetaData kieModuleMetaData = new KieModuleMetaDataImpl((InternalKieModule) kModule, res.getDependenciesAsURI());
    assertThat(kieModuleMetaData).isNotNull();
    List<String> resources = CompilerClassloaderUtils.getStringFromTargets(tmpRoot);
    assertThat(resources).hasSize(3);
}
Also used : Path(org.uberfire.java.nio.file.Path) KieCompilationResponse(org.kie.workbench.common.services.backend.compiler.impl.kie.KieCompilationResponse) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) KieModuleMetaDataImpl(org.kie.scanner.KieModuleMetaDataImpl) WorkspaceCompilationInfo(org.kie.workbench.common.services.backend.compiler.impl.WorkspaceCompilationInfo) KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.impl.DefaultCompilationRequest) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with KieModuleMetaInfo

use of org.drools.core.rule.KieModuleMetaInfo in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method getResourcesFromADroolsPRJWithErrorWithMavenSkip.

@Test
@Ignore("https://issues.redhat.com/browse/AF-2892")
public void getResourcesFromADroolsPRJWithErrorWithMavenSkip() throws Exception {
    System.setProperty(MAVEN_MAIN_SKIP, Boolean.TRUE.toString());
    /**
     * If the test fail check if the Drools core classes used, KieModuleMetaInfo and TypeMetaInfo implements Serializable
     */
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = TestUtil.createAndCopyToDirectory(tmpRoot, "dummy", ResourcesConstants.KJAR_2_SINGLE_RESOURCES_WITH_ERROR);
    final AFCompiler compiler = KieMavenCompilerFactory.getCompiler(EnumSet.of(KieDecorator.STORE_KIE_OBJECTS, KieDecorator.ENABLE_INCREMENTAL_BUILD));
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(Paths.get(tmp.toUri()));
    CompilationRequest req = new DefaultCompilationRequest(mavenRepoPath, info, new String[] { MavenCLIArgs.COMPILE, MavenCLIArgs.ALTERNATE_USER_SETTINGS + TestUtilMaven.getSettingsFile() }, Boolean.FALSE);
    KieCompilationResponse res = (KieCompilationResponse) compiler.compile(req);
    TestUtil.saveMavenLogIfCompilationResponseNotSuccessfull(tmp, res, this.getClass(), testName);
    if (!res.isSuccessful()) {
        List<String> msgs = res.getMavenOutput();
        for (String msg : msgs) {
            logger.info(msg);
        }
    }
    assertThat(res.isSuccessful()).isTrue();
    Optional<KieModuleMetaInfo> metaDataOptional = res.getKieModuleMetaInfo();
    assertThat(metaDataOptional).isPresent();
    KieModuleMetaInfo kieModuleMetaInfo = metaDataOptional.get();
    assertThat(kieModuleMetaInfo).isNotNull();
    Optional<KieModule> kieModuleOptional = res.getKieModule();
    assertThat(kieModuleOptional).isPresent();
    List<String> classloaderOptional = CompilerClassloaderUtils.getStringFromTargets(tmpRoot);
    assertThat(classloaderOptional).hasSize(3);
}
Also used : Path(org.uberfire.java.nio.file.Path) WorkspaceCompilationInfo(org.kie.workbench.common.services.backend.compiler.impl.WorkspaceCompilationInfo) KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.impl.DefaultCompilationRequest) KieCompilationResponse(org.kie.workbench.common.services.backend.compiler.impl.kie.KieCompilationResponse) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

KieModuleMetaInfo (org.drools.core.rule.KieModuleMetaInfo)11 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)6 Ignore (org.junit.Ignore)6 Test (org.junit.Test)6 KieModule (org.kie.api.builder.KieModule)6 DefaultCompilationRequest (org.kie.workbench.common.services.backend.compiler.impl.DefaultCompilationRequest)6 WorkspaceCompilationInfo (org.kie.workbench.common.services.backend.compiler.impl.WorkspaceCompilationInfo)6 KieCompilationResponse (org.kie.workbench.common.services.backend.compiler.impl.kie.KieCompilationResponse)6 Path (org.uberfire.java.nio.file.Path)6 Set (java.util.Set)3 TypeMetaInfo (org.drools.core.rule.TypeMetaInfo)3 KieModuleMetaData (org.kie.scanner.KieModuleMetaData)3 KieModuleMetaDataImpl (org.kie.scanner.KieModuleMetaDataImpl)3 AFCompiler (org.kie.workbench.common.services.backend.compiler.AFCompiler)3 CompilationRequest (org.kie.workbench.common.services.backend.compiler.CompilationRequest)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 InternalKnowledgeBuilder (org.drools.compiler.builder.InternalKnowledgeBuilder)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2