• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
JUDCon São Paulo - Drools in a Nutshell
 

JUDCon São Paulo - Drools in a Nutshell

on

  • 556 views

 

Statistics

Views

Total Views
556
Views on SlideShare
543
Embed Views
13

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 13

https://twitter.com 13

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    JUDCon São Paulo - Drools in a Nutshell JUDCon São Paulo - Drools in a Nutshell Presentation Transcript

    • Friday, April 19, 13
    • Drools in a nutshell Everything you always wanted to know but didnt know whom to ask! ;)Friday, April 19, 13
    • porcelli@redhat.com no:sql(br) @porcellircelli Alexandre Porcelli Program Committee Alexandre Porcelli Program Committee Emerging Languages on the JVM Alexandre Porcelli Core Engineer #guvnordev Alexandre Porcelli #droolsdev Principal Software Engineer #jbpmdev #optaplanner-dev Friday, April 19, 13
    • Drools? was born as a rule engine ... evolved as business modeling solution ... targets as knowledge representation and reasoning platform!Friday, April 19, 13
    • opta expert fusion guvnor planner jbpm jbpm jbpm console designerFriday, April 19, 13
    • expert guvnorFriday, April 19, 13
    • so... what?Friday, April 19, 13
    • Logic and Data Separation Speed and Scalability Understandable Rules Centralization of Knowledge Declarative ProgrammingFriday, April 19, 13
    • rule what isFriday, April 19, 13
    • rule <rule_name> <attribute><value> when <conditions> then <actions> endFriday, April 19, 13
    • rule "Infer Adult" when $p : Person( age >= 18 ) then System.out.println("Adult!"); endFriday, April 19, 13
    • patternmatching Person( age >= 18 ) field name restriction object type constraint patternFriday, April 19, 13
    • rule "Infer Child" rule "Infer Baby" when when Person( age < 18 ) Person( age <= 2 ) then then //code //code end end rule "Infer Adult" rule "Infer Senior" when when Person( age >= 18 ) Person( age >= < 60 ) then then //code //code end endFriday, April 19, 13
    • Person(age=10) rule "Infer Child" rule "Infer Baby" when when Person( age < 18 ) Person( age <= 2 ) then then //code //code end end rule "Infer Adult" rule "Infer Senior" when when Person( age >= 18 ) Person( age >= < 60 ) then then //code //code end endFriday, April 19, 13
    • Person(age=22) rule "Infer Child" rule "Infer Baby" when when Person( age < 18 ) Person( age <= 2 ) then then //code //code end end rule "Infer Adult" rule "Infer Senior" when when Person( age >= 18 ) Person( age >= < 60 ) then then //code //code end endFriday, April 19, 13
    • Person(age=61) rule "Infer Child" rule "Infer Baby" when when Person( age < 18 ) Person( age <= 2 ) then then //code //code end end rule "Infer Adult" rule "Infer Senior" when when Person( age >= 18 ) Person( age >= < 60 ) then then //code //code end endFriday, April 19, 13
    • Person(age=1) rule "Infer Child" rule "Infer Baby" when when Person( age < 18 ) Person( age <= 2 ) then then //code //code end end rule "Infer Adult" rule "Infer Senior" when when Person( age >= 18 ) Person( age >= < 60 ) then then //code //code end endFriday, April 19, 13
    • OtherObject() rule "Infer Child" rule "Infer Baby" when when Person( age < 18 ) Person( age <= 2 ) then then //code //code end end rule "Infer Adult" rule "Infer Senior" when when Person( age >= 18 ) Person( age >= < 60 ) then then //code //code end endFriday, April 19, 13
    • decouple logic rule "Infer Child" rule "Infer Adult" when when $p : Person( age < 18 ) $p : Person( age >= 18 ) then then insertLogical( new IsChild( $p ) ) insertLogical( new IsAdult( $p ) ) end end rule "Issue Child Bus Pass" rule "Issue Adult Bus Pass" when when $p : Person( ) $p : Person( ) IsChild( person == $p ) IsAdult( person =$p ) then then insertLogical( new ChildBusPass( $p ) ); insertLogical( new AdultBusPass( $p ) ); end endFriday, April 19, 13
    • patternmatching Person( $age : age ) Person( age == ( $age + 1 ) ) Person( age > 30 && < 40 || hair in ("black", "brown") ) Person( pets contains $rover ) Person( pets["rover"].type == "dog" ) Person( insurance.valid )Friday, April 19, 13
    • under the hoodFriday, April 19, 13
    • hybrid reasoning systemFriday, April 19, 13
    • WTF?Friday, April 19, 13
    • forward backward chainningFriday, April 19, 13
    • WTF?Friday, April 19, 13
    • forwardchainning data driven/reactionary rule "Adults that like cheddar" when Cheese( $cheddar : name == "cheddar" ) $p : Person( age >= 18, favouriteCheese == $cheddar ) then System.out.println( $p.getName() + " likes cheddar!"); endFriday, April 19, 13
    • backwardchainning goal driven declare Parent declare Man declare Woman parent : String name : String name : String child : String end end end query man( String name ) Man( name := name ) end query woman( String name ) Woman( name := name ) end query parent( String parent, String child ) Parent( parent := parent, child := child ) endFriday, April 19, 13
    • backwardchainning query father( String father, String child ) ?man( father; ) ?parent( father, child; ) end query mother( String mother, String child ) ?woman( mother; ) ?parent( mother, child; ) end query son( String son, String parent ) ?man( son; ) ?parent( parent, son; ) end query daughter( String daughter, String parent ) ?woman( daughter; ) ?parent( parent, daughter; ) endFriday, April 19, 13
    • backwardforward fewer hypotheses lot’s hypotheses query data lot’s data upfrontFriday, April 19, 13
    • rete algorithmFriday, April 19, 13
    • reteoo rule "Like cheddar" when Cheese( $cheddar : name == "cheddar" ) $p : Person( favouriteCheese == $cheddar ) then System.out.println( $p.getName() + " likes cheddar" ); end rule "NOT Like cheddar" when Cheese( $cheddar : name == "cheddar" ) $p : Person( favouriteCheese != $cheddar ) then System.out.println( $p.getName() + " not like cheddar" ); endFriday, April 19, 13
    • reteoo rete node object type node alpha node left input node join node terminalFriday, April 19, 13
    • reteoo rete node object type node alpha node left input node join node terminalFriday, April 19, 13
    • reteoo rete node object type node alpha node left input node join node terminalFriday, April 19, 13
    • reteoo rete node object type node alpha node left input node join node terminalFriday, April 19, 13
    • so... how?Friday, April 19, 13
    • Friday, April 19, 13
    • codeapi final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); // this will parse and compile in one step kbuilder.add( ResourceFactory.newClassPathResource("HelloWorld.drl", HelloWorldExample.class), ResourceType.DRL); // Check the builder for errors if ( kbuilder.hasErrors() ) { throw new RuntimeException("Error compiling."); } // get the compiled packages (which are serializable) final Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages(); // add the packages to a KnowledgeBase (deploy the knowledge packages). final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(pkgs);Friday, April 19, 13
    • codeapi final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession.setGlobal( "myGlobalObj", globalObj ); for( Object fact : _facts_ ) { ksession.insert( fact ); } ksession.fireUntilHalt(); ksession.fireAllRules(); SessionEntryPoint entryPoint = ksession.getEntryPoint( "entrypoint_name" ); ksession.dispose(); entryPoint.insert( _fact_ );Friday, April 19, 13
    • codeapi final StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); ksession.execute( _facts_ );Friday, April 19, 13
    • please one sec!Friday, April 19, 13
    • Friday, April 19, 13
    • new paradigm?Friday, April 19, 13
    • yesnoFriday, April 19, 13
    • yes you can construct your systems using almost only rules!Friday, April 19, 13
    • pong demoFriday, April 19, 13
    • no you can adapt your existing systems (few lines) to take advantage of droolsFriday, April 19, 13
    • public void aprovarPagamento(String apoliceId, String sinistroId, " " int valor, String cpfAprovador) { " if (!existeReserva(sinistroId)) { " " throw new IllegalStateException( " " " " "não existe reserva para o sinistro."); " } " Pessoa aprovador = pessoaService.buscarPessoa(cpfAprovador); " if (aprovador.getPapeis() " " " .contains(Pessoa.Papeis.COORDENADOR_SEGURADORA) " " " || aprovador.getPapeis().contains( " " " " " Pessoa.Papeis.GERENTE_SEGURADORA) " " " || aprovador.getPapeis().contains( " " " " " Pessoa.Papeis.DIRETOR_SEGURADORA) " " " || aprovador.getPapeis().contains(Pessoa.Papeis.VP_SEGURADORA)) { " " if (valor <= 5000) { " " " pagar(apoliceId, sinistroId, valor); " " } else if (valor > 500000) { " " " if (aprovador.getPapeis().contains(Pessoa.Papeis.VP_SEGURADORA)) { " " " " pagar(apoliceId, sinistroId, valor); " " " } else { " " " " throw new IllegalStateException( " " " " " " "Aprovador não tem alçada para aprovação."); " " " } " " } else if (valor <= 10000) { " " " if (aprovador.getPapeis().contains( " " " " " Pessoa.Papeis.GERENTE_SEGURADORA) " " " " " || aprovador.getPapeis().contains( " " " " " " " Pessoa.Papeis.DIRETOR_SEGURADORA) " " " " " || aprovador.getPapeis().contains( " " " " " " " Pessoa.Papeis.VP_SEGURADORA)) { " " " " pagar(apoliceId, sinistroId, valor); " " " } else { " " " " throw new IllegalStateException( " " " " " " "Aprovador não tem alçada para aprovação."); " " " } " " } else if (valor <= 500000) { " " " if (aprovador.getPapeis().contains( " " " " " Pessoa.Papeis.DIRETOR_SEGURADORA) " " " " " || aprovador.getPapeis().contains( " " " " " " " Pessoa.Papeis.VP_SEGURADORA)) { " " " " pagar(apoliceId, sinistroId, valor); " " " } else { " " " " throw new IllegalStateException( " " " " " " "Aprovador não tem alçada para aprovação."); " " " } " " } else { " " " throw new IllegalStateException("Problemas na aprovação."); " " } " } else { " " throw new IllegalStateException( " " " " "Aprovador não é funcionário da seguradora."); " } }Friday, April 19, 13
    • system control flow control flow control flow business rules business rules business rules control flow control flow control flow business rules business rules business rules business rules business rules business rules control flow control flow control flow business rules business rules business rulesFriday, April 19, 13
    • public void aprovarPagamento( String apoliceId, String sinistroId, int valor, String cpfAprovador ) { if ( !existeReserva( sinistroId ) ) { throw new IllegalStateException( "Não existe reserva para o sinistro." ); } final Pessoa aprovador = pessoaService.buscarPessoa( cpfAprovador ); final ValorPagamentoApolice pagamento = new ValorPagamentoApolice( ... ); ksession.execute( asList( aprovador, pagamento ) ); if ( !pagamento.isPago() ) { throw new IllegalArgumentException( "Pagamento nao efetuado." ); } }Friday, April 19, 13
    • rule "Coordenador tentando fraudar" " when " " p: Pessoa ( papeis contains Pessoa.Papeis.COORDENADOR_SEGURADORA ) " " v: ValorPagamentoApolice( valor > 50000 ) " " f: FinanceiroService( ) " then " System.out.println("O coordenador tentou fazer um pagamento não autorizado"); " " retract( f ); end rule "VP" " when " " m : Pessoa( papeis contains Pessoa.Papeis.VP_SEGURADORA ) " " v: ValorPagamentoApolice( valor > 500000 ) " " f: FinanceiroService( ) " then " executar(f, v); " " retract( f ); end rule "Nenhum funcionario pode aprovar sua apolice" salience 8888 " when " " p : Pessoa( $cpfPessoa: cpf ) " " v: ValorPagamentoApolice( cpfSefurado == $cpfPessoa, valor >= 50000 ) " " f: FinanceiroService( ) " then " System.out.println("O profissional tentou aprovar sua apolice."); " retract( f ); rule "Diretor ou VP" end " when " " m : Pessoa( papeis contains Pessoa.Papeis.DIRETOR_SEGURADORA || " " " " " papeis contains Pessoa.Papeis.VP_SEGURADORA ) " " v: ValorPagamentoApolice( valor > 10000 && <= 500000 ) " " f: FinanceiroService( ) " then " executar(f, v); " " retract( f ); endFriday, April 19, 13
    • system control flow control flow control flow business rules business rules business rules control flow control flow control flow business rules business rules business rules business rules business rules business rules control flow control flow control flow business rules guvnorFriday, April 19, 13
    • F.A.Q.Friday, April 19, 13
    • business analyst write rules?Friday, April 19, 13
    • yesnoFriday, April 19, 13
    • no they won’t do this: rule "NOT Like cheddar" when Cheese( $cheddar : name == "cheddar" ) $p : Person( favouriteCheese != $cheddar ) then System.out.println( $p.getName() + " not like cheddar" ); end technical ruleFriday, April 19, 13
    • yes but they can do this: decision tableFriday, April 19, 13
    • yes and this test scenarioFriday, April 19, 13
    • so... next?Friday, April 19, 13
    • droolsv6Friday, April 19, 13
    • retewilldie http://blog.athico.com/2013/01/life-beyond-rete-rip-rete-2013.htmlphreakFriday, April 19, 13
    • maven compatible project structurekjarFriday, April 19, 13
    • KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieSession kSession = kContainer.newKieSession("ksession2"); kSession.setGlobal("out", out); kSession.insert(new Message("Dave", "Hello, HAL. Do you read me, HAL?")); kSession.fireAllRules();newapiFriday, April 19, 13 java code
    • public class KProject { @Inject @KBase("fol4.test1.KBase1") KnowledgeBase kBase1; public KnowledgeBase getKBase1() { return kBase1; } @Inject @KBase("fol4.test2.KBase2") KnowledgeBase kBase2; public KnowledgeBase getKBase2() { return kBase2; } @Inject @KSession("fol4.test1.KSession1") StatelessKnowledgeSession kBase1kSession1;cdi public StatelessKnowledgeSession getKBase1KSession1() { return kBase1kSession1; } @Inject @KSession("fol4.test1.KSession2") StatefulKnowledgeSession kBase1kSession2; public StatefulKnowledgeSession getKBase1KSession2() { return kBase1kSession2; } } java codeFriday, April 19, 13
    • <?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> <kbase name="cdiexample"> <ksession name="ksession1"/> </kbase> </kmodule> meta-inf/kmodule.xml <?xml version="1.0" encoding="UTF-8"?> <kmodulecdi xmlns="http://jboss.org/kie/6.0.0/kmodule"> <kbase name="cdiexamplewithinclusion" includes="cdiexample"> <ksession name="ksession2"/> </kbase> </kmodule> inclusion (pom.xml dependency)Friday, April 19, 13
    • errai gwt cdi best-practices jee java scalability dev tools (ide) (template) html legacyuberfireFriday, April 19, 13
    • database+gitFriday, April 19, 13
    • shameless self promotion building world class web consoles using uberfire auditorium 4 saturday, april 20thFriday, April 19, 13
    • that’sall folks!Friday, April 19, 13
    • questions?Friday, April 19, 13
    • thanks! porcelli@redhat.com @porcelli #guvnordev #droolsdev #jbpmdev Alexandre Porcelli #optaplanner-dev Principal Software EngineerFriday, April 19, 13