Implementation adjustment
Implementation adjustments to the new interface are handled quickly:

-
The interface is stateless and generic now; the type is an additional paramter in all methdos
-
The model is no longer an instance object, but is passed as an argument in all methods
-
The method names in the enums are more descriptive
-
no longer isEnum or doEnum
-
but e.g. isPurchaseOrderValue
-
-
An extra Trace code that is called AFTER each rule execution is added at the end
An adjusted implementation of the interface:
package lfet.demo;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class InvoiceTotal implements InvoiceTotal_iFace<InvoiceTotal_model> {
private final InvoiceTotal_rulesEngine rulesEngine = new InvoiceTotal_rulesEngine();
public void computeInvoiceTotal(InvoiceTotal_model model) {
model.setTotalDiscount(BigDecimal.ZERO);
model.setTotalPostage(BigDecimal.ZERO);
model.setTotalAmount(BigDecimal.ZERO);
rulesEngine.execute(this, model);
}
/**
* @param d any double value,
* NEGATIVE_INFINITY will be replaced Double.MAX_VALUE * -1.0,
* POSITIVE_INFINITY will be replaced Double.MAX_VALUE
* @return the value as BigDecimal, scale 2, RoundingMode.HALF_UP
*/
public BigDecimal bigDecimal(double d) {
return new BigDecimal(Double.toString(
d < Double.MAX_VALUE * -1.0 ? Double.MAX_VALUE * -1.0
: Math.min(d, Double.MAX_VALUE)))
.setScale(2, RoundingMode.HALF_EVEN);
}
/**
* <b>C01: Purchase order value</b><br>
* <br>
* The enum generation has been triggered in project.ini by:<br>
* - <b>ide.java.interface.enum.1</b>: symbol=Interval; include.tags.title=purchase order; exclude.tags.title=discount
*/
@Override
public boolean isPurchaseOrderValue(InvoiceTotal_iFace.PurchaseOrderValue arg0, InvoiceTotal_model model) {
return model.getOrderValue().compareTo(bigDecimal(arg0.getSymbolMin())) >= 0 &&
model.getOrderValue().compareTo(bigDecimal(arg0.getSymbolMax())) <= 0;
}
/**
* <b>C02: Discount</b><br>
* <br>
* <b>C02/01: >0 - discount given</b>
*/
public boolean isDiscount_Greater0(InvoiceTotal_model model) {
return model.getOrderDiscount().compareTo(BigDecimal.ZERO) > 0;
}
/**
* <b>C03: Discount type</b><br>
* <br>
* <b>C03/01: % - percentage discount on the purchase order value</b>
*/
@Override
public boolean isDiscountType_PercentageDiscountOnThePurchaseOrderValue(InvoiceTotal_model model) {
return model.isDiscountPercent();
}
/**
* <b>C04: Discount < purchase order value</b><br>
* <br>
* <b>C04/01: Y - Yes</b>
*/
@Override
public boolean isDiscountLessPurchaseOrderValue(InvoiceTotal_model model) {
return model.getOrderDiscount().compareTo(model.getOrderValue()) < 0;
}
/**
* <b>A01: Apply discount</b>
*/
@Override
public void doApplyDiscount(InvoiceTotal_model model) {
model.setTotalDiscount(model.isDiscountPercent()
? model.getOrderValue()
.multiply(model.getOrderDiscount())
.multiply(bigDecimal(0.01))
.setScale(2, RoundingMode.HALF_UP)
: model.getOrderDiscount());
}
/**
* <b>A02: Calculate shipping costs</b>
*/
@Override
public void doCalculateShippingCosts(InvoiceTotal_model model) {
model.setTotalPostage(model.getPostage());
}
/**
* <b>A03: Invoice total</b><br>
* <br>
* <b>A03/01: upd - Invoice total must be updated</b>
*/
@Override
public void doInvoiceTotal_Upd(InvoiceTotal_model model) {
model.setTotalAmount(model.getOrderValue()
.subtract(model.getTotalDiscount())
.add(model.getTotalPostage()));
}
/**
* <b>A03: Invoice total</b><br>
* <br>
* <b>A03/02: pov - Purchase order value can be taken over as invoice total</b>
*/
@Override
public void doInvoiceTotal_Pov(InvoiceTotal_model model) {
model.setTotalAmount(model.getOrderValue());
}
/**
* @param dtName - the decision table name
* @param version - the timestamp of program generation
* @param rules - the number of rules in the decision table
* @param rule - the rule which is executed now
*/
@Override
public void doTrace(String dtName, String version, int rules, int rule, InvoiceTotal_model model) {
System.out.printf("%s, %s, %d, %d, %s\n", dtName, version, rules, rule, model);
}
/**
* @param dtName - the decision table name
* @param version - the timestamp of program generation
* @param rules - the number of rules in the decision table
* @param rule - the rule which is executed now
*/
@Override
public void doTraceAfterRule(String dtName, String version, int rules, int rule, InvoiceTotal_model model) {
System.out.printf("%36s %s\n\n", "", model);
}
}