Created
December 8, 2013 08:23
-
-
Save mariofusco/7854608 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.drools.java8; | |
import java.util.*; | |
public class Java8Example { | |
Rule rule1 = new Rule() | |
.when( (Person person) -> person.getAge() >= 18 ) | |
.and( (Person person, Pet pet) -> pet.getOwner() == person ) | |
.then( (Person person, Pet pet) -> System.out.println("Person" + person + "is the owner of" + pet) ); | |
public static interface Tuple { } | |
public static interface CTuple1<E1> extends Tuple { | |
boolean invoke(E1 t); | |
} | |
public static interface CTuple2<E1, E2> extends Tuple { | |
boolean invoke(E1 e1, E2 e2); | |
} | |
public static interface CTuple3<E1, E2, E3> extends Tuple { | |
boolean invoke(E1 e1, E2 e2, E3 e3); | |
} | |
public static interface ATuple1<E1> extends Tuple { | |
void invoke(E1 t); | |
} | |
public static interface ATuple2<E1, E2> extends Tuple{ | |
void invoke(E1 e1, E2 e2); | |
} | |
public static interface ATuple3<E1, E2, E3> extends Tuple { | |
void invoke(E1 e1, E2 e2, E3 e3); | |
} | |
public static class Rule { | |
private Condition when; | |
private Tuple then; | |
public Rule() {} | |
public Condition when(CTuple1<?> then) { | |
return when = new Condition(this, then); | |
} | |
public Condition when(CTuple2<?,?> then) { | |
return when = new Condition(this, then); | |
} | |
public Condition when(CTuple3<?,?,?> then) { | |
return when = new Condition(this, then); | |
} | |
public Rule then(ATuple1<?> then) { | |
this.then = then; | |
return this; | |
} | |
public Rule then(ATuple2<?,?> then) { | |
this.then = then; | |
return this; | |
} | |
public Rule then(ATuple3<?,?,?> then) { | |
this.then = then; | |
return this; | |
} | |
} | |
public static class Condition { | |
private final Rule rule; | |
private List<Tuple> when = new ArrayList<Tuple>(); | |
public Condition(Rule rule, Tuple then) { | |
this.rule = rule; | |
when.add(then); | |
} | |
public Condition and(CTuple1<?> then) { | |
when.add(then); | |
return this; | |
} | |
public Condition and(CTuple2<?,?> then) { | |
when.add(then); | |
return this; | |
} | |
public Condition and(CTuple3<?,?,?> then) { | |
when.add(then); | |
return this; | |
} | |
public Rule then(ATuple1<?> then) { | |
return rule.then(then); | |
} | |
public Rule then(ATuple2<?,?> then) { | |
return rule.then(then); | |
} | |
public Rule then(ATuple3<?,?,?> then) { | |
return rule.then(then); | |
} | |
} | |
public static class Person { | |
private String name; | |
private int age; | |
public Person(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
public int getAge() { | |
return age; | |
} | |
} | |
public static class Pet { | |
private String name; | |
private Person owner; | |
private String type; | |
public Pet(String name, Person owner, String type) { | |
this.name = name; | |
this.owner = owner; | |
this.type = type; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Person getOwner() { | |
return owner; | |
} | |
public void setOwner(Person owner) { | |
this.owner = owner; | |
} | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
} | |
public static class Location { | |
private String person; | |
private String place; | |
public Location(String person, String place) { | |
this.person = person; | |
this.place = place; | |
} | |
public String getPerson() { | |
return person; | |
} | |
public void setPerson(String person) { | |
this.person = person; | |
} | |
public String getPlace() { | |
return place; | |
} | |
public void setPlace(String place) { | |
this.place = place; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment