From a85c6b9b5e3b2e9ff02fe61a8f38ffd447e02e58 Mon Sep 17 00:00:00 2001 From: Georgios Gousios Date: Tue, 29 Nov 2011 18:23:55 +0200 Subject: [PATCH] Type checking magic by @loverdos. Also, take care of wholes in the resolution of DSLTimeBoundedItems --- .../logic/accounting/dsl/DSLAlgorithm.scala | 4 +- .../logic/accounting/dsl/DSLTimeBoundedItem.scala | 6 +-- .../aquarium/logic/accounting/dsl/DSLUtils.scala | 45 ++++++++++++++++---- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLAlgorithm.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLAlgorithm.scala index e87619e..b970a3a 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLAlgorithm.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLAlgorithm.scala @@ -43,9 +43,7 @@ package gr.grnet.aquarium.logic.accounting.dsl */ case class DSLAlgorithm ( name: String, - override val overrides: Option[DSLAlgorithm], + override val overrides: Option[DSLAlgorithm], algorithms: Map[DSLResource, String], override val effective: DSLTimeFrame ) extends DSLTimeBoundedItem[DSLAlgorithm](overrides, effective) - - diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLTimeBoundedItem.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLTimeBoundedItem.scala index 9e4c5a3..9695344 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLTimeBoundedItem.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLTimeBoundedItem.scala @@ -36,10 +36,10 @@ package gr.grnet.aquarium.logic.accounting.dsl /** - * A DSL dsltbi whose effectivity is constrained by well defined time bounds. + * A DSL tbi whose effectivity is constrained by well defined time bounds. * All time bounded items also support inheritance. * * @author Georgios Gousios */ -abstract class DSLTimeBoundedItem[T](val overrides: Option[DSLTimeBoundedItem[T]], - val effective: DSLTimeFrame) +abstract class DSLTimeBoundedItem[T <: DSLTimeBoundedItem[T]](val overrides: Option[T], + val effective: DSLTimeFrame) diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala index a37e2ba..8f48a86 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala @@ -45,19 +45,40 @@ import java.util.{Date, GregorianCalendar, Calendar} */ trait DSLUtils extends DateUtils { + def resolveEffectiveAlgorithmsForTimeslot(timeslot: (Date, Date), + agr: DSLAgreement): + Map[(Date, Date), DSLAlgorithm] = + resolveEffective[DSLAlgorithm](timeslot, Some(agr.algorithm)) + + + def resolveEffectivePricelistsForTimeslot(timeslot: (Date, Date), + agr: DSLAgreement): + Map[(Date, Date), DSLPriceList] = + resolveEffective[DSLPriceList](timeslot, Some(agr.pricelist)) + /** - * + * Resolves the DSLTimeBoundedItem which is active within the + * provided timeslot. If the provided timeslot does not fit entirely or at all + * into a timeslot within which a DSLTimeBoundedItem is active, then the + * resolution takes the following paths: + * + * - If the provided timeslot (a) partially fits into the DSLTimeBoundedItem + * timeslot (b) and the next active time slot is (c), then the provided + * timeslot is split in three parts `(a.start...b.end)`, + * `(b.end...c.start)` and `(c.start...a.end)` + * */ - def findEffective[T](timeslot: (Date, Date), - dsltbi: Option[DSLTimeBoundedItem[T]]): - Map[(Date, Date), DSLTimeBoundedItem[T]] = { + def resolveEffective[T <: DSLTimeBoundedItem[T]](timeslot: (Date, Date), + tbi: Option[T]): + Map[(Date, Date), T] = { - val item = dsltbi match { + val item = tbi match { case None => return Map() - case _ => dsltbi.get + case _ => tbi.get } - val eff = allEffectiveTimeslots(item.effective, item.effective.from, timeslot._2) + val eff = allEffectiveTimeslots(item.effective, + item.effective.from, timeslot._2) val res = eff.find(t => contains(t, timeslot)) match { case Some(x) => Map(x -> item) @@ -67,8 +88,14 @@ trait DSLUtils extends DateUtils { (new Date(Int.MaxValue), new Date(Int.MaxValue)) else eff.apply(eff.lastIndexOf(y) + 1) - Map((timeslot._1, y._2) -> item) ++ findEffective((next._1, timeslot._2), item.overrides) - case None => findEffective(timeslot, item.overrides) + Map((timeslot._1, y._2) -> item) ++ ( + if (timeslot._2.before(next._1)) + resolveEffective((y._2, timeslot._2), item.overrides) + else + resolveEffective((y._2, next._1), item.overrides) ++ + resolveEffective((next._1, timeslot._2), item.overrides) + ) + case None => resolveEffective(timeslot, item.overrides) } } -- 1.7.10.4