Skip to main content

mln_expression/
boolean_operator.rs

1// Ports include/mln/style/expression/boolean_operator.hpp and
2// src/mln/style/expression/boolean_operator.cpp.
3
4use crate::expression::{
5    EvaluationContext, EvaluationResult, Expression, ExpressionBase, Kind, children_equal,
6    collect_dependencies, downcast,
7};
8use crate::parsing_context::{ParseResult, ParsingContext};
9use crate::type_::Type;
10use crate::value::Value;
11use mln_conversion::Convertible;
12
13fn parse_boolean_op(value: &Convertible<'_>, ctx: &mut ParsingContext) -> Option<Vec<Box<dyn Expression>>> {
14    debug_assert!(value.is_array());
15    let length = value.array_length();
16
17    let mut parsed_inputs = Vec::with_capacity(length - 1);
18    for i in 1..length {
19        let parsed = ctx.parse(&value.array_member(i), i, Some(Type::Boolean), None)?;
20        parsed_inputs.push(parsed);
21    }
22    Some(parsed_inputs)
23}
24
25pub struct Any {
26    base: ExpressionBase,
27    inputs: Vec<Box<dyn Expression>>,
28}
29
30impl Any {
31    pub fn new(inputs: Vec<Box<dyn Expression>>) -> Self {
32        Any {
33            base: ExpressionBase::new(Kind::Any, Type::Boolean, collect_dependencies(&inputs)),
34            inputs,
35        }
36    }
37
38    pub fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
39        Some(Box::new(Any::new(parse_boolean_op(value, ctx)?)))
40    }
41}
42
43impl Expression for Any {
44    fn base(&self) -> &ExpressionBase {
45        &self.base
46    }
47
48    fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
49        for input in &self.inputs {
50            let result = input.evaluate(params)?;
51            if result.as_bool().expect("boolean input") {
52                return Ok(Value::Bool(true));
53            }
54        }
55        Ok(Value::Bool(false))
56    }
57
58    fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a dyn Expression)) {
59        for input in &self.inputs {
60            visit(&**input);
61        }
62    }
63
64    fn equals(&self, e: &dyn Expression) -> bool {
65        if e.get_kind() == Kind::Any {
66            let rhs = downcast::<Any>(e).expect("Any");
67            return children_equal(&self.inputs, &rhs.inputs);
68        }
69        false
70    }
71
72    fn possible_outputs(&self) -> Vec<Option<Value>> {
73        vec![Some(Value::Bool(true)), Some(Value::Bool(false))]
74    }
75
76    fn get_operator(&self) -> String {
77        "any".to_owned()
78    }
79}
80
81pub struct All {
82    base: ExpressionBase,
83    inputs: Vec<Box<dyn Expression>>,
84}
85
86impl All {
87    pub fn new(inputs: Vec<Box<dyn Expression>>) -> Self {
88        All {
89            base: ExpressionBase::new(Kind::All, Type::Boolean, collect_dependencies(&inputs)),
90            inputs,
91        }
92    }
93
94    pub fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
95        Some(Box::new(All::new(parse_boolean_op(value, ctx)?)))
96    }
97}
98
99impl Expression for All {
100    fn base(&self) -> &ExpressionBase {
101        &self.base
102    }
103
104    fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
105        for input in &self.inputs {
106            let result = input.evaluate(params)?;
107            if !result.as_bool().expect("boolean input") {
108                return Ok(Value::Bool(false));
109            }
110        }
111        Ok(Value::Bool(true))
112    }
113
114    fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a dyn Expression)) {
115        for input in &self.inputs {
116            visit(&**input);
117        }
118    }
119
120    fn equals(&self, e: &dyn Expression) -> bool {
121        if e.get_kind() == Kind::All {
122            let rhs = downcast::<All>(e).expect("All");
123            return children_equal(&self.inputs, &rhs.inputs);
124        }
125        false
126    }
127
128    fn possible_outputs(&self) -> Vec<Option<Value>> {
129        vec![Some(Value::Bool(true)), Some(Value::Bool(false))]
130    }
131
132    fn get_operator(&self) -> String {
133        "all".to_owned()
134    }
135}