场景如下:
我有一个 java pojo 类Name
,它有 2 个变量type
和name
适当的 getter 和 setter。我正在使用决策表流口水,我想要:
Condition
:当类型设置为“1”时
Action
:调用名称类的 setName 设置器并从 excel 中设置适当的参数
现在我想在 java 中访问这个 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 - 有流口水实现!
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
问题是我无法获得名称.. 总是为此获得空值。名称应该在对象中更新,我应该得到 ABCD 作为输出。我想它必须对全局实例做一些事情!