FactType.getFields返回没有字段drools KIE

时间:2015-09-08 17:32:34

标签: eclipse maven drools kie kie-workbench

我创建了一个事实类型(CustomerFact),其中包含两个事实字段(年龄和奖金)。还使用工作台创建了一个示例规则。现在,我想注入一些测试值并检查规则是否被触发。我使用的是Eclipse IDE。

我能够在我的java代码中检索包名和在workbench中创建的规则。但是,我可以获取任何事实字段。 GetFields总是返回一个空列表,而它应该返回2个字段。有没有替代方案?我只想设置事实类型的字段,看看规则是否被触发。任何帮助都非常感谢。

package org.demo.cityproject;

/**
 * This class was automatically generated by the data modeler tool.
 */

public class CustomerFact implements java.io.Serializable
{

   static final long serialVersionUID = 1L;

   @org.kie.api.definition.type.Key
   private int age;
   @org.kie.api.definition.type.Key
   private int bonus;

   public CustomerFact()
   {
   }

   public int getAge()
   {
      return this.age;
   }

   public void setAge(int age)
   {
      this.age = age;
   }

   public int getBonus()
   {
      return this.bonus;
   }

   public void setBonus(int bonus)
   {
      this.bonus = bonus;
   }

   public CustomerFact(int age, int bonus)
   {
      this.age = age;
      this.bonus = bonus;
   }

   @Override
   public boolean equals(Object o)
   {
      if (this == o)
         return true;
      if (o == null || getClass() != o.getClass())
         return false;
      org.demo.cityproject.CustomerFact that = (org.demo.cityproject.CustomerFact) o;
      if (age != that.age)
         return false;
      if (bonus != that.bonus)
         return false;
      return true;
   }

   @Override
   public int hashCode()
   {
      int result = 17;
      result = 31 * result + age;
      result = 31 * result + bonus;
      return result;
   }

}

测试代码

    public static void main(String[] args)
    {

        String url = "http://localhost:8080/kie-drools-wb-distribution-wars-6.2.0.Final-jboss-as7/maven2/Project1/org/demo/CityProject/1.0/CityProject-1.0.jar";
        ReleaseIdImpl releaseId = new ReleaseIdImpl("org.demo", "CityProject", "LATEST");
        KieServices kieServices = KieServices.Factory.get();
        kieServices.getResources().newUrlResource(url);
        KieContainer kieContainer = kieServices.newKieContainer(releaseId);
        KieScanner kieScanner = kieServices.newKieScanner(kieContainer);
        kieScanner.scanNow();
        Scanner scanner = new Scanner(System.in);
        System.out.println("kieContainer.getKieBaseNames() "+kieContainer.getKieBaseNames());

        KieSession newKieSession =kieContainer.newKieSession("session1");
        KieBase lKieBase=newKieSession.getKieBase();
        System.out.println("lPackage "+lKieBase.getKiePackages());
        KiePackage lPackage=lKieBase.getKiePackage("org.demo.cityproject");
        System.out.println("lPackage FactTypes: "+lPackage.getFactTypes());
        for(FactType lFact:lPackage.getFactTypes())
        {
            System.out.println("lFacts: "+lFact.getName());
            System.out.println("lFacts Fields: "+lFact.getFields());

        }
}

eclipse中的控制台o / p

