Skip to content
Snippets Groups Projects
Commit f0e90e10 authored by Robotka István Adrián's avatar Robotka István Adrián
Browse files

use lib of itself

parent 70cc7ace
Branches
No related tags found
No related merge requests found
Showing
with 45 additions and 237 deletions
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# These are explicitly windows files and should use crlf
*.bat text eol=crlf
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.4.10"
id("org.jetbrains.kotlin.jvm") version "1.4.20"
application
}
group = "hu.bme.i9rv7w"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
jcenter()
maven {
name = "GitLab"
url = uri("https://git.sch.bme.hu/api/v4/projects/3345/packages/maven")
}
flatDir {
dirs("/home/robotka/codes/bme/er-lib/build/libs")
}
}
dependencies {
testImplementation(kotlin("test-junit"))
}
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
tasks.test {
useJUnit()
}
implementation("hu.bme.i9rv7w:er-lib:0.3.1-SNAPSHOT")
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "1.8"
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
mainClassName = "MainKt"
// Define the main class for the application.
mainClass.set("hu.bme.i9rv7w.AppKt")
}
kotlin.code.style=official
rootProject.name = "er-graph"
package core.relation
import core.schema.Entity
import core.schema.Relation
class SameAs(from: Entity, to: Entity) : Relation(from, to)
\ No newline at end of file
package core.relation.physical
import core.schema.Entity
import core.schema.Relation
class Connects(a: Entity, b: Entity) : Relation(a, b)
\ No newline at end of file
package core.relation.physical
import core.schema.Entity
import core.schema.Relation
class Consists(container: Entity, containee: Entity) : Relation(container, containee)
\ No newline at end of file
package core.relation.physical
val physicalRelations = setOf(
Connects::class,
Consists::class
)
\ No newline at end of file
package core.schema
import core.relation.physical.Consists
abstract class ComposableEntity<T : Entity> : Entity {
constructor() : super()
constructor(consists: List<T>) : super() {
madeUp(consists)
}
fun madeUp(consists: List<T>): ComposableEntity<T> {
val container = this
consists.forEach { containee ->
Consists(container, containee)
}
return this
}
}
\ No newline at end of file
package core.schema
import 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
}
}
package core.schema
import kotlin.reflect.KClass
/**
* Behaves as an Entity store
*/
object Graph {
val entities = mutableSetOf<Entity>()
fun addEntity(entity: Entity) = entities.add(entity)
fun empty() = entities.clear()
}
fun Entity.traverse(
allowedPath: Set<KClass<out Relation>>,
terminals: Set<KClass<out Entity>>
): Set<Entity> {
// TODO exception on loop
val visited = mutableSetOf<Entity>() // prevents loop
val queue = mutableListOf(this)
while (queue.isNotEmpty()) {
val entityIter = queue.removeLast()
visited.add(entityIter)
if (terminals.terminatesIn(entityIter))
continue
// get neighbours on allowed relations
val relatives = entityIter.allowedRelatives(allowedPath).values
queue.addAll(
relatives.filter { !visited.contains(it) }
)
}
return visited
}
private fun Entity.allowedRelatives(allowedPath: Set<KClass<out Relation>>): Map<Relation, Entity> =
relatives.filterKeys { relation ->
allowedPath.contains(relation::class)
}
private fun Set<KClass<out Entity>>.terminatesIn(entity: Entity): Boolean =
contains(entity::class)
inline fun <reified T : Entity> Collection<Entity>.filterEntities(): List<T> {
return mapNotNull { it.cast<T>() }
}
fun Collection<Entity>.filterMarked(mark: String): List<Entity> =
filter { it.markedWith(mark) }
fun Graph.markedWith(mark: String) =
entities.filterMarked(mark)
package core.schema
data class Label(val name: String, val value: String)
\ No newline at end of file
package core.schema
import java.util.*
open class Relation(val from: Entity, val to: Entity) {
val uuid: UUID = UUID.randomUUID()
init {
from.relate(this, to)
to.relate(this, from)
}
}
\ No newline at end of file
package core.schema
import kotlin.reflect.KClass
fun <T> typeFromClass(type: Class<T>): String {
return type.simpleName.toLowerCase()
}
fun <T> entityType(type: Class<T>) = typeFromClass(type)
fun entityType(type: KClass<out Entity>) = typeFromClass(type.java)
fun relationType(type: KClass<out Relation>) = typeFromClass(type.java)
\ No newline at end of file
package core.util
import core.schema.Entity
object Serial {
var serials = mutableMapOf<String, Int>()
fun generateSerialFor(entity: Entity): Int {
val type: String = entity.type
var counter = 1
if (serials.containsKey(type))
counter += serials[type]!!
serials[type] = counter
return counter
}
}
package hu.bme.i9rv7w.ergraph
import hu.bme.i9rv7w.ergraph.core.schema.Graph
fun main() {
println("jejejejej")
}
fun main() {
println("check out tests")
}
package model.entity.electrical
import core.schema.ComposableEntity
import core.schema.Entity
class InputSource : Entity()
class SocketStrip(
socketNum: Int = 6,
val sockets: List<Socket> = List(socketNum) { Socket() }
) : ComposableEntity<Socket>(sockets)
class Socket : Entity()
class PowerCord : Entity()
class Supply : Entity()
class Device(
supplyNum: Int = 1,
val supplies: List<Supply> = List(supplyNum) { Supply() }
) : ComposableEntity<Supply>(supplies)
package model.entity.network
import core.schema.ComposableEntity
import core.schema.Entity
class Node : ComposableEntity<Port>()
class Port : Entity()
class Cable : Entity()
class Service : Entity()
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package hu.bme.`er-graph`
import hu.bme.i9rv7w.ergraph.App
import kotlin.test.Test
import kotlin.test.assertNotNull
class AppTest {
@Test fun testAppHasAGreeting() {
val classUnderTest = App()
assertNotNull(classUnderTest.greeting, "app should have a greeting")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment