diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1a6fec5d3fea8aadf87949a56cd830257c0de54f..8a50cca89393ed6eb1471c21f5ed72e566ba2c44 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,7 +16,7 @@ test: - gradle test artifacts: reports: - junit: lib/build/test-results/test/**/TEST-*.xml + junit: build/test-results/test/**/TEST-*.xml publish: stage: publish diff --git a/lib/build.gradle.kts b/build.gradle.kts similarity index 98% rename from lib/build.gradle.kts rename to build.gradle.kts index 9248b795e25ae4cfffa675ccc2ee578816d0582e..04e7d6c97eb9491ef0a1c21b54dd0a3c78ff1a20 100644 --- a/lib/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ group = "hu.bme.i9rv7w" -version = "0.3" +version = "0.4" plugins { id("org.jetbrains.kotlin.jvm") version "1.4.20" diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Entity.kt b/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Entity.kt deleted file mode 100644 index 2a43066022bbd11d7954ead883e061436e197b36..0000000000000000000000000000000000000000 --- a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Entity.kt +++ /dev/null @@ -1,49 +0,0 @@ -package hu.bme.i9rv7w.ergraph.core.schema - -import hu.bme.i9rv7w.ergraph.core.util.Serial -import java.util.* - -open class Entity { - - val uuid: UUID = UUID.randomUUID() - val labels = mutableSetOf<Label>() - val marks = mutableSetOf<String>() - - // related entities - val relatives = mutableMapOf<Relation, Entity>() - - val type: String = entityType(this.javaClass) - - val serialNumber: String - get() = labels.first { it.name == "serial" }.value - - init { - Graph.addEntity(this) // sorry, it's by design - val serial = Serial.generateSerialFor(this).toString() - labels.add(Label("serial", serial)) - } - - fun relate(relation: Relation, entity: Entity) { - relatives[relation] = entity - } - - override fun toString(): String { - var out = "$type-$serialNumber" - if (marks.isEmpty()) - return out - - val marks = marks.joinToString() - out += "|marks:$marks" - return out - } - - fun mark(mark: String) = marks.add(mark) - fun markedWith(mark: String) = marks.contains(mark) - - inline fun <reified T : Entity> cast(): T? { - val target: String = entityType(T::class.java) - if (target != type) return null - return this as T - } - -} diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Label.kt b/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Label.kt deleted file mode 100644 index f937bb22124d607ed4c795fe577141ea12cfb010..0000000000000000000000000000000000000000 --- a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Label.kt +++ /dev/null @@ -1,3 +0,0 @@ -package hu.bme.i9rv7w.ergraph.core.schema - -data class Label(val name: String, val value: String) \ No newline at end of file diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/model/network/NetzwerkStuff.kt b/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/model/network/NetzwerkStuff.kt deleted file mode 100644 index ee8f58a82fb3b08b5b4969b9b9001e221a0468a8..0000000000000000000000000000000000000000 --- a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/model/network/NetzwerkStuff.kt +++ /dev/null @@ -1,9 +0,0 @@ -package hu.bme.i9rv7w.ergraph.model.network - -import hu.bme.i9rv7w.ergraph.core.schema.ComposableEntity -import hu.bme.i9rv7w.ergraph.core.schema.Entity - -class Node : ComposableEntity<Port>() -class Port : Entity() -class Cable : Entity() -class Service : Entity() diff --git a/settings.gradle.kts b/settings.gradle.kts index 5b4957f93a1e38e26637a25e770a8a3ad7c4cee4..e6545385994ec02c73018810285f71e301bee621 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,2 +1 @@ rootProject.name = "er-lib" -include("lib") diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/SameAs.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/SameAs.kt similarity index 100% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/SameAs.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/SameAs.kt diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Connects.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Connects.kt similarity index 100% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Connects.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Connects.kt diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Consists.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Consists.kt similarity index 100% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Consists.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/Consists.kt diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/connectionTypes.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/connectionTypes.kt similarity index 100% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/connectionTypes.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/relation/physical/connectionTypes.kt diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/ComposableEntity.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/ComposableEntity.kt similarity index 100% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/ComposableEntity.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/ComposableEntity.kt diff --git a/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Entity.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Entity.kt new file mode 100644 index 0000000000000000000000000000000000000000..eb85f5072ae01278d9528706867e2120a06be516 --- /dev/null +++ b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Entity.kt @@ -0,0 +1,86 @@ +package hu.bme.i9rv7w.ergraph.core.schema + +import hu.bme.i9rv7w.ergraph.core.util.Serial +import java.util.* + +open class Entity { + val uuid: UUID = UUID.randomUUID() + val type: String = entityType(this.javaClass) + val labels = mutableSetOf<Label>() + val relatives = mutableMapOf<Relation, Entity>() + + init { + Graph.addEntity(this) // sorry, it's by design + val serial = Serial.generateSerialFor(this).toString() + labels.add(Label(SERIAL_LABEL, serial)) + } + + fun relate(relation: Relation, entity: Entity) { + relatives[relation] = entity + } + + override fun toString(): String { + var out = "" + if (name != null) + out += "#$name | $type-$serialNumber" + else + out += "$type-$serialNumber" + if (marksString.isBlank()) + return out + + out += "|marks:$marksString" + return out + } + + inline fun <reified T : Entity> cast(): T? { + val target: String = entityType(T::class.java) + if (target != type) return null + return this as T + } + + fun add(label: Label): Entity { + labels.add(label) + return this + } + + // ------------------ extensions ------------------------ + // It is a java lib in the end, so extension method would not be exported + + companion object { + const val NAME_LABEL = "name" + const val MARKS_LABEL = "marks" + const val SERIAL_LABEL = "serial" + } + + val name: String? + get() = labels.firstOrNull { it.name == NAME_LABEL }?.value + + fun name(name: String): Entity { + val label = labels.firstOrNull { it.name == NAME_LABEL } + if (label == null) + labels.add(Label(NAME_LABEL, name)) + else + label.value = name + return this + } + + val serialNumber: String? + get() = labels.firstOrNull { it.name == SERIAL_LABEL }?.value + + private val marksString: String + get() = labels.firstOrNull { it.name == MARKS_LABEL }?.value ?: " " + + val marks: List<String> + get() = marksString.split(",") + + fun mark(mark: String): Entity { + val label = labels.firstOrNull { it.name == MARKS_LABEL } + if (label == null) + labels.add(Label(MARKS_LABEL, mark)) + else + label.value += ",$mark" + return this + } + + fun isMarked(mark: String) = marksString.contains(mark) +} diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Graph.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Graph.kt similarity index 72% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Graph.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Graph.kt index 5d49fbef458764895751be67f530c26aa9111cad..bb131fa07ad095f820bcd03237e70a748cc1790c 100644 --- a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Graph.kt +++ b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Graph.kt @@ -7,14 +7,18 @@ import kotlin.reflect.KClass */ object Graph { val entities = mutableSetOf<Entity>() + val relations: List<Relation> + get() = entities.map { it.relatives.keys }.flatten() fun addEntity(entity: Entity) = entities.add(entity) fun empty() = entities.clear() + + fun getByName(name: String) = entities.firstOrNull { it.name == name } } fun Entity.traverse( allowedPath: Set<KClass<out Relation>>, - terminals: Set<KClass<out Entity>> + terminals: Set<KClass<out Entity>>, ): Set<Entity> { // TODO exception on loop val visited = mutableSetOf<Entity>() // prevents loop @@ -48,7 +52,23 @@ inline fun <reified T : Entity> Collection<Entity>.filterEntities(): List<T> { } fun Collection<Entity>.filterMarked(mark: String): List<Entity> = - filter { it.markedWith(mark) } + filter { it.isMarked(mark) } -fun Graph.markedWith(mark: String) = +fun Graph.getMarked(mark: String) = entities.filterMarked(mark) + +fun Graph.printEntities() { + println("Entities:") + entities.forEach(::println) +} + +fun Graph.printRelations() { + println("Relations:") + relations.forEach(::println) +} + +fun Graph.printAll() { + printEntities() + println() + printRelations() +} \ No newline at end of file diff --git a/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Label.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Label.kt new file mode 100644 index 0000000000000000000000000000000000000000..fa27aca7f80fb0dde741c39d961ff077c59372c5 --- /dev/null +++ b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Label.kt @@ -0,0 +1,3 @@ +package hu.bme.i9rv7w.ergraph.core.schema + +data class Label(val name: String, var value: String) \ No newline at end of file diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Relation.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Relation.kt similarity index 63% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Relation.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Relation.kt index 9885392b736ee838e4c26d729f842500b3b6dd7f..9195944611cf85da641edffd4b71d98e3ed0594f 100644 --- a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Relation.kt +++ b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/Relation.kt @@ -4,9 +4,14 @@ import java.util.* open class Relation(val from: Entity, val to: Entity) { val uuid: UUID = UUID.randomUUID() + val type: String = relationType(this.javaClass) init { from.relate(this, to) to.relate(this, from) } + + override fun toString(): String { + return "$type: $from -> $to" + } } \ No newline at end of file diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/typesStrings.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/typesStrings.kt similarity index 85% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/typesStrings.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/typesStrings.kt index f8d318d6a5b3615015a0552395f75d60d04d3f45..962b67093109fe3f47afc20027aabe8264eac75a 100644 --- a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/typesStrings.kt +++ b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/schema/typesStrings.kt @@ -9,4 +9,5 @@ fun <T> typeFromClass(type: Class<T>): String { fun <T> entityType(type: Class<T>) = typeFromClass(type) fun entityType(type: KClass<out Entity>) = typeFromClass(type.java) +fun <T> relationType(type: Class<T>) = typeFromClass(type) fun relationType(type: KClass<out Relation>) = typeFromClass(type.java) \ No newline at end of file diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/util/Serial.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/util/Serial.kt similarity index 100% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/util/Serial.kt rename to src/main/kotlin/hu/bme/i9rv7w/ergraph/core/util/Serial.kt diff --git a/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/util/creations.kt b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/util/creations.kt new file mode 100644 index 0000000000000000000000000000000000000000..f0461b300c3d3d9775aef74ceed84d03f2100a98 --- /dev/null +++ b/src/main/kotlin/hu/bme/i9rv7w/ergraph/core/util/creations.kt @@ -0,0 +1,7 @@ +package hu.bme.i9rv7w.ergraph.core.util + +fun String.splitLet(init: (String) -> Any) { + trim().split("\\s+".toRegex()).forEach( { + init(it) + }) +} \ No newline at end of file diff --git a/lib/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/BasicCreations.kt b/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/BasicCreations.kt similarity index 93% rename from lib/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/BasicCreations.kt rename to src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/BasicCreations.kt index 12b0a0d99154662f4f737e1dcb7a39bb3c2b5f70..f17e9b4b1c0b5d14002412e33f56584fab35f356 100644 --- a/lib/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/BasicCreations.kt +++ b/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/BasicCreations.kt @@ -1,6 +1,7 @@ package hu.bme.i9rv7w.ergraph.model.electrical import hu.bme.i9rv7w.ergraph.core.schema.Graph +import hu.bme.i9rv7w.ergraph.schema.SocketStrip import org.junit.Test class BasicCreations { diff --git a/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MassCreation.kt b/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MassCreation.kt new file mode 100644 index 0000000000000000000000000000000000000000..17a16e54a80c4bb5f5d71fe4f0c1b348636beb9f --- /dev/null +++ b/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MassCreation.kt @@ -0,0 +1,24 @@ +package hu.bme.i9rv7w.ergraph.model.electrical + +import hu.bme.i9rv7w.ergraph.core.schema.Graph +import hu.bme.i9rv7w.ergraph.core.schema.Label +import hu.bme.i9rv7w.ergraph.core.util.splitLet +import hu.bme.i9rv7w.ergraph.schema.Supply +import org.junit.Test + +class MassCreation { + @Test + fun `string to supplies`() { + Graph.empty() + """ + 5b 5j 6j 6b 7j 7b + 10j 10b 12j 12b 13j 13b 14j 14b + """.splitLet { part -> + Supply() + .name("salgo-$part") + .add(Label("inventory.rack", "salgo")) + } + assert(Graph.entities.count() == 14) + } + +} diff --git a/lib/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MiniGraphTests.kt b/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MiniGraphTests.kt similarity index 88% rename from lib/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MiniGraphTests.kt rename to src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MiniGraphTests.kt index c7cdd1700da1bd14e4cf32bbec8a9dba4f9a050e..56d3a3e93a8caf89ec992b8eae5ee8fd0f30e58d 100644 --- a/lib/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MiniGraphTests.kt +++ b/src/test/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/MiniGraphTests.kt @@ -3,6 +3,7 @@ package hu.bme.i9rv7w.ergraph.model.electrical import hu.bme.i9rv7w.ergraph.core.relation.physical.Connects import hu.bme.i9rv7w.ergraph.core.relation.physical.physicalRelations import hu.bme.i9rv7w.ergraph.core.schema.* +import hu.bme.i9rv7w.ergraph.schema.* import org.junit.Test class MiniGraphTests { @@ -44,20 +45,23 @@ class MiniGraphTests { @Test fun `check traverse`() { Graph.empty() - assert(Graph.markedWith("affected").count() == 0) + assert(Graph.getMarked("affected").count() == 0) val strip1 = loadTestElectricalNetwork() + Graph.printAll() + strip1.affectedSupplies().forEach { it.mark("affected") } - val marked = Graph.markedWith("affected") + Graph.printAll() + val marked = Graph.getMarked("affected") assert(marked.count() == 3) val devices = Graph.entities.filterEntities<Device>() devices.forEach { dev -> val numOfSupplies = dev.supplies.count() - val affected = dev.supplies.count { it.markedWith("affected") } + val affected = dev.supplies.count { it.isMarked("affected") } if (numOfSupplies == affected) dev.mark("affected") } @@ -72,6 +76,3 @@ class MiniGraphTests { } - - - diff --git a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/ElectricalStuff.kt b/src/test/kotlin/hu/bme/i9rv7w/ergraph/schema/electrical.kt similarity index 91% rename from lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/ElectricalStuff.kt rename to src/test/kotlin/hu/bme/i9rv7w/ergraph/schema/electrical.kt index d5552130cc20d67ec0a56027d4822fc074b7568a..5cf02f7b1049cf2d979cbcde87cd48ba5492c5e9 100644 --- a/lib/src/main/kotlin/hu/bme/i9rv7w/ergraph/model/electrical/ElectricalStuff.kt +++ b/src/test/kotlin/hu/bme/i9rv7w/ergraph/schema/electrical.kt @@ -1,4 +1,4 @@ -package hu.bme.i9rv7w.ergraph.model.electrical +package hu.bme.i9rv7w.ergraph.schema import hu.bme.i9rv7w.ergraph.core.schema.ComposableEntity import hu.bme.i9rv7w.ergraph.core.schema.Entity