Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.
-------결과 --------
[2016-07-21 18:03:40] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:1:1309129055:1309129055:1:DEFAULT:Server( name=server001, processors=4, memory=8192, diskSpace=2048, virtualizations=null, cpuUsage=3 )] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@563e4951
[2016-07-21 18:03:40] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:2:1826334428:1826334428:2:DEFAULT:BusinessTrip [name=사용자1, hour=3.5, getName()=사용자1, getHour()=3.5, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=1826334428, toString()=com.gooper.drool_test.BusinessTrip@6cdba6dc]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@563e4951
[2016-07-21 18:03:40] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:3:1558103808:1558103808:3:DEFAULT:BusinessTrip [name=사용자2, hour=5.0, getName()=사용자2, getHour()=5.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=1558103808, toString()=com.gooper.drool_test.BusinessTrip@5cdec700]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@563e4951
[2016-07-21 18:03:40] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:4:2024415090:2024415090:4:DEFAULT:BusinessTrip [name=사용자3, hour=8.0, getName()=사용자3, getHour()=8.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=2024415090, toString()=com.gooper.drool_test.BusinessTrip@78aa1f72]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@563e4951
[2016-07-21 18:03:40] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:5:527804008:527804008:5:DEFAULT:BusinessTrip [name=사용자4, hour=10.0, getName()=사용자4, getHour()=10.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=527804008, toString()=com.gooper.drool_test.BusinessTrip@1f75a668]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@563e4951
[2016-07-21 18:03:40] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:6:1266534280:1266534280:6:DEFAULT:BusinessTrip [name=사용자5, hour=13.0, getName()=사용자5, getHour()=13.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=1266534280, toString()=com.gooper.drool_test.BusinessTrip@4b7dc788]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@563e4951
[2016-07-21 18:03:40] [CustomAgendaEventListener] [137] [INFO] Match Created : [fact 0:5:527804008:527804008:5:DEFAULT:BusinessTrip [name=사용자4, hour=10.0, getName()=사용자4, getHour()=10.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=527804008, toString()=com.gooper.drool_test.BusinessTrip@1f75a668]]
[2016-07-21 18:03:40] [CustomAgendaEventListener] [137] [INFO] Match Created : [fact 0:4:2024415090:2024415090:4:DEFAULT:BusinessTrip [name=사용자3, hour=8.0, getName()=사용자3, getHour()=8.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=2024415090, toString()=com.gooper.drool_test.BusinessTrip@78aa1f72]]
[2016-07-21 18:03:40] [CustomAgendaEventListener] [117] [INFO] Before Match Fired : [fact 0:5:527804008:527804008:5:DEFAULT:BusinessTrip [name=사용자4, hour=10.0, getName()=사용자4, getHour()=10.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=527804008, toString()=com.gooper.drool_test.BusinessTrip@1f75a668]]
[2016-07-21 18:03:40] [출장일비(B타입)] [20] [INFO] 사용자4 님의 출장 시간은 10.0 시간 이므로, 출장일비는 12만원 입니다. ( 규정 : 출장일비(B타입) )
[2016-07-21 18:03:40] [CustomAgendaEventListener] [80] [DEBUG] Rule fired: 출장일비(B타입)
[2016-07-21 18:03:40] [CustomAgendaEventListener] [117] [INFO] Before Match Fired : [fact 0:4:2024415090:2024415090:4:DEFAULT:BusinessTrip [name=사용자3, hour=8.0, getName()=사용자3, getHour()=8.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=2024415090, toString()=com.gooper.drool_test.BusinessTrip@78aa1f72]]
[2016-07-21 18:03:40] [출장일비(B타입)] [20] [INFO] 사용자3 님의 출장 시간은 8.0 시간 이므로, 출장일비는 12만원 입니다. ( 규정 : 출장일비(B타입) )
[2016-07-21 18:03:40] [CustomAgendaEventListener] [80] [DEBUG] Rule fired: 출장일비(B타입)
[2016-07-21 18:03:40] [CustomAgendaEventListener] [137] [INFO] Match Created : [fact 0:3:1558103808:1558103808:3:DEFAULT:BusinessTrip [name=사용자2, hour=5.0, getName()=사용자2, getHour()=5.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=1558103808, toString()=com.gooper.drool_test.BusinessTrip@5cdec700]]
[2016-07-21 18:03:40] [CustomAgendaEventListener] [117] [INFO] Before Match Fired : [fact 0:3:1558103808:1558103808:3:DEFAULT:BusinessTrip [name=사용자2, hour=5.0, getName()=사용자2, getHour()=5.0, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=1558103808, toString()=com.gooper.drool_test.BusinessTrip@5cdec700]]
[2016-07-21 18:03:40] [출장일비(C타입)] [20] [INFO] 사용자2 님의 출장 시간은 5.0 시간 이므로, 출장일비는 8만원 입니다. ( 규정 : 출장일비(C타입) )
[2016-07-21 18:03:40] [CustomAgendaEventListener] [80] [DEBUG] Rule fired: 출장일비(C타입)
[2016-07-21 18:03:40] [CustomAgendaEventListener] [137] [INFO] Match Created : [fact 0:2:1826334428:1826334428:2:DEFAULT:BusinessTrip [name=사용자1, hour=3.5, getName()=사용자1, getHour()=3.5, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=1826334428, toString()=com.gooper.drool_test.BusinessTrip@6cdba6dc]]
[2016-07-21 18:03:40] [CustomAgendaEventListener] [117] [INFO] Before Match Fired : [fact 0:2:1826334428:1826334428:2:DEFAULT:BusinessTrip [name=사용자1, hour=3.5, getName()=사용자1, getHour()=3.5, getClass()=class com.gooper.drool_test.BusinessTrip, hashCode()=1826334428, toString()=com.gooper.drool_test.BusinessTrip@6cdba6dc]]
[2016-07-21 18:03:40] [출장일비(없음)] [20] [INFO] 사용자1 님의 출장 시간은 3.5 시간 이므로 출장일비가 없습니다. ( 규정 : 출장일비(없음) )
[2016-07-21 18:03:40] [CustomAgendaEventListener] [80] [DEBUG] Rule fired: 출장일비(없음)
[2016-07-21 18:03:40] [CustomAgendaEventListener] [137] [INFO] Match Created : [fact 0:1:1309129055:1309129055:1:DEFAULT:Server( name=server001, processors=4, memory=8192, diskSpace=2048, virtualizations=null, cpuUsage=3 )]
[2016-07-21 18:03:40] [CustomAgendaEventListener] [117] [INFO] Before Match Fired : [fact 0:1:1309129055:1309129055:1:DEFAULT:Server( name=server001, processors=4, memory=8192, diskSpace=2048, virtualizations=null, cpuUsage=3 )]
[2016-07-21 18:03:40] [check minimum server configuration] [20] [INFO] Server "server001" was rejected...
[2016-07-21 18:03:40] [CustomWorkingMemoryEventListener] [18] [INFO] Object Deleted : [fact 0:1:1309129055:1309129055:1:DEFAULT:Server( name=server001, processors=4, memory=8192, diskSpace=2048, virtualizations=null, cpuUsage=3 )] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@563e4951
[2016-07-21 18:03:40] [CustomAgendaEventListener] [80] [DEBUG] Rule fired: check minimum server configuration
session.getFactCount() = 5
matched count of Fact = 5
------DroolsTest.java--------
package com.gooper.drool_test;
import org.kie.api.KieBase;
import org.kie.api.definition.type.FactType;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.builder.KnowledgeBuilderError;
import com.gooper.drool_test.CustomWorkingMemoryEventListener;
import com.gooper.drool_test.CustomAgendaEventListener;
public class DroolsTest {
public static final void main(String[] args) {
try {
// 지식 빌더 생성
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
. newKnowledgeBuilder();
// 지식 빌더에 룰정의파일 설정
kbuilder.add(ResourceFactory. newClassPathResource("test_rule.drl"),
ResourceType. DRL );
if (kbuilder.hasErrors()) {
if(kbuilder.getErrors().size() > 0) {
for(KnowledgeBuilderError kerror : kbuilder.getErrors()) {
System.out.println("error :" + kerror);
}
}
}
// 새로운 지식 베이스 생성
KieBase kiebase = kbuilder.newKnowledgeBase();
// 새로운 지식 베이스 세션 생성
KieSession session = kiebase.newKieSession();
// logger등록(팩트에 의해 생성된 엑티베이션(Activation)관련 이벤트만 로깅
session.addEventListener(new CustomAgendaEventListener());
// logger등록(팩트의 추가/수정/제거 이벤트에 대한 정보)
session.addEventListener(new CustomWorkingMemoryEventListener());
// 지식 베이스의 getFactType 메서드를 이용하여 동적으로 팩트를 인스턴스화 시킨다.
// (팩트가 선언되어 있는 룰의 패키지 이름과 팩트의 이름을 인자로 받는다.)
FactType serverType = kiebase.getFactType("com.gooper.drool_test", "Server");
Object debianServer = null;
//KnowledgeRuntimeLoggerFactory.newConsoleLogger((KnowledgeRuntimeEventManager) session);
try {
// 동적으로 팩트를 인스턴스화 시킨다.
debianServer = serverType.newInstance();
} catch (InstantiationException e) {
System.out.println("the class Server on ... hasss");
} catch(IllegalAccessException e) {
System.out.println("sdsdfsdf");
}
// 세션에 추가할 팩트를 준비한다.
serverType.set(debianServer, "name", "server001");
serverType.set(debianServer, "processors", 4);
serverType.set(debianServer, "memory", 8192);
serverType.set(debianServer, "diskSpace", 2048);
serverType.set(debianServer, "cpuUsage", 3);
// 동적으로 팩트를 세션에 추가한다
session.insert(debianServer);
// BusinessTrip클래스를 이용한 팩트 추가
session.insert( new BusinessTrip("사용자1" ,3.5));
session.insert( new BusinessTrip("사용자2" ,5.0));
session.insert( new BusinessTrip("사용자3" ,8.0));
session.insert( new BusinessTrip("사용자4" ,10.0));
session.insert( new BusinessTrip("사용자5" ,13.0));
int matchedCnt = session.fireAllRules();
// 전체 Fact의 개수
System.out.println(" session.getFactCount() = " + session.getFactCount());
// 룰에 matching된 건수
System.out.println(" matched count of Fact = " + matchedCnt);
session.dispose();
} catch (Throwable t) {
t.printStackTrace();
}
}
}