use of org.drools.core.rule.TypeMetaInfo in project drools by kiegroup.
the class JavaBeansEventRoleTest method testBeanAndDeclarativeTypeInSamePackage.
@Test
public void testBeanAndDeclarativeTypeInSamePackage() throws Exception {
String declaration = "package org.drools.compiler;\n" + "public class Bean {}";
String drl = "package org.drools.compiler;\n" + "declare Bean\n" + " @role(event)\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/java/org/drools/compiler/Bean.java", declaration).write("src/main/resources/bean1.drl", drl);
final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
final KieModule kieModule = kieBuilder.buildAll().getKieModule();
final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
final String packageName = "org.drools.compiler";
final String className = "Bean";
final Class clazz = kieModuleMetaData.getClass(packageName, className);
final TypeMetaInfo typeMetaInfo = kieModuleMetaData.getTypeMetaInfo(clazz);
assertTrue(typeMetaInfo.isEvent());
}
use of org.drools.core.rule.TypeMetaInfo in project drools by kiegroup.
the class JavaBeansEventRoleTest method testImportBean.
@Test
public void testImportBean() throws Exception {
String declaration = "package org.drools.compiler;\n" + "public class Bean {}";
String drl = "package some.other.package;\n" + "import org.drools.compiler.Bean;\n" + "declare Bean\n" + " @role(event)\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/java/org/drools/compiler/Bean.java", declaration).write("src/main/resources/bean1.drl", drl);
final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
final KieModule kieModule = kieBuilder.buildAll().getKieModule();
final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
final String packageName = "org.drools.compiler";
final String className = "Bean";
final Class clazz = kieModuleMetaData.getClass(packageName, className);
final TypeMetaInfo typeMetaInfo = kieModuleMetaData.getTypeMetaInfo(clazz);
assertTrue(typeMetaInfo.isEvent());
}
use of org.drools.core.rule.TypeMetaInfo in project drools by kiegroup.
the class KieMetaInfoBuilder 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);
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);
}
}
}
return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
use of org.drools.core.rule.TypeMetaInfo in project drools by kiegroup.
the class AbstractKieModule method isTypeDeclaration.
private boolean isTypeDeclaration(String fileName) {
Map<String, TypeMetaInfo> info = getTypesMetaInfo();
TypeMetaInfo typeInfo = info == null ? null : info.get(convertResourceToClassName(fileName));
return typeInfo != null && typeInfo.isDeclaredType();
}
use of org.drools.core.rule.TypeMetaInfo in project kie-wb-common by kiegroup.
the class BuilderTest method testBuilderKModuleHasDependencyMetaData.
@Test
public void testBuilderKModuleHasDependencyMetaData() throws Exception {
URL url = this.getClass().getResource("/GuvnorM2RepoDependencyExample2");
SimpleFileSystemProvider p = new SimpleFileSystemProvider();
org.uberfire.java.nio.file.Path path = p.getPath(url.toURI());
final Module module = moduleService.resolveModule(Paths.convert(path));
final Builder builder = new Builder(module, ioService, moduleService, importsService, new ArrayList<>(), dependenciesClassLoaderCache, pomModelCache, getPackageNameAllowListService(), alwaysTrue);
final BuildResults results = builder.build();
// Debug output
if (!results.getMessages().isEmpty()) {
for (BuildMessage m : results.getMessages()) {
logger.debug(m.getText());
}
}
assertTrue(results.getMessages().isEmpty());
final KieModuleMetaData metaData = KieModuleMetaData.Factory.newKieModuleMetaData(builder.getKieModule());
// Check packages
final Set<String> packageNames = new HashSet<>();
final Iterator<String> packageNameIterator = metaData.getPackages().iterator();
while (packageNameIterator.hasNext()) {
packageNames.add(packageNameIterator.next());
}
assertEquals(2, packageNames.size());
assertTrue(packageNames.contains("defaultpkg"));
assertTrue(packageNames.contains("org.kie.workbench.common.services.builder.tests.test1"));
// Check classes
final String packageName = "org.kie.workbench.common.services.builder.tests.test1";
assertEquals(1, metaData.getClasses(packageName).size());
final String className = metaData.getClasses(packageName).iterator().next();
assertEquals("Bean", className);
// Check metadata
final Class clazz = metaData.getClass(packageName, className);
final TypeMetaInfo typeMetaInfo = metaData.getTypeMetaInfo(clazz);
assertNotNull(typeMetaInfo);
assertFalse(typeMetaInfo.isEvent());
}
Aggregations