HonestPoliticianExample 2008-02-28 10:51

字号:    

1、HonestPoliticianExample.java

package org.drools.examples;

import java.io.InputStreamReader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.compiler.PackageBuilder;

public class HonestPoliticianExample {

    /**
     * @param args
     */
    public static void main(final String[] args) throws Exception {

        final PackageBuilder builder = new PackageBuilder();
        builder.addPackageFromDrl( new InputStreamReader( HonestPoliticianExample.class.getResourceAsStream( "HonestPolitician.drl" ) ) );

        final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
        ruleBase.addPackage( builder.getPackage() );

        final StatefulSession session = ruleBase.newStatefulSession();

        final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger( session );
        logger.setFileName( "log/honest-politician" );

        final Politician blair  = new Politician("blair", true);
        final Politician bush  = new Politician("bush", true);
       final Politician chirac  = new Politician("chirac", true);
       final Politician schroder   = new Politician("schroder", true);
       session.insert( blair );
       session.insert( bush );
       session.insert( chirac );
      session.insert( schroder );

       session.fireAllRules();
        logger.writeToDisk();
        session.dispose();
    }
   
    public static class Politician {
     private String name;
     private boolean honest;
     
     public Politician() {
      
     }
     
  public Politician(String name, boolean honest) {
   super();
   this.name = name;
   this.honest = honest;
  }
  
  public boolean isHonest() {
   return honest;
  }
  
  public void setHonest(boolean honest) {
   this.honest = honest;
  }

  public String getName() {
   return name;
  }          
    }

    public static class Hope {
     
     public Hope() {
      
     }
     
    }
   
}

2、HonestPolitician.drl

package org.drools.examples
 
import org.drools.examples.HonestPoliticianExample.Politician;
import org.drools.examples.HonestPoliticianExample.Hope;
 
rule "We have an honest Politician"
 when
  exists( Politician( honest == true ) )
  insertLogical( new Hope() );

end

rule "Hope Lives"
    salience 10
 when
  exists( Hope() )
 then
  System.out.println("Hurrah!!! Democracy Lives");
end

rule "Hope is Dead"
 when
  not( Hope() )
 then
  System.out.println( "We are all Doomed!!! Democracy is Dead" );
end

rule "Currupt the Honest"
   salience 5
 when
  politician : Politician( honest == true ) 
  exists( Hope() )
 then
     System.out.println( "I'm an evil corporation and I have corrupted " + politician.getName() );
  politician.setHonest( false );
  update( politician );
end

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
网易公司版权所有 ©1997-2009