Search in sources :

Example 1 with TypeMetaInfo

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());
}
Also used : TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) Test(org.junit.Test)

Example 2 with TypeMetaInfo

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());
}
Also used : TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) Test(org.junit.Test)

Example 3 with TypeMetaInfo

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);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassDefinition(org.drools.core.factmodel.ClassDefinition) FactType(org.kie.api.definition.type.FactType) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KiePackage(org.kie.api.definition.KiePackage) 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 4 with TypeMetaInfo

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();
}
Also used : TypeMetaInfo(org.drools.core.rule.TypeMetaInfo)

Example 5 with TypeMetaInfo

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());
}
Also used : BuildResults(org.guvnor.common.services.project.builder.model.BuildResults) IncrementalBuildResults(org.guvnor.common.services.project.builder.model.IncrementalBuildResults) URL(java.net.URL) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) SimpleFileSystemProvider(org.uberfire.java.nio.fs.file.SimpleFileSystemProvider) BuildMessage(org.guvnor.common.services.project.builder.model.BuildMessage) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) Module(org.guvnor.common.services.project.model.Module) HashSet(java.util.HashSet) Test(org.junit.Test)

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