2 * Copyright © 2019 IBM.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.core.data
19 enum class EdgeLabel(val id: String) {
25 enum class EdgeStatus(val id: String) {
26 NOT_STARTED("not_started"),
31 enum class NodeStatus(val id: String) {
32 NOT_STARTED("not_started"),
34 EXECUTING("executing"),
40 val nodes: MutableMap<String, Node> = hashMapOf()
41 val edges: MutableSet<Edge> = mutableSetOf()
43 fun addNode(value: String): Node {
44 val node = Node(value)
49 fun addEdge(source: String, destination: String, label: EdgeLabel) {
50 if (!nodes.containsKey(source)) {
53 if (!nodes.containsKey(destination)) {
56 val edge = Edge(nodes[source]!!, nodes[destination]!!, label)
57 if (!edges.contains(edge)) {
59 nodes[source]!!.edges.add(edge)
63 override fun toString(): String {
64 val standaloneNodes = nodes.values.filter { node -> edges.all { it.source != node && it.target != node } }
65 val s = (edges.map { it.toString() } + standaloneNodes.map { it.toString() }).joinToString()
70 val buffer = StringBuffer("Nodes :")
71 nodes.values.forEach {
72 buffer.append("\n\t$it")
74 buffer.append("\nEdges :")
76 buffer.append("\n\t$it")
78 return buffer.toString()
81 override fun equals(other: Any?): Boolean {
82 if (this === other) return true
83 if (other?.javaClass != javaClass) return false
85 return nodes == other.nodes && edges == other.edges
88 override fun hashCode() = 31 * nodes.hashCode() + edges.hashCode()
90 fun equivalentTo(other: Graph): Boolean {
91 return nodes == other.nodes && edges.all { edge -> other.edges.any { it.equivalentTo(edge) } }
94 data class Node(val id: String, var status: NodeStatus = NodeStatus.NOT_STARTED) {
95 val edges: MutableList<Edge> = ArrayList()
97 fun neighbors(): List<Node> = edges.map { edge -> edge.target(this) }
99 fun neighbors(label: EdgeLabel): List<Node> = edges.filter { it.label == label }
100 .map { edge -> edge.target(this) }
102 fun labelEdges(label: EdgeLabel): List<Edge> = edges.filter { it.label == label }
104 override fun toString() = "$id, Status($status)"
110 val label: EdgeLabel,
111 var status: EdgeStatus = EdgeStatus.NOT_STARTED) {
113 fun target(node: Node): Node = target
115 fun equivalentTo(other: Edge) =
116 (source == other.source && target == other.target)
117 || (source == other.target && target == other.source)
119 override fun toString() =
120 "${source.id}>${target.id}/$label($status)"
123 data class TermForm(val nodes: Collection<String>, val edges: List<Term>) {
125 data class Term(val source: String, val target: String, val label: EdgeLabel) {
126 override fun toString() = "Term($source, $target, $label)"
130 data class AdjacencyList<String, out EdgeLabel>(val entries: List<Entry<String, EdgeLabel>>) {
131 constructor(vararg entries: Entry<String, EdgeLabel>) : this(entries.asList())
133 override fun toString() = "AdjacencyList(${entries.joinToString()})"
135 data class Entry<out String, out EdgeLabel>(val node: String, val links: List<Link<String, EdgeLabel>> = emptyList<Nothing>()) {
136 constructor(node: String, vararg links: Link<String, EdgeLabel>) : this(node, links.asList())
138 override fun toString() = "Entry($node, links[${links.joinToString()}])"
141 data class Link<out String, out EdgeLabel>(val node: String, val label: EdgeLabel) {
142 override fun toString() = if (label == null) "$node" else "$node/$label"
148 fun labeledDirectedTerms(termForm: TermForm): Graph =
149 createFromTerms(termForm) { graph, n1, n2, value -> graph.addEdge(n1, n2, value) }
151 fun labeledDirectedAdjacent(adjacencyList: AdjacencyList<String, EdgeLabel>): Graph =
152 fromAdjacencyList(adjacencyList) { graph, n1, n2, value ->
153 graph.addEdge(n1, n2, value)
156 private fun createFromTerms(termForm: TermForm,
157 addFunction: (Graph, String, String, EdgeLabel) -> Unit): Graph {
159 termForm.nodes.forEach { graph.addNode(it) }
160 termForm.edges.forEach { addFunction(graph, it.source, it.target, it.label) }
164 private fun fromAdjacencyList(adjacencyList: AdjacencyList<String, EdgeLabel>,
165 addFunction: (Graph, String, String, EdgeLabel) -> Unit): Graph {
167 adjacencyList.entries.forEach { graph.addNode(it.node) }
168 adjacencyList.entries.forEach { (node, links) ->
169 links.forEach { addFunction(graph, node, it.node, it.label) }