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();
}
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);
}
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());
}
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);
}
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());
}
Aggregations