mirror of
https://github.com/corda/corda.git
synced 2025-02-20 17:33:15 +00:00
simpler syntax for rollout recursion parameters
This commit is contained in:
parent
446e373722
commit
2f7e05ee00
@ -70,6 +70,7 @@ enum class Operation {
|
||||
data class PerceivableOperation<T>(val left: Perceivable<T>, val op: Operation, val right: Perceivable<T>) : Perceivable<T>
|
||||
|
||||
operator fun Perceivable<BigDecimal>.plus(n: BigDecimal) = PerceivableOperation(this, Operation.PLUS, const(n))
|
||||
fun Perceivable<BigDecimal>.plus() = PerceivableOperation(this, Operation.PLUS, const(BigDecimal(0))) // todo
|
||||
operator fun Perceivable<BigDecimal>.minus(n: BigDecimal) = PerceivableOperation(this, Operation.MINUS, const(n))
|
||||
operator fun Perceivable<BigDecimal>.plus(n: Double) = PerceivableOperation(this, Operation.PLUS, const(BigDecimal(n)))
|
||||
operator fun Perceivable<BigDecimal>.minus(n: Double) = PerceivableOperation(this, Operation.MINUS, const(BigDecimal(n)))
|
||||
|
@ -2,6 +2,7 @@ package com.r3corda.contracts.universal
|
||||
|
||||
import com.r3corda.core.contracts.Amount
|
||||
import com.r3corda.core.contracts.Frequency
|
||||
import com.r3corda.core.contracts.USD
|
||||
import com.r3corda.core.crypto.Party
|
||||
import java.math.BigDecimal
|
||||
import java.util.*
|
||||
@ -106,53 +107,39 @@ fun arrange(init: ContractBuilder.() -> Unit ) : Arrangement {
|
||||
return b.final()
|
||||
}
|
||||
|
||||
interface Parameter {}
|
||||
class ParameterDeclaration<T> : Parameter {}
|
||||
data class Parameter<T>(val initialValue: T) : Perceivable<T>
|
||||
|
||||
class Foo
|
||||
{
|
||||
companion object {
|
||||
val Counter = ParameterDeclaration<Int>()
|
||||
val CurrencyAmount = ParameterDeclaration<Amount<Currency>>()
|
||||
}
|
||||
}
|
||||
fun<T> variable(v: T) = Parameter<T>(v)
|
||||
|
||||
|
||||
class RolloutBuilder<T>(val startDate: String, val endDate: String, val frequency: Frequency, val vars: T) {
|
||||
class RollOutBuilder<T>(val startDate: String, val endDate: String, val frequency: Frequency, val vars: T) {
|
||||
|
||||
val start = StartDate()
|
||||
val end = EndDate()
|
||||
|
||||
// fun next(vararg pairs: kotlin.Pair<Parameter, Any>) = Continuation()
|
||||
fun next() = Continuation()
|
||||
|
||||
fun<T1> next( @Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<ParameterDeclaration<T1>, Perceivable<T1>>) = Continuation()
|
||||
fun<T1, T2> next( @Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<ParameterDeclaration<T1>, Perceivable<T1>>,
|
||||
@Suppress("UNUSED_PARAMETER") p2: kotlin.Pair<ParameterDeclaration<T2>, Perceivable<T2>>) = Continuation()
|
||||
fun<T1, T2, T3> next( @Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<ParameterDeclaration<T1>, Perceivable<T1>>,
|
||||
@Suppress("UNUSED_PARAMETER") p2: kotlin.Pair<ParameterDeclaration<T2>, Perceivable<T2>>,
|
||||
@Suppress("UNUSED_PARAMETER") p3: kotlin.Pair<ParameterDeclaration<T3>, Perceivable<T3>>) = Continuation()
|
||||
|
||||
fun tmp_hack() : Any? = null
|
||||
|
||||
fun<U> get(@Suppress("UNUSED_PARAMETER") par: ParameterDeclaration<U>) : Perceivable<U> {
|
||||
@Suppress("UNCHECKED_CAST") return tmp_hack() as Perceivable<U>
|
||||
}
|
||||
fun<T1> next( @Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<Parameter<T1>, Perceivable<T1>>) = Continuation()
|
||||
fun<T1, T2> next(@Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<Parameter<T1>, Perceivable<T1>>,
|
||||
@Suppress("UNUSED_PARAMETER") p2: kotlin.Pair<Parameter<T2>, Perceivable<T2>>) = Continuation()
|
||||
fun<T1, T2, T3> next(@Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<Parameter<T1>, Perceivable<T1>>,
|
||||
@Suppress("UNUSED_PARAMETER") p2: kotlin.Pair<Parameter<T2>, Perceivable<T2>>,
|
||||
@Suppress("UNUSED_PARAMETER") p3: kotlin.Pair<Parameter<T3>, Perceivable<T3>>) = Continuation()
|
||||
|
||||
fun final() =
|
||||
RollOut(startDate, endDate, frequency, zero)
|
||||
}
|
||||
|
||||
|
||||
class Dummy {}
|
||||
|
||||
fun rollout(startDate: String, endDate: String, frequency: Frequency, init: RolloutBuilder<Dummy>.() -> Unit) : Arrangement {
|
||||
val b = RolloutBuilder<Dummy>(startDate, endDate, frequency, Dummy())
|
||||
fun rollOut(startDate: String, endDate: String, frequency: Frequency, init: RollOutBuilder<Dummy>.() -> Unit) : Arrangement {
|
||||
val b = RollOutBuilder(startDate, endDate, frequency, Dummy())
|
||||
b.init()
|
||||
return b.final()
|
||||
}
|
||||
|
||||
fun<T> rollout(startDate: String, endDate: String, frequency: Frequency, vars: T, init: RolloutBuilder<T>.() -> Unit) : Arrangement {
|
||||
val b = RolloutBuilder<T>(startDate, endDate, frequency, vars)
|
||||
fun<T> rollOut(startDate: String, endDate: String, frequency: Frequency, vars: T, init: RollOutBuilder<T>.() -> Unit) : Arrangement {
|
||||
val b = RollOutBuilder(startDate, endDate, frequency, vars)
|
||||
b.init()
|
||||
|
||||
return b.final()
|
||||
|
@ -43,7 +43,7 @@ class Swaption {
|
||||
}
|
||||
|
||||
|
||||
val elegant_contract = rollout( "01/04/2015", "01/04/2025", Frequency.Quarterly ) {
|
||||
val elegant_contract = rollOut( "01/04/2015", "01/04/2025", Frequency.Quarterly ) {
|
||||
(wileECoyote or roadRunner).may {
|
||||
"proceed".givenThat(after(start)) {
|
||||
wileECoyote.gives(roadRunner, libor( notional, start, end ) )
|
||||
@ -59,28 +59,47 @@ class Swaption {
|
||||
|
||||
|
||||
val strike = 1.2
|
||||
val tarf = rollout( "01/04/2015", "01/04/2016", Frequency.Quarterly, object {
|
||||
val cap = Foo.CurrencyAmount
|
||||
|
||||
val tarf = rollOut( "01/04/2015", "01/04/2016", Frequency.Quarterly, object {
|
||||
val cap = variable( 150.K*USD )
|
||||
}) {
|
||||
roadRunner.may {
|
||||
"exercise".givenThat(before(start)) {
|
||||
val payout = (EUR / USD - strike) * notional
|
||||
"exercise".givenThat(before(end)) {
|
||||
val payout = (EUR / USD - strike).plus() * notional
|
||||
|
||||
wileECoyote.gives(roadRunner, payout)
|
||||
|
||||
next(vars.cap to get(vars.cap) - payout)
|
||||
(roadRunner or wileECoyote).may {
|
||||
"proceed".givenThat(after(end)) {
|
||||
wileECoyote.gives(roadRunner, payout)
|
||||
next(vars.cap to vars.cap - payout)
|
||||
}
|
||||
}
|
||||
}
|
||||
} or (roadRunner or wileECoyote).may {
|
||||
"proceed".givenThat(after(start)) {
|
||||
"proceedWithoutExercise".givenThat(after(end)) {
|
||||
next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val tarf2 = rollout( "01/04/2015", "01/04/2016", Frequency.Quarterly, object { val uses = Foo.Counter } ) {
|
||||
val z = get(vars.uses)
|
||||
val tarf2 = rollOut( "01/04/2015", "01/04/2016", Frequency.Quarterly, object {
|
||||
val uses = variable( 4 )
|
||||
}) {
|
||||
roadRunner.may {
|
||||
"exercise".givenThat(before(end)) {
|
||||
val payout = (EUR / USD - strike).plus() * notional
|
||||
|
||||
next( vars.uses to z - 1)
|
||||
(roadRunner or wileECoyote).may {
|
||||
"proceed".givenThat(after(end)) {
|
||||
wileECoyote.gives(roadRunner, payout)
|
||||
next(vars.uses to vars.uses - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
} or (roadRunner or wileECoyote).may {
|
||||
"proceedWithoutExercise".givenThat(after(end)) {
|
||||
next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user