方案如下:
我有一个java pojo类Name
,它有2个变量type
和name
,带有适当的getter和setter。我正在使用决策表流氓,我想:
Condition
:当type设置为" 1"
Action
:调用名称类的setName setter并从excel中设置适当的参数
现在我想在java中访问这个name的设置值。
这就是我所做的事情:
Name.java -
public class Name{
String name,type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
MainClass.java-已经实现了drools !!
public class MainClass {
public static StatefulKnowledgeSession session;
public static KnowledgeBase kbase;
public static Name name;
public static void main(String args[])
{
init();
initialiseNameObject();
fireAllRules();
}
public static void init()
{
System.out.println("inside init");
KnowledgeBuilder kbuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();
DecisionTableConfiguration dconf=KnowledgeBuilderFactory.newDecisionTableConfiguration();
dconf.setInputType(DecisionTableInputType.XLS);
dconf.setWorksheetName("rulesW");
kbuilder.add(ResourceFactory.newClassPathResource("/drools.xls",MainClass.class), ResourceType.DTABLE, dconf);
KnowledgeBuilderErrors errors=kbuilder.getErrors();
if(!errors.isEmpty())
{
System.out.println("Errors in package");
Iterator i=errors.iterator();
while(i.hasNext())
{
System.out.println("The error: "+i.next());
}
}
kbase=KnowledgeBaseFactory.newKnowledgeBase();
try
{
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session=kbase.newStatefulKnowledgeSession();
name=new Name();
}
catch(Exception e)
{
System.out.println("The exception "+e);
}
}
public static void initialiseNameObject()
{
System.out.println("insideNameObject");
name.setType("1");
}
public static void fireAllRules()
{
System.out.println("inside fire all");
session.fireAllRules();
System.out.println(name.getName());
}
决策表::
http://www.imageurlhost.com/images/tf2m0omib1jkt8wyjra8_Decision.png
问题是我无法获得名称..总是得到null。应该在对象中更新名称,我应该将ABCD作为输出。我想它必须对全局实例做一些事情!
答案 0 :(得分:3)
你的代码对我没有意义。其中一个主要问题是,在创建StatefulKnowledgeSession之后,您没有在会话中插入任何对象。在会议中没有任何内容,我怀疑Drool可以做什么。
尝试做类似的事情:
name.setType(1);
session.insert(name); // Important! Drools need the objects to work on
// inserted to session
session.fireAllRules();
然后在您的决策表中,将操作更改为namea.setName($param)
我相信它应该在这些变化之后起作用