1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! The conditions module holds the structures for composing a low level rule definition

/// A ConditionField describes when a Fact element matches
///
/// The simplest match is a constant match. Constants are checked in the alphamemories, and
/// must match exactly against the field. They are used as the most basic rules, and will be
/// checked before any further processing occurs.
///
/// Variable fields are more complex. They are used to compare between different facts. A Variable
/// on a single fact does nothing. Only when it is compared against another variable does it
/// affect the matching. Variables are used to construct the more complex rules.
///
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ConditionField {
    /// A Const must match exactly.
    Const(String),
    /// A Variable is named, and matches anything. It will be compared against an other Variable.
    Variable(String),
    /// This field is irrelevant for the matching rules.
    Any,
}

/// The ConditionBody defines what fields in a fact are relevant to the matching, and in which way.
///
/// When a fact enters the network, we will consider each field of the fact. The fields that need
/// to match a Const expression are evaluated first. Then the fields that need to match other
/// fields, in the same or in an other fact, are compared against those fields.
///
/// This struct defines how a single fact is of interest to the network.
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct ConditionBody {
    pub class: String,
    pub f1: ConditionField,
    pub f2: ConditionField,
    pub f3: ConditionField,
}

/// The NccCondition checks a child-rule, and inverts the results of that match.
///
/// The NccCondition is used to define a chain of conditions to match, and inverts the outcome of
/// that match. This way, we can recursively define rules inside a rule.
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct NccCondition {
    pub children: Vec<Condition>
}

/// A Condition defines a single rule in a chain of rules that need to be fullfilled to find a match.
///
/// A Condition can be:
///   - Some fact must occur
///   - Some fact must not occur
///   - A certain combination of Conditions must not occur
///
/// The Ncc condition is a negated recursive rule. It defines a combination of facts that are
/// considered a match, and then negates the result.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum Condition {
    /// A certain fact must occur, which can be compared against consts and previous matches.
    Positive(ConditionBody),
    /// A certain fact must not occur, which can be compared against consts and previous matches.
    Negative(ConditionBody),
    /// A certain combination of facts must not occur.
    Ncc(NccCondition),
}

/// The ConditionBuilder is a helper to create Conditions.
///
/// # Examples
/// We create a rule for the presence of a thing that is green and not small, and the abscence
/// of an other thing, which is blue and large. Both condition must apply before this rule matches.
/// ```
/// # use phreak_engine::condition::*;
///
/// let mut conditions: Vec<Condition> = Vec::new();
/// let mut subconditions: Vec<Condition> = Vec::new();
///
/// let condition =
///     ConditionBuilder::new(String::from("car"))
///         // default is positive()
///         .variable_field1(String::from("a"))
///         .const_field2(String::from("is"))
///         .const_field3(String::from("green"))
///         .build();
/// conditions.push(condition);
///
/// let condition =
///     ConditionBuilder::new(String::from("car"))
///         .negative(String::from("car"))
///         .variable_field1(String::from("a"))
///         .const_field2(String::from("is"))
///         .const_field3(String::from("small"))
///         .build();
/// conditions.push(condition);
///
/// let subcondition =
///     ConditionBuilder::new(String::from("car"))
///         .variable_field1(String::from("b"))
///         .const_field2(String::from("is"))
///         .const_field3(String::from("blue"))
///         .build();
/// subconditions.push(subcondition);
///
/// let subcondition =
///     ConditionBuilder::new(String::from("car"))
///         .variable_field1(String::from("b"))
///         .const_field2(String::from("is"))
///         .const_field3(String::from("large"))
///         .build();
/// subconditions.push(subcondition);
///
/// let condition =
///     ConditionBuilder::new(String::from("car"))
///         .ncc(subconditions)
///         .build();
/// conditions.push(condition);
/// ```
pub struct ConditionBuilder {
    data: Condition,
}

impl ConditionBuilder {
    #[inline]
    pub fn new(class: String) -> ConditionBuilder {
        ConditionBuilder {
            data: Condition::Positive(
                ConditionBody {
                    class,
                    f1: ConditionField::Any,
                    f2: ConditionField::Any,
                    f3: ConditionField::Any,
                }
            ),
        }
    }

    #[inline]
    pub fn positive(mut self, class: String) -> ConditionBuilder {
        self.data = Condition::Positive(
            ConditionBody {
                class,
                f1: ConditionField::Any,
                f2: ConditionField::Any,
                f3: ConditionField::Any,
            }
        );
        self
    }

    #[inline]
    pub fn negative(mut self, class: String) -> ConditionBuilder {
        self.data = Condition::Negative(
            ConditionBody {
                class,
                f1: ConditionField::Any,
                f2: ConditionField::Any,
                f3: ConditionField::Any,
            }
        );
        self
    }

    #[inline]
    pub fn ncc(mut self, tests: Vec<Condition>) -> ConditionBuilder {
        self.data = Condition::Ncc(
            NccCondition {
                children: tests
            }
        );
        self
    }

    #[inline]
    pub fn field1(mut self, a_field: ConditionField) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f1 = a_field;
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn field2(mut self, a_field: ConditionField) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f2 = a_field;
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }
        self
    }

    #[inline]
    pub fn field3(mut self, a_field: ConditionField) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f3 = a_field;
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn const_field1(mut self, a_field: String) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f1 = ConditionField::Const(a_field);
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn const_field2(mut self, a_field: String) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f2 = ConditionField::Const(a_field);
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn const_field3(mut self, a_field: String) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f3 = ConditionField::Const(a_field);
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn variable_field1(mut self, a_field: String) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f1 = ConditionField::Variable(a_field);
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn variable_field2(mut self, a_field: String) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f2 = ConditionField::Variable(a_field);
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn variable_field3(mut self, a_field: String) -> ConditionBuilder {
        match &mut self.data {
            Condition::Positive(cond) |
            Condition::Negative(cond) => {
                cond.f3 = ConditionField::Variable(a_field);
            }
            Condition::Ncc(_) => { unimplemented!("impossible") }
        }

        self
    }

    #[inline]
    pub fn build(self) -> Condition {
        self.data
    }
}