Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
feature(Draft) New wrapVariable representation for conditional variab…
Browse files Browse the repository at this point in the history
…les with whenTrue/whenFalse

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Feb 18, 2020
1 parent 352c0eb commit 1ac046d
Show file tree
Hide file tree
Showing 17 changed files with 7,629 additions and 7,405 deletions.
17 changes: 17 additions & 0 deletions examples/latedeliveryandpenaltyelse/data-noforcemajeure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$class": "org.accordproject.latedeliveryandpenalty.TemplateModel",
"forceMajeure": false,
"penaltyDuration": {
"$class": "org.accordproject.time.Duration",
"amount": 2,
"unit": "days"
},
"penaltyPercentage": 10.5,
"capPercentage": 55,
"termination": {
"$class": "org.accordproject.time.Duration",
"amount": 15,
"unit": "days"
},
"fractionalPart": "days"
}
17 changes: 17 additions & 0 deletions examples/latedeliveryandpenaltyelse/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$class": "org.accordproject.latedeliveryandpenalty.TemplateModel",
"forceMajeure": true,
"penaltyDuration": {
"$class": "org.accordproject.time.Duration",
"amount": 2,
"unit": "days"
},
"penaltyPercentage": 10.5,
"capPercentage": 55,
"termination": {
"$class": "org.accordproject.time.Duration",
"amount": 15,
"unit": "days"
},
"fractionalPart": "days"
}
51 changes: 51 additions & 0 deletions examples/latedeliveryandpenaltyelse/logic/logic.ergo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.latedeliveryandpenalty

import org.accordproject.cicero.runtime.*
import org.accordproject.time.*

// Declare a contract over a template model
contract LateDeliveryAndPenalty over TemplateModel {
// Clause checking for late delivery and calculating penalty
clause latedeliveryandpenalty(request : LateDeliveryAndPenaltyRequest) : LateDeliveryAndPenaltyResponse {
// Guard against calling late delivery clause too early
let agreed = request.agreedDelivery;
enforce isBefore(agreed,now()) else
throw ErgoErrorResponse{ message: "Cannot exercise late delivery before delivery date" }
;
// Guard against force majeure
enforce !contract.forceMajeure or !request.forceMajeure else
return LateDeliveryAndPenaltyResponse{
penalty: 0.0,
buyerMayTerminate: true
}
;

// Calculate the time difference between current date and agreed upon date
let diff = diffDurationAs(now(),agreed,~org.accordproject.time.TemporalUnit.days).amount;
// Penalty formula
let penalty =
(integerToDouble(diff) / integerToDouble(contract.penaltyDuration.amount))
* contract.penaltyPercentage/100.0 * request.goodsValue;
// Penalty may be capped
let capped = min([penalty, contract.capPercentage * request.goodsValue / 100.0]);
// Return the response with the penalty and termination determination
return LateDeliveryAndPenaltyResponse{
penalty: capped,
buyerMayTerminate: diff > contract.termination.amount
}
}
}
96 changes: 96 additions & 0 deletions examples/latedeliveryandpenaltyelse/model/model.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.latedeliveryandpenalty

import org.accordproject.time.*

/**
* Defines the data model for the LateDeliveryAndPenalty template.
* This defines the structure of the abstract syntax tree that the parser for the template
* must generate from input source text.
*/
@AccordTemplateModel("latedeliveryandpenalty")
concept TemplateModel {
/**
* Does the clause include a force majeure provision?
*/
o Boolean forceMajeure

/**
* For every penaltyDuration that the goods are late
*/
o Duration penaltyDuration

/**
* Seller pays the buyer penaltyPercentage % of the value of the goods
*/
o Double penaltyPercentage

/**
* Up to capPercentage % of the value of the goods
*/
o Double capPercentage

/**
* If the goods are >= termination late then the buyer may terminate the contract
*/
o Duration termination

/**
* Fractional part of a ... is considered a whole ...
*/
o TemporalUnit fractionalPart
}

/**
* Defines the input data required by the template
*/
transaction LateDeliveryAndPenaltyRequest {

/**
* Are we in a force majeure situation?
*/
o Boolean forceMajeure

/**
* What was the agreed delivery date for the goods?
*/
o DateTime agreedDelivery

/**
* If the goods have been delivered, when where they delivered?
*/
o DateTime deliveredAt optional

/**
* What is the value of the goods?
*/
o Double goodsValue
}

