0

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

我能够在我的 java 代码中检索包名称和在工作台中创建的规则。但是,我可以获取任何事实字段。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: []**

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

4

2 回答 2

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("*************************************************************");                

    }   
于 2015-09-09T16:09:02.403 回答
0

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

  1. DRL 文件中的声明语句定义的事实类型
  2. 由 Java 类产生并显式或隐式导入的事实类型
  3. 用作事实类型的 JDK 类型

方法 getFactTypes 不返回 #3。方法 getFields 返回 #2 的空列表。

于 2015-09-08T18:41:47.213 回答