Skip to content

Commit

Permalink
simplify Cacheables
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed May 25, 2020
1 parent 1e8a741 commit 368601f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,4 @@ internal interface Cacheable<T> {
/**Forcefully invalidate the node cache and all of its subtree cache*/
fun invalidateCache()

fun <U> invalidateCacheIfNonEmpty(seq: Sequence<U>): Sequence<U> =
sequence {
val i = seq.iterator()

if (i.hasNext()) {
invalidateCache()
yield(i.next())
}
while (i.hasNext()) {
yield(i.next())
}
}

fun <U> Sequence<U>.invalidatingCacheIfNonEmpty(): Sequence<U> = this
// invalidateCacheIfNonEmpty(this)

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ internal class CompoundIndex(

override fun retractAll(clause: Clause): Sequence<Clause> =
if (ordered) {
retractAllOrdered(clause).invalidatingCacheIfNonEmpty()
retractAllOrdered(clause)
} else {
retractAllUnordered(clause).invalidatingCacheIfNonEmpty()
retractAllUnordered(clause)
}


Expand All @@ -73,25 +73,25 @@ internal class CompoundIndex(
if (clause.isGlobal()) {
Utils.merge(
functors.values.map {
it.retractAllIndexed(clause).invalidatingCacheIfNonEmpty()
it.retractAllIndexed(clause)
}
).map { it.innerClause }
} else {
functors[clause.nestedFunctor()]
?.retractAll(clause)?.invalidatingCacheIfNonEmpty()
?.retractAll(clause)
?: emptySequence()
}

private fun retractAllUnordered(clause: Clause): Sequence<Clause> =
if (clause.isGlobal()) {
Utils.flatten(
functors.values.map {
it.retractAll(clause).invalidatingCacheIfNonEmpty()
it.retractAll(clause)
}
)
} else {
functors[clause.nestedFunctor()]
?.retractAll(clause)?.invalidatingCacheIfNonEmpty()
?.retractAll(clause)
?: emptySequence()
}

Expand Down Expand Up @@ -126,12 +126,12 @@ internal class CompoundIndex(
if (clause.isGlobal()) {
Utils.merge(
functors.values.map {
it.retractAllIndexed(clause).invalidatingCacheIfNonEmpty()
it.retractAllIndexed(clause)
}
)
} else {
functors[clause.nestedFunctor()]
?.retractAllIndexed(clause)?.invalidatingCacheIfNonEmpty()
?.retractAllIndexed(clause)
?: emptySequence()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ internal sealed class ArityNode : ReteNode {

override fun retractAll(clause: Clause): Sequence<Clause> =
if (ordered) {
retractAllOrdered(clause).invalidatingCacheIfNonEmpty()
retractAllOrdered(clause)
} else {
retractAllUnordered(clause).invalidatingCacheIfNonEmpty()
retractAllUnordered(clause)
}

override fun getCache(): Sequence<SituatedIndexedClause> =
theoryCache.value.asSequence()

private fun retractAllOrdered(clause: Clause): Sequence<Clause> =
retractAllOrderedIndexed(clause).map { it.innerClause }.invalidatingCacheIfNonEmpty()
retractAllOrderedIndexed(clause).map { it.innerClause }

private fun getOrdered(clause: Clause): Sequence<Clause> =
getOrderedIndexed(clause).map { it.innerClause }

private fun retractAllUnordered(clause: Clause): Sequence<Clause> =
retractAllUnorderedIndexed(clause).map { it.innerClause }.invalidatingCacheIfNonEmpty()
retractAllUnorderedIndexed(clause).map { it.innerClause }

private fun getUnordered(clause: Clause): Sequence<Clause> =
getUnorderedIndexed(clause).map { it.innerClause }
Expand Down Expand Up @@ -129,7 +129,7 @@ internal sealed class ArityNode : ReteNode {
compoundIndex.retractAllIndexed(clause)
)
}
}.invalidatingCacheIfNonEmpty()
}

protected fun retractAllUnorderedIndexed(clause: Clause): Sequence<SituatedIndexedClause> =
when (clause.nestedFirstArgument()) {
Expand All @@ -151,7 +151,7 @@ internal sealed class ArityNode : ReteNode {
variableIndex.retractAllIndexed(clause),
compoundIndex.retractAllIndexed(clause)
)
}.invalidatingCacheIfNonEmpty()
}


protected fun getOrderedIndexed(clause: Clause): Sequence<SituatedIndexedClause> =
Expand Down Expand Up @@ -315,9 +315,9 @@ internal sealed class ArityNode : ReteNode {

override fun retractAllIndexed(clause: Clause): Sequence<SituatedIndexedClause> =
if (ordered) {
retractAllOrderedIndexed(clause).invalidatingCacheIfNonEmpty()
retractAllOrderedIndexed(clause)
} else {
retractAllUnorderedIndexed(clause).invalidatingCacheIfNonEmpty()
retractAllUnorderedIndexed(clause)
}

override fun extractGlobalIndexedSequence(clause: Clause): Sequence<SituatedIndexedClause> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ internal sealed class FunctorNode : ReteNode {
}

override fun retractFirst(clause: Clause): Sequence<Clause> =
selectArity(clause)?.retractFirst(clause)?.invalidatingCacheIfNonEmpty()
selectArity(clause)?.retractFirst(clause)
?: emptySequence()

override fun retractAll(clause: Clause): Sequence<Clause> =
selectArity(clause)?.retractAll(clause)?.invalidatingCacheIfNonEmpty()
selectArity(clause)?.retractAll(clause)
?: emptySequence()

override fun getCache(): Sequence<SituatedIndexedClause> =
Expand Down Expand Up @@ -109,7 +109,7 @@ internal sealed class FunctorNode : ReteNode {
}

override fun retractAll(clause: Clause): Sequence<Clause> =
arities[clause.nestedArity()]?.retractAll(clause)?.invalidatingCacheIfNonEmpty()
arities[clause.nestedArity()]?.retractAll(clause)
?: emptySequence()

override fun getCache(): Sequence<SituatedIndexedClause> =
Expand Down Expand Up @@ -151,7 +151,7 @@ internal sealed class FunctorNode : ReteNode {
}
partialResult.asSequence()
} else {
arities[clause.nestedArity()]?.retractAllIndexed(clause)?.invalidatingCacheIfNonEmpty()
arities[clause.nestedArity()]?.retractAllIndexed(clause)
?: emptySequence()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ internal class RuleNode(
}

override fun retractFirst(clause: Clause): Sequence<Clause> =
functors[clause.nestedFunctor()]?.retractFirst(clause)?.invalidatingCacheIfNonEmpty()
functors[clause.nestedFunctor()]?.retractFirst(clause)
?: emptySequence()

override fun retractAll(clause: Clause): Sequence<Clause> =
functors[clause.nestedFunctor()]?.retractAll(clause)?.invalidatingCacheIfNonEmpty()
functors[clause.nestedFunctor()]?.retractAll(clause)
?: emptySequence()

override fun getCache(): Sequence<SituatedIndexedClause> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,6 @@ class OrderedReteTreeTest {
val head = Struct.of(functor, args)
val template = Rule.of(head, Var.anonymous())

println(template)

val clauses = (0..4).map { Rule.of(head, Integer.of(it)) }

val tree = reteTreeOf(clauses)
Expand Down

0 comments on commit 368601f

Please sign in to comment.