Skip to content

Commit

Permalink
19 tackle code scanning alerts (#20)
Browse files Browse the repository at this point in the history
* Moved Json objects from CarlaDataClasses.kt to dedicated files.

* Restructured RunSegmentation.kt. A

* Restructured CarlaToAVConverter.kt. 

* Restructured CarlaDataFileHandling.kt. 

* Extracted AV data classes to dedicated files.

* Removed outdated version binding from kotlin-dsl plugin

* Split CMFTBL extensions into separate files.

* Upgraded kotlin version from 1.7.10 to 1.7.20.

* Upgraded serialization plugin version to 1.9.10.
  • Loading branch information
dominikmaeckel authored Dec 1, 2023
1 parent bf1a5b2 commit 6ec4fc9
Show file tree
Hide file tree
Showing 130 changed files with 6,329 additions and 3,563 deletions.
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

plugins { `kotlin-dsl`.version("3.1.0") }
plugins { `kotlin-dsl` }

repositories {
gradlePluginPortal()
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Platforms

kotlin = "1.7.10"
kotlin = "1.9.10"
spring = "2.7.2"
vaadin = "23.1.6"

Expand Down
51 changes: 46 additions & 5 deletions stars-core/src/main/kotlin/tools/aqua/stars/core/ListExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
* limitations under the License.
*/

@file:Suppress("unused")

package tools.aqua.stars.core

/** creates TxT cross product */
fun <T> List<T>.x2() = this.flatMap { a -> this.map { b -> a to b } }
/** Creates TxT cross product. */
fun <T> List<T>.x2(): List<Pair<T, T>> = this.flatMap { a -> this.map { b -> a to b } }

/** creates TxTxT cross product */
fun <T> List<T>.x3() = this.flatMap { a -> this.flatMap { b -> this.map { c -> Triple(a, b, c) } } }
/** Creates TxTxT cross product. */
fun <T> List<T>.x3(): List<Triple<T, T, T>> =
this.flatMap { a -> this.flatMap { b -> this.map { c -> Triple(a, b, c) } } }

/**
* Adaption of com.marcinmoskala.math.powerset for lists while preserving the order of the original
Expand All @@ -31,5 +34,43 @@ fun <T> List<T>.x3() = this.flatMap { a -> this.flatMap { b -> this.map { c -> T
fun <T> List<T>.powerlist(): List<List<T>> =
when {
isEmpty() -> listOf(listOf())
else -> dropLast(1).powerlist().let { it + it.map { it + last() } }.sortedBy { it.size }
else -> dropLast(1).powerlist().let { it + it.map { t -> t + last() } }.sortedBy { it.size }
}

/**
* Build all possible combinations of the lists in the input list. Example instances kept until
* better documentation will be written.
* ```
* val input = listOf(
* listOf(listOf("a"), listOf("b"), listOf("c")),
* listOf(listOf("x"), listOf("y")),
* listOf(listOf("1"), listOf("2"), listOf("3"), listOf("4"))
* )
*
* val afterFirstStep = listOf(
* listOf(listOf("a", "x"), listOf("a", "y"), /*...*/ listOf("c", "y")),
* listOf(listOf("1"), listOf("2"), listOf("3"), listOf("4"))
* )
*
* val afterSecondStep = listOf(
* listOf(listOf("a", "x", "1"), listOf("a", "x", "2"), /*...*/ listOf("c", "y", "4"))
* )
* ```
*/
fun <T> List<List<List<T>>>.crossProduct(): List<List<T>> {
require(size >= 2) { "List for cross-product building must at least contain two elements." }

val nextLevelList = mutableListOf<List<T>>()
this[0].forEach { it1 ->
this[1].forEach { it2 ->
val nextEntry = mutableListOf<T>()
nextEntry.addAll(it1)
nextEntry.addAll(it2)
nextLevelList += nextEntry
}
}

// val monitorFunction: (PredicateContext, Segment) -> Boolean = { _, _ -> true }

return if (size == 2) nextLevelList else (listOf(nextLevelList) + subList(2, size)).crossProduct()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2023 The STARS Project Authors
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

package tools.aqua.stars.core.evaluation

import kotlin.reflect.KClass
import tools.aqua.stars.core.types.EntityType
import tools.aqua.stars.core.types.SegmentType
import tools.aqua.stars.core.types.TickDataType

/**
* Binary predicate.
*
* @param E1 [EntityType]
* 1.
* @param E2 [EntityType]
* 2.
* @param E [EntityType].
* @param T [TickDataType].
* @param S [SegmentType].
* @property eval The evaluation function on the [PredicateContext].
* @property kClasses The actors.
*/
class BinaryPredicate<
E1 : E, E2 : E, E : EntityType<E, T, S>, T : TickDataType<E, T, S>, S : SegmentType<E, T, S>>(
val eval: (PredicateContext<E, T, S>, E1, E2) -> Boolean,
val kClasses: Pair<KClass<E1>, KClass<E2>>
) {

/**
* Checks if this predicate holds (i.e. is true) in the given context.
*
* @param ctx The context this predicate is evaluated in.
* @param tickId The time stamp to evaluate this predicate in. default: first tick in context.
* @param actor1 The ID of the first actor to evaluate this predicate for. default: ego vehicle.
* @param actor2 The ID of the second actor to evaluate this predicate for.
*/
fun holds(
ctx: PredicateContext<E, T, S>,
tickId: Double = ctx.segment.firstTickId,
actor1: Int = ctx.primaryEntityId,
actor2: Int
): Boolean = ctx.holds(this, tickId, actor1, actor2)

/**
* Checks if this predicate holds (i.e. is true) in the given context on current tick.
*
* @param ctx The context this predicate is evaluated in.
* @param actor1 The ID of the first actor to evaluate this predicate for. default: ego vehicle.
* @param actor2 The ID of the second actor to evaluate this predicate for.
*/
fun holds(ctx: PredicateContext<E, T, S>, actor1: E1, actor2: E2): Boolean =
holds(
ctx,
actor1.tickData.currentTick.apply {
if (this != actor2.tickData.currentTick) error("ticks don't match")
},
actor1.id,
actor2.id)

companion object {
/** Creates a binary tick predicate in this context. */
fun <
E1 : E,
E2 : E,
E : EntityType<E, T, S>,
T : TickDataType<E, T, S>,
S : SegmentType<E, T, S>> predicate(
kClasses: Pair<KClass<E1>, KClass<E2>>,
eval: (PredicateContext<E, T, S>, E1, E2) -> Boolean
): BinaryPredicate<E1, E2, E, T, S> = BinaryPredicate(eval, kClasses)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2023 The STARS Project Authors
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

package tools.aqua.stars.core.evaluation

import tools.aqua.stars.core.types.EntityType
import tools.aqua.stars.core.types.SegmentType
import tools.aqua.stars.core.types.TickDataType

/**
* Nullary predicate.
*
* @param E [EntityType].
* @param T [TickDataType].
* @param S [SegmentType].
* @property eval The evaluation function on the [PredicateContext].
*/
class NullaryPredicate<
E : EntityType<E, T, S>, T : TickDataType<E, T, S>, S : SegmentType<E, T, S>>(
val eval: (PredicateContext<E, T, S>, T) -> Boolean,
) {

/** Evaluates predicate on [PredicateContext]. */
fun evaluate(ctx: PredicateContext<E, T, S>): List<Double> = ctx.evaluate(this)

/**
* Checks if this predicate holds (i.e. is true) in the given context and tick identifier.
*
* @param ctx The context this predicate is evaluated in.
* @param tickId The time stamp to evaluate this predicate in. default: first tick in context.
*/
fun holds(ctx: PredicateContext<E, T, S>, tickId: Double): Boolean =
evaluate(ctx).contains(tickId)

companion object {
/** Creates a nullary tick predicate. */
fun <E : EntityType<E, T, S>, T : TickDataType<E, T, S>, S : SegmentType<E, T, S>> predicate(
eval: (PredicateContext<E, T, S>, T) -> Boolean
): NullaryPredicate<E, T, S> = NullaryPredicate(eval)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,36 @@ package tools.aqua.stars.core.evaluation
/**
* This is a simple class to store two "predicates" given by their [String] representation.
*
* @param predicate1 The name of the first predicate
* @param predicate2 The name of the second predicate
* @property predicate1 The name of the first predicate.
* @property predicate2 The name of the second predicate.
*/
class PredicateCombination(val predicate1: String, val predicate2: String) {
data class PredicateCombination(val predicate1: String, val predicate2: String) {
/**
* Check if this is equals to another object.
* Checks if this is equals to another object.
*
* If [other] is also a [PredicateCombination] then the two are equals when the two predicates are
* equal. The order is irrelevant. ["pre1", "pre2"] == ["pre2", "pre1"] is true
* equal. The order is irrelevant. ["pre1", "pre2"] == ["pre2", "pre1"] is true.
*
* @param other The other object to which "this" should be checked for equality
* @param other The other object to which "this" should be checked for equality.
*
* @return Whether the two object are equal
* @return Whether the two object are equal.
*/
override fun equals(other: Any?): Boolean {
if (other is PredicateCombination) {
return (predicate1 == other.predicate1 && predicate2 == other.predicate2) ||
(predicate1 == other.predicate2 && predicate2 == other.predicate1)
}
return super.equals(other)
}
override fun equals(other: Any?): Boolean =
other is PredicateCombination &&
((predicate1 == other.predicate1 && predicate2 == other.predicate2) ||
(predicate1 == other.predicate2 && predicate2 == other.predicate1))

/**
* Returns the hashCode as [Int] based on the two predicates
* Returns the hashCode as [Int] based on the two predicates.
*
* @return the hashCode for this object
* @return the hashCode for this object.
*/
override fun hashCode(): Int {
return (predicate1 + predicate2).toSortedSet().hashCode()
}
override fun hashCode(): Int = (predicate1 + predicate2).toSortedSet().hashCode()

/**
* Formats the [PredicateCombination] nicely
* Formats the [PredicateCombination] nicely.
*
* @return The [PredicateCombination] as a nicely formatted [String]
* @return The [PredicateCombination] as a nicely formatted [String].
*/
override fun toString(): String {
return "[$predicate1, $predicate2]"
}
override fun toString(): String = "[$predicate1, $predicate2]"
}
Loading

0 comments on commit 6ec4fc9

Please sign in to comment.