Skip to content

Commit

Permalink
refactor: wip on BeliefBase for the 2nd time
Browse files Browse the repository at this point in the history
  • Loading branch information
anitvam committed Oct 22, 2024
1 parent b3a0f5f commit 2b08223
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 134 deletions.
95 changes: 53 additions & 42 deletions jakta-api/src/main/kotlin/it/unibo/jakta/beliefs/BeliefBase.kt
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
package it.unibo.jakta.beliefs

/** A BDI Agent's collection of [Belief]s */
interface BeliefBase<Query : Any, Belief, SelfType: BeliefBase<Query, Belief, SelfType>> : Collection<Belief> {
interface BeliefBase<Query : Any, Belief, SelfType : BeliefBase<Query, Belief, SelfType>> : Collection<Belief> {

/**
* List of [BeliefUpdate] that populated the current [BeliefBase].
**/
val delta: List<Update<Belief>>

/**
* Resets the [BeliefBase] operations.
* @return a [BeliefBase] where the delta list is empty.
* Performs unification between [B] and values in this [BeliefBase]
*/
fun resetDelta(): SelfType
fun select(query: Query): SelfType

/**
* Adds a [Belief] to this [BeliefBase]
* @param belief: the [Belief] to be added
* @return the new [BeliefBase] and the added [Belief]
**/
operator fun plus(belief: Belief): SelfType
interface MutableBeliefBase<Query, Belief, SelfType> : BeliefBase<Query, Belief, SelfType> where
Query : Any,
SelfType : BeliefBase<Query, Belief, SelfType> {

/**
* Adds all the given beliefs into this [BeliefBase]
* @param beliefs: beliefs to be added
* @return the new [BeliefBase] and the added [Belief]s
**/
operator fun plus(beliefs: BeliefBase<Query, Belief>): SelfType
/**
* List of [Update] that populated the current [BeliefBase].
**/
var delta: List<Update<Belief>>

/**
* Removes a [Belief] from the [BeliefBase]
* @param belief: the [Belief] to be removed
* @return the new [BeliefBase] and the removed [Belief]
*/
operator fun minus(belief: Belief): SelfType
/**
* @return the immutable [BeliefBase] version of this instance.
*/
fun snapshot(): SelfType

/**
* Removes all the specified beliefs from this [BeliefBase]
* @param beliefs: beliefs to be removed
* @return the new [BeliefBase] and the removed [Belief]s
*/
operator fun minus(beliefs: BeliefBase<Query, Belief>): BeliefBase<Query, Belief>
/**
* Adds a [Belief] to this [BeliefBase], if not present.
* @param belief: the [Belief] to be added.
* @return true if the [BeliefBase] was changed as the result of the operation.
**/
fun add(belief: Belief): Boolean

/**
* Performs unification between [B] and values in this [BeliefBase]
*/
fun solve(query: Query): SelfType
/**
* Adds all the given beliefs into this [BeliefBase], if not present.
* @param beliefs: the [BeliefBase] to be added.
* @return true if the [BeliefBase] was changed as the result of the operation.
**/
fun addAll(beliefs: SelfType): Boolean {
var result = false
for (b in beliefs) {
if (add(b)) result = true
}
return result
}

/**
* Removes a [Belief] from the [BeliefBase], if present.
* @param belief: the [Belief] to be removed
* @return true if the [BeliefBase] was changed as the result of the operation.
*/
fun remove(belief: Belief): Boolean

/**
* Removes all the specified beliefs from this [BeliefBase], if present.
* @param beliefs: beliefs to be removed
* @return true if the [BeliefBase] was changed as the result of the operation.
*/
fun removeAll(beliefs: SelfType): Boolean {
var result = false
for (b in beliefs) {
if (remove(b)) result = true
}
return result
}
}

// TODO: this may be a container of multiple updates that happen atomically
sealed interface Update<Belief> {
val diff: Belief

data class Addition<Belief>(override val diff: Belief) : Update<Belief>
data class Removal<Belief>(override val diff: Belief) : Update<Belief>
// data object NoOp : Update<Nothing> {
// override val diff: Nothing = error("ASSDASDADAS")
// }
}

interface
}
15 changes: 0 additions & 15 deletions jakta-api/src/main/kotlin/it/unibo/jakta/beliefs/BeliefUpdate.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package it.unibo.jakta.beliefs

import it.unibo.jakta.beliefs.impl.PrologBeliefBaseImpl
import it.unibo.jakta.resolution.Solution
import it.unibo.tuprolog.core.Rule
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.solve.Solution as TuprologSolution

/** A BDI Agent's collection of [PrologBelief] */
interface PrologBeliefBase : BeliefBase<Struct, Rule, PrologBeliefBase> {
interface PrologBeliefBase : BeliefBase<Struct, PrologBelief, PrologBeliefBase> {

override fun solve(query: Struct): PrologBeliefBase
fun select(query: PrologBelief): PrologBeliefBase

/**
* Updates the content of the [BeliefBase] without saving the change in delta variable.
* @param belief the [Belief] to update
* @return a [BeliefBase]
*/
fun update(belief: PrologBelief): PrologBeliefBase
interface PrologMutableBeliefBase : BeliefBase.MutableBeliefBase<Struct, PrologBelief, PrologBeliefBase> {
/**
* Updates the content of the [BeliefBase].
* @param belief the [Belief] to update
* @return true if the [BeliefBase] was changed as the result of the operation.
*
*/
fun update(belief: PrologBelief): Boolean
}

companion object {
/** @return an empty [PrologBeliefBase] */
Expand All @@ -37,3 +37,5 @@ interface PrologBeliefBase : BeliefBase<Struct, Rule, PrologBeliefBase> {
of(beliefs.asList())
}
}

// Posso fare una select di una mutable bb ???
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package it.unibo.jakta.beliefs.impl

import it.unibo.jakta.Jakta
import it.unibo.jakta.beliefs.BeliefUpdate
import it.unibo.jakta.beliefs.BeliefBase
import it.unibo.jakta.beliefs.PrologBelief
import it.unibo.jakta.beliefs.PrologBeliefBase
import it.unibo.jakta.resolution.Solution
import it.unibo.jakta.resolution.toJaktaSolution
import it.unibo.tuprolog.collections.ClauseMultiSet
import it.unibo.tuprolog.core.Rule
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.solve.Solver
import it.unibo.tuprolog.solve.flags.TrackVariables
Expand All @@ -16,87 +14,70 @@ import it.unibo.tuprolog.theory.Theory
import it.unibo.tuprolog.unify.Unificator
import it.unibo.tuprolog.solve.Solution as TuprologSolution

internal class PrologBeliefBaseImpl private constructor(
private val beliefs: ClauseMultiSet,
override val delta: List<BeliefUpdate<PrologBelief>> = emptyList(),
) : PrologBeliefBase {
internal data class PrologBeliefBaseImpl(
private var beliefs: ClauseMultiSet,
override var delta: List<BeliefBase.Update<PrologBelief>> = emptyList(),
) : PrologBeliefBase.PrologMutableBeliefBase, PrologBeliefBase {

constructor() : this(ClauseMultiSet.empty(Unificator.default))

override operator fun plus(belief: PrologBelief) = when (beliefs.count(belief.content)) {
// There's no Belief that unify the param inside the MultiSet, so it's inserted
0L -> PrologBeliefBaseImpl(beliefs.add(belief.content), listOf(BeliefUpdate.addition(belief)))
// There are Beliefs that unify the param, so the belief it's not inserted
else -> this
}

override operator fun plus(beliefs: PrologBeliefBase): PrologBeliefBase {
var bb: PrologBeliefBase = this
beliefs.forEach { bb += it }
return bb
}

override fun hashCode() = beliefs.hashCode()
override fun snapshot(): PrologBeliefBase = TODO()

override fun select(query: Struct): PrologBeliefBase =
PrologBeliefBase.of(
PrologBelief.of(
Solver.prolog.newBuilder()
.flag(Unknown, Unknown.FAIL)
.staticKb(operatorExtension + Theory.of(beliefs))
.flag(TrackVariables) { ON }
.build()
.solveOnce(query)
.solvedQuery,
),
)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as PrologBeliefBaseImpl
return beliefs == other.beliefs
override fun select(query: PrologBelief): PrologBeliefBase {
TODO("Not yet implemented")
}

override operator fun minus(belief: PrologBelief): PrologBeliefBase {
return if (beliefs.count(belief.content) > 0) {
PrologBeliefBaseImpl(
ClauseMultiSet.of(Unificator.default, beliefs.filter { it != belief }),
listOf(BeliefUpdate.removal(first { it == belief })),
)
override fun update(belief: PrologBelief): Boolean {
val element = beliefs.find { it.head?.functor == belief.head.functor }
return if (element != null) {
beliefs = ClauseMultiSet.of(Unificator.default, beliefs.filter { it != belief }).add(belief)
true
} else {
this
false
}
}

override operator fun minus(beliefs: PrologBeliefBase): PrologBeliefBase {
var bb: PrologBeliefBase = this
beliefs.forEach { bb -= it }
return bb
override fun remove(belief: PrologBelief): Boolean = when (beliefs.count(belief)) {
0L -> false
else -> true.also {
val match = beliefs.filterIsInstance<PrologBelief>().first { it == belief }
delta += BeliefBase.Update.Removal(match)
beliefs = ClauseMultiSet.of(Unificator.default, beliefs.filter { it != belief })
}
}

override fun update(belief: PrologBelief): PrologBeliefBase {
val element = beliefs.find { it.head?.functor == belief.content.head.functor }
return if (element != null) {
this - (PrologBelief.from(element.head!!)) + belief
} else {
this
override fun add(belief: PrologBelief): Boolean = when (beliefs.count(belief)) {
// There's no Belief that unify the param inside the MultiSet, so it's inserted
0L -> true.also {
beliefs.add(belief)
delta += BeliefBase.Update.Addition(belief)
}
// There are Beliefs that unify the param, so the belief it's not inserted
else -> false
}

override fun solve(struct: Struct): Solution<TuprologSolution> =
Solver.prolog.newBuilder()
.flag(Unknown, Unknown.FAIL)
.staticKb(operatorExtension + Theory.of(beliefs))
.flag(TrackVariables) { ON }
.build()
.solveOnce(struct)
.toJaktaSolution()

override fun solve(belief: PrologBelief): Solution<TuprologSolution> = solve(belief.content.head)

override fun resetDelta(): PrologBeliefBase = PrologBeliefBaseImpl(beliefs)

override fun isEmpty(): Boolean = beliefs.isEmpty()

override fun iterator(): Iterator<PrologBelief> =
beliefs.filterIsInstance<Rule>().map { PrologBelief.from(it) }.iterator()

override val size = beliefs.size

override fun contains(element: PrologBelief) = beliefs.contains(element.content)
override fun isEmpty() = beliefs.isEmpty()

override fun iterator() = beliefs.filterIsInstance<PrologBelief>().iterator()

override fun containsAll(elements: Collection<PrologBelief>) = beliefs.containsAll(elements.map { it.content })
override fun containsAll(elements: Collection<PrologBelief>) = beliefs.containsAll(elements)

override fun toString(): String =
beliefs.joinToString { PrologBelief.from(it.castToRule()).toString() }
override fun contains(element: PrologBelief) = beliefs.contains(element)

companion object {
private val operatorExtension = Theory.of(
Expand All @@ -107,3 +88,17 @@ internal class PrologBeliefBaseImpl private constructor(
)
}
}

override fun solve(struct: Struct): Solution<TuprologSolution> =
Solver.prolog.newBuilder()
.flag(Unknown, Unknown.FAIL)
.staticKb(operatorExtension + Theory.of(beliefs))
.flag(TrackVariables) { ON }
.build()
.solveOnce(struct)
.toJaktaSolution()

override fun solve(belief: PrologBelief): Solution<TuprologSolution> = solve(belief.content.head)

override fun toString(): String =
beliefs.joinToString { PrologBelief.from(it.castToRule()).toString() }
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import it.unibo.jakta.actions.effects.PopMessage
import it.unibo.jakta.actions.effects.Sleep
import it.unibo.jakta.actions.effects.Stop
import it.unibo.jakta.beliefs.Belief
import it.unibo.jakta.beliefs.BeliefUpdate
import it.unibo.jakta.beliefs.PrologBeliefBase
import it.unibo.jakta.beliefs.RetrieveResult
import it.unibo.jakta.context.AgentContext
Expand Down

0 comments on commit 2b08223

Please sign in to comment.