/**
* Defines the output data for the template
*/
transaction LateDeliveryAndPenaltyResponse {
/**
* The penalty to be paid by the seller
*/
o Double penalty

/**
* Whether the buyer may terminate the contract
*/
o Boolean buyerMayTerminate
}
7 changes: 7 additions & 0 deletions examples/latedeliveryandpenaltyelse/request-early.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$class": "org.accordproject.latedeliveryandpenalty.LateDeliveryAndPenaltyRequest",
"forceMajeure": false,
"agreedDelivery": "2019-01-31 03:24:00Z",
"deliveredAt": null,
"goodsValue": 200.00
}
7 changes: 7 additions & 0 deletions examples/latedeliveryandpenaltyelse/request-forcemajeure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$class": "org.accordproject.latedeliveryandpenalty.LateDeliveryAndPenaltyRequest",
"forceMajeure": true,
"agreedDelivery": "2018-01-31 03:24:00Z",
"deliveredAt": null,
"goodsValue": 200.00
}
7 changes: 7 additions & 0 deletions examples/latedeliveryandpenaltyelse/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$class": "org.accordproject.latedeliveryandpenalty.LateDeliveryAndPenaltyRequest",
"forceMajeure": false,
"agreedDelivery": "2018-01-31 03:24:00Z",
"deliveredAt": null,
"goodsValue": 200.00
}
2 changes: 2 additions & 0 deletions examples/latedeliveryandpenaltyelse/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ "$class": "org.accordproject.cicero.contract.AccordContractState",
"stateId" : "1" }
1 change: 1 addition & 0 deletions examples/latedeliveryandpenaltyelse/text/grammar.tem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Late Delivery and Penalty. In case of delayed delivery{{#if forceMajeure}} except for Force Majeure cases,{{else}} even in cases of Force Majeure,{{/if}} the Seller shall pay to the Buyer for every {{penaltyDuration}} of delay penalty amounting to {{penaltyPercentage}}% of the total value of the Equipment whose delivery has been delayed. Any fractional part of a {{fractionalPart}} is to be considered a full {{fractionalPart}}. The total amount of penalty shall not however, exceed {{capPercentage}}% of the total value of the Equipment involved in late delivery. If the delay is more than {{termination}}, the Buyer is entitled to terminate this Contract.
21 changes: 10 additions & 11 deletions extraction/src/ErgoParser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ let wrap_template_variable_as prov name ve fe =
(relative_name_of_qname (Some "org.accordproject.ergo.template","variableTagAs"))
[varparam;ve;fe]

let wrap_template_if_block prov name ve =
let wrap_template_if_block prov name veCond veTrue veFalse =
let varparam =
ErgoCompiler.econst prov
(ErgoCompiler.ErgoData.dstring (Util.char_list_of_string name))
in
ErgoCompiler.ecallfun
prov
(relative_name_of_qname (Some "org.accordproject.ergo.template","ifBlockTag"))
[varparam;ve]
[varparam;veCond;veTrue;veFalse]

let wrap_template_computed prov e =
let textparam = e in
Expand Down Expand Up @@ -152,20 +152,19 @@ let make_template_with prov name ve =
let make_template_clause prov name ve =
make_template_with prov name ve (* XXX May have to be revised eventually *)

let make_template_if_else prov name ve1 ve2 =
let make_template_if_else prov name veTrue veFalse =
let a = Util.char_list_of_string name in
let econd = ErgoCompiler.eunaryoperator prov (EOpDot a) (ErgoCompiler.ethis_this prov) in
wrap_template_if_block
prov
name
(ErgoCompiler.eif prov
econd
(ErgoCompiler.etext prov ve1)
(ErgoCompiler.etext prov ve2))

let make_template_if prov name ve1 =
let ve2 = [ErgoCompiler.econst prov (ErgoCompiler.ErgoData.dstring (Util.char_list_of_string ""))] in
make_template_if_else prov name ve1 ve2
econd
(ErgoCompiler.etext prov veTrue)
(ErgoCompiler.etext prov veFalse)

let make_template_if prov name veTrue =
let veFalse = [ErgoCompiler.econst prov (ErgoCompiler.ErgoData.dstring (Util.char_list_of_string ""))] in
make_template_if_else prov name veTrue veFalse

%}

Expand Down
Loading

0 comments on commit 1ac046d

Please sign in to comment.