22:58:28.998 [main] DEBUG o.d.c.k.b.impl.KieRepositoryImpl - KieModule Lookup. ReleaseId org.demo:CityProject:LATEST was not in cache, checking maven repository
22:58:37.430 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:37.743 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:39.455 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:39.503 [main] DEBUG o.e.a.i.i.DefaultUpdateCheckManager - Skipped remote request for org.demo:CityProject/maven-metadata.xml, locally installed metadata up-to-date.
22:58:39.503 [main] DEBUG o.e.a.i.i.DefaultUpdateCheckManager - Skipped remote request for org.demo:CityProject/maven-metadata.xml, locally installed metadata up-to-date.
22:58:39.503 [main] DEBUG o.e.a.i.i.DefaultUpdateCheckManager - Skipped remote request for org.demo:CityProject/maven-metadata.xml, locally installed metadata up-to-date.
22:58:41.744 [main] DEBUG o.e.a.i.i.DefaultDependencyCollector - Dependency collection stats: {ConflictMarker.analyzeTime=0, ConflictMarker.markTime=0, ConflictMarker.nodeCount=1, ConflictIdSorter.graphTime=0, ConflictIdSorter.topsortTime=0, ConflictIdSorter.conflictIdCount=1, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=0, ConflictResolver.conflictItemCount=1, DefaultDependencyCollector.collectTime=32, DefaultDependencyCollector.transformTime=15}
22:58:42.230 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:42.230 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:42.246 [main] DEBUG o.e.a.i.i.DefaultDependencyCollector - Dependency collection stats: {ConflictMarker.analyzeTime=0, ConflictMarker.markTime=0, ConflictMarker.nodeCount=1, ConflictIdSorter.graphTime=0, ConflictIdSorter.topsortTime=0, ConflictIdSorter.conflictIdCount=0, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=0, ConflictResolver.conflictItemCount=0, DefaultDependencyCollector.collectTime=0, DefaultDependencyCollector.transformTime=0}
22:58:47.596 [main] INFO  o.d.c.k.b.impl.KieRepositoryImpl - KieModule was added: ZipKieModule[releaseId=org.demo:CityProject:1.0,file=C:\Users\USER\.m2\repository\org\demo\CityProject\1.0\CityProject-1.0.jar]
22:58:47.957 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:47.957 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:47.957 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:48.426 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:48.442 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:48.457 [main] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\USER\.m2\repository
22:58:48.457 [main] DEBUG o.e.a.i.i.DefaultUpdateCheckManager - Skipped remote request for org.demo:CityProject/maven-metadata.xml, locally installed metadata up-to-date.
22:58:48.457 [main] DEBUG o.e.a.i.i.DefaultUpdateCheckManager - Skipped remote request for org.demo:CityProject/maven-metadata.xml, locally installed metadata up-to-date.
22:58:48.457 [main] DEBUG o.e.a.i.i.DefaultUpdateCheckManager - Skipped remote request for org.demo:CityProject/maven-metadata.xml, locally installed metadata up-to-date.
kieContainer.getKieBaseNames() [KnowledgeBaseALL]
22:58:50.532 [main] DEBUG o.drools.core.impl.KnowledgeBaseImpl - Starting Engine in PHREAK mode
lPackage [[Package name=org.demo.cityproject]]
lPackage FactTypes: [ClassDefinition{className='org.demo.cityproject.CustomerFact', superClass='java.lang.Object', interfaces=[java.io.Serializable], definedClass=class org.demo.cityproject.CustomerFact, traitable=null, abstract=false, fields={}, annotations=null}]
lFacts: org.demo.cityproject.CustomerFact
**lFacts Fields: []**

正如您在输出中看到的那样,事实字段是空的。

2 个答案:

答案 0 :(得分:0)

相当令人失望的情况是,有两种甚至三种FactType:

  1. 由DRL文件中的声明语句定义的事实类型
  2. 由Java类生成并明确或隐式导入的事实类型
  3. 用作事实类型的JDK类型
  4. 方法getFactTypes不返回#3。方法getFields返回#2的空列表。

答案 1 :(得分:0)

最后我找到了答案。这些字段可以通过反射来调用。 以下是规则的触发方式

    for(FactType lFct:lusedpackage.getFactTypes())
    {
        // System.out.println("Customer class "+lFct.getFactClass());
        **Constructor constructor=lFct.getFactClass().getConstructor(String.class,java.math.BigInteger.class);**
        **Object s1=constructor.newInstance(lcity,lcustomerID);**

        //System.out.println("constructor "+constructor);
        //System.out.println("Object "+s1);

        **Method lCustID=lFct.getFactClass().getMethod("getCustomerID");**
        System.out.println("*************************************************************");
        System.out.println("Customer value before firing the rule: "+ lCustID.invoke(s1));
        **Method setCity  = lFct.getFactClass().getMethod("getCity");** 

        System.out.println("City value before firing the rule: "+ setCity.invoke(s1));          
        //System.out.println("method "+lCustID);
        System.out.println("*************************************************************");
        newKieSession.insert(s1);
        newKieSession.fireAllRules();

        System.out.println("Customer value after firing the rule: "+ **lCustID.invoke(s1)**);
        System.out.println("City value after firing the rule: "+ setCity.invoke(s1));
        System.out.println("*************************************************************");                

    }