Skip to content

Commit

Permalink
[WIP] repeat 增加 重复区间+时间点/区间 的能力
Browse files Browse the repository at this point in the history
  • Loading branch information
du00cs committed Jul 31, 2024
1 parent ef11b77 commit 855a48c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ case object Repeat extends Dimension with Rules {
override val dimDependents: List[Dimension] = List(TimeGrain, Duration, Time)
}

case class RepeatData(interval: Option[DurationData] = None,
case class RepeatData(interval: Option[DurationData] = None, // 间隔,如果与其它的配合,表示外层间隔
n: Option[Int] = None,
start: Option[TimeData] = None,
workdayType: Option[WorkdayType] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ trait Rules extends DimRules with LazyLogging {
}
)

private val predicateEveryGrain = "每(一个?|个)?(年度?|月|周|星期|天|小时|分钟)的?".regex

val ruleEveryGrainDatetime = Rule(
name = "<every> <grain> <datetime>",
pattern = List(
"每(一个?|个)?(年度?|月|周|星期|天|小时|分钟)的?".regex,
predicateEveryGrain,
and(isDimension(Time), isNotLatent).predicate),
prod = tokens {
case Token(_, GroupMatch(_ :: _ :: grainToken :: _)) :: Token(_, td: TimeData) :: _
Expand Down Expand Up @@ -179,4 +181,31 @@ trait Rules extends DimRules with LazyLogging {
Token(Repeat, RepeatData(start = start, repeatNFromInterval = outer))
}
)

val ruleEveryRepeat = Rule(
name = "每 x <repeat>",
pattern = List(predicateEveryGrain, isDimension(Repeat).predicate),
prod = tokens { case Token(_, GroupMatch(_ :: _ :: grainToken :: _)) :: Token(_, repeat: RepeatData):: _ =>
val grainHint: Option[Grain] = toGrain(grainToken)
(grainHint, repeat.repeatNFromInterval) match {
case (Some(everyGrain), Some(td)) if everyGrain >= td.timeGrain =>
val interval = DurationData(1, grainHint.getOrElse(everyGrain))
Token(Repeat, repeat.copy(interval = interval))
case _ => None
}
}
)

val ruleEveryRepeat1 = Rule(
name = "每 <repeat>",
pattern = List("每个?".regex, isDimension(Repeat).predicate),
prod = tokens { case _ :: Token(_, repeat: RepeatData):: _ =>
repeat.repeatNFromInterval match {
case Some(td) if td.timePred.maxGrain.isDefined =>
val interval = DurationData(1, td.timePred.maxGrain.get)
Token(Repeat, repeat.copy(interval = interval))
case _ => None
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ object Examples extends DimExamples {
implicit def _toTuple(tv: TimeValue) = Option(tv, None: Option[Form])

override def pairs: List[(ResolvedValue, List[String])] = List(
(RepeatValue(interval = DurationData(1, Week), start = (datetimeInterval(
new DuckDateTime(LocalDateTime.of(2013, 2, 18, 12, 0, 0)),
new DuckDateTime(LocalDateTime.of(2013, 2, 18, 18, 0, 0)),
Hour), None), repeatGrain = Day, n = 3), List("每个周一到周三下午", "每周一到周三的下午")),
(RepeatValue(start = (datetimeInterval(
new DuckDateTime(LocalDateTime.of(2013, 2, 18, 12, 0, 0)),
new DuckDateTime(LocalDateTime.of(2013, 2, 18, 18, 0, 0)),
Hour), None), repeatGrain = Day, n = 3), List("周一到周三下午")),
Hour), None), repeatGrain = Day, n = 3), List("周一到周三下午", "周一到周三的下午")),
(RepeatValue(DurationData(1, Day, schema = "P1D")), List("每天")),
(RepeatValue(DurationData(1, Week, schema = "P1W")), List("每周")),
(RepeatValue(DurationData(15, Minute, schema = "PT15M")), List("每隔15分钟", "隔15分钟")),
Expand Down

0 comments on commit 855a48c

Please sign in to comment.