Java Code Examples for org.kie.internal.runtime.StatefulKnowledgeSession#insert()

The following examples show how to use org.kie.internal.runtime.StatefulKnowledgeSession#insert() . 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: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public List executeWithResults(Iterable objects, ObjectFilter filter) {
    List list = new ArrayList();
    StatefulKnowledgeSession ksession = newWorkingMemory();
    try {
        for ( Object object : objects ) {
            ksession.insert( object );
        }
        ksession.fireAllRules();
        for (FactHandle fh : ksession.getFactHandles(filter)) {
            list.add(((InternalFactHandle) fh).getObject());
        }
    } finally {
        dispose(ksession);
    }
    return list;
}
 
Example 2
Source File: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public void execute(Object object) {
    StatefulKnowledgeSession ksession = newWorkingMemory();
    try {
        ksession.insert( object );
        ksession.fireAllRules();
    } finally {
        dispose(ksession);
    }
}
 
Example 3
Source File: StatelessKnowledgeSessionImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public void execute(Iterable objects) {
    StatefulKnowledgeSession ksession = newWorkingMemory();
    try {
        for ( Object object : objects ) {
            ksession.insert( object );
        }
        ksession.fireAllRules();
    } finally {
        dispose(ksession);
    }
}
 
Example 4
Source File: DdLoadTest.java    From drools-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	//rule,rule2可以放在数据库中,有个唯一code和他们对于,代码要执行规则的时候,根据code从数据库获取出来就OK了,这样自己开发的规则管理系统那边对数据库里的规则进行维护就行了
	String rule = "package com.neo.drools\r\n";
	rule += "import com.neo.drools.model.Message;\r\n";
	rule += "rule \"rule1\"\r\n";
	rule += "\twhen\r\n";
	rule += "Message( status == 1, myMessage : msg )";
	rule += "\tthen\r\n";
	rule += "\t\tSystem.out.println( 1+\":\"+myMessage );\r\n";
	rule += "end\r\n";


	String rule2 = "package com.neo.drools\r\n";
	rule += "import com.neo.drools.model.Message;\r\n";

	rule += "rule \"rule2\"\r\n";
	rule += "\twhen\r\n";
	rule += "Message( status == 2, myMessage : msg )";
	rule += "\tthen\r\n";
	rule += "\t\tSystem.out.println( 2+\":\"+myMessage );\r\n";
	rule += "end\r\n";


	StatefulKnowledgeSession kSession = null;
	try {


		KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
		//装入规则,可以装入多个
		kb.add(ResourceFactory.newByteArrayResource(rule.getBytes("utf-8")), ResourceType.DRL);
		kb.add(ResourceFactory.newByteArrayResource(rule2.getBytes("utf-8")), ResourceType.DRL);

		KnowledgeBuilderErrors errors = kb.getErrors();
		for (KnowledgeBuilderError error : errors) {
			System.out.println(error);
		}
		KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
		kBase.addKnowledgePackages(kb.getKnowledgePackages());

		kSession = kBase.newStatefulKnowledgeSession();


		Message message1 = new Message();
		message1.setStatus(1);
		message1.setMsg("hello world!");

		Message message2 = new Message();
		message2.setStatus(2);
		message2.setMsg("hi world!");

		kSession.insert(message1);
		kSession.insert(message2);
		kSession.fireAllRules();

	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} finally {
		if (kSession != null)
			kSession.dispose();
	}

}