add example with rule project dependancy

This commit is contained in:
Nicolas Héron 2017-01-28 15:51:42 +01:00
commit f2d9bc13ca
41 changed files with 2991 additions and 35 deletions

View file

@ -0,0 +1,57 @@
package cost;
public class CalculatedElement {
private String key;
private int numberValue;
private String stringValue;
private long longValue;
private double doubleValue;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public int getNumberValue() {
return numberValue;
}
public void setNumberValue(int numberValue) {
this.numberValue = numberValue;
}
public String getStringValue() {
return stringValue;
}
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
public long getLongValue() {
return longValue;
}
public void setLongValue(long longValue) {
this.longValue = longValue;
}
public double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(double doubleValue) {
this.doubleValue = doubleValue;
}
@Override
public String toString() {
return "CalculatedElement [key=" + key + ", numberValue=" + numberValue + ", stringValue=" + stringValue
+ ", longValue=" + longValue + ", doubleValue=" + doubleValue + "]";
}
}

View file

@ -0,0 +1,30 @@
package cost;
public class City {
public static String ShangaiCityName = "Shangai";
public static String RotterdamCityName = "Rotterdam";
public static String TournaiCityName = "Tournai";
public static String LilleCityName = "Lille";
private String name;
public City(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "City [name=" + name + "]";
}
}

View file

@ -0,0 +1,47 @@
package cost;
import java.util.ArrayList;
import java.util.List;
public class CostCalculationRequest {
private Order order;
private Trip trip;
private List<Pallet> pallets = new ArrayList<Pallet>();
private List<CostElement> costElements = new ArrayList<CostElement>();
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Trip getTrip() {
return trip;
}
public void setTrip(Trip trip) {
this.trip = trip;
}
public List<Pallet> getPallets() {
return pallets;
}
public void setPallets(List<Pallet> pallets) {
this.pallets = pallets;
}
public List<CostElement> getCostElements() {
return costElements;
}
public void setCostElements(List<CostElement> costElements) {
this.costElements = costElements;
}
}

View file

@ -0,0 +1,5 @@
package cost;
public interface CostElement {
}

View file

@ -0,0 +1,31 @@
package cost;
public class HandlingCostElement implements CostElement {
private double amount;
private City city;
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
@Override
public String toString() {
return "HandlingCostElement [amount=" + amount + ", city=" + city + "]";
}
}

View file

@ -0,0 +1,53 @@
package cost;
public class LeftToDistribute {
private OrderLine orderLine;
private long quantityLeft;
private double weightLeft;
public LeftToDistribute(OrderLine orderLine, double weightLeft) {
super();
this.orderLine = orderLine;
this.weightLeft = weightLeft;
}
public LeftToDistribute(OrderLine orderLine, long quantityLeft) {
super();
this.orderLine = orderLine;
this.quantityLeft = quantityLeft;
}
public OrderLine getOrderLine() {
return orderLine;
}
public void setOrderLine(OrderLine orderLine) {
this.orderLine = orderLine;
}
public long getQuantityLeft() {
return quantityLeft;
}
public void setQuantityLeft(long quantityLeft) {
this.quantityLeft = quantityLeft;
}
public double getWeightLeft() {
return weightLeft;
}
public void setWeightLeft(double weightLeft) {
this.weightLeft = weightLeft;
}
@Override
public String toString() {
return "LeftToDistribute [orderLine=" + orderLine + ", quantityLeft=" + quantityLeft + ", weightLeft="
+ weightLeft + "]";
}
}

View file

@ -0,0 +1,37 @@
package cost;
import java.util.ArrayList;
import java.util.List;
public class Order {
private String id;
private List<OrderLine> orderLines = new ArrayList();
public Order(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<OrderLine> getOrderLines() {
return orderLines;
}
public void setOrderLines(List<OrderLine> orderLines) {
this.orderLines = orderLines;
}
}

View file

@ -0,0 +1,51 @@
package cost;
public class OrderLine {
private int numberItems;
private double weight;
private Product product;
public OrderLine(int numberItems, Product product) {
super();
this.numberItems = numberItems;
this.product = product;
}
public OrderLine(double weight, Product product) {
super();
this.weight = weight;
this.product = product;
}
public int getNumberItems() {
return numberItems;
}
public void setNumberItems(int numberItems) {
this.numberItems = numberItems;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "OrderLine [numberItems=" + numberItems + ", weight=" + weight + ", product=" + product + "]";
}
}

View file

@ -0,0 +1,89 @@
package cost;
import java.util.HashMap;
import java.util.Map;
public class Pallet {
private boolean isFull = false;
private double heightLeft = 2.0;
private double width = 0.8;
private double depth = 1.2;
private Map<Product, Long> contentQuantity = new HashMap<Product, Long>();
private Map<Product, Double> contentWeight = new HashMap<Product, Double>();
private int palletType;
public Map<Product, Long> getContentQuantity() {
return contentQuantity;
}
public Map<Product, Double> getContentWeight() {
return contentWeight;
}
public void addContent(Product product, Long quantity) {
if (contentQuantity.containsKey(product) == false) {
contentQuantity.put(product, quantity);
} else {
Long oldQuantity = contentQuantity.get(product);
oldQuantity = oldQuantity + quantity;
}
}
public void addContent(Product product, Double weight) {
if (contentWeight.containsKey(product) == false) {
contentWeight.put(product, weight);
} else {
Double oldQuantity = contentWeight.get(product);
oldQuantity = oldQuantity + weight;
}
}
public boolean isFull() {
return isFull;
}
public void setFull(boolean isFull) {
this.isFull = isFull;
}
public double getHeightLeft() {
return heightLeft;
}
public void setHeightLeft(double heightLeft) {
this.heightLeft = heightLeft;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getDepth() {
return depth;
}
public void setDepth(double depth) {
this.depth = depth;
}
public int getPalletType() {
return palletType;
}
public void setPalletType(int palletType) {
this.palletType = palletType;
}
@Override
public String toString() {
return "Pallet [isFull=" + isFull + ", heightLeft=" + heightLeft + ", width=" + width + ", depth=" + depth
+ ", palletType=" + palletType + "]";
}
}

View file

@ -0,0 +1,106 @@
package cost;
public class Product {
public static int transportType_pallet = 1;
public static int transportType_individual = 2;
public static int transportType_bulkt = 3;
private String name;
private double height;
private double width;
private double depth;
private double weight;
private int transportType;
public Product(String name, double height, double width, double depth, double weight, int transportType) {
super();
this.name = name;
this.height = height;
this.width = width;
this.depth = depth;
this.weight = weight;
this.transportType = transportType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getDepth() {
return depth;
}
public void setDepth(double depth) {
this.depth = depth;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getTransportType() {
return transportType;
}
public void setTransportType(int transportType) {
this.transportType = transportType;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Product [name=" + name + ", height=" + height + ", width=" + width + ", depth=" + depth + ", weight="
+ weight + ", transportType=" + transportType + "]";
}
}

View file

@ -0,0 +1,42 @@
# Cost Calculation Data model
The first thing to do when using drools is to build the java class model on which the rules will apply.
![](diagram.jpg)
* CostCalculationRequest
This is the top entity that will contain the trip description as well as the order the customer did and witch trip has to be calculated.
And in the same object, we shall put the final result in a list of costs and the complete list of pallets where we will put the products.
* Trip
Contains the list of steps of the trip.
* Step
A step happens between two cities and a unique transport type : train, truck or boat
* City
A city which can ne a harbour, a train station or a a final destination by truck.
* Order
The detail of the customer order.
* OrderLine
It contains the product as well as the number of items.
* LeftToDistribute
As we have to distribute all products in the pallets, we shall store here during the distribution what is left to be distributed.
* Product
The product that is bought and has to be in the transport.
* Pallet
Here is a picture of a pallet.
![](pallet.jpg)
* HandlingCostElement, TaxesCostElement and TransportCostElement
they represent the different costs that we have to calculate.
* CalculatedElement
We shall use this class to store intermediate values.

View file

@ -0,0 +1,61 @@
package cost;
public class Step {
public static int Ship_TransportType = 1;
public static int train_TransportType = 2;
public static int truck_TransportType = 3;
private City stepStart;
private City stepEnd;
private double distance;
private int transportType;
public Step(City stepStart, City stepEnd, double distance, int transportType) {
super();
this.stepStart = stepStart;
this.stepEnd = stepEnd;
this.distance = distance;
this.transportType = transportType;
}
public City getStepStart() {
return stepStart;
}
public void setStepStart(City stepStart) {
this.stepStart = stepStart;
}
public City getStepEnd() {
return stepEnd;
}
public void setStepEnd(City stepEnd) {
this.stepEnd = stepEnd;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public int getTransportType() {
return transportType;
}
public void setTransportType(int transportType) {
this.transportType = transportType;
}
@Override
public String toString() {
return "Step [stepStart=" + stepStart + ", stepEnd=" + stepEnd + ", distance=" + distance + ", transportType="
+ transportType + "]";
}
}

View file

@ -0,0 +1,30 @@
package cost;
public class TaxesCostElement implements CostElement {
private double amount;
private City city;
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
@Override
public String toString() {
return "TaxesCostElement [amount=" + amount + ", city=" + city + "]";
}
}

View file

@ -0,0 +1,29 @@
package cost;
public class TransportCostElement implements CostElement {
private double amount;
private Step step;
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Step getStep() {
return step;
}
public void setStep(Step step) {
this.step = step;
}
@Override
public String toString() {
return "TransportCostElement [amount=" + amount + ", step=" + step + "]";
}
}

View file

@ -0,0 +1,37 @@
package cost;
import java.util.ArrayList;
import java.util.List;
public class Trip {
private String id;
private List<Step> steps = new ArrayList<Step>();
public Trip(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Step> getSteps() {
return steps;
}
public void setSteps(List<Step> steps) {
this.steps = steps;
}
@Override
public String toString() {
return "Trip [id=" + id + "]";
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View file

@ -0,0 +1,19 @@
package util;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateHelper {
public static String sFormat = "yyyy-MM-dd";
public static Date getDate(String sDate) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
return sdf.parse(sDate);
}
public static Date getDate(String sDate, String anotherFormat)
throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(anotherFormat);
return sdf.parse(sDate);
}
}

View file

@ -0,0 +1,175 @@
package util;
import org.jbpm.workflow.instance.node.RuleSetNodeInstance;
import org.kie.api.KieServices;
import org.kie.api.event.process.*;
import org.kie.api.event.rule.*;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.StatelessKieSession;
public class KnowledgeSessionHelper {
public static KieContainer createRuleBase() {
KieServices ks = KieServices.Factory.get();
KieContainer kieContainer = ks.getKieClasspathContainer();
return kieContainer;
}
public static StatelessKieSession getStatelessKnowledgeSession(KieContainer kieContainer, String sessionName) {
StatelessKieSession kSession = kieContainer.newStatelessKieSession(sessionName);
return kSession;
}
public static KieSession getStatefulKnowledgeSession(KieContainer kieContainer, String sessionName) {
KieSession kSession = kieContainer.newKieSession();
return kSession;
}
public static KieSession getStatefulKnowledgeSessionForJBPM(
KieContainer kieContainer, String sessionName) {
KieSession session = getStatefulKnowledgeSessionWithCallback(kieContainer, sessionName);
session.addEventListener(new ProcessEventListener() {
@Override
public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeProcessStarted(ProcessStartedEvent arg0) {
System.out.println("Process Name " + arg0.getProcessInstance().getProcessName() + " has been started");
}
@Override
public void beforeProcessCompleted(ProcessCompletedEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {
if (arg0.getNodeInstance() instanceof RuleSetNodeInstance) {
System.out.println("Node Name " + arg0.getNodeInstance().getNodeName() + " has been left");
}
}
@Override
public void afterVariableChanged(ProcessVariableChangedEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void afterProcessStarted(ProcessStartedEvent arg0) {
}
@Override
public void afterProcessCompleted(ProcessCompletedEvent arg0) {
System.out.println("Process Name " + arg0.getProcessInstance().getProcessName() + " has stopped");
}
@Override
public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {
if (arg0.getNodeInstance() instanceof RuleSetNodeInstance) {
System.out.println("Node Name " + arg0.getNodeInstance().getNodeName() + " has been entered");
}
}
@Override
public void afterNodeLeft(ProcessNodeLeftEvent arg0) {
}
});
return session;
}
public static KieSession getStatefulKnowledgeSessionWithCallback(
KieContainer kieContainer, String sessionName) {
KieSession session = getStatefulKnowledgeSession(kieContainer, sessionName);
session.addEventListener(new RuleRuntimeEventListener() {
public void objectInserted(ObjectInsertedEvent event) {
System.out.println("Object inserted \n"
+ event.getObject().toString());
}
public void objectUpdated(ObjectUpdatedEvent event) {
System.out.println("Object was updated \n"
+ "new Content \n" + event.getObject().toString());
}
public void objectDeleted(ObjectDeletedEvent event) {
System.out.println("Object retracted \n"
+ event.getOldObject().toString());
}
});
session.addEventListener(new AgendaEventListener() {
public void matchCreated(MatchCreatedEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " can be fired in agenda");
}
public void matchCancelled(MatchCancelledEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " cannot b in agenda");
}
public void beforeMatchFired(BeforeMatchFiredEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " will be fired");
}
public void afterMatchFired(AfterMatchFiredEvent event) {
System.out.println("The rule "
+ event.getMatch().getRule().getName()
+ " has be fired");
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
});
return session;
}
}

View file

@ -0,0 +1,10 @@
package util;
public class OutputDisplay {
public void showText(String someText) {
long time = System.currentTimeMillis();
System.out.println("time=" + time + "-" + someText);
}
}