Efficient Temporal Datalog Materialisation for Composite Event Recognition Periklis Mantenoglou \affiliations Örebro University, Sweden \emails periklis.mantenoglou@oru.se Abstract Several applications demand the timely detection of critical situations, such as threats to safety and transparency, over high-velocity streams of symbolic events. This demand has motivated the development of (i) event specification languages , which define composite events via temporal patterns over simpler events, and (ii) stream reasoning frameworks , evaluating patterns expressed in these languages. However, event specification languages are typically studied in isolation, complicating their comparison in terms of expressivity and obscuring the scope of their associated stream reasoners. To mitigate this issue, we map practical fragments of prominent event specification languages into Temporal Datalog → ⊝ \it{}^{\rightarrow}_{\circleddash} , a temporal Datalog with stratified negation and no future dependencies. To support efficient stream reasoning over Temporal Datalog → ⊝ \it{}^{\rightarrow}_{\circleddash} , we propose Streaming Trigger Graphs, an extension of a state-of-the-art technique for Datalog materialisation. Our approach yields a uniform composite event recognition mechanism that has the potential to generalise across a wide range of practical event specification languages. 1 Introduction Composite event recognition (CER) frameworks detect instances of situations of interest, such as safety and transparency threats, over streams of symbolic events [ 25 ] . Effective CER is critical in various domains, such as smart cities [ 28 ] , health systems [ 24 ] , and fleet management [ 52 ] . A CER solution typically involves two components: an event specification language (ESL), defining a class of composite event patterns via syntactic constructs and operators, and a reasoner evaluating these patterns with minimal latency over unbounded event streams. This separation enables a principled study of the trade-offs between ESL expressivity and reasoning tractability, avoiding ad-hoc solutions, and is thus prominent in CER [ 21 , 12 , 50 , 1 , 59 , 34 ] . Although there are various formal studies on specific ESLs [ 61 , 26 , 62 , 31 ] , a unifying approach is lacking, complicating ESL comparisons and obscuring the scope of the associated stream reasoners. This also leads to practical issues as composite event patterns expressed in one ESL often cannot be translated into another ESL in a faithful and optimised manner, which may prohibit the use of the reasoner that is best suited to a specific CER problem while also complicating comparisons between reasoners. Towards addressing this issue, we compile two prominent logic-based ESLs—based on the Event Calculus [ 29 ] and LARS [ 8 ] —into a variant of Temporal Datalog [ 14 ] with stratified negation and no future dependencies, which we term Temporal Datalog \it{}^{\rightarrow}_{\circleddash} . The Event Calculus has been used in ESLs for large-scale CER [ 35 , 3 , 24 , 5 , 32 , 50 , 34 ] , while LARS features practical fragments for CER [ 9 , 6 ] , along with extensions for increased expressivity [ 22 , 55 ] , and distribution [ 23 ] . Temporal Datalog has been studied extensively in the context of stream reasoning [ 14 , 20 , 43 , 41 , 42 ] , while being closely related to expressive ESLs with dedicated stream reasoners, such as DatalogMTL [ 57 , 58 , 59 ] . Thus, Temporal Datalog \it{}^{\rightarrow}_{\circleddash} serves as a natural formalism for embedding and studying ESLs. To support efficient CER over such embedded specifications, we propose Streaming Trigger Graphs (STGs), an extension of a state-of-the-art materialisation method for Datalog [ 47 ] that supports Temporal Datalog \it{}^{\rightarrow}_{\circleddash} programs. An STG leverages a so-called incremental stratification of the program to enable delta model updates over streaming data and optimisations that reduce redundant computations. Our contributions may be summarised as follows. First , we provide mappings from practical Event Calculus and LARS fragments to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} , and prove that they are equivalence-preserving with respect to query answering. Second , we propose STG-based materialisation as a novel reasoning algorithm for Temporal Datalog \it{}^{\rightarrow}_{\circleddash} , demonstrate its correctness and discuss reasoning optimisations. Through the use of Temporal Datalog \it{}^{\rightarrow}_{\circleddash} and STGs, we provide a uniform CER solution for two prominent ESLs, facilitating their formal analysis and comparison. Our contributions apply to ESLs with stratified negation and no future dependencies, commonly required in stream reasoning [ 60 ] , helping to further consolidate and advance CER. 2 Background 2.1 Logic Programming We investigate ESLs that are based on logic programming. Logic programs include variables, constants, predicates, and functors. Variables start with an upper-case letter, whereas predicates, functors and constants start with a lower-case letter. A term is either a variable, a constant, or an expression \it f(\boldsymbol{X}) , where \it f is a functor and \it\boldsymbol{X} is a term tuple. An expression \it p(\boldsymbol{X}) , where \it p is a predicate and \it\boldsymbol{X} is a term tuple, is called an atom. A fact is a variable-free atom. A literal is an atom optionally preceded by “ not ”, expressing negation-by-failure [ 15 ] . Arity means number of arguments. A logic program is a set of rules \it{h\leftarrow b_{1},\dots,b_{n}} , where \it\leftarrow denotes implication, atom \it h is the head of the rule and its body \it b_{1},\dots,b_{n} is a conjunction of conditions. All variables in a rule are implicitly universally quantified. In definite (resp. normal) logic programs, all body conditions are atoms (literals). An expression is called ground if it contains no variables. A substitution is a mapping of the terms in an expression to constants. A predicate is called intensional if it appears in the head of a rule with non-empty body, and extensional otherwise. A database \it D is a set of facts with extensional predicates. A Datalog program is a definite logic program without functors. Every Datalog program can be rewritten into an equivalent program partitioned into extensional rules (with only extensional body predicates) and intensional rules (with only intensional body predicates). An interpretation of a logic program \it P is a set of facts from \it P . To define the semantics of \it P , we identify its models, i.e., interpretations satisfying all rules in \it P . Definite logic programs have a unique minimal model, while normal logic programs (which include negation) may have multiple minimal models, complicating their semantic characterisation. To interpret such programs, the closed world assumption (CWA) allows us to infer “ \it\text{not}\ a ” if \it a cannot be finitely proven [ 40 ] . Thus, for a program with rule “ \it a\leftarrow b,\text{not}\ c ” and fact \it b , model \it\{b,a\} is preferred over model \it\{b,c\} , because, since it is impossible to prove \it c , the CWA infers “ \it\text{not}\ c ”, leading to the derivation of \it a via the rule. When the program does not include cyclic dependencies via not , i.e., it is stratified , we can derive an intended interpretation for it by applying the CWA to negated atoms following rule dependencies bottom-up [ 39 ] . Definition 1 (Global Stratification and Local Stratification) . Consider a normal logic program \it P with the predicates in set \it\Sigma and the ground atoms in set \it B . A rule \it r in \it P is: \it\displaystyle\begin{array}[]{l}h(\boldsymbol{X_{h}})\leftarrow b_{1}(\boldsymbol{X_{b_{1}}}),\dots,b_{m}(\boldsymbol{X_{b_{m}}}),\\ \hskip 20.44434pt\hskip 20.44434pt\;\text{not}\ c_{1}(\boldsymbol{X_{c_{1}}}),\dots,\text{not}\ c_{k}(\boldsymbol{X_{c_{k}}}).\end{array} \it P is globally stratified if \it\Sigma can be decomposed into disjoint sets (strata) \it\Sigma_{1} , …, \it\Sigma_{n} , so that, for every \it r in \it P , we have that, if \it h\in\Sigma_{i} , where \it i\in[n] , then: (i) \it b_{t}\in\Sigma_{j} , where \it t\in[m] and \it j\leq i , and (ii) \it c_{t}\in\Sigma_{j} , where \it t\in[k] and \it j1 ), we use \it k -compatibility to determine whether a node \it v for rule \it r and incoming edges from nodes \it u_{1} , …, \it u_{n} will be constructed (lines 8 – 9 ). The third requirement in Definition 8 ensures that node combinations used in previous iterations are not \it k -compatible, thereby preventing redundant node constructions, in line with semi-naïve evaluation [ 53 ] . After each iteration, we remove the nodes of depth \it k that are subsumed by another node with respect to the query they express (line 11 ), and then populate set \it I^{k} with the facts in the remaining nodes of depth \it k (line 12 ). \it v(D,I^{k\minus 1}) denotes an optimised materialisation strategy for these facts, restricting attention to tuples that have not been derived at a previous step, i.e., they are not in \it I^{k\minus 1} . We terminate if no new facts were added in the last iteration (line 14 ). 3 Event Specification Languages We focus on two prominent logical ESLs based on LARS and the Event Calculus. A thorough review of ESLs with informative comparisons is provided in Section 6 . 3.1 LARS LARS extends ASP with window operators and temporal modalities for stream reasoning [ 8 ] . A window operator \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{w}\mskip-2.0mua over formula \it a uses window function \it w to restrict its evaluation on a particular subset of a stream \it S . For example, time windows \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{n}\mskip-2.0mu restrict attention to the events that took place over the last \it n time-points. Temporal modalities express the temporal validity of a formula \it a . \it\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu{a} (resp. \it\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu{a} ) requires that \it a holds at some (every) time-point in \it S . \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!a requires that \it a holds when the evaluation time is shifted to \it T^{\prime} , granted that \it T^{\prime}\in S . In practice, it is used to express that \it a holds at some time-point in \it S (i.e., same as \it\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu ), but also to bind \it T^{\prime} to such a time-point. The main LARS-based stream reasoners are Ticker [ 9 ] and Laser [ 6 ] . Both are based on the “plain LARS” fragment, whose core building blocks are the so-called extended atoms . Definition 9 (Extended atom) . Let \it a be an atom \it p(\boldsymbol{X}) , where \it p is a predicate and \it\boldsymbol{X} is a sequence of object terms, \it T^{\prime} a time variable and \it d a positive integer. An extended atom \it e is defined by the following grammar: \it\displaystyle e::=a\ |\ \mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!a\ |\ \mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!a\ |\ \mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mua\ |\ \mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mua We focus on time windows and rules without \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}} operators in the head, leading to forward-propagating programs. Definition 10 (Plain LARS) . A plain LARS program is a set of rules of the form “ \it h\leftarrow b_{1},\dots,b_{m}. ”, where \it h is an atom, \it b_{i} , \it i\in[m] , is a (possibly negated) extended atom, and all window operators are time windows. In plain LARS, if a rule \it r includes condition \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!a , then \it T^{\prime} must be grounded earlier in the body of \it r , in order to avoid evaluating \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!a over arbitrary future time-points. Therefore, every such condition is preceded by a positive \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!a condition, which constrains \it T^{\prime} to the window \it\{T\minus d,\dots,T\} , where \it T is the rule evaluation time. While both Ticker and Laser employ plain LARS, they differ on their support for negation. Ticker allows non-stratified negation, while Laser is restricted to globally stratified programs. We focus on globally stratified plain LARS programs, motivated by the superior reasoning efficiency of Laser [ 8 ] , as well as by the common assumption in CER that there is a single, implicit model of the world expressing the actual composite event occurrences [ 19 ] . Example 2 (Plain LARS Program) . The following plain LARS program monitors safety in a network of devices. \it\displaystyle\begin{array}[]{l}verified(X)\leftarrow\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{2}\mskip-2.0mu\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0murepair(X).\end{array} \it\displaystyle\begin{array}[]{l}trusted(X)\leftarrow\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{6}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0muverified(X).\end{array} \it\displaystyle\begin{array}[]{l}unverified(X)\leftarrow\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{2}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu\neg repair(X).\end{array} \it\displaystyle\begin{array}[]{l}integrity\_threat\_of(X,Y)\leftarrow\\ \hskip 10.22217pt\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{2}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mutrusted(X),\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{2}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!unverified(Y),\\ \hskip 10.22217pt\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{2}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!connected(X,Y).\end{array} Rules ( 2 )–( 2 ) state that device \it X is \it verified if a \it repair occurred in a two time-step window, and \it trusted if it has been \it verified over a six time-step window. Rule ( 2 ) marks \it X as \it unverified if no \it repair occurred in a two time-step window, while rule ( 2 ) reports that a device \it Y poses an integrity threat to a trusted device \it X if \it Y was unverified and connected to \it X at some time-point \it T^{\prime} within a two time-step window. A global stratification places \it repair in the first stratum and the remaining predicates in the second stratum. 3.2 Event Calculus with Delayed Effects The Event Calculus (EC) is a temporal logic programming formalism where the functors are classified as event types and fluent types, expressing time-varying properties, and a constant sort is reserved for the possible values of each fluent type [ 29 ] . An event \it e(\boldsymbol{X}) (resp. fluent \it f(\boldsymbol{X}) ) is a term where \it e ( \it f ) is an event (fluent) type and \it\boldsymbol{X} contains only objects and variables. Expression \it f(\boldsymbol{X})\val v states that fluent \it f(\boldsymbol{X}) has value \it v and is often used in CER to denote composite events [ 3 , 24 ] . We focus on an EC variant where events may have d elayed e ffects on fluent values—which we term EC DE —as it is employed in a state-of-the-art CER framework [ 34 ] . The predicates in EC DE and their meaning are as follows. • \it\text{happensAt}(e(\boldsymbol{X}),T) : event \it e(\boldsymbol{X}) occurs at time \it T . • \it\text{holdsAt}(f(\boldsymbol{X})\val v,T) : fluent \it f(\boldsymbol{X}) has value \it v at \it T . • \it\text{initiatedAt}(f(\boldsymbol{X})\val v,T) / \it\text{terminatedAt}(f(\boldsymbol{X})\val v,T) : a time period where \it f(\boldsymbol{X})\val v is initiated/terminated at \it T . • \it\text{fi}(f(\boldsymbol{X})\val v,f(\boldsymbol{X})\val v^{\prime},d) : an initiation of \it f(\boldsymbol{X})\val v leads to a f uture i nitiation of \it f(\boldsymbol{X})\val v^{\prime} after \it d time-points, unless \it f(\boldsymbol{X})\val v is terminated in the meantime. • \it\text{p}(f(\boldsymbol{X})\val v) : the above future initiation of \it f(\boldsymbol{X})\val v^{\prime} is p ostponed by intermediate initiations of \it f(\boldsymbol{X})\val v . In EC DE , fluents are classified as simple or statically determined . The values of a simple fluent change based on the satisfaction of initiation and termination conditions and persist via the law of inertia. A statically determined fluent is defined as a Boolean combination of other fluents. Simple fluent definitions. The rules defining \it f(\boldsymbol{X_{f}})\val v based on immediate effects of events have this form: \it\begin{array}[]{l}\text{initiatedAt}\mathit{(f(\boldsymbol{X_{f}})\val v,\ T)}\leftarrow\\ \hskip 10.22217pt\text{happensAt}(e_{1}(\boldsymbol{X_{e_{1}}}),\ T)[,\\ \hskip 10.22217pt[\text{not}]\ \text{happensAt}(e_{2}(\boldsymbol{X_{e_{2}}}),\ T),\dots\\ \hskip 10.22217pt[\text{not}]\ \text{happensAt}(e_{n}(\boldsymbol{X_{e_{n}}}),\ T),\\ \hskip 10.22217pt[\text{not}]\ \text{holdsAt}(f_{1}(\boldsymbol{X_{f_{1}}})\val v_{1},\ T),\ \dots,\\ \hskip 10.22217pt[\text{not}]\ \text{holdsAt}(f_{m}(\boldsymbol{X_{f_{m}}})\val v_{m},\ T)].\\ \end{array} (15) We use “ \it[\ ] ” to denote optional parts of the rule, i.e., only the first positive happensAt atom is necessary. Rules with head \it\text{terminatedAt}(f(\boldsymbol{X_{f}})\val v,T) have the same form. fi and p facts express future initiations as follows: \it\displaystyle\begin{array}[]{l}\text{initiatedAt}\mathit{(F\val V^{\prime},T{+}d)}\leftarrow\\ \hskip 10.22217pt\text{fi}(F\val V,F\val V^{\prime},d),\text{initiatedAt}(F\val V,T),\\ \hskip 10.22217pt\text{not}\ \textsf{cancelled}(F\val V,T,T{+}d).\end{array} \it\displaystyle\begin{array}[]{l}\mathit{\textsf{cancelled}(F\val V,T,T\plus d)}\leftarrow\\ \hskip 10.22217pt\mathit{\textsf{termIn}(F\val V,T,T\plus d).}\end{array} \it\displaystyle\begin{array}[]{l}\mathit{\textsf{cancelled}(F\val V,T,T\plus d)}\leftarrow\\ \hskip 10.22217pt\text{p}(F\val V),\textsf{initIn}(F\val V,T,T\plus d).\end{array} \it\textsf{initIn}(F\val V{,}T{,}T\plus d) and \it\textsf{termIn}(F\val V{,}T{,}T\plus d) express that \it F\val V is initiated or terminated, respectively, at some time-point strictly between \it T and \it T\plus d . In rules ( 3.2 )–( 3.2 ), \it F is a variable spanning over fluents, while \it V and \it V^{\prime} span over its possible values. This abstraction emphasises that these rules are included regardless of the domain being modelled. However, they may only fire if fi —and possibly p —facts are included, expressing the dynamics of delayed fluent value changes in the domain. EC DE also uses the following rules regardless of domain. \it\displaystyle\begin{array}[]{l}\text{holdsAt}(F\val V,T)\leftarrow\text{initiatedAt}(F\val V,T\minus 1).\end{array} \it\displaystyle\begin{array}[]{l}\text{holdsAt}(F\val V,T)\leftarrow\\ \hskip 10.22217pt\text{holdsAt}(F\val V,T\minus 1),\\ \hskip 10.22217pt\text{not}\ \text{terminatedAt}(F\val V,T\minus 1).\end{array} \it\displaystyle\begin{array}[]{l}\text{terminatedAt}(F\val V,T)\leftarrow\\ \hskip 10.22217pt\text{initiatedAt}(F\val V^{\prime},T),V^{\prime}\nval V.\end{array} Rules ( 3.2 )–( 3.2 ) express the law of inertia, i.e., \it F\val V holds at \it T if it was initiated at the previous time-point \it T\minus 1 or held at \it T\minus 1 and it was not terminated at \it T\minus 1 . Rule ( 3.2 ) terminates \it F\val V when \it F is initiated with a value other than \it V , restricting fluents to at most one value at a time. Statically determined fluent definitions. Such a definition for \it f(\boldsymbol{X_{f}})\val v contains \it m rules where the \it i -th rule is: \it\begin{array}[]{l}\text{holdsAt}\mathit{(f(\boldsymbol{X_{f}})\val v,\ T)}\leftarrow\\ \hskip 10.22217pt[\text{not}]\ \text{holdsAt}(f_{i1}(\boldsymbol{X_{f_{i1}}})\val v_{i1},\ T)[,\\ \hskip 10.22217pt[\text{not}]\ \text{holdsAt}(f_{i2}(\boldsymbol{X_{f_{i2}}})\val v_{i2},\ T),\ \dots,\\ \hskip 10.22217pt[\text{not}]\ \text{holdsAt}(f_{in}(\boldsymbol{X_{f_{in}}})\val v_{in},\ T)].\end{array} (29) Together, these \it m rules define \it f(\boldsymbol{X_{f}})\val v as a DNF formula on the values held by the fluents in their bodies. This definition has proven to be equivalent to the interval-based analog used for practical CER [ 31 ] . EC DE programs are locally stratified, and thus admit a unique perfect model [ 34 ] . The reasoning task in EC DE is to compute holdsAt , i.e., the values of fluents at each time-point. Example 3 (EC DE Program) . An EC DE program monitoring device safety may include the following rules. \it\displaystyle\begin{array}[]{l}\text{initiatedAt}(safety(X)\val verified,T)\leftarrow\\ \hskip 10.22217pt\text{happensAt}(repair(X),T).\end{array} \it\displaystyle\begin{array}[]{l}\text{initiatedAt}(safety(X)\val unverified,T)\leftarrow\\ \hskip 10.22217pt\text{happensAt}(warning(X),T).\end{array} \it\displaystyle\begin{array}[]{l}\text{holdsAt}(integrity\_threat\_of(X,Y)\val\text{true},T)\leftarrow\\ \hskip 10.22217pt\text{holdsAt}(safety(X)\val trusted,T),\\ \hskip 10.22217pt\text{holdsAt}(safety(Y)\val unverified,T),\\ \hskip 10.22217pt\text{holdsAt}(connected(X,Y)\val\text{true},T).\end{array} \it\displaystyle\begin{array}[]{l}\text{fi}(safety(X)\val verified,safety(X)\val trusted,3).\end{array} Rule ( 3 ) (resp. rule ( 3 )) states that a device \it X is considered \it verified ( \it unverified ) after a \it repair(X) ( \it warning(X) ) event occurs. Rule ( 3 ) expresses that an \it unverified device \it Y poses an integrity threat to a \it trusted device \it X if they are connected. Fact ( 3 ) expresses that \it X becomes \it trusted after being \it verified for three consecutive time-points. 4 Embedding ESLs in Temporal Datalog \it{}^{\rightarrow}_{\circleddash} Plain LARS to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} . A plain LARS program \it P is not Temporal Datalog \it{}^{\rightarrow}_{\circleddash} because it includes temporal operators (Definition 10 ). We propose a translation of \it P into a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P^{\prime} that is equivalent with respect to query evaluation for any input stream. Algorithm 2 \it\mathsf{pLARS2TD} Input : A plain LARS program \it P . Output : A Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P^{\prime} . 1: \it Pred(P^{\prime})\mathrel{:=}\emptyset , \it P^{\prime}\mathrel{:=}\emptyset 2: for all predicates \it p\text{/}n in \it P do \it Pred(P^{\prime}).\mathsf{add}(p\text{/}(n\plus 1)) 3: \it all\_rules\_t\mathrel{:=}\emptyset 4: for all rules \it r\in P do 5: \it rules\_t\mathrel{:=}\{(r,\emptyset)\} 6: for all time variables \it T^{\prime} appearing in \it r do 7: let \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!p(\boldsymbol{X}) be \it first\_positive\_at(r,T^{\prime}) 8: for all \it(r^{\prime},\mu)\in rules\_t do 9: \it rules\_t.\mathsf{remove}((r^{\prime},\mu)) 10: for all \it i{:}0{\leq}i{\leq}d do \it rules\_t.\mathsf{add}((r^{\prime}{,}\mu{\cup}\{T^{\prime}{\mapsto}T\minus i\})) 11: \it all\_rules\_t.\mathsf{add\_all}(rules\_t) 12: for all \it(r,\mu)\in all\_rules\_t do 13: \it replace(r,head(r),p_{h}(\boldsymbol{X},T)) where \it head(r)\val p_{h}(\boldsymbol{X}) 14: for all body conditions \it b\in r do 15: if \it b is \it p(\boldsymbol{X}) then \it replace(r,b,p(\boldsymbol{X},T)) 16: else if \it b is \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!p(\boldsymbol{X}) then \it replace(r,b,p(\boldsymbol{X},\mu(T^{\prime}))) 17: else if \it b is \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!p(\boldsymbol{X}) then 18: if \it T\minus d\leq\mu(T^{\prime}) then \it replace(r,b,p(\boldsymbol{X},\mu(T^{\prime}))) 19: else \it replace(r,b,\bot) 20: else if \it b is \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mup(\boldsymbol{X}) then 21: \it Pred(P^{\prime}).\mathsf{add}(p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}) 22: for all \it i{:}0{\leq}i{\leq}d do \it P^{\prime}.\mathsf{add}(p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}(\boldsymbol{X},T){\leftarrow}p(\boldsymbol{X},T\minus i)) 23: \it replace(r,b,p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}(\boldsymbol{X},T)) 24: else if \it b is \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mup(\boldsymbol{X}) then 25: \it Pred(P^{\prime}).\mathsf{add}(p^{d}_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu}) 26: \it P^{\prime}{.}\mathsf{add}(p^{d}_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu}(\boldsymbol{X}{,}T){\leftarrow}p(\boldsymbol{X}{,}T){,}\dots{,}p(\boldsymbol{X}{,}T\minus d)) 27: \it replace(r,b,p^{d}_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu}(\boldsymbol{X},T)) 28: \it P^{\prime}.\mathsf{add}(r) 29: return \it P^{\prime} Algorithm 2 outlines our translation. To construct \it P^{\prime} , we first introduce predicates that denote the atoms in \it P . For each predicate \it p with arity \it n in \it P , we introduce a predicate \it p with arity \it n\plus 1 in \it P^{\prime} , where the extra argument is reserved for a time term (see line 2 ). In order for \it P^{\prime} to be Temporal Datalog \it{}^{\rightarrow}_{\circleddash} , each rule in \it P^{\prime} must contain one time variable \it T , reflecting its evaluation time (see rule schema ( 3 )). To cater for this, we need to map, in each rule \it r of \it P , each time variable \it T^{\prime} appearing in \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}} operators to time terms \it T\minus k . In plain LARS, \it T^{\prime} is grounded based on the first positive \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!p(\boldsymbol{X}) body condition of \it r . Thus, we replace \it r with \it d\plus 1 rules such that in the \it i -th rule, where \it i\in\{0,\dots,d\} , \it T^{\prime} is mapped to \it T\minus i , denoted via a mapping \it\mu (see lines 3 – 11 ). This reflects the semantics of \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!p(\boldsymbol{X}) as an existential quantifier that also maintains via \it T^{\prime} the time-points in the window where \it p(\boldsymbol{X}) holds. Example 4 (Plain LARS to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} ) . Consider the plain LARS program in Example 2 . Rule ( 2 ) contains one time variable \it T^{\prime} , grounded via \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{2}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!unverified(Y) . Thus, Algorithm 2 produces three instances of this rule, with mappings \it T^{\prime}{\mapsto}T\minus 2 , \it T^{\prime}{\mapsto}T\minus 1 and \it T^{\prime}{\mapsto}T . Afterwards, we iterate over each discovered \it(r,\mu) pair and substitute each atom in \it r with a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} atom (lines 12 – 27 ). For the head \it p_{h}(\boldsymbol{X}) of \it r , we append to \it\boldsymbol{X} a time variable \it T denoting the rule evaluation time (line 13 ). Then, we iterate over each body condition \it b in \it r . If \it b contains negation, we implicitly replace “ \it\neg ” with “ not ”, as their semantics coincide for globally stratified plain LARS programs [ 6 ] . Then, setting negation aside, we distinguish the following cases. If \it b is \it p(\boldsymbol{X}) (resp. \it\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!p(\boldsymbol{X}) ), then we replace \it b with \it p(\boldsymbol{X},T) ( \it p(\boldsymbol{X},\mu(T^{\prime})) ) in \it r (lines 15 – 16 ). If \it b is \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{T^{\prime}}\!p(\boldsymbol{X}) , then we distinguish two cases. If \it\mu(T^{\prime}) falls within \it[T\minus d,T] , then we replace \it b with \it p(\boldsymbol{X},\mu(T^{\prime})) (line 18 ). Otherwise, if \it\mu(T^{\prime}){<}T\minus d , then we cannot satisfy \it b , and thus disable the rule by replacing \it b with \it\bot (line 19 ). If \it b is \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mup(\boldsymbol{X}) (resp. \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mup(\boldsymbol{X}) ), we introduce predicate \it p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu} ( \it p^{d}_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu} ) with arity \it n\plus 1 and replace \it b with \it p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}(\boldsymbol{X},T) ( \it p^{d}_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu}(\boldsymbol{X},T) ) (lines 20 – 27 ). Then, we add rules in \it P^{\prime} that define \it p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}(\boldsymbol{X},T) ( \it p^{d}_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu}(\boldsymbol{X},T) ) as the disjunction (conjunction) of \it p(\boldsymbol{X}{,}T),\dots,p(\boldsymbol{X}{,}T\minus d) . Example 5 (Example 4 cont’d) . To translate rule ( 2 ), Algorithm 2 replaces its head with \it verified(X,T) , its body with \it repair_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}^{2}(X,T) and introduces three rules of the form: “ \it repair_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}^{2}(X,T)\leftarrow repair(X,T\minus i). ”, for \it i\in\{0,1,2\} . Rules ( 2 )–( 2 ) are translated similarly by mapping the \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu operators they contain according to lines 24 – 27 . Rule ( 2 ) is translated into three rules, one for each mapping of \it T^{\prime} (see Example 4 ). For \it T^{\prime}{\mapsto}T\minus 1 , e.g., we construct this rule: \it\displaystyle\begin{array}[]{l}integrity\_threat\_of(X,Y,T)\leftarrow\\ \hskip 10.22217pttrusted_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu}^{2}(X,T),unverified(Y,T\minus 1),\\ \hskip 10.22217ptconnected(X,Y,T\minus 1).\end{array} Theorem 1 ( \it\mathsf{pLARS2TD} Correctness) . Consider a plain LARS program \it P , a stream \it S and let Algorithm 2 map \it P to \it P^{\prime} . \it P^{\prime} is Temporal Datalog \it{}^{\rightarrow}_{\circleddash} , and given that \it I^{*} is the perfect model of \it P^{\prime}\cup S , at time-point \it t , \it P\cup S derives: • \it p(\boldsymbol{X}) iff \it I^{*}\models p(\boldsymbol{X},t) • \it\mathop{\scalebox{0.9}{@}}\nolimits_{t^{\prime}}p(\boldsymbol{X}) iff \it I^{*}\models p(\boldsymbol{X},t^{\prime}) • \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathop{\scalebox{0.9}{@}}\nolimits_{t^{\prime}}p(\boldsymbol{X}) iff \it I^{*}\models p(\boldsymbol{X},t^{\prime}) and \it t\minus d\leq t^{\prime}\leq t . • \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mup(\boldsymbol{X}) iff \it I^{*}\models p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu}(\boldsymbol{X},t) • \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mup(\boldsymbol{X}) iff \it I^{*}\models p^{d}_{\mathord{\scalebox{0.7}{$\it\Box$}}\mskip 2.0mu}(\boldsymbol{X},t) . Proof Sketch. Rules in \it P^{\prime} each contain one time variable, are forward-propagating (see, e.g., lines 22 , 26 and 10 ), and \it P^{\prime} is temporally stratified since \it P is globally stratified and Algorithm 2 introduces no negative dependencies. Thus, \it P^{\prime} is Temporal Datalog \it{}^{\rightarrow}_{\circleddash} (Definition 4 ). For model equivalence, we follow an inductive proof on the global stratification \it\Sigma_{1},\dots,\Sigma_{n} of \it P . For extensional predicates, correctness follows from the faithfulness of the translation: e.g., \it\mathbin{\scalebox{0.8}{$\it\boxplus$}}^{d}\mskip-2.0mu\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mup(\boldsymbol{X}) holds iff \it p(\boldsymbol{X}) holds now or at least once over the last \it d time-points, matching the definition of \it p^{d}_{\mathord{\scalebox{0.8}{$\it\Diamond$}}\mskip 2.0mu} (line 22 ). For the inductive step at \it\Sigma_{i} , negative body conditions in any rule defining \it p\in\Sigma_{i} refer to lower strata and are thus evaluated identically by \it P and \it P^{\prime} . Correctness then reduces to the faithfulness of the translation of the temporal operators, which guarantees that the positive conditions are also evaluated equivalently. ∎ EC DE to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} . We consider an EC DE program \it P consisting of rules ( 3.2 )–( 3.2 ), as well as rules following schemata ( 15 ) and ( 29 ), and possibly fi and p facts. \it P contains functors, in the form of event and fluent types, atoms with multiple time terms (see rules ( 3.2 )–( 3.2 )), and an inequality comparison in rule ( 3.2 ). These elements are not in Temporal Datalog \it{}^{\rightarrow}_{\circleddash} and thus we need to translate \it P into a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P^{\prime} . Algorithm 3 \it\mathsf{EC2TD} Input : An EC DE program \it P . Output : A Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P^{\prime} . 1: \it Pred(P^{\prime})\mathrel{:=}\emptyset , \it P^{\prime}\mathrel{:=}\emptyset 2: for all event types \it e\text{/}n in \it P do \it Pred(P^{\prime}).\mathsf{add}(e\text{/}(n\plus 1)) 3: for all fluent types \it f\text{/}n in \it P do 4: \it Pred(P^{\prime}).\mathsf{add}(f^{h}\text{/}(n\plus 2)) 5: if \it f simple then \it Pred(P^{\prime}).\mathsf{add\_all}(f^{i}\text{/}(n\plus 2){,}f^{t}\text{/}(n\plus 2)) 6: for all rules \it r\in P following schema ( 15 ) or schema ( 29 ) do 7: \it P^{\prime}.\mathsf{add}(\mathsf{mapEC}(head(r)){\leftarrow}\bigwedge_{b\in body(r)}[\mathsf{not}]\ \mathsf{mapEC}(b)) 8: for all simple fluent types \it f and values \it v of \it f do 9: \it P^{\prime}.\mathsf{add}(f^{h}(\boldsymbol{X},v,T)\leftarrow f^{i}(\boldsymbol{X},v,T\minus 1)) 10: \it P^{\prime}.\mathsf{add}(f^{h}(\boldsymbol{X}{,}v{,}T){\leftarrow}f^{h}(\boldsymbol{X}{,}v{,}T\minus 1),\mathsf{not}\ f^{t}(\boldsymbol{X}{,}v{,}T\minus 1)) 11: for all values \it v^{\prime}\nval v of \it f do 12: \it P^{\prime}.\mathsf{add}(f^{t}(\boldsymbol{X},v,T)\leftarrow f^{i}(\boldsymbol{X},v^{\prime},T)) 13: for all \it\mathsf{fi}(f(\boldsymbol{X})\val v,f(\boldsymbol{X})\val v^{\prime},d)\in P do 14: \it b\mathrel{:=}f^{i}(\boldsymbol{X},v,T\minus d),\mathsf{not}\ f^{t}(\boldsymbol{X},v,T\minus d\plus 1),\dots, 15: \it\mathsf{not}\ f^{t}(\boldsymbol{X},v,T\minus 1) 16: if \it\mathsf{p}(f(\boldsymbol{X})\val v)\in P then 17: \it b.\mathsf{add}(\mathsf{not}\ f^{i}(\boldsymbol{X},v,T\minus d\plus 1),\dots,\mathsf{not}\ f^{i}(\boldsymbol{X},v,T\minus 1)) 18: \it P^{\prime}.\mathsf{add}(f^{i}(\boldsymbol{X},v^{\prime},T)\leftarrow b) 19: return \it P^{\prime} 20: function \it\mathsf{mapEC} ( \it a ) 21: case \it a : \it\mathsf{happensAt}(e(\boldsymbol{X}),T) return \it e(\boldsymbol{X},T) 22: case \it a : \it\mathsf{initiatedAt}(f(\boldsymbol{X})\val v{,}T) return \it f^{i}(\boldsymbol{X}{,}v{,}T) 23: case \it a : \it\mathsf{terminatedAt}(f(\boldsymbol{X})\val v{,}T) return \it f^{t}(\boldsymbol{X}{,}v{,}T) 24: case \it a : \it\mathsf{holdsAt}(f(\boldsymbol{X})\val v{,}T) return \it f^{h}(\boldsymbol{X}{,}v{,}T) Algorithm 3 outlines the translation steps. Towards an equivalent program without functors, for each event type \it e in \it P , we introduce to \it P^{\prime} one predicate \it e , adding one more argument to express time (see line 2 ). Similarly, for each fluent type \it f in \it P we introduce a predicate \it f^{h} in \it P^{\prime} , denoting when the fluent holds, and equip it with a value argument and a time argument (lines 3 – 4 ). If \it f is a simple fluent type, then we additionally introduce predicates \it f^{i} and \it f^{t} to denote its initiations and terminations (line 5 ). As denoted by the \it\mathsf{mapEC} function (lines 20 – 24 ), these new predicates correspond to EC DE predicates for particular events and fluents. Next, we compile each domain rule, following schema ( 15 ) or schema ( 29 ), by mapping its head and body atoms to the introduced Temporal Datalog \it{}^{\rightarrow}_{\circleddash} predicates (lines 6 – 7 ). Rules ( 3.2 )–( 3.2 ) involve fluent variables, while the predicates we introduced are fluent-specific. To cater for this, we add in \it P^{\prime} one such rule for each fluent type \it f in \it P , while grounding variable \it V according to the possible values of \it f (lines 8 – 12 ). We follow a similar route for rules ( 3.2 )–( 3.2 ), while additionally compiling away the predicates with two time terms by explicitly adding a body condition referring to each time-point in the range they define (lines 13 – 18 ). Example 6 (EC DE to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} ) . Consider the EC DE program in Example 3 . Algorithm 3 translates rules ( 3 )–( 3 ) by mapping their atoms using \it\mathsf{mapEC} . Rule ( 3 ), e.g., is translated into: “ \it safety^{i}(X,verified,T){\leftarrow}repair(X,T). ”. Fact ( 3 ) is expressed in Temporal Datalog \it{}^{\rightarrow}_{\circleddash} using the following rule: \it\displaystyle\begin{array}[]{l}safety^{i}(X,trusted,T)\leftarrow\\ \hskip 10.22217ptsafety^{i}(X,verified,T\minus 3),\\ \hskip 10.22217pt\text{not}\ safety^{t}(X,verified,T\minus 2),\\ \hskip 10.22217pt\text{not}\ safety^{t}(X,verified,T\minus 1).\end{array} Moreover, for each value of \it safety(X) , we construct rules expressing its default persistence and its incompatibility with other values. For value \it trusted , e.g., we have: \it\displaystyle\begin{array}[]{l}safety^{h}(X,trusted,T)\leftarrow safety^{i}(X,trusted,T\minus 1).\end{array} \it\displaystyle\begin{array}[]{l}safety^{h}(X,trusted,T)\leftarrow safety^{h}(X{,}trusted{,}T\minus 1){,}\\ \hskip 113.0pt\text{not}\ safety^{t}(X{,}trusted{,}T\minus 1).\end{array} \it\displaystyle\begin{array}[]{l}safety^{t}(X,trusted,T)\leftarrow safety^{i}(X,unverified,T).\end{array} \it\displaystyle\begin{array}[]{l}safety^{t}(X,trusted,T)\leftarrow safety^{i}(X,verified,T).\end{array} Theorem 2 ( \it\mathsf{EC2TD} Correctness) . Consider an EC DE program \it P , a stream \it S and let Algorithm 3 map \it P to \it P^{\prime} . \it P^{\prime} is Temporal Datalog \it{}^{\rightarrow}_{\circleddash} , and, for each fluent \it f(\boldsymbol{X}) , value \it v of \it f and time-point \it t , \it P\cup S derives \it\text{holdsAt}(f(\boldsymbol{X})\val v,t) iff \it{I^{*}\models f^{h}(\boldsymbol{X},v,t)} , where \it I^{*} is the perfect model of \it P^{\prime}\cup S . Proof sketch. All rules added by Algorithm 3 contain one time variable and are forward-propagating. Negation appears (i) on conditions with a positive temporal offset (e.g., line 10 ), (ii) in a globally stratified statically determined fluent definition, lacking cyclic dependencies [ 33 ] , and (iii) in an \it f^{h} condition of a rule defining \it f^{\prime i} or \it f^{\prime t} ( \it f\nval f^{\prime} ). Case (iii) preserves temporal stratification as \it f^{h} may depend negatively on \it f^{\prime i} or \it f^{\prime t} only via a positive time offset. Therefore, \it P^{\prime} is Temporal Datalog \it{}^{\rightarrow}_{\circleddash} (Definition 4 ). Semantic equivalence follows from the faithfulness of the translation up to predicate renaming. ∎ 5 Streaming Trigger Graphs We cannot use TGs to materialise Temporal Datalog \it{}^{\rightarrow}_{\circleddash} programs because (i) they do not support negation and (ii) they do not support variables with countably infinite groundings, such as time variables. The presence of such variables makes the order of rule evaluation crucial, as materialisation can become trapped in an infinite chain of derivations for one predicate, thereby preventing the derivation of facts for other predicates. Tackling this issue by generating a TG \it G^{t} that materialises the program up to the latest time point \it t is problematic because incrementally updating \it G^{t} in the presence of new data may lead to incorrect results, while recomputing a TG from scratch at every time step is prohibitively expensive in the streaming setting. To address these issues, we propose Streaming Trigger Graphs (STGs), which constitute an optimised computational model for Temporal Datalog \it{}^{\rightarrow}_{\circleddash} . We start with Streaming Execution Graphs (SEGs), which extend EGs with negation and time. Mirroring the relationship between EGs and TGs, we then define STGs as SEGs that materialise Temporal Datalog \it{}^{\rightarrow}_{\circleddash} programs correctly. Definition 11 (Streaming Execution Graph) . A Streaming Execution Graph (SEG) for a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P is a graph \it G\val(V,E^{+},E^{-},\mathrm{rule},\mathrm{time},l) , where: • \it V is a set of nodes. • \it E^{+} is a set of directed edges called p-edges. • \it E^{-} is a set of n-edges, i.e., labelled directed hyperedges \it(U,\{v\}) , where \it U\subset V . • \it\mathrm{rule} is a function mapping each node in \it V to a rule in \it P . • \it\mathrm{time} is a mapping of each node in \it V to a time-point. • \it l is a function labelling each p-edge \it(u,v) in \it E^{+} and each n-edge \it(U,\{v\}) in \it E^{-} with a positive integer \it j , denoted respectively as \it u\rightarrow_{j}v and \it{U\rightarrow_{j}\{v\}} . We have \it u\rightarrow_{j}v (resp. \it u\in U , where \it{U\rightarrow_{j}\{v\}} ), only if: – the predicate of the \it j -th positive (negative) condition of \it\mathrm{rule}(v) equals the head predicate of \it\mathrm{rule}(u) , and – \it\mathrm{time}(u) is equal to \it\mathrm{time}(v) minus the time offset of the \it j -th positive (negative) body condition in \it\mathrm{rule}(v) . For each non-leaf node \it v , there is exactly one p-edge \it{u{\rightarrow_{j}}v} or n-edge \it U{\rightarrow_{j}}\{v\} for the \it j -th condition in \it\mathrm{rule}(v) . Considering a rule \it r with head \it h , and sets \it b^{+} and \it b^{-} , containing respectively the atoms in positive and in negative body conditions of \it r , if there is a substitution \it\sigma on the variables in \it r such that \it b^{+}_{i}\sigma holds for all \it b^{+}_{i} in \it b^{+} , \it r derives \it h\sigma only if \it b^{-}_{i}\sigma does not hold for any \it b^{-}_{i} in \it b^{-} . Intuitively, the atoms in \it b^{-} act as filters that disqualify substitutions satisfying at least one such atom. To reflect this, Definition 11 uses an n-edge \it U\rightarrow_{i}\{v\} , where \it\mathrm{rule}(v)\val r , to denote that if at least one node \it u\in U contains fact \it b^{-}_{i}\sigma then fact \it h\sigma should not be introduced in \it v . Moreover, according to Definition 11 , SEGs restrict body condition evaluation over facts with the appropriate time offset compared to the head predicate. This minimises the atoms considered in joins during materialisation without compromising correctness. Definition 12 (Facts in SEG) . Let \it P be a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program, \it S a stream, \it G a SEG for \it P , \it v a node in \it G and \it\sigma a substitution for \it\mathrm{rule}(v) . The rules in \it P are partitioned into extensional and intensional. \it h and \it b^{+} (resp. \it b^{-} ) are the head and the set of atoms in positive (negative) conditions of \it\mathrm{rule}(v) . \it v(S) contains a fact \it h\sigma if: • \it\mathrm{rule}(v) is extensional and for each atom \it b_{i} in \it b^{+} ( \it b^{-} ) we have \it b_{i}\sigma\in S ( \it b_{i}\sigma\notin S ), or • \it\mathrm{rule}(v) is intensional, for each atom \it b_{i} in \it b^{+} we have \it b_{i}\sigma\in u(S) , where \it u\rightarrow_{i}v is a p-edge in \it G , and for each atom \it b_{i} in \it b^{-} and each node \it u\in U , where \it U\rightarrow_{i}\{v\} is an n-edge in \it G , we have \it b_{i}\sigma\notin u(S) . \it G(S)\val S\cup\bigcup_{v\in V}v(S) contains all facts derived by materialising SEG \it G on stream \it S . If \it G(S) contains all facts that follow from \it P\cup S , and no other facts, then we say that SEG \it G is an STG. Definition 13 (Streaming Trigger Graph) . Considering a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P , a stream \it S and a SEG \it G for \it P , we say that \it G is a Streaming Trigger Graph (STG) for \it P\cup S if, for each fact \it a , we have \it P\cup S\models a iff \it a\in G(S) . \it G is a STG for \it P if, for each stream \it S , \it G is a STG for \it P\cup S . Towards practical STG-based materialisation, we introduce a form of temporal stratification that allows us to materialise Temporal Datalog \it{}^{\rightarrow}_{\circleddash} programs incrementally in time. Definition 14 (Incremental Stratification) . Consider a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P , and the program \it P_{T} derived by removing from \it P all body conditions with time offset \it k>0 . A temporal stratification \it\boldsymbol{B_{1}},\boldsymbol{B_{2}},\dots of \it P is called an incremental stratification of \it P if, \it\forall t\geq 1 , all tuples in the strata of \it\boldsymbol{B_{t}} have time-point \it t , and the predicate sets obtained from \it\boldsymbol{B_{t}} form a global stratification of \it P_{T} . The following corollary holds due to our restriction on forward-propagating and temporally stratified programs. Corollary 1 . Every Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program has an incremental stratification. To construct an incremental stratification \it\boldsymbol{B_{1}},\boldsymbol{B_{2}},\dots for program \it P , we compute a global stratification \it\Sigma_{1} , …, \it\Sigma_{n} for \it P_{T} , and set \it\boldsymbol{B_{t}}\val B_{t}^{1},\dots,B_{t}^{n} and \it B_{t}^{i}\val\{(p,t)|p{\in}\Sigma_{i}\} , where \it i\in[n] , for each \it t\geq 1 . We may then compute the perfect model of \it P over a stream \it S by setting \it I^{0}\val S and processing \it\boldsymbol{B_{1}},\boldsymbol{B_{2}},\dots bottom-up. For each \it\boldsymbol{B_{t}} , we substitute \it T with \it t in \it P and evaluate its rules following the predicate stratification in \it\boldsymbol{B_{t}} , leading to interpretation \it I^{t} . Crucially, \it I^{t} is the perfect model of \it P\cup S up to time-point \it t , enabling the incremental materialisation of \it P . For TGs, \it k -compatibility ensures that, for each node \it v , its incoming edges stem from nodes defining the predicates in the conditions of \it\mathrm{rule}(v) (see Definition 8 ). For STGs, we update \it k -compatibility to ensure that the time labels of these nodes are also consistent with the conditions of \it\mathrm{rule}(v) . Definition 15 ( \it k -compatibility) . Consider program \it P with intensional rule \it r and SEG G. Tuple \it(u_{1},\dots,u_{n}) of nodes from \it G is \it k -compatible with \it r at time \it t if: • the predicate in the head of \it u_{j} equals the predicate of the \it j -th positive condition of \it r ; • \it\mathrm{time}(u_{j}) is equal to \it t minus the offset in the \it j -th positive condition of \it r ; • each \it u_{j} was constructed at an iteration prior to \it k ; and • at least one \it u_{j} was constructed at iteration \it k\minus 1 . Unfortunately, \it k -compatibility is inadequate when all tuple nodes were constructed when processing a previous stratum. Consider, e.g., this traffic light monitoring program: \it\displaystyle red(L,T) \it\displaystyle\leftarrow green(L,T\minus 1). (39) \it\displaystyle red(L,T) \it\displaystyle\leftarrow green(L,T\minus 2). (40) \it\displaystyle green(L,T) \it\displaystyle\leftarrow red(L,T\minus 1),red(L,T\minus 2). (41) Given \it red(a,1) and \it red(a,2) , we build an EG \it G^{4} up to \it t\val 4 . In the first iteration, we build node for rule ( 41 ) with fact \it green(a,3) . In the second iteration, we add node for rule ( 39 ) with fact \it red(a,4) , completing materialisation up to \it t\val 4 . To update \it G^{4} beyond \it t\val 4 , we need further iterations. However, since \it green(a,3) was computed two iterations ago, there are no \it k -compatible nodes with rule ( 40 ) at \it t\val 5 , prohibiting the derivation of \it red(a,5) . To address this issue, we propose the notion of consistency . Definition 16 (Consistency) . Consider program \it P with intensional rule \it r and SEG G. Tuple \it(u_{1},\dots,u_{n}) of nodes from \it G is consistent with \it r at time \it t if: • the predicate in the head of \it u_{j} equals the predicate of the \it j -th positive condition of \it r ; • \it\mathrm{time}(u_{j}) is equal to \it t minus the offset in the \it j -th positive condition of \it r ; and • each \it u_{j} was constructed at an iteration prior to \it k . Algorithm 4 \it\mathsf{STGmat} Input : A Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P and a stream \it S . Output : The facts in an STG for \it P\cup S up to time-point \it t . 1: \it t\mathrel{:=}0 , \it k\mathrel{:=}0 , \it S_{\leq 0}\mathrel{:=}\emptyset , \it G\mathrel{:=}\emptyset , \it I^{0}\mathrel{:=}\emptyset 2: \it\boldsymbol{B_{t}}\mathrel{:=}\textsc{IncrementallyStratify}(P) 3: while True do 4: \it t{\mathrel{:=}}t\plus 1 , \it S_{\leq t}{\mathrel{:=}}S_{\leq t\minus 1}{\cup}read(S,t) , \it P^{t}{\mathrel{:=}}ground(P,t) 5: for all \it B_{t}^{j}\in\boldsymbol{B_{t}} do 6: repeat 7: \it k\mathrel{:=}k\plus 1 , \it I^{k}\mathrel{:=}I^{k\minus 1} 8: for all rules \it r in \it P^{t} : \it pred(head(r))\in B_{t}^{j} do 9: if \it r is extensional then 10: \it G.\mathsf{add}((v,\emptyset,\emptyset,\mathrm{rule}(v)\val r,\mathrm{time}(v)\val t,\emptyset)) 11: continue 12: \it E^{-}\mathrel{:=}\emptyset 13: for all \it b^{-}_{i}\in r do 14: \it U\mathrel{:=}\{u|pred(head(\mathrm{rule}(u)))\val pred(b^{-}_{i})\wedge 15: \it\mathrm{time}(u)\val t\minus offset(b^{-}_{i})\} 16: \it E^{-}.\mathsf{add}((U\rightarrow_{i}\{v\})) 17: if \it\forall b^{+}_{i}\in r : \it pred(b^{+}_{i})\not\in B_{t}^{j}\vee offset(b^{+}_{i})>0 then 18: for all \it(u_{1}{,}\dots{,}u_{n}) consistent with \it r do 19: \it G.\mathsf{add}((v,\bigcup_{i\in[n]}(u_{i},v),E^{-},\mathrm{rule}(v)\val r, 20: \it\mathrm{time}(v)\val t,\bigcup_{i\in[n]}l((u_{i},v))\val i)) 21: else for all \it(u_{1},\dots,u_{n}) \it k -compatible with \it r do 22: \it G.\mathsf{add}((v,\bigcup_{i\in[n]}(u_{i},v),E^{-},\mathrm{rule}(v)\val r, 23: \it\mathrm{time}(v)\val t,\bigcup_{i\in[n]}l((u_{i},v))\val i)) 24: \it G\mathrel{:=}\textsc{minDatalog}(G) 25: for all nodes \it v of iteration \it k do \it I^{k}{.}\mathsf{add}(v(S_{\leq t},I^{k\minus 1})) 26: until \it I^{k}\val I^{k\minus 1} 27: \it output(I^{k}) , \it\textsc{Forget}(G,S_{\leq t}) Figure 1: STG for Example 7 up to \it t\val 4 . For each node \it v , blue (resp. vermillion) coloured labels mark \it\mathrm{time}(v) (the “forget time” of \it v ). Algorithm 4 outlines the steps of STG-based materialisation for program \it P and stream \it S . First, we compute an incremental stratification for \it P , with \it\boldsymbol{B_{t}} denoting the strata with time-point \it t (line 2 ). Next, for each time-point \it t , we (i) read the facts of \it S occurring at \it t (line 4 ); (ii) ground the time variable of \it P to \it t , leading to program \it P^{t} (line 4 ); (iii) iterate over the strata \it B_{t}^{j} in \it\boldsymbol{B_{t}} and update the STG \it G constructed thus far with nodes computing the predicates in \it B_{t}^{j} over \it P^{t} (lines 5 – 26 ); (iv) output the facts computed at \it t based on \it G (line 27 ); and (v) remove from \it G and \it S_{\leq t} —the stream read thus far—all nodes and extensional facts that cannot be used for derivations after \it t according to \it P (line 27 ). For step (iii), we materialise the predicates in \it B_{t}^{j} in a process similar to TG-materialisation (compare lines 6 – 26 and Algorithm 1 ). The first key difference concerns negation: for each negative condition \it b_{i}^{-} in the rule of a node, we add one incoming n-edge whose source is the set of nodes with the same predicate as \it b_{i}^{-} and a time label corresponding to the time-point in \it b_{i}^{-} (lines 12 – 16 ). The second difference is that we rely on node consistency—instead of \it k -compatibility—for rules where all body conditions refer to a previous stratum, i.e., their predicate is not in \it B_{t}^{j} or their time-point is earlier than the current one (lines 17 – 19 ). Optimisations. TGs reduce redundant computations via two optimisations [ 47 ] : (i) after each round \it k , nodes constructed at \it k whose query is subsumed by another node, based on rewritings recursively replacing intensional predicates with extensional ones following TG dependencies, are removed; and (ii) joins during node materialisation are restricted to tuples that are not in the previous interpretation. Both optimisations extend to STGs. For (i), n-edges stem from nodes in previous strata, so their facts can be treated as extensional in query rewritings (line 24 ). Optimisation (ii) applies unchanged (line 25 ). We may further optimise STGs by forgetting nodes whose facts can no longer contribute to future derivations based on their time-stamps. Proposition 1 (Forgetting) . Consider a Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P and an STG \it G for \it P with node \it v where \it\mathrm{rule}(v) defines predicate \it p . If \it k_{p} is the maximum temporal offset of any body condition mentioning \it p across all rules in \it P , then \it v can be removed from \it G at evaluation time \it t\plus k_{p}\plus 1 . Since \it k_{p} can be derived offline from \it P , forgetting adds no overhead to online materialisation. Example 7 ( \it\mathsf{STGmat} ) . Consider the following program \it P , where \it rp , \it wn , \it vf , \it uvf , \it sf and \it tr abbreviate \it repair , \it warning , \it verified , \it unverified , \it safe and \it trusted . \it\displaystyle\begin{array}[]{l}vf(X,T)\leftarrow rp(X,T\minus 1).\end{array} ( \it r^{e}_{1} ) \it\displaystyle\begin{array}[]{l}uvf(X,T)\leftarrow wn(X,T\minus 1).\end{array} ( \it r^{e}_{2} ) \it\displaystyle\begin{array}[]{l}sf(X,T)\leftarrow vf(X,T),\text{not}\ uvf(X,T).\end{array} ( \it r^{i}_{1} ) \it\displaystyle\begin{array}[]{l}sf(X,T)\leftarrow sf(X,T\minus 1),\text{not}\ uvf(X,T).\end{array} ( \it r^{i}_{2} ) \it\displaystyle\begin{array}[]{l}tr(X,T)\leftarrow sf(X,T),sf(X,T\minus 1).\end{array} ( \it r^{i}_{3} ) \it r^{e}_{1} and \it r^{e}_{2} are extensional, while \it r^{i}_{1} , \it r^{i}_{2} and \it r^{i}_{3} are intensional. Strata \it B_{t}^{1}\val\{(rp,t),(wn,t),(vf,t),(uvf,t)\} and \it B_{t}^{2}\val\{(sf,t),(tr,t)\} incrementally stratify \it P . Figure 1 presents the STG constructed by Algorithm 4 over a stream with \it rp(a,1) and \it wn(a,3) up to \it t\val 4 . Starting from \it t\val 1 and \it B_{1}^{1} , we first construct nodes \it v_{1} and \it v_{2} for extensional rules \it r^{e}_{1} and \it r^{e}_{2} , defining \it vf and \it uvf . These rules cannot fire at \it t\val 1 , since their conditions refer to the previous time-point, and thus \it v_{1} and \it v_{2} are empty. Moving to the next stratum \it B_{1}^{2} , \it v_{1} is consistent with intensional rule \it r^{i}_{1} —defining \it sf —at time \it t\val 1 , leading to the creation of node \it v_{3} , with incoming p-edge \it v_{1}\rightarrow_{1}v_{3} and n-edge \it\{v_{2}\}\rightarrow_{2}v_{3} . No further nodes can be constructed for rules defining predicates in \it B_{1}^{2} , completing materialisation for \it t\val 1 . All conditions in \it P for predicates \it vf and \it uvf have temporal offset \it 0 , while \it sf appears in conditions with offset \it 1 . Thus, \it v_{1} and \it v_{2} are forgotten at the next time-step \it t\val 2 , while \it v_{3} is forgotten when \it t\val 3 . For \it t\val 2 and \it B_{2}^{1} , \it r^{e}_{1} fires because of fact \it rp(a,1) , populating node \it v_{4} with \it vf(a,2) . Since \it wn(a,1) is not in the input, \it uvf(a,2) cannot be proven, and thus an empty node \it v_{5} is constructed for \it r^{e}_{2} . Moving to \it B_{2}^{2} , there are two distinct proofs for \it sf(a,2) : the former requires \it vf(a,2) via \it r^{i}_{1} , while the latter requires \it sf(a,1) via \it r^{i}_{2} . Node \it v_{6} captures the former proof via p-edge \it v_{4}\rightarrow_{1}v_{6} , and is populated with \it sf(a,2) , while \it v_{7} captures the latter proof. Next, we construct nodes \it v_{8} and \it v_{9} , denoting the possible ways of proving \it tr(a,2) . These proofs, however, are not distinct—given a proof of \it sf(a,1) via \it r^{i}_{1} , reducing \it sf(a,2) via \it r^{i}_{1} yields a subquery of the query produced through its reduction with \it r^{i}_{2} —and thus \it v_{8} is removed before materialisation. At \it t\val 3 , \it sf(a,3) is derived via \it r^{i}_{2} , and \it tr(a,3) then follows from \it r^{i}_{3} . At \it t\val 4 , \it wn(a,3) leads to \it uvf(a,4) in node \it v_{19} . The n-edge from \it v_{19} then blocks the derivation of \it sf(a,4) via both \it r^{i}_{1} and \it r^{i}_{2} , capturing their negative condition. The full trace up to \it t\val 4 is shown in Figure 1 . Theorem 3 ( \it\mathsf{STGmat} Correctness) . Algorithm 4 builds an STG \it G for Temporal Datalog \it{}^{\rightarrow}_{\circleddash} program \it P and stream \it S . Proof Sketch. We show by induction on the strata in \it\boldsymbol{B_{t}} that \it P\cup S\models a(\boldsymbol{x},t) iff \it a(\boldsymbol{x},t)\in G(S) . Correctness for \it B_{1}^{1} follows from the correctness of \it\mathsf{TGmat} (Algorithm 1 ), because the predicates in \it B_{1}^{1} have no negative dependencies and their temporal dependencies are vacuous at \it t\val 1 . Assuming correctness up to \it B_{1}^{i\minus 1} , we prove it for \it B_{1}^{i} : every negated condition “ \it\text{not}\ b ” in a rule \it r defining a predicate \it a where \it(a,1)\in B^{i}_{1} refers to a lower stratum, so \it P\cup S\models b iff \it b\in G(S) by the inductive assumption, while lines 13 – 16 add n-edges from all nodes that may contain \it b , ensuring correct evaluation. Assuming correctness up to \it B_{t-1}^{n} , correctness for \it B_{t}^{1} follows from the consistency criterion (lines 17 – 19 ), which correctly materialises rules whose conditions all refer to previous strata. The same arguments support the step from \it B^{i\minus 1}_{t} to \it B^{i}_{t} , completing the proof. ∎ 6 Summary, Related and Further Work We proposed mappings from two prominent event specification languages (ESLs) for composite event recognition (CER), which are fragments of LARS and EC, to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} , i.e., a temporal Datalog with stratified negation and no future dependencies, and proved their correctness. We also introduced Streaming Trigger Graphs as an optimised evaluation mechanism for Temporal Datalog \it{}^{\rightarrow}_{\circleddash} . Numerous ESLs have been proposed for CER [ 18 , 5 , 1 ] . DatalogMTL, e.g., combines Datalog with quantitative temporal modalities in a streaming setting [ 59 ] . Contrary to LARS and EC, DatalogMTL is defined over a rational time model and uses intervals to quantify temporal offsets. DatalogMTL variants with forward-propagating rules [ 57 ] , negation [ 17 ] and an integer time model [ 56 ] have also been studied. We conjecture that such a restricted variant can be mapped to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} in a similar way to our plain LARS translation, due to similarities between their temporal operators. Another prominent CER system is CORE [ 26 , 12 ] . Contrary to Temporal Datalog \it{}^{\rightarrow}_{\circleddash} , CORE is restricted to unary predicates, and thus cannot express relational composite events. A LARS-to-ASP translation has been proposed for an incremental evaluation of LARS queries [ 9 ] , but introduces multiple time variables per rule, producing programs outside Temporal Datalog \it{}^{\rightarrow}_{\circleddash} . Furthermore, the stratification in [ 7 ] prohibits recursion via window operators in LARS, facilitating efficient reasoning. Our approach does not make this restriction. Our work is motivated by earlier attempts to avoid “blocking queries”, requiring arbitrary wait times on new streaming data [ 4 ] . Temporal Datalog \it{}^{\rightarrow}_{\circleddash} prohibits such queries by construction. Streamlog is also a temporal Datalog without blocking queries. Unlike our work, it allows equality constraints on time terms and reasons only over time-points appearing in the input [ 21 ] . Several large-scale Datalog engines exist [ 45 , 44 , 27 ] , and incremental evaluation has been studied for streaming data [ 13 ] , but these approaches do not model temporal modalities. Temporal Vadalog supports temporal knowledge graph reasoning over existential rules [ 10 ] . Such rules have also been studied in the context of LARS [ 55 ] , DatalogMTL [ 30 ] , and trigger graphs [ 47 ] , thus constituting a natural future extension for STGs. Our work assumes ordered input, as is common in stream reasoning [ 60 ] . Handling out-of-order streams [ 49 , 16 ] via temporal delays in reasoning [ 42 ] is left for future work. Finally, we aim to leverage a probabilistic extension of trigger graphs [ 48 ] to support CER under uncertainty [ 2 , 46 , 51 ] . AI Declaration No generative AI tools were used in the research, development, or writing of this paper.