Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

java - Deprecated code of drools to version 6.2

I have an old and deprectaed code of drools, this code can read a rule file (drl) outside of the container and can use any Value Object and any class.

            CommandExecutor ksession;
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
            String file = System.getenv("DOMAIN_HOME")+"/mydrls/business.drl";
            kbuilder.add(ResourceFactory.newFileResource(file), ResourceType.DRL);
            KnowledgeBase kbase = kbuilder.newKnowledgeBase();
            ksession = kbase.newStatelessKnowledgeSession();
            DataTest data = new DataTest();
            data.setAge(25);
            List cmds = new ArrayList();
            cmds.add( CommandFactory.newInsert( data ) );
            ksession.execute(CommandFactory.newBatchExecution( cmds ));

How I can do the same thing but with drools version 6.2?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
FileInputStream fis = new FileInputStream( "simple/simple.drl" );
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();
StatelessKieSession kieSession =
    kieContainer.newStatelessKieSession();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...