Search in sources :

Example 1 with KieModuleMetaData

use of org.kie.scanner.KieModuleMetaData 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 KieModuleMetaData

use of org.kie.scanner.KieModuleMetaData 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 KieModuleMetaData

use of org.kie.scanner.KieModuleMetaData in project kie-wb-common by kiegroup.

the class Builder method build.

public BuildResults build() {
    final BuildResults results = new BuildResults(projectGAV);
    synchronized (kieFileSystem) {
        // KieBuilder is not re-usable for successive "full" builds
        kieBuilder = createKieBuilder(kieFileSystem);
        // Record RTEs from KieBuilder - that can fail if a rule uses an inaccessible class
        try {
            final Results kieResults = ((InternalKieBuilder) kieBuilder).buildAll(classFilter).getResults();
            results.addAllBuildMessages(convertMessages(kieResults.getMessages(), handles));
        } catch (LinkageError e) {
            final String msg = MessageFormat.format(ERROR_CLASS_NOT_FOUND, e.getLocalizedMessage());
            logger.warn(msg);
            results.addBuildMessage(makeWarningMessage(msg));
        } catch (Throwable e) {
            final String msg = e.getLocalizedMessage();
            logger.error(msg, e);
            results.addBuildMessage(makeErrorMessage(msg));
        } finally {
            final PomModel pomModel = ((KieBuilderImpl) kieBuilder).getPomModel();
            if (pomModel != null) {
                pomModelCache.setEntry(project, pomModel);
            }
        }
    }
    // Add validate messages from external helpers
    for (Map.Entry<Path, BuildValidationHelper> e : nonKieResourceValidationHelpers.entrySet()) {
        final org.uberfire.backend.vfs.Path vfsPath = Paths.convert(e.getKey());
        final List<ValidationMessage> validationMessages = e.getValue().validate(vfsPath);
        nonKieResourceValidationHelperMessages.put(e.getKey(), validationMessages);
        results.addAllBuildMessages(convertValidationMessages(validationMessages));
    }
    // Check external imports are available. These are loaded when a DMO is requested, but it's better to report them early
    final Path nioExternalImportsPath = projectRoot.resolve("project.imports");
    if (Files.exists(nioExternalImportsPath)) {
        final org.uberfire.backend.vfs.Path externalImportsPath = Paths.convert(nioExternalImportsPath);
        final ProjectImports projectImports = importsService.load(externalImportsPath);
        final Imports imports = projectImports.getImports();
        for (final Import item : imports.getImports()) {
            final String fullyQualifiedClassName = item.getType();
            try {
                Class clazz = this.getClass().getClassLoader().loadClass(item.getType());
            } catch (ClassNotFoundException cnfe) {
                logger.warn(cnfe.getMessage());
                final String msg = MessageFormat.format(ERROR_CLASS_NOT_FOUND, fullyQualifiedClassName);
                results.addBuildMessage(makeWarningMessage(msg));
            }
        }
    }
    // Store the project dependencies ClassLoader for optimization purposes.
    final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(getKieModuleIgnoringErrors(), DependencyFilter.COMPILE_FILTER);
    updateDependenciesClassLoader(project, kieModuleMetaData);
    results.addAllBuildMessages(verifyClasses(kieModuleMetaData));
    return results;
}
Also used : Path(org.uberfire.java.nio.file.Path) BuildValidationHelper(org.guvnor.common.services.project.builder.service.BuildValidationHelper) BuildResults(org.guvnor.common.services.project.builder.model.BuildResults) IncrementalBuildResults(org.guvnor.common.services.project.builder.model.IncrementalBuildResults) KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) ValidationMessage(org.guvnor.common.services.shared.validation.model.ValidationMessage) Import(org.kie.soup.project.datamodel.imports.Import) ProjectImports(org.guvnor.common.services.project.model.ProjectImports) ProjectImports(org.guvnor.common.services.project.model.ProjectImports) Imports(org.kie.soup.project.datamodel.imports.Imports) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) IncrementalResults(org.kie.internal.builder.IncrementalResults) BuildResults(org.guvnor.common.services.project.builder.model.BuildResults) Results(org.kie.api.builder.Results) IncrementalBuildResults(org.guvnor.common.services.project.builder.model.IncrementalBuildResults) PomModel(org.appformer.maven.support.PomModel) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with KieModuleMetaData

use of org.kie.scanner.KieModuleMetaData in project kie-wb-common by kiegroup.

the class KieMetadataTest method compileAndloadKieJarSingleMetadataWithPackagedJar.

@Test
@Ignore("https://issues.redhat.com/browse/AF-2892")
public void compileAndloadKieJarSingleMetadataWithPackagedJar() throws Exception {
    /**
     * If the test fail check if the Drools core classes used, KieModuleMetaInfo and TypeMetaInfo implements Serializable
     */
    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();
// comment if you want read the log file after the test run
}
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) 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 5 with KieModuleMetaData

use of org.kie.scanner.KieModuleMetaData 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

KieModuleMetaData (org.kie.scanner.KieModuleMetaData)9 Test (org.junit.Test)7 KieModule (org.kie.api.builder.KieModule)6 TypeMetaInfo (org.drools.core.rule.TypeMetaInfo)5 Path (org.uberfire.java.nio.file.Path)4 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)3 KieModuleMetaInfo (org.drools.core.rule.KieModuleMetaInfo)3 Ignore (org.junit.Ignore)3 KieServices (org.kie.api.KieServices)3 KieBuilder (org.kie.api.builder.KieBuilder)3 KieFileSystem (org.kie.api.builder.KieFileSystem)3 KieModuleMetaDataImpl (org.kie.scanner.KieModuleMetaDataImpl)3 DefaultCompilationRequest (org.kie.workbench.common.services.backend.compiler.impl.DefaultCompilationRequest)3 WorkspaceCompilationInfo (org.kie.workbench.common.services.backend.compiler.impl.WorkspaceCompilationInfo)3 KieCompilationResponse (org.kie.workbench.common.services.backend.compiler.impl.kie.KieCompilationResponse)3 BuildResults (org.guvnor.common.services.project.builder.model.BuildResults)2 IncrementalBuildResults (org.guvnor.common.services.project.builder.model.IncrementalBuildResults)2 AFCompiler (org.kie.workbench.common.services.backend.compiler.AFCompiler)2 CompilationRequest (org.kie.workbench.common.services.backend.compiler.CompilationRequest)2 URL (java.net.URL)1