All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zepben.evolve.services.network.tracing.connectivity.ConnectivityTrace.kt Maven / Gradle / Ivy

There is a newer version: 0.24.0rc1
Show newest version
/*
 * Copyright 2021 Zeppelin Bend Pty Ltd
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.zepben.evolve.services.network.tracing.connectivity

import com.zepben.evolve.cim.iec61970.base.wires.BusbarSection
import com.zepben.evolve.services.network.NetworkService
import com.zepben.evolve.services.network.tracing.OpenTest
import com.zepben.evolve.services.network.tracing.traversals.BasicQueue
import com.zepben.evolve.services.network.tracing.traversals.BasicTraversal
import com.zepben.evolve.services.network.tracing.traversals.TraversalQueue

/**
 * A class that creates commonly used connectivity based traces. These ignore phases, they are purely to trace equipment that are connected
 * in any way giving the connectivity between them. You can add custom step actions and stop conditions to the returned traversal.
 */
object ConnectivityTrace {

    /**
     * @return a traversal that traces equipment that are connected, ignoring open status.
     */
    fun newConnectivityTrace(): BasicTraversal = createTraversal(OpenTest.IGNORE_OPEN, BasicQueue.depthFirst())

    /**
     * @return a traversal that traces equipment that are connected, ignoring open status.
     */
    fun newConnectivityBreadthTrace(): BasicTraversal = createTraversal(OpenTest.IGNORE_OPEN, BasicQueue.breadthFirst())

    /**
     * @return a traversal that traces equipment that are connected stopping at normally open points.
     */
    fun newNormalConnectivityTrace(): BasicTraversal = createTraversal(OpenTest.NORMALLY_OPEN, BasicQueue.depthFirst())

    /**
     * @return a traversal that traces equipment that are connected stopping at currently open points.
     */
    fun newCurrentConnectivityTrace(): BasicTraversal = createTraversal(OpenTest.CURRENTLY_OPEN, BasicQueue.depthFirst())

    private fun createTraversal(openTest: OpenTest, queue: TraversalQueue) =
        BasicTraversal(queueNext(openTest), queue, ConnectivityTracker())

    private fun queueNext(openTest: OpenTest): BasicTraversal.QueueNext = BasicTraversal.QueueNext { cr, traversal ->
        val to = cr.to ?: return@QueueNext
        if (openTest.isOpen(to, null))
            return@QueueNext

        if (to is BusbarSection)
            to.terminals
                .asSequence()
                .flatMap { NetworkService.connectedTerminals(it) }
                .filter { it.toTerminal != cr.fromTerminal }
                .forEach { traversal.queue.add(it) }
        else {
            val connectivity = to.terminals
                .filter { it != cr.toTerminal }
                .flatMap { NetworkService.connectedTerminals(it) }

            val busbars = connectivity.filter { it.to is BusbarSection }
            if (busbars.isNotEmpty())
                busbars.forEach { traversal.queue.add(it) }
            else
                connectivity.forEach { traversal.queue.add(it) }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy