org.kie.api.builder.Message.Level Java Examples

The following examples show how to use org.kie.api.builder.Message.Level. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KieBuilderImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private static void buildKieProject( ResultsImpl messages,
                                     KieModuleKieProject kProject,
                                     MemoryFileSystem trgMfs ) {
    kProject.init();
    kProject.verify( messages );

    if ( messages.filterMessages( Level.ERROR ).isEmpty() ) {
        InternalKieModule kModule = kProject.getInternalKieModule();
        if ( trgMfs != null ) {
            new KieMetaInfoBuilder( kModule ).writeKieModuleMetaInfo( trgMfs );
            kProject.writeProjectOutput(trgMfs, messages);
        }
        KieRepository kieRepository = KieServices.Factory.get().getRepository();
        kieRepository.addKieModule( kModule );
        for ( InternalKieModule kDep : kModule.getKieDependencies().values() ) {
            kieRepository.addKieModule( kDep );
        }
    }
}
 
Example #2
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPomXmlContent() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";

    KieModuleModel kProj = createKieProject(namespace);
    
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0");
    
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.write( "pom.xml", "xxxx" );
    generateKProjectXML( kfs, namespace, kProj );
    generateMessageClass( kfs, namespace );
    generateRule( kfs, namespace );        
      
    KieBuilder kieBuilder = createKieBuilder(kfs);
    kieBuilder.buildAll();
    assertTrue ( kieBuilder.getResults().hasMessages(Level.ERROR) );
}
 
Example #3
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPomXmlGAV() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";

    KieModuleModel kProj = createKieProject(namespace);
    
    ReleaseId releaseId = new ReleaseIdImpl( "", "", "" );
    
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML( kfs, releaseId );
    
    generateMessageClass( kfs, namespace );
    generateRule( kfs, namespace );
    
    MemoryFileSystem mfs = ((KieFileSystemImpl)kfs).asMemoryFileSystem();
      
    KieBuilder kieBuilder = createKieBuilder( kfs );
    kieBuilder.buildAll();
    assertTrue( kieBuilder.getResults().hasMessages( Level.ERROR ) );
}
 
Example #4
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";

    KieModuleModel kProj = createKieProject( namespace );
    
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId( namespace, "memory", "1.0" );
    
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML( kfs, releaseId );
    kfs.writeKModuleXML( "xxxx" );
    generateMessageClass( kfs, namespace );
    generateRule( kfs, namespace );
    
    KieBuilder kieBuilder = createKieBuilder(kfs);
    kieBuilder.buildAll();
    assertTrue( kieBuilder.getResults().hasMessages( Level.ERROR ) );
}
 
Example #5
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void createAndTestKieContainer(ReleaseId releaseId, KieBuilder kb, String kBaseName) throws IOException,
        ClassNotFoundException,
        InterruptedException {
    KieServices ks = KieServices.Factory.get();
    
    kb.buildAll();
    
    if ( kb.getResults().hasMessages(Level.ERROR) ) {
        fail("Unable to build KieModule\n" + kb.getResults( ).toString() );
    }
    KieRepository kr = ks.getRepository();
    KieModule kJar = kr.getKieModule(releaseId);
    assertNotNull( kJar );
    
    KieContainer kContainer = ks.newKieContainer(releaseId);
    KieBase kBase = kBaseName != null ? kContainer.getKieBase( kBaseName ) : kContainer.getKieBase();

    KieSession kSession = kBase.newKieSession();
    List list = new ArrayList();
    kSession.setGlobal( "list", list );
    kSession.fireAllRules();

    assertEquals( 1, list.size() );
    assertEquals( "org.kie.test.Message", list.get(0).getClass().getName() );       
}
 
Example #6
Source File: DeleteTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() {
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.write(KieServices.Factory.get().getResources()
            .newClassPathResource(DELETE_TEST_DRL, DeleteTest.class));

    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();

    List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);
    assertThat(res).isEmpty();

    KieBase kbase = KieServices.Factory.get()
            .newKieContainer(kbuilder.getKieModule().getReleaseId())
            .getKieBase();

    ksession = kbase.newKieSession();
}
 
