Remove dead code.
[whenjobs.git] / lib / whenexpr.mli
1 (* whenjobs
2  * Copyright (C) 2012 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 (** When- and every-expression definition and evaluation, variables
20     and jobs. *)
21
22 type whenexpr =
23   | Expr_unit                           (** Unit constant. *)
24   | Expr_bool of bool                   (** A boolean constant. *)
25   | Expr_str of string                  (** A string constant. *)
26   | Expr_int of Big_int.big_int         (** An integer constant. *)
27   | Expr_float of float                 (** A float constant. *)
28   | Expr_var of string                  (** A variable name. *)
29   | Expr_and of whenexpr * whenexpr     (** && *)
30   | Expr_or of whenexpr * whenexpr      (** || *)
31   | Expr_lt of whenexpr * whenexpr      (** < *)
32   | Expr_le of whenexpr * whenexpr      (** <= *)
33   | Expr_eq of whenexpr * whenexpr      (** == *)
34   | Expr_ge of whenexpr * whenexpr      (** >= *)
35   | Expr_gt of whenexpr * whenexpr      (** > *)
36   | Expr_not of whenexpr                (** boolean not *)
37   | Expr_add of whenexpr * whenexpr     (** arithmetic addition or string cat *)
38   | Expr_sub of whenexpr * whenexpr     (** arithmetic subtraction *)
39   | Expr_mul of whenexpr * whenexpr     (** arithmetic multiplication *)
40   | Expr_div of whenexpr * whenexpr     (** arithmetic division *)
41   | Expr_mod of whenexpr * whenexpr     (** arithmetic modulo *)
42   | Expr_len of whenexpr                (** length *)
43   | Expr_changes of string              (** changes var *)
44   | Expr_increases of string            (** increases var *)
45   | Expr_decreases of string            (** decreases var *)
46   | Expr_prev of string                 (** prev var *)
47   | Expr_reloaded                       (** reloaded () *)
48 (** Internal type used to represent 'when' expressions. *)
49
50 type periodexpr =
51   | Every_seconds of int
52   | Every_days of int
53   | Every_months of int
54   | Every_years of int
55 (** Internal type used to represent 'every' expressions. *)
56
57 type shell_script = {
58   sh_loc : Camlp4.PreCast.Loc.t;
59   sh_script : string;
60 }
61 (** A shell script. *)
62
63 type variable =
64   | T_unit
65   | T_bool of bool
66   | T_string of string
67   | T_int of Big_int.big_int
68   | T_float of float
69 (** Typed variable (see also [whenproto.x]) *)
70
71 val string_of_variable : variable -> string
72
73 val variable_of_rpc : Whenproto_aux.variable -> variable
74 val rpc_of_variable : variable -> Whenproto_aux.variable
75
76 type variables = variable Whenutils.StringMap.t
77 (** A set of variables. *)
78
79 type job_cond =
80   | When_job of whenexpr                (** when ... : << >> *)
81   | Every_job of periodexpr             (** every ... : << >> *)
82
83 type job = {
84   job_loc : Camlp4.PreCast.Loc.t;
85   job_name : string;
86   job_cond : job_cond;
87   job_script : shell_script;
88 }
89 (** A job. *)
90
91 val expr_of_ast : Camlp4.PreCast.Ast.Loc.t -> Camlp4.PreCast.Ast.expr -> whenexpr
92 (** Convert OCaml AST to an expression.  Since OCaml ASTs are much
93     more general than the expressions we can use, this can raise
94     [Invalid_argument] in many different situations. *)
95
96 val string_of_whenexpr : whenexpr -> string
97 (** Pretty-print an expression to a string. *)
98
99 val string_of_periodexpr : periodexpr -> string
100 (** Pretty-print a period expression to a string. *)
101
102 val dependencies_of_whenexpr : whenexpr -> string list
103 (** Return list of variables that an expression depends on.  This is
104     used to work out when an expression needs to be reevaluated. *)
105
106 val dependencies_of_job : job -> string list
107 (** Which variables does this job depend on? *)
108
109 val eval_whenexpr : variables -> variables option -> bool -> whenexpr -> variable
110 val eval_whenexpr_as_bool : variables -> variables option -> bool -> whenexpr -> bool
111 (** [eval_whenexpr variables prev_variables onload expr] is the
112     evaluation function for when expressions.  It full evaluates
113     [expr], returning its typed value.  It can also throw exceptions
114     (at least [Invalid_argument] and [Failure]).
115
116     [eval_whenexpr_as_bool] is the same but it forces the returned
117     value to be a boolean.
118
119     The other parameters represent the current and past state:
120
121     [variables] is the current set of variables and their values.
122
123     [prev_variables] is the set of variables from the previous
124     run.  It is used to implement {i prev}, {i changes} etc operators.
125     This can be [None], meaning there is no previous state.
126
127     [onload] is used to implement the {i reloaded} operator.  It is
128     true if the file is being reloaded, and false otherwise. *)
129
130 val next_periodexpr : float -> periodexpr -> float
131 (** [next_periodexpr t period] returns the earliest event of [period]
132     strictly after time [t].
133
134     Visualising periods as repeated events on a timeline, this
135     returns [t']:
136
137     {v
138     events:  ---+---------+---------+---------+---------+---------+-----
139     times:          t     t'
140     }
141
142     Note that [periodexpr] events are not necessarily regular.
143     eg. The start of a month is not a fixed number of seconds
144     after the start of the previous month.  'Epoch' refers
145     to the Unix Epoch (ie. 1970-01-01 00:00:00 UTC).
146
147     If [period = Every_seconds i] then events are when
148     [t' mod i == 0] when t' is the number of seconds since
149     the Epoch.  This returns the next t' > t.
150
151     If [period = Every_days i] then events happen at
152     midnight UTC every [i] days since the Epoch.
153     This returns the next midnight > t.
154
155     If [period = Every_months i] then events happen at
156     midnight UTC on the 1st day of the month every [i] months
157     since the Epoch.  This returns midnight on the
158     1st day of the next month > t.
159
160     If [period = Every_years i] then events happen at
161     midnight UTC on the 1st day of the year when
162     [(y - 1970) mod i == 0].  This returns midnight on the
163     1st day of the next year > t. *)
164
165 val check_valid_variable_name : string -> unit
166 (** Check that [name] is a valid variable name that users are permitted
167     to set, and raise [Failure] if it is not.  *)