2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.aquarium.logic.accounting.dsl
39 import scala.collection._
40 //TODO: REMOVE THIS CLASS
42 * A semantic checker for the Aquarium accounting DSL.
44 * @author Georgios Gousios <gousiosg@gmail.com>
46 /*trait DSLSemanticChecks {
49 * Functions to apply by default when checking consistency for
50 * [[gr.grnet.aquarium.logic.dsl.DSLAlgorithm]] resources.
52 val policyChecks = List[DSLAlgorithm => List[DSLConsistencyMsg]](
56 * Functions to apply by default when checking consistency for
57 * [[gr.grnet.aquarium.logic.dsl.DSLPriceList]] resources.
59 val priceListChecks = List[DSLPriceList => List[DSLConsistencyMsg]](
63 * Functions to apply by default when checking consistency for
64 * [[gr.grnet.aquarium.logic.dsl.DSLAgreement]] resources.
66 val agreementChecks = List[DSLAgreement => List[DSLConsistencyMsg]](
70 * Functions to apply by default when checking consistency for
71 * [[gr.grnet.aquarium.logic.dsl.DSLTimeFrame]] resources.
73 val timeFrameChecks = List[DSLTimeFrame => List[DSLConsistencyMsg]](
75 checkTimeNotInitialized,
80 * Apply a list of consistency checking functions to a DSL entity and
83 def check[A](resource: A, checks: List[A => List[DSLConsistencyMsg]]) :
84 List[DSLConsistencyMsg] = {
85 checks.map(f => f(resource)).flatten.toList
89 * Top level consistency check functions. Applies all tests on all resources.
91 def check(creditPolicy: DSLPolicy) : List[DSLConsistencyMsg] = {
92 List[DSLConsistencyMsg]() ++
93 creditPolicy.pricelists.flatMap(p => check(p)) ++
94 creditPolicy.algorithms.flatMap(p => check(p)) ++
95 creditPolicy.agreements.flatMap(a => check(a))
98 /** Apply [[gr.grnet.aquarium.logic.dsl.DSLPriceList]] related checks on a pricelist */
99 def check(pl: DSLPriceList): List[DSLConsistencyMsg] = check(pl, priceListChecks)
101 /** Apply [[gr.grnet.aquarium.logic.dsl.DSLAlgorithm]] related checks on a algorithm */
102 def check(pl: DSLAlgorithm): List[DSLConsistencyMsg] = check(pl, policyChecks)
104 /** Apply [[gr.grnet.aquarium.logic.dsl.DSLAgreement]] related checks on a algorithm */
105 def check(pl: DSLAgreement): List[DSLConsistencyMsg] = check(pl, agreementChecks)
107 /** Apply [[gr.grnet.aquarium.logic.dsl.DSLTimeframe]] related checks on a timeframe */
108 def check(time: DSLTimeFrame): List[DSLConsistencyMsg] = check(time, timeFrameChecks)
110 /* -- Checker functions -- */
111 private def checkTimeFrameFromTo(time: DSLTimeFrame) : List[DSLConsistencyMsg] = {
112 if (time.from.after(time.to.getOrElse(new Date(0))))
113 List(DSLConsistencyError("Validity period %s ends before starting".format(time)))
118 private def checkTimeNotInitialized(time: DSLTimeFrame) : List[DSLConsistencyMsg] = {
119 if (time.repeat.isEmpty)
122 val result = new mutable.ListBuffer[DSLConsistencyMsg]
124 time.repeat.foreach {
127 r => if (r.hour == -1 || r.min == -1)
128 result += DSLConsistencyError(
129 "Hours and mins must always be initialized: %s".format(time))
133 e => if (e.hour == -1 || e.min == -1)
134 result += DSLConsistencyError(
135 "Hours and mins must always be initialized: %s".format(time))
141 private def checkRepeatHoles(time: DSLTimeFrame) : List[DSLConsistencyMsg] = {
142 val repeat = time.repeat
148 sealed trait DSLConsistencyMsg
149 case class DSLConsistencyWarn(warn: String) extends DSLConsistencyMsg
150 case class DSLConsistencyError(err: String) extends DSLConsistencyMsg*/