Example #7
Source File: KieBuilderImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private PomModel buildPomModel() {
    pomXml = getOrGeneratePomXml( srcMfs );
    if ( pomXml == null ) {
        // will be null if the provided pom is invalid
        return null;
    }
    try {
        PomModel tempPomModel = PomModel.Parser.parse( "pom.xml",
                                                       new ByteArrayInputStream( pomXml ) );
        validatePomModel( tempPomModel ); // throws an exception if invalid
        return tempPomModel;
    } catch ( Exception e ) {
        results.addMessage( Level.ERROR,
                            "pom.xml",
                            "maven pom.xml found, but unable to read\n" + e.getMessage() );
    }
    return null;
}
 
Example #8
Source File: KieBuilderImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private void buildKieModuleModel() {
    if ( srcMfs.isAvailable( KieModuleModelImpl.KMODULE_SRC_PATH ) ) {
        kModuleModelXml = srcMfs.getBytes( KieModuleModelImpl.KMODULE_SRC_PATH );
        try {
            kModuleModel = KieModuleModelImpl.fromXML( new ByteArrayInputStream( kModuleModelXml ) );
        } catch ( Exception e ) {
            results.addMessage( Level.ERROR,
                                "kmodule.xml",
                                "kmodule.xml found, but unable to read\n" + e.getMessage() );
            // Create a default kModuleModel in the event of errors parsing the XML
            kModuleModel = KieServices.Factory.get().newKieModuleModel();
        }
    } else {
        // There's no kmodule.xml, create a default one
        kModuleModel = KieServices.Factory.get().newKieModuleModel();
    }
    
    if ( setDefaultsforEmptyKieModule( kModuleModel ) ) {
        kModuleModelXml = kModuleModel.toXML().getBytes( IoUtils.UTF8_CHARSET );
    }
}
 
Example #9
Source File: KieContainerImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public KieBase getKieBase(String kBaseName) {
    KieBase kBase = kBases.get( kBaseName );
    if ( kBase == null ) {
        KieBaseModelImpl kBaseModel = getKieBaseModelImpl(kBaseName);
        synchronized (kBaseModel) {
            kBase = kBases.get( kBaseName );
            if ( kBase == null ) {
                ResultsImpl msgs = new ResultsImpl();
                kBase = createKieBase(kBaseModel, kProject, msgs, null);
                if (kBase == null) {
                    // build error, throw runtime exception
                    throw new RuntimeException("Error while creating KieBase" + msgs.filterMessages(Level.ERROR));
                }
                kBases.put(kBaseName, kBase);
            }
        }
    }
    return kBase;
}
 
Example #10
Source File: JbpmBpmn2TestCase.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
protected KieBase createKnowledgeBaseFromResources(Resource... process)
        throws Exception {

    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    if (process.length > 0) {
        KieFileSystem kfs = ks.newKieFileSystem();

        for (Resource p : process) {
            kfs.write(p);
        }

        KieBuilder kb = ks.newKieBuilder(kfs);

        kb.buildAll(); // kieModule is automatically deployed to KieRepository
                       // if successfully built.

        if (kb.getResults().hasMessages(Level.ERROR)) {
            throw new RuntimeException("Build Errors:\n"
                    + kb.getResults().toString());
        }
    }

    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    return kContainer.getKieBase();
}
 
Example #11
Source File: KieContainerImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public KieBase newKieBase(String kBaseName, KieBaseConfiguration conf) {
    ResultsImpl msgs = new ResultsImpl();
    KieBase kBase = createKieBase(getKieBaseModelImpl(kBaseName), kProject, msgs, conf);
    if ( kBase == null ) {
        // build error, throw runtime exception
        throw new RuntimeException( "Error while creating KieBase" + msgs.filterMessages( Level.ERROR  ) );
    }
    return kBase;
}
 
Example #12
Source File: KieBuilderImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public KieBuilder buildAll( BiFunction<InternalKieModule, ClassLoader, KieModuleKieProject> kprojectSupplier, Predicate<String> classFilter ) {
    PomModel pomModel = init();

    // kModuleModel will be null if a provided pom.xml or kmodule.xml is invalid
    if ( !isBuilt() && kModuleModel != null ) {
        trgMfs = new MemoryFileSystem();
        writePomAndKModule();
        addKBasesFilesToTrg();
        markSource();

        MemoryKieModule memoryKieModule = new MemoryKieModule( adapt( releaseId ), kModuleModel, trgMfs );

        if ( kieDependencies != null && !kieDependencies.isEmpty() ) {
            for ( KieModule kieModule : kieDependencies ) {
                memoryKieModule.addKieDependency( (InternalKieModule) kieModule );
            }
        }
        if ( pomModel != null ) {
            memoryKieModule.setPomModel( pomModel );
        }

        KieModuleKieProject kProject = kprojectSupplier.apply( memoryKieModule, classLoader );
        for ( ReleaseId unresolvedDep : memoryKieModule.getUnresolvedDependencies() ) {
            results.addMessage( Level.ERROR, "pom.xml", "Unresolved dependency " + unresolvedDep );
        }
        
        compileJavaClasses( kProject.getClassLoader(), classFilter );

        buildKieProject( results, kProject, trgMfs );
        kModule = kProject.getInternalKieModule();
    }
    return this;
}
 
Example #13
Source File: KieBuilderImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private KieModule getKieModule( boolean ignoreErrors ) {
    if ( !isBuilt() ) {
        buildAll();
    }

    if ( !ignoreErrors && ( getResults().hasMessages( Level.ERROR ) || kModule == null ) ) {
        throw new RuntimeException( "Unable to get KieModule, Errors Existed: " + getResults() );
    }
    return kModule;
}
 
Example #14
Source File: KieContainerImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Results updateToVersion(ReleaseId newReleaseId) {
    checkNotClasspathKieProject();
    Results results = update(((KieModuleKieProject) kProject).getInternalKieModule(), newReleaseId);
    if (results != null) {
        containerReleaseId = newReleaseId;
    } else {
        results = new ResultsImpl();
        ( (ResultsImpl) results ).addMessage( Message.Level.ERROR, null, "Cannot find KieModule with ReleaseId: " + newReleaseId );
    }
    return results;
}
 
Example #15
Source File: Message.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Message(Level level, MessageCategory category, String type, String sourceId, String text, MessageFEELEvent feelEvent, MessageExceptionField exception) {
    this.level = level;
    this.category = category;
    this.type = type;
    this.sourceId = sourceId;
    this.text = text;
    this.feelEvent = feelEvent;
    this.exception = exception;
}
 
Example #16
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateToNonExistingRelease() {
    // DROOLS-1562
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-release", "1.0.0");
    createAndDeployJar( ks, releaseId, createDRL("ruleA") );

    KieContainer kieContainer = ks.newKieContainer(releaseId);

    Results results = kieContainer.updateToVersion( ks.newReleaseId( "org.kie", "test-release", "1.0.1" ) );
    assertEquals( 1, results.getMessages( Level.ERROR ).size() );
    assertEquals( "1.0.0", ( (InternalKieContainer) kieContainer ).getContainerReleaseId().getVersion() );
}
 
Example #17
Source File: RuleEngine.java    From support-rulesengine with Apache License 2.0 5 votes vote down vote up
private void initKie() {
  KieServices ks = KieServices.Factory.get();
  kfs = ks.newKieFileSystem();
  uploadDroolFiles();
  KieBuilder kbuilder = ks.newKieBuilder(kfs);
  kbuilder.buildAll();
  if (kbuilder.getResults().hasMessages(Level.ERROR)) {
    throw new IllegalArgumentException(kbuilder.getResults().toString());
  }
  KieContainer kcontainer = ks.newKieContainer(kbuilder.getKieModule().getReleaseId());
  KieBaseConfiguration kbConfig = KieServices.Factory.get().newKieBaseConfiguration();
  kbConfig.setOption(ConstraintJittingThresholdOption.get(-1));
  kbase = kcontainer.newKieBase(kbConfig);
}
 
Example #18
Source File: DroolsUtil.java    From qzr with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new {@link KieServices} using a collection of resources. It
 * does this by creating a new virtual file system and adding the resources
 * to it.
 * 
 * @param resources
 *            An array of {@link DroolsResource} indicating where the
 *            various resources should be loaded from. These could be
 *            classpath, file or URL resources.
 * @return A new {@link KieServices}.
 * @throws KieBuildException I there are errors whilst building the {@link KieServices}.
 */
public static KieServices createAndBuildKieServices(DroolsResource[] resources) throws KieBuildException {
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kfs = kieServices.newKieFileSystem();
    
    for (DroolsResource resource : resources) {
        log.info("Resource: " + resource.getType() + ", path type="
                + resource.getPathType() + ", path=" + resource.getPath());
        switch (resource.getPathType()) {
        case CLASSPATH:
            kfs.write(ResourceFactory.newClassPathResource(resource.getPath()));
            break;
        case FILE:
            kfs.write(ResourceFactory.newFileResource(resource.getPath()));
            break;
        case URL:
            UrlResource urlResource = (UrlResource) ResourceFactory
                    .newUrlResource(resource.getPath());
            
            if (resource.getUsername() != null) {
                log.info("Setting authentication for: " + resource.getUsername());
                urlResource.setBasicAuthentication("enabled");
                urlResource.setUsername(resource.getUsername());
                urlResource.setPassword(resource.getPassword());
            }
            
            kfs.write(urlResource);
            
            break;
        default:
            throw new IllegalArgumentException(
                    "Unable to build this resource path type.");
        }
    }
    
    KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
    
    // The KieBuilder contains a collection of messages, which is built up
    // as it does its job. If any of these have a level of 'ERROR', then the
    // rules did not compile correctly. Therefore we log such messages and 
    // throw an exception to indicate failure.
    if (kieBuilder.getResults().hasMessages(Level.ERROR)) {
        List<Message> errors = kieBuilder.getResults().getMessages(Level.ERROR);
        StringBuilder sb = new StringBuilder("Errors:");
        for (Message msg : errors) {
            sb.append("\n  " + prettyBuildMessage(msg));
        }
        throw new KieBuildException(sb.toString());
    }
    
    return kieServices;
}
 
Example #19
Source File: Message.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public Level getLevel() {
    return level;
}
 
Example #20
Source File: KieSessionFactory.java    From NiFi-Rule-engine-processor with Apache License 2.0 4 votes vote down vote up
public static StatelessKieSession getNewKieSession(String drlFileName) {
	KieServices kieServices = KieServices.Factory.get();
	
	KieResources kieResources = kieServices.getResources();
	KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
	KieRepository kieRepository = kieServices.getRepository();

	Resource resource = kieResources.newFileSystemResource(drlFileName);
	kieFileSystem.write(resource);

	KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);

	kb.buildAll();

	if (kb.getResults().hasMessages(Level.ERROR)) {
		throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
	}

	KieContainer kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
	
	return kContainer.newStatelessKieSession();
}
 
