# 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($c
没有合适的资源?快使用搜索试试~ 我知道了~
规则引擎代码
共229个文件
java:53个
xml:51个
class:33个
5星 · 超过95%的资源 需积分: 22 8 下载量 130 浏览量
2018-01-31
10:06:15
上传
评论
收藏 642KB RAR 举报
温馨提示
规则引擎(RuleEngine)是一个有限状态机,通过入参实现状态转移,在Java中定义为JSR94规范。规则引擎目前的开源实现主要是JBoss家族的Drools,采用友好的Apache协议(意味着可以作为商业产品)。以及据说非常贵的ILOG引擎,还有一些国内引擎
资源详情
资源评论
资源推荐
收起资源包目录
规则引擎代码 (229个子文件)
P1.bpmn2 4KB
P1.bpmn2 4KB
P1.bpmn2 4KB
TestExecercice.class 5KB
KnowledgeSessionHelper$3.class 3KB
KnowledgeSessionHelper$2.class 3KB
Pallet.class 3KB
KnowledgeSessionHelper$1.class 3KB
KnowledgeSessionHelper$3.class 3KB
Product.class 3KB
CashFlow.class 2KB
KnowledgeSessionHelper.class 2KB
KnowledgeSessionHelper.class 2KB
Step.class 2KB
CalculatedElement.class 2KB
KnowledgeSessionHelper$2.class 2KB
KnowledgeSessionHelper$1.class 2KB
AccountingPeriod.class 2KB
Customer.class 2KB
LeftToDistribute.class 2KB
CostCalculationRequest.class 2KB
OrderLine.class 2KB
Account.class 1KB
Trip.class 1KB
TransportCostElement.class 1KB
HandlingCostElement.class 1KB
TaxesCostElement.class 1KB
City.class 1KB
Order.class 1019B
PrivateAccount.class 1010B
CustomerService.class 918B
DateHelper.class 876B
DateHelper.class 876B
OutputDisplay.class 850B
OutputDisplay.class 822B
CostElement.class 108B
config 305B
description 73B
CalculationRules.drl 6KB
CalculationRules.drl 6KB
CalculationRules.drl 6KB
PalletRules.drl 4KB
PalletRules.drl 4KB
PalletRules.drl 4KB
SumRules.drl 3KB
SumRules.drl 3KB
SumRules.drl 3KB
lesson35.drl 2KB
lesson31.drl 1KB
lesson34.drl 1KB
lesson2.drl 1009B
lesson1.drl 1001B
lesson2.drl 858B
lesson32.drl 721B
demo-ruleflow3.drl 683B
lesson33.drl 680B
demo-ruleflow1.drl 567B
exclude 240B
.gitignore 201B
.gitignore 17B
HEAD 191B
HEAD 191B
HEAD 32B
HEAD 23B
pack-9e857066f5d756cc8f9fb2c539182cb1ee9ff814.idx 28KB
cost-calculation.iml 3KB
cost-calculation-run.iml 3KB
cost-calculation-base.iml 3KB
drools-lesson2.iml 3KB
drools-lesson31.iml 3KB
drools-lesson35.iml 3KB
drools-lesson32.iml 3KB
drools-lesson4.iml 3KB
drools-lesson1.iml 3KB
drools-lesson34.iml 3KB
drools-lesson33.iml 3KB
cost-calculation-extension.iml 2KB
drools-lesson-pojo.iml 2KB
AccountProject.iml 2KB
droolscourse.iml 617B
droolscourse.iml 606B
drools-lesson1 (1).iml 219B
drools-lesson3_1.iml 219B
drools-lesson2 (6).iml 219B
drools-lesson8.iml 219B
index 20KB
Testlesson1.java 6KB
KnowledgeSessionHelper.java 6KB
KnowledgeSessionHelper.java 6KB
KnowledgeSessionHelper.java 6KB
TestExecercice.java 4KB
TestExecercice.java 4KB
Testlesson2bis.java 4KB
Testlesson31.java 3KB
Product.java 2KB
Product.java 2KB
Pallet.java 2KB
Pallet.java 2KB
Testlesson4.java 2KB
CashFlow.java 2KB
共 229 条
- 1
- 2
- 3
蚊子987469
- 粉丝: 4
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1