Simple filter form with all/any and is/is not, read back from the rule tree (#172)
This commit is contained in:
1 parent
4d9d809f85
commit
e1a81682ac
2 files changed
+357
No files matched your search
@@ -0,0 +1,195 @@
|
||||
package de.jeanlucmakiola.agendula.ui.filters
|
||||
|
||||
import de.jeanlucmakiola.agendula.domain.DueSpec
|
||||
import de.jeanlucmakiola.agendula.domain.DueWindow
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.TagMatch
|
||||
|
||||
/** How the conditions of a group combine: every one ([ALL]) or at least one ([ANY]). */
|
||||
enum class Match { ALL, ANY }
|
||||
|
||||
/**
|
||||
* A rule as the simple editor shows it: up to four criteria joined by [match],
|
||||
* each of which can be turned round with *is not*. An empty criterion doesn't
|
||||
* narrow: no [due] means any due date, no lists any list, no [tags] any tag,
|
||||
* [Priority.NONE] any priority.
|
||||
*
|
||||
* [toRule] writes, in this order, the criteria that narrow:
|
||||
* - due: `or(due…)` over [due], under `not(…)` when [dueNegated];
|
||||
* - lists: `list(…)`, under `not(…)` when [listsNegated];
|
||||
* - priority: `prio(…)`;
|
||||
* - tags: `or(tag…)`, or `and(tag…)` for [TagMatch.ALL], under `not(…)` when [tagsNegated];
|
||||
*
|
||||
* all inside `and(…)` for [Match.ALL] or `or(…)` for [Match.ANY]. A form that
|
||||
* narrows by nothing is every task, whatever [match] says.
|
||||
*
|
||||
* [of] reads those rules back unchanged, plus a few shapes that mean the same
|
||||
* (see [of]); anything else needs the advanced editor.
|
||||
*/
|
||||
data class SimpleRule(
|
||||
val match: Match = Match.ALL,
|
||||
val due: Set<DueSpec> = emptySet(),
|
||||
val dueNegated: Boolean = false,
|
||||
val listIds: Set<Long> = emptySet(),
|
||||
val listsElsewhere: Set<String> = emptySet(),
|
||||
val listsNegated: Boolean = false,
|
||||
val tags: Set<String> = emptySet(),
|
||||
val tagMatch: TagMatch = TagMatch.ANY,
|
||||
val tagsNegated: Boolean = false,
|
||||
val minPriority: Priority = Priority.NONE,
|
||||
) {
|
||||
/** Whether the form narrows by list at all. */
|
||||
val anyList: Boolean get() = listIds.isEmpty() && listsElsewhere.isEmpty()
|
||||
|
||||
/** The "next N days" range among [due], if one is picked. */
|
||||
val nextDays: DueSpec.NextDays? get() = due.firstNotNullOfOrNull { it as? DueSpec.NextDays }
|
||||
|
||||
/** [due] with [spec] added, or taken out when it is there. A second "next N days" replaces the first. */
|
||||
fun toggleDue(spec: DueSpec): SimpleRule = when {
|
||||
spec in due -> copy(due = due - spec)
|
||||
spec is DueSpec.NextDays -> copy(due = due.filterNot { it is DueSpec.NextDays }.toSet() + spec)
|
||||
else -> copy(due = due + spec)
|
||||
}
|
||||
|
||||
/** [due] with the "next N days" range set to [spec], or dropped for `null`. */
|
||||
fun withNextDays(spec: DueSpec.NextDays?): SimpleRule =
|
||||
copy(due = due.filterNot { it is DueSpec.NextDays }.toSet() + listOfNotNull(spec))
|
||||
|
||||
fun toRule(): Rule {
|
||||
val parts = buildList {
|
||||
if (due.isNotEmpty()) add(Rule.AnyOf(due.sortedBy(::dueRank).map(Rule::Due)).negatedIf(dueNegated))
|
||||
if (!anyList) add(Rule.InList(listIds, listsElsewhere).negatedIf(listsNegated))
|
||||
if (minPriority != Priority.NONE) add(Rule.MinPriority(minPriority))
|
||||
if (tags.isNotEmpty()) {
|
||||
val each = tags.sorted().map(Rule::HasTag)
|
||||
add((if (tagMatch == TagMatch.ALL) Rule.AllOf(each) else Rule.AnyOf(each)).negatedIf(tagsNegated))
|
||||
}
|
||||
}
|
||||
return if (match == Match.ANY && parts.isNotEmpty()) Rule.AnyOf(parts) else Rule.AllOf(parts)
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* [rule] as the simple form, or `null` when it has none. The form reads:
|
||||
* - what [toRule] writes, so `of(form.toRule()) == form` for any form that
|
||||
* narrows, and `of(rule)!!.toRule() == rule` for any rule [toRule] wrote;
|
||||
* - a lone criterion in place of the `and(…)`, as a pinned list's scope is;
|
||||
* - each criterion in any order, and as its bare condition (`tag(a)` for
|
||||
* `or(tag(a))`, `due(TODAY)` for `or(due(TODAY))`);
|
||||
* - several bare tags straight in the top group, read with its own
|
||||
* all/any, and several bare due conditions in an `or(…)` top group.
|
||||
*
|
||||
* Those read back written the canonical way, meaning the same. Not readable:
|
||||
* a criterion given twice in other ways, a negated priority, a `not(not(…))`,
|
||||
* an empty or doubled-up due/tag group, two "next N days", a list condition
|
||||
* naming no list, nested groups beyond these, and an empty `or()`, which
|
||||
* matches nothing.
|
||||
*/
|
||||
fun of(rule: Rule): SimpleRule? {
|
||||
val (match, parts) = when (rule) {
|
||||
is Rule.AllOf -> Match.ALL to rule.children
|
||||
is Rule.AnyOf -> if (rule.children.isEmpty()) return null else Match.ANY to rule.children
|
||||
else -> Match.ALL to listOf(rule)
|
||||
}
|
||||
val pieces = parts.map { piece(it) ?: return null }.groupBy { it::class }
|
||||
var form = SimpleRule(match = match)
|
||||
for ((_, kind) in pieces) {
|
||||
val merged = if (kind.size == 1) kind.single() else merge(kind, match) ?: return null
|
||||
form = when (merged) {
|
||||
is Piece.Due -> {
|
||||
if (merged.specs.count { it is DueSpec.NextDays } > 1) return null
|
||||
form.copy(due = merged.specs, dueNegated = merged.negated)
|
||||
}
|
||||
is Piece.Lists -> form.copy(
|
||||
listIds = merged.list.listIds,
|
||||
listsElsewhere = merged.list.elsewhere,
|
||||
listsNegated = merged.negated,
|
||||
)
|
||||
is Piece.Prio -> form.copy(minPriority = merged.priority)
|
||||
is Piece.Tags -> form.copy(
|
||||
tags = merged.tags,
|
||||
tagMatch = merged.match ?: TagMatch.ANY,
|
||||
tagsNegated = merged.negated,
|
||||
)
|
||||
}
|
||||
}
|
||||
return form
|
||||
}
|
||||
|
||||
/** One criterion of the top group; [bare] when it was a lone condition, not a group of them. */
|
||||
private sealed interface Piece {
|
||||
val negated: Boolean
|
||||
val bare: Boolean
|
||||
|
||||
data class Due(val specs: Set<DueSpec>, override val negated: Boolean, override val bare: Boolean) : Piece
|
||||
data class Lists(val list: Rule.InList, override val negated: Boolean) : Piece {
|
||||
override val bare get() = true
|
||||
}
|
||||
data class Prio(val priority: Priority) : Piece {
|
||||
override val negated get() = false
|
||||
override val bare get() = true
|
||||
}
|
||||
|
||||
/** [match] is `null` for a bare tag, which reads either way. */
|
||||
data class Tags(val tags: Set<String>, val match: TagMatch?, override val negated: Boolean, override val bare: Boolean) : Piece
|
||||
}
|
||||
|
||||
private fun piece(part: Rule): Piece? {
|
||||
val negated = part is Rule.Not
|
||||
val inner = if (part is Rule.Not) part.child else part
|
||||
return when (inner) {
|
||||
is Rule.Due -> Piece.Due(setOf(inner.spec), negated, bare = true)
|
||||
is Rule.InList -> if (inner.listIds.isEmpty() && inner.elsewhere.isEmpty()) null else Piece.Lists(inner, negated)
|
||||
is Rule.MinPriority -> if (negated || inner.priority == Priority.NONE) null else Piece.Prio(inner.priority)
|
||||
is Rule.HasTag -> Piece.Tags(setOf(inner.tag), null, negated, bare = true)
|
||||
is Rule.AnyOf -> group(inner.children, TagMatch.ANY, negated, dueAllowed = true)
|
||||
is Rule.AllOf -> group(inner.children, TagMatch.ALL, negated, dueAllowed = false)
|
||||
is Rule.Not -> null
|
||||
}
|
||||
}
|
||||
|
||||
/** A group of due conditions (an `or` only) or of tags; `null` for anything else, or a repeat. */
|
||||
private fun group(children: List<Rule>, match: TagMatch, negated: Boolean, dueAllowed: Boolean): Piece? {
|
||||
if (children.isEmpty()) return null
|
||||
val specs = children.mapNotNull { (it as? Rule.Due)?.spec }
|
||||
val tags = children.mapNotNull { (it as? Rule.HasTag)?.tag }
|
||||
return when {
|
||||
dueAllowed && specs.size == children.size && specs.toSet().size == specs.size ->
|
||||
Piece.Due(specs.toSet(), negated, bare = false)
|
||||
tags.size == children.size && tags.toSet().size == tags.size ->
|
||||
Piece.Tags(tags.toSet(), match, negated, bare = false)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
/** Bare tags joined by the top group, or bare due conditions under an `or(…)` top. */
|
||||
private fun merge(pieces: List<Piece>, top: Match): Piece? {
|
||||
if (pieces.any { it.negated || !it.bare }) return null
|
||||
return when (val first = pieces.first()) {
|
||||
is Piece.Tags -> {
|
||||
val tags = pieces.flatMap { (it as Piece.Tags).tags }
|
||||
if (tags.toSet().size != tags.size) return null
|
||||
Piece.Tags(tags.toSet(), if (top == Match.ALL) TagMatch.ALL else TagMatch.ANY, negated = false, bare = false)
|
||||
}
|
||||
is Piece.Due -> {
|
||||
if (top != Match.ANY) return null
|
||||
val specs = pieces.flatMap { (it as Piece.Due).specs }
|
||||
if (specs.toSet().size != specs.size) return null
|
||||
first.copy(specs = specs.toSet(), bare = false)
|
||||
}
|
||||
is Piece.Lists, is Piece.Prio -> null
|
||||
}
|
||||
}
|
||||
|
||||
/** The order [toRule] writes due conditions in: the windows as listed, then the ranges. */
|
||||
internal fun dueRank(spec: DueSpec): Int = when (spec) {
|
||||
is DueSpec.Window -> spec.window.ordinal
|
||||
DueSpec.ThisWeek -> DueWindow.entries.size
|
||||
DueSpec.ThisMonth -> DueWindow.entries.size + 1
|
||||
is DueSpec.NextDays -> DueWindow.entries.size + 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Rule.negatedIf(negated: Boolean): Rule = if (negated) Rule.Not(this) else this
|
||||
@@ -0,0 +1,162 @@
|
||||
package de.jeanlucmakiola.agendula.ui.filters
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.agendula.domain.DueSpec
|
||||
import de.jeanlucmakiola.agendula.domain.DueWindow
|
||||
import de.jeanlucmakiola.agendula.domain.FilterCriteria
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.RuleClock
|
||||
import de.jeanlucmakiola.agendula.domain.TagMatch
|
||||
import de.jeanlucmakiola.agendula.domain.allDayInstantOf
|
||||
import de.jeanlucmakiola.agendula.domain.matches
|
||||
import de.jeanlucmakiola.agendula.domain.startOfDay
|
||||
import de.jeanlucmakiola.agendula.domain.testTask
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.DayOfWeek
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
class SimpleRuleTest {
|
||||
|
||||
private val today = window(DueWindow.TODAY)
|
||||
private val overdue = window(DueWindow.OVERDUE)
|
||||
private val noDate = window(DueWindow.NO_DATE)
|
||||
|
||||
private val forms = listOf(
|
||||
SimpleRule(listIds = setOf(1L)),
|
||||
SimpleRule(match = Match.ANY, listIds = setOf(1L), tags = setOf("nextAction")),
|
||||
SimpleRule(due = setOf(overdue, DueSpec.ThisWeek), dueNegated = true),
|
||||
SimpleRule(due = setOf(DueSpec.NextDays(7), noDate, today)),
|
||||
SimpleRule(listIds = setOf(1L, 2L), listsElsewhere = setOf("EXTERNAL:3"), listsNegated = true),
|
||||
SimpleRule(listsElsewhere = setOf("EXTERNAL:3")),
|
||||
SimpleRule(tags = setOf("a", "b"), tagMatch = TagMatch.ALL, tagsNegated = true),
|
||||
SimpleRule(tags = setOf("a"), tagMatch = TagMatch.ALL),
|
||||
SimpleRule(tags = setOf("a")),
|
||||
SimpleRule(minPriority = Priority.MEDIUM),
|
||||
SimpleRule(
|
||||
match = Match.ANY,
|
||||
due = setOf(DueSpec.ThisMonth),
|
||||
listIds = setOf(4L),
|
||||
tags = setOf("x", "y"),
|
||||
tagsNegated = true,
|
||||
minPriority = Priority.HIGH,
|
||||
),
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `every form that narrows reads back from its rule unchanged`() {
|
||||
for (form in forms) assertThat(SimpleRule.of(form.toRule())).isEqualTo(form)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every rule the form writes is written again unchanged`() {
|
||||
for (rule in forms.map { it.toRule() }) assertThat(SimpleRule.of(rule)!!.toRule()).isEqualTo(rule)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a form that narrows by nothing is every task, whatever its match`() {
|
||||
assertThat(SimpleRule().toRule()).isEqualTo(Rule.Everything)
|
||||
assertThat(SimpleRule(match = Match.ANY).toRule()).isEqualTo(Rule.Everything)
|
||||
assertThat(SimpleRule.of(Rule.Everything)).isEqualTo(SimpleRule())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `filters made by the flat editor keep their rule`() {
|
||||
val criteria = listOf(
|
||||
FilterCriteria(),
|
||||
FilterCriteria(due = setOf(DueWindow.TODAY, DueWindow.OVERDUE)),
|
||||
FilterCriteria(listIds = setOf(1L), listsElsewhere = setOf("EXTERNAL:2"), minPriority = Priority.LOW),
|
||||
FilterCriteria(tags = setOf("b", "a"), tagMatch = TagMatch.ALL),
|
||||
FilterCriteria(due = setOf(DueWindow.NO_DATE), tags = setOf("x"), minPriority = Priority.HIGH, listIds = setOf(5L)),
|
||||
)
|
||||
for (flat in criteria) {
|
||||
val rule = flat.toRule()
|
||||
assertThat(SimpleRule.of(rule)!!.toRule()).isEqualTo(rule)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the GTD case is any of list Next and tag nextAction, also as built in the advanced editor`() {
|
||||
val form = SimpleRule(match = Match.ANY, listIds = setOf(7L), tags = setOf("nextAction"))
|
||||
assertThat(form.toRule()).isEqualTo(
|
||||
Rule.AnyOf(listOf(Rule.InList(setOf(7L)), Rule.AnyOf(listOf(Rule.HasTag("nextAction"))))),
|
||||
)
|
||||
assertThat(SimpleRule.of(Rule.AnyOf(listOf(Rule.HasTag("nextAction"), Rule.InList(setOf(7L)))))).isEqualTo(form)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a lone criterion reads as all rules`() {
|
||||
assertThat(SimpleRule.of(Rule.InList(setOf(3L)))).isEqualTo(SimpleRule(listIds = setOf(3L)))
|
||||
assertThat(SimpleRule.of(Rule.Not(Rule.HasTag("someday"))))
|
||||
.isEqualTo(SimpleRule(tags = setOf("someday"), tagsNegated = true))
|
||||
assertThat(SimpleRule.of(Rule.Due(DueSpec.ThisWeek))).isEqualTo(SimpleRule(due = setOf(DueSpec.ThisWeek)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `bare tags take the top group's all or any, bare due conditions only an any`() {
|
||||
val tags = listOf(Rule.HasTag("a"), Rule.InList(setOf(1L)), Rule.HasTag("b"))
|
||||
assertThat(SimpleRule.of(Rule.AllOf(tags)))
|
||||
.isEqualTo(SimpleRule(listIds = setOf(1L), tags = setOf("a", "b"), tagMatch = TagMatch.ALL))
|
||||
assertThat(SimpleRule.of(Rule.AnyOf(tags)))
|
||||
.isEqualTo(SimpleRule(match = Match.ANY, listIds = setOf(1L), tags = setOf("a", "b"), tagMatch = TagMatch.ANY))
|
||||
val dues = listOf(Rule.Due(today), Rule.Due(DueSpec.ThisMonth))
|
||||
assertThat(SimpleRule.of(Rule.AnyOf(dues))).isEqualTo(SimpleRule(match = Match.ANY, due = setOf(today, DueSpec.ThisMonth)))
|
||||
assertThat(SimpleRule.of(Rule.AllOf(dues))).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `shapes read differently from how they are written mean the same`() {
|
||||
val zone = ZoneId.of("Europe/Berlin")
|
||||
val day = LocalDate.of(2026, 11, 26)
|
||||
val clock = RuleClock(startOfDay(day, zone), startOfDay(day.plusDays(1), zone), zone, DayOfWeek.MONDAY)
|
||||
val tasks = listOf(
|
||||
testTask(listId = 1, tags = listOf("a")),
|
||||
testTask(listId = 2, tags = listOf("a", "b")),
|
||||
testTask(listId = 1, due = allDayInstantOf(day), isAllDay = true),
|
||||
testTask(listId = 3, tags = listOf("b"), due = allDayInstantOf(day.plusDays(9)), isAllDay = true),
|
||||
testTask(listId = 3, priority = Priority.HIGH),
|
||||
)
|
||||
val rules = listOf(
|
||||
Rule.AllOf(listOf(Rule.HasTag("a"), Rule.InList(setOf(1L)), Rule.HasTag("b"))),
|
||||
Rule.AnyOf(listOf(Rule.HasTag("b"), Rule.Due(today), Rule.MinPriority(Priority.HIGH), Rule.Due(DueSpec.NextDays(3)))),
|
||||
Rule.AllOf(listOf(Rule.Not(Rule.InList(setOf(3L))), Rule.HasTag("a"))),
|
||||
Rule.Not(Rule.AnyOf(listOf(Rule.Due(today)))),
|
||||
)
|
||||
for (rule in rules) {
|
||||
val written = SimpleRule.of(rule)!!.toRule()
|
||||
for (task in tasks) assertThat(written.matches(task, clock)).isEqualTo(rule.matches(task, clock))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `shapes the form can't show need the advanced editor`() {
|
||||
val notSimple = listOf(
|
||||
Rule.AnyOf(emptyList()),
|
||||
Rule.Not(Rule.Not(Rule.HasTag("a"))),
|
||||
Rule.Not(Rule.MinPriority(Priority.HIGH)),
|
||||
Rule.MinPriority(Priority.NONE),
|
||||
Rule.InList(emptySet()),
|
||||
Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.InList(setOf(2L)))),
|
||||
Rule.AllOf(listOf(Rule.HasTag("a"), Rule.AnyOf(listOf(Rule.HasTag("b"))))),
|
||||
Rule.AllOf(listOf(Rule.Not(Rule.HasTag("a")), Rule.HasTag("b"))),
|
||||
Rule.AllOf(listOf(Rule.AnyOf(listOf(Rule.Due(today), Rule.HasTag("a"))))),
|
||||
Rule.AllOf(listOf(Rule.AllOf(listOf(Rule.Due(today))))),
|
||||
Rule.AllOf(listOf(Rule.AnyOf(listOf(Rule.Due(DueSpec.NextDays(3)), Rule.Due(DueSpec.NextDays(7)))))),
|
||||
Rule.AllOf(listOf(Rule.AnyOf(listOf(Rule.HasTag("a"), Rule.HasTag("a"))))),
|
||||
Rule.AllOf(listOf(Rule.AllOf(emptyList()))),
|
||||
Rule.Not(Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("a")))),
|
||||
)
|
||||
for (rule in notSimple) assertThat(SimpleRule.of(rule)).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a second next N days replaces the first`() {
|
||||
val form = SimpleRule().toggleDue(DueSpec.NextDays(3)).toggleDue(today).toggleDue(DueSpec.NextDays(7))
|
||||
assertThat(form.due).containsExactly(today, DueSpec.NextDays(7))
|
||||
assertThat(form.withNextDays(null).due).containsExactly(today)
|
||||
assertThat(form.toggleDue(today).due).containsExactly(DueSpec.NextDays(7))
|
||||
}
|
||||
|
||||
private fun window(window: DueWindow) = DueSpec.Window(window)
|
||||
}
|
||||
Reference in new issue
Block a user