Drools KnowledgeBase Deprecated

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

I am integrating the Drools Rules engine into my application. 99% of the examples I have found to get started look like:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newUrlResource( url ),                       ResourceType.DRL ); if ( kbuilder.hasErrors() ) {     System.err.println( builder.getErrors().toString() ); }                       KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages( builder.getKnowledgePackages() );  StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession(); ksession.insert( new Fibonacci( 10 ) ); ksession.fireAllRules();  ksession.dispose(); 

I have something similar working, but my question is that KnowledgeBase is marked as deprecated, which is a red flag to me that I am doing it wrong. Now KnowledgeBaseFactory.newKnowledgeBase() is not marked deprecated, but it also returns a KnowledgeBase type.

So what should I be using since KnowledgeBase is deprecated?

回答1:

This is the code I've found to work with 6.x:

    KieServices kieServices = KieServices.Factory.get();     KieFileSystem kfs = kieServices.newKieFileSystem();     FileInputStream fis = new FileInputStream( pathToSomeDrl );     kfs.write( "src/main/resources/simple.drl",                 kieServices.getResources().newInputStreamResource( fis ) );     KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();     Results results = kieBuilder.getResults();     if( results.hasMessages( Message.Level.ERROR ) ){         System.out.println( results.getMessages() );         throw new IllegalStateException( "### errors ###" );     }     KieContainer kieContainer =         kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );     KieBase kieBase = kieContainer.getKieBase();     KieSession kieSession = kieContainer.newKieSession(); 

References to KnowledgeBase have remained in the documentation, but it is indeed deprecated.



回答2:

Tanks Llaune for your answer. For me it works

1.- Spring context to get many resources

<bean id="kbase" class="com.fsaldivars.drools.KnowledgeBase" scope="singleton" lazy-init="true" init-method="buildKbase">     <property name="decisionTables">         <list>             <bean class="com.fsaldivars.drools.DroolsAlfrescoResource">                 <property name="resourceName" value="ChooseModel.xls" />             </bean>         </list>     </property>     <property name="drls">         <list>             <bean class="com.fsaldivars.drools.DroolsAlfrescoResource">                 <property name="resourceName" value="BPMAutorizaFacultad.drl" />             </bean>         </list>     </property> </bean> 

2.- for get knowledgeBase

public class KnowledgeBase {     private KieContainer  kieContainer = null;     private KieServices   kieServices  = null;     KieFileSystem         kfs          = null;     private static Logger logger       = LoggerFactory.getLogger( KnowledgeBase.class );      public KnowledgeBase()     {         kieServices = KieServices.Factory.get();         kfs = this.kieServices.newKieFileSystem();         logger.info( "Create new Container base" );     }      /**      * Reads object <code>KieFileSystem</code> that contains all files loaded from alfresco      * @see <a href="http://docs.jboss.org/drools/release/6.2.0.CR4/drools-docs/html/KIEChapter.html">http://docs.jboss.org/drools/release/6.2.0.CR4/drools-docs/html/KIEChapter.html</a>      */     public void buildKbase()     {         KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();         Results results = kieBuilder.getResults();         if ( results.hasMessages( Message.Level.ERROR ) )         {             for ( Message message : results.getMessages() )             {                 System.out.println( "ERROR in Drools parsing: " + message );                 logger.error( "ERROR in Drools parsing: " + message );             }             throw new IllegalArgumentException( "Could not parse knowledge base for decison table resource" );         }         else         {             logger.info( "Added decision table:" + results.getMessages() + "  with no errors" );         }         kieContainer = kieServices.newKieContainer( kieBuilder.getKieModule().getReleaseId() );     }      /**      * Init all Decision tables that are declared in the spring context      */     private void initDecisionTables( List<DroolsResource> decisionTables )     {         for ( DroolsResource resource : decisionTables )         {             try             {                 kfs.write( "src/main/resources/" + resource.getResourceName(), resource.getResource() );             }             catch ( Exception e )             {                 logger.error( "Could not parse file  base for decison table resource: " + resource.getResourceName()                         + " Thrown Exception is [" + GeneralUtils.mensajeError( e ) + "]" );                 throw new IllegalArgumentException( "Could not parse file  base for decison table resource: "                         + resource.getResourceName() );             }         }         decisionTables = null; // not needed any more free memory for resources     }      /**      * Init all Rules that are declared in the spring context      */     private void initDrls( List<DroolsResource> drls )     {         for ( DroolsResource resource : drls )         {             try             {                 kfs.write( "src/main/resources/" + resource.getResourceName(), resource.getResource() );             }             catch ( Exception e )             {                 logger.error( "Could not parse file  base for decison table resource: " + resource.getResourceName()                         + " Thrown Exception is [" + GeneralUtils.mensajeError( e ) + "]" );                 throw new IllegalArgumentException( "Could not parse file  base for decison table resource: "                         + resource.getResourceName() );             }         }         drls = null; // not needed any more free memory for resources     }      /**      * Creates a new stateless session. Can be called from spring also      */     public StatelessKieSession newStatelessSession()     {         logger.info( "Create a new stateless session" );         return kieContainer.getKieBase().newStatelessKieSession();     }      /**      * Creates a new stateful session. Can be called from spring also      */     /*public StatefulKnowledgeSession newStatefulSession() {         logger.info("Create a new stateful session");          return kieBase.newStatefulKnowledgeSession();     }*/     public void runRules( Object inputData )         throws Exception     {         KieSessionConfiguration kieConfiguration = kieServices.newKieSessionConfiguration();         StatelessKieSession ksession = kieContainer.getKieBase().newStatelessKieSession( kieConfiguration );         logger.info( "Fire the rules with data:" + inputData.toString() );         ksession.execute( inputData );     }      public void runRules( StatelessKieSession ksession, Object inputData )         throws Exception     {         logger.info( "Fire the rules with data in session:" + inputData.toString() );         ksession.execute( inputData );     }      public void setDecisionTables( List<DroolsResource> decisionTables )     {         initDecisionTables( decisionTables );     }      public void setDrls( ArrayList<DroolsResource> drls )     {         initDrls( drls );     } } 

3.- So you can run any rule just with pojo.

kbase.runRules(ObjectDeclaredInYourRule); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!