Guvnor上で作成したルールをクライアントから起動する。
Gunvnor上で作成したルールをクライアントから起動してみます。
※このサンプルはGuvnorで作った宣言的Factをクライアントから起動する方法になります。
- 1.Guvnorでルールを作ります。
- 20歳以上であれば、ファクト.resultに「○○さんは○なので、お酒が飲めます」とセットする簡単なものです。この前にFactの設定がありますが、省略します。
- 2.クライアントから実行してみます。
import org.drools.KnowledgeBase; import org.drools.agent.KnowledgeAgent; import org.drools.agent.KnowledgeAgentFactory; import org.drools.definition.type.FactType; import org.drools.io.ResourceFactory; import org.drools.runtime.StatelessKnowledgeSession; public class KnowledgeAgentSample { public static final void main(String[] args) throws Exception{ KnowledgeAgent agent = KnowledgeAgentFactory.newKnowledgeAgent("sampleAgent"); agent.applyChangeSet( ResourceFactory.newClassPathResource("agent/mychangeset.xml")); KnowledgeBase rb = agent.getKnowledgeBase(); StatelessKnowledgeSession session = rb.newStatelessKnowledgeSession(); FactType factType = rb.getFactType("test", "Profile"); Object profile = factType.newInstance(); factType.set(profile, "name", "uzr"); factType.set(profile, "age", 31); session.execute(profile); System.out.println("結果:" + factType.get(profile,"result")); ResourceFactory.getResourceChangeScannerService().stop(); } }
-
- mychange.xml
- sourceのURLは、パッケージ直下の「パッケージバイナリURL」かスナップショットのURLを設定します。
- mychange.xml