commonMain.it.unibo.tuprolog.datalog.visitors.AbstractClauseVisitor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datalog-js Show documentation
Show all versions of datalog-js Show documentation
Datalog-compliant utilities for knowledge representation
package it.unibo.tuprolog.datalog.visitors
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.visitors.ExhaustiveTermVisitor
import it.unibo.tuprolog.datalog.ClauseVisitor
import it.unibo.tuprolog.datalog.asLiteral
import it.unibo.tuprolog.datalog.isNegated
abstract class AbstractClauseVisitor : ClauseVisitor, ExhaustiveTermVisitor() {
override fun visitLiteral(literal: Struct): T =
literal.argsSequence.map {
if (it.isClause) visitStruct(it.castToStruct()) else it.accept(this)
}.let { reduce(it) }
private fun dispatchHead(head: Struct): T =
visitHead(head)
private fun dispatchLiteral(literal: Struct): T =
if (literal.isNegated) {
visitNegatedLiteral(literal[0].asLiteral())
} else {
visitNonNegatedLiteral(literal)
}
protected fun dispatchHead(clause: Clause): Sequence =
sequenceOf(clause.head).filterNotNull().map { dispatchHead(it) }
protected fun dispatchBody(clause: Clause): Sequence =
clause.bodyItems.asSequence().map {
dispatchLiteral(it.asLiteral(ofClause = clause))
}
override fun visitClause(term: Clause): T = reduce(dispatchHead(term) + dispatchBody(term))
protected abstract fun reduce(results: Sequence): T
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy