InsertLogical

From Training Material
Jump to navigation Jump to search

Test01.java

package com.nobleprog;

import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.event.KieRuntimeEventManager;
import org.kie.api.logger.KieRuntimeLogger;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.logger.KnowledgeRuntimeLoggerFactory;

import com.nobleprog.FactModel.Emp;

public class Test01 {
	@Test
	public void test1() {
		KieServices ks = KieServices.Factory.get();
		KieContainer kContainer = ks.getKieClasspathContainer();
		KieSession ksession = kContainer.newKieSession("ksession-rules");

		/**
		 * Prepare Data
		 */
		ksession.insert(new Emp(7839, "BUSH", "PRESIDENT", 0, 1981, 5000, 10));
		ksession.insert(new Emp(7782, "MERKEL", "MANAGER", 7839, 1981, 2450, 10));
		ksession.insert(new Emp(7566, "PUTIN", "MANAGER", 7839, 1981, 2975, 20));
		ksession.insert(new Emp(7654, "CHIRACK", "SALESMAN", 7698, 1981, 1250,30));
		ksession.insert(new Emp(7499, "BAROSSO", "SALESMAN", 7698, 1981, 1600,30));
		ksession.insert(new Emp(7844, "GATES", "SALESMAN", 7698, 1981, 1500, 30));
		ksession.insert(new Emp(7900, "BUFFETT", "CLERK", 7698, 1981, 950, 30));
		ksession.insert(new Emp(7521, "WALTON", "SALESMAN", 7698, 1981, 1250,30));
		ksession.insert(new Emp(7902, "TOOSK", "ANALYST", 7566, 1981, 3000, 20));
		ksession.insert(new Emp(7369, "THATCHER", "CLERK", 7902, 1980, 800, 20));
		ksession.insert(new Emp(7788, "CARNEGIE", "ANALYST", 7566, 1982, 3000,20));
		ksession.insert(new Emp(7876, "FORD", "CLERK", 7788, 1983, 1100, 20));
		ksession.insert(new Emp(7934, "ELISON", "CLERK", 7782, 1982, 1300, 10));

		Emp $blair = new Emp(7698, "BLAIR", "MANAGER", 7839, 1981, 2850, 30);
		FactHandle $blairFH = ksession.insert($blair);

		ksession.fireAllRules();
		KieRuntimeLogger logger =
				KnowledgeRuntimeLoggerFactory.newFileLogger((KieRuntimeEventManager) ksession, "/tmp/logicalinsert");


		$blair.setJob("CLEANER");
		ksession.update($blairFH, $blair);
		ksession.fireAllRules();

		//Use Audit view (/tmp/logicalinsert.log) to see whether Blair access to BusinessLounge has been removed
		
		logger.close();
	}
}