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

com.github.treepat.expression.operators.VisitorFunctionResponse.kt Maven / Gradle / Ivy

Go to download

Treepat is a language to recognise patterns in trees in a similar way as regular expressions recognize patterns in strings. Treepat includes analogous operators to regex union, concatenation, and closure, which are extended to the notion of trees.

There is a newer version: 2.0.0
Show newest version
package com.github.treepat.expression.operators

import com.github.treepat.target_tree.TargetTreeNode

data class VisitorFunctionResponse(
    val responses: List = listOf(VisitorFunctionSimpleResponse()),
    val hasMatch: Boolean = false
)

data class VisitorFunctionSimpleResponse(
    val matches: List = emptyList(),
    val lastVisitedSibling: TargetTreeNode? = null,
    val depthTerm: TargetTreeNode? = null,
    val oddParent: TargetTreeNode? = null
) {
    constructor(targetTreeNode: TargetTreeNode) : this(listOf(targetTreeNode), targetTreeNode)
}

object VisitorFunctionResponseFactory {
    fun createResponseWithMatches(targetTreeNode: TargetTreeNode) =
        VisitorFunctionResponse(listOf(VisitorFunctionSimpleResponse(targetTreeNode)), true)

    fun createResponseWithZeroMatches(targetTreeNode: TargetTreeNode? = null, hasMatch: Boolean = false) =
        VisitorFunctionResponse(listOf(VisitorFunctionSimpleResponse(lastVisitedSibling = targetTreeNode)), hasMatch)

    fun createResponseWithDepthTerm(
        response: VisitorFunctionResponse,
        depthTerm: TargetTreeNode?,
        oddParent: TargetTreeNode?
    ): VisitorFunctionResponse {
        val responses = response.responses.map { it.copy(depthTerm = depthTerm, oddParent = oddParent) }
        return VisitorFunctionResponse(responses, response.hasMatch)
    }

    fun createResponse(
        responses: List,
        targetTreeNode: TargetTreeNode?
    ): VisitorFunctionResponse {
        val allHasMatches = responses.filter { it.hasMatch }.flatMap { it.responses }
        return if (allHasMatches.isNotEmpty()) {
            VisitorFunctionResponse(allHasMatches, true)
        } else {
            createResponseWithZeroMatches(targetTreeNode)
        }
    }

    fun createMergeResponse(
        simpleResponse: VisitorFunctionSimpleResponse,
        secondResponse: VisitorFunctionResponse,
        lastVisitedSiblingFirst: Boolean = false,
        hasMatch: Boolean = false
    ): VisitorFunctionResponse {
        val answerMatches = mutableListOf()
        for (secondIterator in secondResponse.responses) {
            answerMatches.add(
                VisitorFunctionSimpleResponse(
                    simpleResponse.matches + secondIterator.matches,
                    if (lastVisitedSiblingFirst || secondIterator.matches.isEmpty()) {
                        simpleResponse.lastVisitedSibling
                    } else {
                        secondIterator.lastVisitedSibling
                    },
                    if (lastVisitedSiblingFirst && simpleResponse.depthTerm != null) {
                        secondIterator.lastVisitedSibling?.moveToRightSibling()
                    } else {
                        simpleResponse.depthTerm ?: secondIterator.depthTerm
                    },
                    secondIterator.oddParent ?: simpleResponse.oddParent
                )
            )
        }
        return VisitorFunctionResponse(answerMatches, secondResponse.hasMatch || hasMatch)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy