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,78 @@
# Cost Calculation project
# Requirements
We are working for a retail company that buys its products from different places in Asian mostly. each shop can go to the buying web site and say what he wants and put an order.
Up to now, the transport cost was a estimated but not calculated on the content of the order.
The purpose is to implement such a calculator.
## Data model
A customer puts an order that contains products. An order contains a lost of products with a number.
A product has
* a name
* a height,
* a width,
* a depth,
* transport type : if the product can be put with other products in a pallet, individual(alone) or bulk (like sand for example)
* a weight
A product can be put on a pallet. a pallet 120 cm width and and 80 cm depth. It can be maximum 2 meters height and the weight should not exceed 1400 kg. We should use a simple algorithm to fill each pallet. It will be not optimized but we should used that as a margin of the costs. a product that is bigger than 60 cm in width or depth or higher than 1m should be put alone in a pallet.
All products start from the same city and go to the same city in an order. A trip is composed f steps. Each step can be done by train, boat or truck.
Here is the list of products in our test order.
| Product | Transport type | Number | Height | Weight | depth | weight |
| -- | -- | -- | -- | -- | -- | -- |
| drill | pallet | 1000 | 20cm | 40cm | 30cm | 2 kg |
| screwdriver | pallet | 30000 | 3cm | 2cm | 20cm | 0,2 kg |
| Sand | bulk | | | | | 35 Tons |
| Gravel | bulk | | | | | 14 Tons |
| furniture | individual |23 | | | | 500 kg |
And the trip is a follows :
| # | start city | arrival city | distance (km) | travel mode |
| -- | -- | -- | -- | -- |
| 1 | Shangai | Rotterdam | 22000 | Boat|
| 2 | Rotterdam | Tournai | 300 | Train |
| 3 | Tournai | Lille | 20 | Truck |
there are 3 types of costs :
- transport costs per Pallet
| Transport| Cost
| -- | -- |
| boat | 0,2€/km |
| Train | 0,5€/km |
| Truck | 1€/km |
- Taxes
| City| Cost
| -- | -- |
| Shangai | 0,02€/kg |
| Rotterdam | 0,05€/kg and 1€ per handling person |
| Tournai | 2€ per handling person |
| Lille | 30€ per handling person |
- handling
| City| Cost
| -- | -- |
| Shangai | 20€/hour and a person can handle 13 pallets/hour |
| Rotterdam | 45€/hour and a person can handle 60 pallets/hour |
| Tournai | 67€/hour and can handle 40 pallets/hour |
| Lille | 79€/hour and can handle 30 pallets/hour |
The handling should not take more than 12 hours.
[Data Model ](src/main/java/cost/README.md)
[The rules we implemented ](src/main/resources/rules/README.md)

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="cost-calculation-base" />
<orderEntry type="library" name="Maven: org.drools:drools-compiler:6.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.drools:drools-core:6.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.4" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-internal:6.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.antlr:antlr-runtime:3.5" level="project" />
<orderEntry type="library" name="Maven: org.mvel:mvel2:2.2.8.Final" level="project" />
<orderEntry type="library" name="Maven: com.thoughtworks.xstream:xstream:1.4.9" level="project" />
<orderEntry type="library" name="Maven: xmlpull:xmlpull:1.1.3.1" level="project" />
<orderEntry type="library" name="Maven: xpp3:xpp3_min:1.1.4c" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.2" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jdt.core.compiler:ecj:4.4.2" level="project" />
<orderEntry type="library" name="Maven: org.jbpm:jbpm-bpmn2:6.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.jbpm:jbpm-flow-builder:6.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.jbpm:jbpm-flow:6.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: joda-time:joda-time:2.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.5" level="project" />
<orderEntry type="library" name="Maven: org.kie:kie-api:6.5.0.Final" level="project" />
<orderEntry type="module" module-name="drools-lesson-pojo" />
</component>
</module>

View file

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>droolscourse</artifactId>
<groupId>com.pymma-software.droolscourse</groupId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cost-calculation-extension</artifactId>
<dependencies>
<dependency>
<groupId>com.pymma-software.droolscourse</groupId>
<artifactId>cost-calculation-base</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
</dependency>
<dependency>
<groupId>org.jbpm</groupId>
<artifactId>jbpm-bpmn2</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
</dependency>
<dependency>
<groupId>com.pymma-software.droolscourse</groupId>
<artifactId>drools-lesson-pojo</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="kbase4" packages="rules-extension, rules">
</kbase>
</kmodule>

View file

@ -0,0 +1,219 @@
package cost.calculation
import cost.OrderLine;
import cost.LeftToDistribute;
import cost.Product;
import cost.Pallet;
import cost.CostCalculationRequest;
import cost.CalculatedElement;
import cost.HandlingCostElement;
import cost.TaxesCostElement;
import cost.TransportCostElement;
import cost.Step;
import cost.City;
import java.lang.Math;
import java.util.ArrayList;
rule "boatTransportCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$s : Step(transportType ==Step.Ship_TransportType)
then
TransportCostElement t = new TransportCostElement();
t.setStep($s);
t.setAmount($c.getPallets().size()*$s.getDistance()*0.2);
$c.getCostElements().add(t);
insert(t);
end
rule "trainTransportCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$s : Step(transportType ==Step.train_TransportType)
then
TransportCostElement t = new TransportCostElement();
t.setStep($s);
t.setAmount($c.getPallets().size()*$s.getDistance()*0.5);
$c.getCostElements().add(t);
insert(t);
end
rule "truckTransportCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$s : Step(transportType ==Step.truck_TransportType)
then
TransportCostElement t = new TransportCostElement();
t.setStep($s);
t.setAmount($c.getPallets().size()*$s.getDistance()*1.0);
$c.getCostElements().add(t);
insert(t);
end
rule "ShangaiTaxesCost_bulk"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.ShangaiCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType == Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Double d : pp.getContentWeight().values()){
totalWeight=totalWeight+d;
}
}
t.setAmount(totalWeight*0.02);
$c.getCostElements().add(t);
insert(t);
end
rule "ShangaiTaxesCost_notbulk"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.ShangaiCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType != Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Product p : pp.getContentQuantity().keySet()){
totalWeight=totalWeight+p.getWeight()* pp.getContentQuantity().get(p);
}
}
t.setAmount(totalWeight*0.05);
$c.getCostElements().add(t);
insert(t);
end
rule "RotterdamTaxesCost_bulk_weight"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.RotterdamCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType == Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Double d : pp.getContentWeight().values()){
totalWeight=totalWeight+d;
}
}
t.setAmount(totalWeight*0.05);
$c.getCostElements().add(t);
insert(t);
end
rule "RotterdamTaxesCost_notbulk_weight"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.RotterdamCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType != Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Product p : pp.getContentQuantity().keySet()){
totalWeight=totalWeight+p.getWeight()* pp.getContentQuantity().get(p);
}
}
t.setAmount(totalWeight*0.05);
$c.getCostElements().add(t);
insert(t);
end
rule "ShangaiHandlingCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.ShangaiCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/13)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*20.0);
$c.getCostElements().add(t);
insert(t);
end
rule "RotterdamHandlingCostAndPersonTaxes"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.RotterdamCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/60)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*45.0);
$c.getCostElements().add(t);
insert(t);
TaxesCostElement t1 = new TaxesCostElement();
t1.setAmount(nbrePerson*1.0);
$c.getCostElements().add(t1);
insert(t1);
end
rule "TournaiHandlingCostAndPersonTaxes"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.TournaiCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/40)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*67.0);
$c.getCostElements().add(t);
insert(t);
TaxesCostElement t1 = new TaxesCostElement();
t1.setAmount(nbrePerson*2.0);
$c.getCostElements().add(t1);
insert(t1);
end
rule "LilleHandlingCostAndPersonTaxes"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.LilleCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/30)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*79.0);
$c.getCostElements().add(t);
insert(t);
TaxesCostElement t1 = new TaxesCostElement();
t1.setAmount(nbrePerson*30.0);
$c.getCostElements().add(t1);
insert(t1);
end

View file

@ -0,0 +1,134 @@
package cost.calculation
import cost.OrderLine;
import cost.LeftToDistribute;
import cost.Product;
import cost.Pallet;
import cost.CostCalculationRequest;
import cost.CalculatedElement;
import java.lang.Math;
rule "Create Counter leftToDistribute quantity"
ruleflow-group "distribution"
when
$elt : OrderLine( numberItems >0 )
not (LeftToDistribute(orderLine==$elt))
then
LeftToDistribute e= new LeftToDistribute($elt,$elt.getNumberItems());
insert(e);
end
rule "Create Counter leftToDistribute weight"
ruleflow-group "distribution"
when
$elt : OrderLine( weight >0 )
not (LeftToDistribute(orderLine==$elt))
then
LeftToDistribute e= new LeftToDistribute($elt,$elt.getWeight());
insert(e);
end
rule "Create PalletForIndividual"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$p : Product(transportType==Product.transportType_individual)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(quantityLeft> 0,orderLine==$elt)
then
Pallet pp = new Pallet();
pp.setPalletType($p.getTransportType());
pp.addContent($p,new Long(1));
insert(pp);
$c.getPallets().add(pp);
$l.setQuantityLeft($l.getQuantityLeft()-1);
update($l);
end
rule "Create PalletForBulkMore1400Kg"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$p : Product(transportType==Product.transportType_bulkt)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(weightLeft > 1400,orderLine==$elt)
then
Pallet pp = new Pallet();
pp.setPalletType($p.getTransportType());
pp.addContent($p,new Long(1400));
insert(pp);
$c.getPallets().add(pp);
$l.setWeightLeft($l.getWeightLeft()-1400);
update($l);
end
rule "Create PalletForBulkless1400Kg"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$p : Product(transportType==Product.transportType_bulkt)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(weightLeft <= 1400,weightLeft > 0,orderLine==$elt)
then
Pallet pp = new Pallet();
pp.setPalletType($p.getTransportType());
pp.addContent($p,$l.getWeightLeft());
insert(pp);
$c.getPallets().add(pp);
$l.setWeightLeft(0);
update($l);
end
rule "Create Empty Pallet"
ruleflow-group "distribution"
when
not (Pallet (palletType == Product.transportType_pallet,full ==false))
then
Pallet p = new Pallet();
p.setPalletType(Product.transportType_pallet);
insert (p);
end
rule "Create PalletFortransportType_pallet"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$pp : Pallet($hl : heightLeft ,full ==false,palletType == Product.transportType_pallet)
$p : Product(height <= $hl,transportType==Product.transportType_pallet)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(quantityLeft > 0,orderLine==$elt)
then
long a = (long) Math.round($pp.getWidth()/$p.getWidth());
long b = (long) Math.round($pp.getDepth()/$p.getDepth());
long n = Math.min(a*b,$l.getQuantityLeft());
$pp.addContent($p,n);
$pp.setHeightLeft($pp.getHeightLeft()-$p.getHeight());
update($pp);
$l.setQuantityLeft($l.getQuantityLeft()-n);
update($l);
end
rule "FillPalletIfNoProductGoesIn"
ruleflow-group "distribution"
when
CalculatedElement($min : doubleValue ,key=="minValue.product")
$p : Pallet(heightLeft!= 2.0,$hl : heightLeft < $min ,full ==false,palletType == Product.transportType_pallet)
then
$p.setFull(true);
System.out.println("MinValue="+$min);
update($p);
end
rule "GetSmallestHeight"
ruleflow-group "distribution"
when
accumulate( Product( $h : height ,transportType==Product.transportType_pallet );
$min : min( $h );
true )
then
CalculatedElement elt = new CalculatedElement();
elt.setKey("minValue.product");
elt.setDoubleValue((Double)$min);
insert(elt);
end

View file

@ -0,0 +1,468 @@
# Rules
We shall use a Ruleflow and implement the rules in 3 steps :
* Distribute the products in the pallet
* Calculate the costs
* Calculate and Display the results
This is done in the P1.bpmn2.
![](ruleflow.png)
## Distribute the products in the pallet
This rules will be executed in the rule step "distribution". So we shall add the keyworruleflow-group "distribution".
There are 3 types of product : those we have to put individually, the bulk and the others.
### Initialize a counter for each order line
First we shall the rule that created the LeftToDistribute object.
For each orderline we create and link to the orderline element
```
rule "Create Counter leftToDistribute quantity"
ruleflow-group "distribution"
when
$elt : OrderLine( numberItems >0 )
not (LeftToDistribute(orderLine==$elt))
then
LeftToDistribute e= new LeftToDistribute($elt,$elt.getNumberItems());
insert(e);
end
rule "Create Counter leftToDistribute weight"
ruleflow-group "distribution"
when
$elt : OrderLine( weight >0 )
not (LeftToDistribute(orderLine==$elt))
then
LeftToDistribute e= new LeftToDistribute($elt,$elt.getWeight());
insert(e);
end
```
So here the rule above for each OrderLine that is not yet connected to a LeftToDistribute fact.
Then we create it and initialize the counter with the number of elements in the line.
We have one rule when items are
### individual items
For each product of type individual, we shall put it on the pallet.
And in the then part, we create the pallet, decrease the number of element left and update the objects?
```
rule "Create PalletForIndividual"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$p : Product(transportType==Product.transportType_individual)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(quantityLeft> 0,orderLine==$elt)
then
Pallet pp = new Pallet();
pp.setPalletType($p.getTransportType());
pp.addContent($p,new Long(1));
insert(pp);
$c.getPallets().add(pp);
$l.setQuantityLeft($l.getQuantityLeft()-1);
update($l);
end
```
### bulk item
For the bulk product, we shall do the same way.
The first rule files 1400kg on each pallet.
The second rule is if there is less than 1400kg left, then we put the rest in the pallet.
```
rule "Create PalletForBulkMore1400Kg"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$p : Product(transportType==Product.transportType_bulkt)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(weightLeft > 1400,orderLine==$elt)
then
Pallet pp = new Pallet();
pp.setPalletType($p.getTransportType());
pp.addContent($p,new Long(1400));
insert(pp);
$c.getPallets().add(pp);
$l.setWeightLeft($l.getWeightLeft()-1400);
update($l);
end
rule "Create PalletForBulkless1400Kg"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$p : Product(transportType==Product.transportType_bulkt)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(weightLeft <= 1400,weightLeft > 0,orderLine==$elt)
then
Pallet pp = new Pallet();
pp.setPalletType($p.getTransportType());
pp.addContent($p,$l.getWeightLeft());
insert(pp);
$c.getPallets().add(pp);
$l.setWeightLeft(0);
update($l);
end
```
### Distribute the other product type.
We shall first create a rule that create a new Pallet if none is available
```
rule "Create Empty Pallet"
ruleflow-group "distribution"
when
not (Pallet (palletType == Product.transportType_pallet,full ==false))
then
Pallet p = new Pallet();
p.setPalletType(Product.transportType_pallet);
insert (p);
end
```
As we are going to fill the pallet in height, we shall declare a pallet as full if the place left in height is smaller than the smallest product in height.
```
rule "GetSmallestHeight"
ruleflow-group "distribution"
when
accumulate( Product( $h : height ,transportType==Product.transportType_pallet );
$min : min( $h );
true )
then
CalculatedElement elt = new CalculatedElement();
elt.setKey("minValue.product");
elt.setDoubleValue((Double)$min);
insert(elt);
end
```
And then the rule that sets the pallet as filled (full has the true value). We only set the value of a pallet that has been filled by one type of product by adding constranint heightLeft!= 2.0.
```
rule "FillPalletIfNoProductGoesIn"
ruleflow-group "distribution"
when
CalculatedElement($min : doubleValue ,key=="minValue.product")
$p : Pallet(heightLeft!= 2.0,$hl : heightLeft < $min ,full ==false,palletType == Product.transportType_pallet)
then
$p.setFull(true);
System.out.println("MinValue="+$min);
update($p);
end
```
Then the rule that fills the pallet with product depending on the size of the product.
```
rule "Create PalletFortransportType_pallet"
ruleflow-group "distribution"
when
$c : CostCalculationRequest()
$pp : Pallet($hl : heightLeft ,full ==false,palletType == Product.transportType_pallet)
$p : Product(height <= $hl,transportType==Product.transportType_pallet)
$elt : OrderLine(product==$p )
$l : LeftToDistribute(quantityLeft > 0,orderLine==$elt)
then
long a = (long) Math.round($pp.getWidth()/$p.getWidth());
long b = (long) Math.round($pp.getDepth()/$p.getDepth());
long n = Math.min(a*b,$l.getQuantityLeft());
$pp.addContent($p,n);
$pp.setHeightLeft($pp.getHeightLeft()-$p.getHeight());
update($pp);
$l.setQuantityLeft($l.getQuantityLeft()-n);
update($l);
end
```
## Calculate the costs
Now we take each cost and add a CostElement depending on the number of pallets we created in previous step
### Transport cost
For each step type we calculate the cost
```
rule "boatTransportCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$s : Step(transportType ==Step.Ship_TransportType)
then
TransportCostElement t = new TransportCostElement();
t.setStep($s);
t.setAmount($c.getPallets().size()*$s.getDistance()*0.2);
$c.getCostElements().add(t);
insert(t);
end
rule "trainTransportCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$s : Step(transportType ==Step.train_TransportType)
then
TransportCostElement t = new TransportCostElement();
t.setStep($s);
t.setAmount($c.getPallets().size()*$s.getDistance()*0.5);
$c.getCostElements().add(t);
insert(t);
end
rule "truckTransportCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$s : Step(transportType ==Step.truck_TransportType)
then
TransportCostElement t = new TransportCostElement();
t.setStep($s);
t.setAmount($c.getPallets().size()*$s.getDistance()*1.0);
$c.getCostElements().add(t);
insert(t);
end
```
### Taxes Cost
For each City we go through, we calculate the cost (Shangai and Rotterdam)
As sometimes it depends on the weight, we have to sum the total amount of weigh on all pallets.
Here we chose not to put all calculation in the drools session, so we do the sum in the them part of the rules.
Se here how we obtain the list of Pallet we want in an ArrayList. In the then part we then iterate on the founded pallet and in the pallet we go through all the products to sum the weight.
```
rule "ShangaiTaxesCost_bulk"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.ShangaiCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType == Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Double d : pp.getContentWeight().values()){
totalWeight=totalWeight+d;
}
}
t.setAmount(totalWeight*0.02);
$c.getCostElements().add(t);
insert(t);
end
rule "ShangaiTaxesCost_notbulk"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.ShangaiCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType != Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Product p : pp.getContentQuantity().keySet()){
totalWeight=totalWeight+p.getWeight()* pp.getContentQuantity().get(p);
}
}
t.setAmount(totalWeight*0.05);
$c.getCostElements().add(t);
insert(t);
end
rule "RotterdamTaxesCost_bulk_weight"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.RotterdamCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType == Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Double d : pp.getContentWeight().values()){
totalWeight=totalWeight+d;
}
}
t.setAmount(totalWeight*0.05);
$c.getCostElements().add(t);
insert(t);
end
rule "RotterdamTaxesCost_notbulk_weight"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.RotterdamCityName)
$listPallet : ArrayList( )
from collect( Pallet(palletType != Product.transportType_bulkt) )
then
TaxesCostElement t = new TaxesCostElement();
t.setCity($ci);
double totalWeight=0;
for (Object oo : $listPallet){
Pallet pp = (Pallet)oo;
for (Product p : pp.getContentQuantity().keySet()){
totalWeight=totalWeight+p.getWeight()* pp.getContentQuantity().get(p);
}
}
t.setAmount(totalWeight*0.05);
$c.getCostElements().add(t);
insert(t);
end
```
### Handling Cost and taxes on the Handling
Here we admit that it takes 12 hours to do the handling.
Based on that we then see how many pallets we have to handle per hour and then depending on the what a person can handle per hour, we calculate the number of persons we need.
```
rule "ShangaiHandlingCost"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.ShangaiCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/13)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*20.0);
$c.getCostElements().add(t);
insert(t);
end
rule "RotterdamHandlingCostAndPersonTaxes"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.RotterdamCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/60)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*45.0);
$c.getCostElements().add(t);
insert(t);
TaxesCostElement t1 = new TaxesCostElement();
t1.setAmount(nbrePerson*1.0);
$c.getCostElements().add(t1);
insert(t1);
end
rule "TournaiHandlingCostAndPersonTaxes"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.TournaiCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/40)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*67.0);
$c.getCostElements().add(t);
insert(t);
TaxesCostElement t1 = new TaxesCostElement();
t1.setAmount(nbrePerson*2.0);
$c.getCostElements().add(t1);
insert(t1);
end
rule "LilleHandlingCostAndPersonTaxes"
ruleflow-group "calculate"
when
$c : CostCalculationRequest( )
$ci : City(name==City.LilleCityName)
then
int n = $c.getPallets().size();
int nbrePalletPerHour = Math.round( n/12)+1;
int nbrePerson = Math.round( nbrePalletPerHour/30)+1;
HandlingCostElement t = new HandlingCostElement();
t.setCity($ci);
t.setAmount(nbrePerson*12*79.0);
$c.getCostElements().add(t);
insert(t);
TaxesCostElement t1 = new TaxesCostElement();
t1.setAmount(nbrePerson*30.0);
$c.getCostElements().add(t1);
insert(t1);
end
```
## Sum the results
We use the accumulate function to show the results. We just show the result in the console. On a real project, the results could habe stored in a data structure.
```
rule "CalculateTotal"
ruleflow-group "total"
when
$c : CostCalculationRequest( )
$totalBoatTransport : Number( doubleValue > 100 )
from accumulate( $s : Step(transportType ==Step.Ship_TransportType) &&
TransportCostElement(step ==$s, $value : amount ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
$totalTrainTransport : Number( doubleValue > 100 )
from accumulate( $s : Step(transportType ==Step.train_TransportType) &&
TransportCostElement(step ==$s, $value : amount ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
$totalTruckTransport : Number( doubleValue > 100 )
from accumulate( $s : Step(transportType ==Step.truck_TransportType) &&
TransportCostElement(step ==$s, $value : amount ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
$totalTransport : Number( doubleValue > 100 )
from accumulate( TransportCostElement( $value : amount ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
$totalTaxes : Number( doubleValue > 100 )
from accumulate( TaxesCostElement( $value : amount ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
$totalHandling : Number( doubleValue > 100 )
from accumulate( HandlingCostElement( $value : amount ),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
then
System.out.println("NumberOfPallets="+$c.getPallets().size());
System.out.println("TotalShipTransport="+$totalBoatTransport);
System.out.println("TotalTrainTransport="+$totalTrainTransport);
System.out.println("TotalTruckTransport="+$totalTruckTransport);
System.out.println("TotalTransport="+$totalTransport);
System.out.println("TotalTaxes="+$totalTaxes);
System.out.println("TotalHandling="+$totalHandling);
end;
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB