Search in sources :

Example 6 with TypeMetaInfo

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

the class KieMetadataTest method compileAndLoadKieJarMetadataAllResourcesPackagedJar.

@Ignore("https://issues.redhat.com/browse/AF-2741\n" + "https://issues.redhat.com/browse/AF-2892")
// AF-1459 it tooks 30% of the time of the time spent by all module's test (108), alone it took 30 sec
@Test
public void compileAndLoadKieJarMetadataAllResourcesPackagedJar() throws Exception {
    /**
     * If the test fail check if the Drools core classes used, KieModuleMetaInfo and TypeMetaInfo implements Serializable
     */
    Path temp = TestUtil.createAndCopyToDirectory(tmpRoot, "dummy", ResourcesConstants.KJAR_2_ALL_RESOURCES);
    final AFCompiler compiler = KieMavenCompilerFactory.getCompiler(EnumSet.of(KieDecorator.ENABLE_LOGGING, KieDecorator.STORE_KIE_OBJECTS, KieDecorator.STORE_BUILD_CLASSPATH, KieDecorator.ENABLE_INCREMENTAL_BUILD));
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(temp);
    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(temp, 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();
    Map<String, Set<String>> rulesBP = kieModuleMetaInfo.getRulesByPackage();
    assertThat(rulesBP).hasSize(6);
    Map<String, TypeMetaInfo> typesMI = kieModuleMetaInfo.getTypeMetaInfos();
    // This is a "magic number" test and may or may not be valid since changes
    // to the mechanism for generating classes, especially in PMML processing,
    // may cause this value to change.
    assertThat(typesMI).hasSize(22);
    Optional<KieModule> kieModuleOptional = res.getKieModule();
    assertThat(kieModuleOptional).isPresent();
    assertThat(res.getDependenciesAsURI()).hasSize(4);
    KieModule kModule = kieModuleOptional.get();
    KieModuleMetaData kieModuleMetaData = new KieModuleMetaDataImpl((InternalKieModule) kModule, res.getDependenciesAsURI());
    assertThat(kieModuleMetaData).isNotNull();
}
Also used : Path(org.uberfire.java.nio.file.Path) EnumSet(java.util.EnumSet) Set(java.util.Set) KieCompilationResponse(org.kie.workbench.common.services.backend.compiler.impl.kie.KieCompilationResponse) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) KieModuleMetaDataImpl(org.kie.scanner.KieModuleMetaDataImpl) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) 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) AFCompiler(org.kie.workbench.common.services.backend.compiler.AFCompiler) CompilationRequest(org.kie.workbench.common.services.backend.compiler.CompilationRequest) 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 7 with TypeMetaInfo

use of org.drools.core.rule.TypeMetaInfo 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 8 with TypeMetaInfo

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

the class KieModuleMetaDataTest method testKieModuleMetaDataInMemory.

private void testKieModuleMetaDataInMemory(boolean useTypeDeclaration) throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
    ReleaseId releaseId = ks.newReleaseId("org.kie", "metadata-test", "1.0-SNAPSHOT");
    InternalKieModule kieModule = createKieJarWithClass(ks, releaseId, useTypeDeclaration, 2, 7, dependency);
    KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
    checkDroolsCoreDep(kieModuleMetaData);
    Collection<String> testClasses = kieModuleMetaData.getClasses("org.kie.test");
    assertEquals(1, testClasses.size());
    assertEquals("Bean", testClasses.iterator().next());
    Class<?> beanClass = kieModuleMetaData.getClass("org.kie.test", "Bean");
    assertNotNull(beanClass.getMethod("getValue"));
    TypeMetaInfo beanTypeInfo = kieModuleMetaData.getTypeMetaInfo(beanClass);
    assertNotNull(beanTypeInfo);
    assertTrue(beanTypeInfo.isEvent());
    Role role = beanClass.getAnnotation(Role.class);
    assertNotNull(role);
    assertEquals(Role.Type.EVENT, role.value());
    assertEquals(useTypeDeclaration, beanTypeInfo.isDeclaredType());
}
Also used : Role(org.kie.api.definition.type.Role) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 9 with TypeMetaInfo

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

the class KieModuleMetaDataTest method testKieModuleMetaDataInMemoryWithJavaClassDefaultPackage.

@Test
public void testKieModuleMetaDataInMemoryWithJavaClassDefaultPackage() throws Exception {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "javaDefaultPackage", "1.0-SNAPSHOT");
    final KieModuleModel kproj = ks.newKieModuleModel();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML()).writePomXML(generatePomXml(releaseId)).write("src/main/java/test/Bean.java", createJavaSource());
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    final List<Message> messages = kieBuilder.buildAll().getResults().getMessages();
    assertTrue(messages.isEmpty());
    final KieModule kieModule = kieBuilder.getKieModule();
    final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
    // The call to kieModuleMetaData.getClass() assumes a Java file has an explicit package
    final Class<?> beanClass = kieModuleMetaData.getClass("", "test.Bean");
    assertNotNull(beanClass);
    final TypeMetaInfo beanMetaInfo = kieModuleMetaData.getTypeMetaInfo(beanClass);
    assertNotNull(beanMetaInfo);
}
Also used : TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 10 with TypeMetaInfo

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

the class KieModuleMetaDataTest method testKieModuleMetaDataForDependenciesInMemory.

private void testKieModuleMetaDataForDependenciesInMemory(boolean useTypeDeclaration) throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
    ReleaseId releaseId = ks.newReleaseId("org.kie", "metadata-test", "1.0-SNAPSHOT");
    InternalKieModule kieModule = createKieJarWithClass(ks, releaseId, useTypeDeclaration, 2, 7, dependency);
    KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
    checkDroolsCoreDep(kieModuleMetaData);
    Collection<String> testClasses = kieModuleMetaData.getClasses("org.drools");
    assertEquals(55, testClasses.size());
    Class<?> beanClass = kieModuleMetaData.getClass("org.drools", "QueryResult");
    assertNotNull(beanClass);
    // Classes in dependencies should have TypeMetaInfo
    TypeMetaInfo beanTypeInfo = kieModuleMetaData.getTypeMetaInfo(beanClass);
    assertNotNull(beanTypeInfo);
    if (useTypeDeclaration) {
        assertTrue(beanTypeInfo.isEvent());
    }
    assertEquals(useTypeDeclaration, beanTypeInfo.isDeclaredType());
}
Also used : TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Aggregations

TypeMetaInfo (org.drools.core.rule.TypeMetaInfo)11 Test (org.junit.Test)6 KieServices (org.kie.api.KieServices)6 KieModule (org.kie.api.builder.KieModule)5 KieModuleMetaData (org.kie.scanner.KieModuleMetaData)5 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)4 KieBuilder (org.kie.api.builder.KieBuilder)4 KieFileSystem (org.kie.api.builder.KieFileSystem)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 KieModuleMetaInfo (org.drools.core.rule.KieModuleMetaInfo)3 ReleaseId (org.kie.api.builder.ReleaseId)3 KieModuleModel (org.kie.api.builder.model.KieModuleModel)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 InternalKnowledgeBuilder (org.drools.compiler.builder.InternalKnowledgeBuilder)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2 ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2