Example #21
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testKieModuleDependencies() throws ClassNotFoundException, InterruptedException, IOException {
    KieServices ks = KieServices.Factory.get();
    
    String namespace1 = "org.kie.test1";
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId(namespace1, "memory", "1.0");
    KieModuleModel kProj1 = createKieProject(namespace1);        
    KieFileSystem kfs1 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs1, namespace1, releaseId1, kProj1);

    KieBuilder kb1 = createKieBuilder(kfs1);
    kb1.buildAll();        
    if ( kb1.getResults().hasMessages(Level.ERROR) ) {
        fail("Unable to build KieJar\n" + kb1.getResults( ).toString() );
    }
    KieRepository kr = ks.getRepository();
    KieModule kModule1 = kr.getKieModule(releaseId1);
    assertNotNull( kModule1 );
    
    
    String namespace2 = "org.kie.test2";
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId(namespace2, "memory", "1.0");
    KieModuleModel kProj2 = createKieProject(namespace2);        
    KieBaseModelImpl kieBase2 = ( KieBaseModelImpl ) kProj2.getKieBaseModels().get( namespace2 );
    kieBase2.addInclude( namespace1 );
    
    KieFileSystem kfs2 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs2, namespace2, releaseId2, kProj2);
    

    KieBuilder kb2 = createKieBuilder(kfs2);
    kb2.setDependencies( kModule1 );
    kb2.buildAll();        
    if ( kb2.getResults().hasMessages(Level.ERROR) ) {
        fail("Unable to build KieJar\n" + kb2.getResults( ).toString() );
    }
    KieModule kModule2= kr.getKieModule(releaseId2);
    assertNotNull( kModule2);
    
    KieContainer kContainer = ks.newKieContainer(releaseId2);
    KieBase kBase = kContainer.getKieBase( namespace2 );
    
    KieSession kSession = kBase.newKieSession();
    List list = new ArrayList();
    kSession.setGlobal( "list", list );
    kSession.fireAllRules();

    assertEquals( 2, list.size() );
    if ("org.kie.test1.Message".equals(list.get(0).getClass().getName())) {
        assertEquals( "org.kie.test2.Message", list.get(1).getClass().getName() );
    } else {
        assertEquals( "org.kie.test2.Message", list.get(0).getClass().getName() );
        assertEquals( "org.kie.test1.Message", list.get(1).getClass().getName() );
    }
}
 
Example #22
Source File: ResultsImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public List<Message> filterMessages(Level... levels) {
    return MessageImpl.filterMessages( messages,
                                       levels );
}
 
Example #23
Source File: ResultsImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public InternalMessage addMessage(Level level, String path, String text) {
    InternalMessage message = new MessageImpl(idGenerator++, level, path, text);
    messages.add( message );
    return message;
}
 
Example #24
Source File: ResultsImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public List<Message> getMessages(Level... levels) {
    return filterMessages( levels );
}
 
Example #25
Source File: ResultsImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public boolean hasMessages(Level... levels) {
    return !filterMessages( levels ).isEmpty();
}
 
Example #26
Source File: WindowTest.java    From kogito-runtimes with Apache License 2.0 3 votes vote down vote up
@BeforeEach
public void initialization() {
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();

    kfs.write("src/main/resources/kbase1/window_test.drl", drl);

    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);

    kbuilder.buildAll();

    List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);

    assertEquals(0, res.size(), res.toString());

    KieBaseConfiguration kbconf = KnowledgeBaseFactory
            .newKnowledgeBaseConfiguration();

    kbconf.setOption(EventProcessingOption.STREAM);

    KieBase kbase = KieServices.Factory.get()
                               .newKieContainer(kbuilder.getKieModule().getReleaseId())
                               .newKieBase(kbconf);

    KieSessionConfiguration ksconfig = KnowledgeBaseFactory
            .newKnowledgeSessionConfiguration();
    ksconfig.setOption(ClockTypeOption.get("pseudo"));

    ksession = kbase.newKieSession(ksconfig, null);

    clock = ksession.getSessionClock();
}
 
Example #27
Source File: Results.java    From kogito-runtimes with Apache License 2.0 2 votes vote down vote up
/**
 * Returns all the Messages of the given levels in these Results
 */
List<Message>  getMessages(Level... levels);
 
Example #28
Source File: Results.java    From kogito-runtimes with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if these Results contains at least one Message of one of the given levels
 */
boolean hasMessages(Level... levels);