|
| 1 | +/* |
| 2 | + * Copyright 2023 The STARS Project Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package tools.aqua.stars.core.metric.metrics |
| 19 | + |
| 20 | +import kotlinx.coroutines.* |
| 21 | +import tools.aqua.stars.core.metric.providers.* |
| 22 | +import tools.aqua.stars.core.tsc.instance.TSCInstance |
| 23 | +import tools.aqua.stars.core.tsc.projection.TSCProjection |
| 24 | +import tools.aqua.stars.core.types.EntityType |
| 25 | +import tools.aqua.stars.core.types.SegmentType |
| 26 | +import tools.aqua.stars.core.types.TickDataType |
| 27 | + |
| 28 | +/** Wrapper class for [EvaluationMetricProvider]s. */ |
| 29 | +class EvaluationMetrics< |
| 30 | + E : EntityType<E, T, S>, T : TickDataType<E, T, S>, S : SegmentType<E, T, S>>( |
| 31 | + private val segmentMetrics: |
| 32 | + MutableMap<Class<SegmentMetricProvider<E, T, S>>, SegmentMetricProvider<E, T, S>> = |
| 33 | + mutableMapOf(), |
| 34 | + private val projectionMetrics: |
| 35 | + MutableMap<Class<ProjectionMetricProvider<E, T, S>>, ProjectionMetricProvider<E, T, S>> = |
| 36 | + mutableMapOf(), |
| 37 | + private val tscInstanceMetrics: |
| 38 | + MutableMap<Class<TSCInstanceMetricProvider<E, T, S>>, TSCInstanceMetricProvider<E, T, S>> = |
| 39 | + mutableMapOf(), |
| 40 | + private val tscInstanceAndProjectionMetrics: |
| 41 | + MutableMap< |
| 42 | + Class<TSCInstanceAndProjectionNodeMetricProvider<E, T, S>>, |
| 43 | + TSCInstanceAndProjectionNodeMetricProvider<E, T, S>> = |
| 44 | + mutableMapOf() |
| 45 | +) : Metrics<EvaluationMetricProvider<E, T, S>, E, T, S>() { |
| 46 | + |
| 47 | + /** Returns all registered [EvaluationMetricProvider]s. */ |
| 48 | + override fun all(): |
| 49 | + Map<Class<out EvaluationMetricProvider<E, T, S>>, EvaluationMetricProvider<E, T, S>> = |
| 50 | + segmentMetrics + projectionMetrics + tscInstanceMetrics + tscInstanceAndProjectionMetrics |
| 51 | + |
| 52 | + /** Registers metric providers. */ |
| 53 | + fun register(metrics: List<EvaluationMetricProvider<E, T, S>>) { |
| 54 | + metrics.forEach { |
| 55 | + when (it) { |
| 56 | + is SegmentMetricProvider -> segmentMetrics[it.javaClass] = it |
| 57 | + is ProjectionMetricProvider -> projectionMetrics[it.javaClass] = it |
| 58 | + is TSCInstanceMetricProvider -> tscInstanceMetrics[it.javaClass] = it |
| 59 | + is TSCInstanceAndProjectionNodeMetricProvider -> |
| 60 | + tscInstanceAndProjectionMetrics[it.javaClass] = it |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Run the "evaluate" function for all [SegmentMetricProvider]s on the current [segment]. |
| 67 | + * |
| 68 | + * @param segment The current [SegmentType]. |
| 69 | + */ |
| 70 | + fun evaluateSegmentMetrics(segment: SegmentType<E, T, S>) { |
| 71 | + segmentMetrics.values.forEach { it.evaluate(segment) } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Run the "evaluate" function for all [ProjectionMetricProvider]s on the current [projection]. |
| 76 | + * |
| 77 | + * @param projection The current [TSCProjection]. |
| 78 | + */ |
| 79 | + fun evaluateProjectionMetrics(projection: TSCProjection<E, T, S>) { |
| 80 | + projectionMetrics.values.forEach { it.evaluate(projection) } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Run the "evaluate" function for all [TSCInstanceMetricProvider]s on the current [instance]. |
| 85 | + * |
| 86 | + * @param instance The current [TSCInstance]. |
| 87 | + */ |
| 88 | + fun evaluateTSCInstanceMetrics(instance: TSCInstance<E, T, S>) { |
| 89 | + tscInstanceMetrics.values.forEach { it.evaluate(instance) } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Run the "evaluate" function for all [TSCInstanceAndProjectionNodeMetricProvider]s on the |
| 94 | + * current [instance] and [projection]. |
| 95 | + * |
| 96 | + * @param instance The current [TSCInstance]. |
| 97 | + * @param projection The current [TSCProjection]. |
| 98 | + */ |
| 99 | + fun evaluateTSCInstanceAndProjectionMetrics( |
| 100 | + instance: TSCInstance<E, T, S>, |
| 101 | + projection: TSCProjection<E, T, S> |
| 102 | + ) { |
| 103 | + tscInstanceAndProjectionMetrics.values.forEach { it.evaluate(instance, projection) } |
| 104 | + } |
| 105 | + |
| 106 | + /** Deeply copies the [EvaluationMetrics] object resetting all saved instances. */ |
| 107 | + override fun copy(): EvaluationMetrics<E, T, S> = |
| 108 | + EvaluationMetrics( |
| 109 | + segmentMetrics.map { (k, v) -> Pair(k, v.copy()) }.toMap(mutableMapOf()), |
| 110 | + projectionMetrics.map { (k, v) -> Pair(k, v.copy()) }.toMap(mutableMapOf()), |
| 111 | + tscInstanceMetrics.map { (k, v) -> Pair(k, v.copy()) }.toMap(mutableMapOf()), |
| 112 | + tscInstanceAndProjectionMetrics.map { (k, v) -> Pair(k, v.copy()) }.toMap(mutableMapOf())) |
| 113 | + |
| 114 | + companion object { |
| 115 | + /** |
| 116 | + * Merges given list of [EvaluationMetrics] in parallel. Returns a new [EvaluationMetrics] |
| 117 | + * instance. |
| 118 | + */ |
| 119 | + fun <E : EntityType<E, T, S>, T : TickDataType<E, T, S>, S : SegmentType<E, T, S>> merge( |
| 120 | + metrics: List<EvaluationMetrics<E, T, S>> |
| 121 | + ): EvaluationMetrics<E, T, S> = |
| 122 | + EvaluationMetrics<E, T, S>().apply { |
| 123 | + register( |
| 124 | + runBlocking { |
| 125 | + metrics |
| 126 | + // Receive all provider maps |
| 127 | + .map { it.all() } |
| 128 | + .asSequence() |
| 129 | + // Flatmap and group to one map |
| 130 | + .flatMap { it.asSequence() } |
| 131 | + .groupBy({ it.key }, { it.value }) |
| 132 | + // Create async jobs |
| 133 | + .map { async { combineResults(it.value.toMutableList()) } } |
| 134 | + .awaitAll() // Await completion |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + private fun < |
| 139 | + K : EvaluationMetricProvider<E, T, S>, |
| 140 | + E : EntityType<E, T, S>, |
| 141 | + T : TickDataType<E, T, S>, |
| 142 | + S : SegmentType<E, T, S>> combineResults(providers: MutableList<K>): K { |
| 143 | + check(providers.isNotEmpty()) { "Empty list of metric providers encountered." } |
| 144 | + |
| 145 | + val instances = providers.toMutableList() |
| 146 | + val instance = instances.removeFirst() |
| 147 | + |
| 148 | + instances.forEach { instance.merge(it) } |
| 149 | + |
| 150 | + return instance |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments