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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Utilities for creating rewrite rules for a [`System`].

use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

use rand::{Rng, thread_rng};

use crate::{DisplaySystem, Result};
use crate::error::{Error, ErrorKind};
use crate::prelude::*;
use crate::Token;

#[derive(Debug, Copy, Clone)]
pub enum ChanceKind {
    /// This chance value was set by the user
    Set,
    /// This chance value was derived based on the value
    /// that was left over after considering all the [`ChanceKind::Set`] chance values.
    Derived
}

#[derive(Debug, Copy, Clone)]
pub struct Chance {
    kind: ChanceKind,
    chance: Option<f32>
}

impl Chance {
    /// Creates a new [`ChanceKind::Set`] chance value.
    pub fn new(chance: f32) -> Self {
        assert!(chance > 0_f32, "chance should be positive");
        assert!(chance <= 1.0_f32, "chance should be less than or equal to 1.0");

        Chance {
            kind: ChanceKind::Set,
            chance: Some(chance)
        }
    }

    /// Returns an unset chance object that is meant to be automatically
    /// determined by the system.
    #[inline]
    pub fn empty() -> Self {
        Chance {
            kind: ChanceKind::Derived,
            chance: None
        }
    }

    /// Returns true iff this is of kind [`ChanceKind::Derived`]
    #[inline]
    pub fn is_derived(&self) -> bool {
        matches!(self.kind, ChanceKind::Derived)
    }

    /// Returns true iff this is of kind [`ChanceKind::Set`]
    #[inline]
    pub fn is_user_set(&self) -> bool {
        matches!(self.kind, ChanceKind::Set)
    }

    #[inline]
    pub fn expect(&self, message: &str) -> f32 {
        self.chance.expect(message)
    }

    #[inline]
    pub fn unwrap(&self) -> f32 {
        self.chance.unwrap()
    }

    #[inline]
    pub fn unwrap_or(&self, default: f32) -> f32 {
        self.chance.unwrap_or(default)
    }
}


#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ProductionHead {
    target: Token
}

impl ProductionHead {
    /// Create a new production head.
    ///
    /// This will return [`Err`] if the given token is not a [`crate::tokens::TokenKind::Production`]
    pub fn build(target: Token) -> Result<Self> {
        if !target.is_production() {
            return Err(Error::general("token should be a Production"));
        }

        Ok(ProductionHead {
            target
        })
    }

    /// Returns the token that this production is a target of.
    #[inline]
    pub fn target(&self) -> Token {
        self.target
    }

    /// Returns true iff this matches the given
    /// string's index position of the string.
    pub fn matches(&self, string: &ProductionString, index: usize) -> bool {
        string.tokens()
            .get(index)
            .map(|token| self.target == *token)
            .unwrap_or(false)
    }
}

impl DisplaySystem for ProductionHead {
    fn format(&self, names: &HashMap<Token, String>) -> Result<String> {
        names.get(&self.target)
            .cloned()
            .ok_or_else(|| Error::general(format!("No name for token {}", self.target)))
    }
}


#[derive(Debug, Clone)]
pub struct ProductionBody {
    string: ProductionString,
    chance: Chance
}

impl ProductionBody {
    /// Creates a new production body from the given
    /// [`ProductionString`].
    pub fn new(string: ProductionString) -> Self {
        ProductionBody {
            string,
            chance: Chance::empty()
        }
    }

    /// Creates a new production body from the given
    /// [`ProductionString`] that can occur with the given chance.
    pub fn try_with_chance(chance: f32, string: ProductionString) -> Result<Self> {
        if !(0.0..=1.0).contains(&chance) {
            return Err(Error::new(ErrorKind::Parse, "chance should be between 0.0 and 1.0 inclusive"));
        }

        Ok(ProductionBody {
            string,
            chance: Chance::new(chance),
        })
    }

    /// Create a production body that is just the empty string
    pub fn empty() -> Self {
        ProductionBody {
            string: ProductionString::empty(),
            chance: Chance::empty()
        }
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.string.is_empty()
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.string.len()
    }
    
    #[inline]
    pub fn string(&self) -> &ProductionString {
        &self.string
    }

    #[inline]
    pub fn chance(&self) -> &Chance {
        &self.chance
    }
}

impl DisplaySystem for ProductionBody {
    fn format(&self, names: &HashMap<Token, String>) -> Result<String> {
        let body = self.string.format(names)?;
        if self.chance.is_user_set() {
            return Ok(format!("{} {body}", self.chance.unwrap()));
        }

        Ok(body)
    }
}



/// Represents production rules in an L-System.
///
/// These are rules
/// that may be represented in the form `A -> B`, where
/// A (called here the [`ProductionHead`]) is the token
/// that will be matched again, and the tokens after
/// the arrow (in this case the `B`, called here the [`ProductionBody`] is what
/// the tokens matching the head in the input string / axiom will be replaced with.
///
/// See:
/// * [`Production::head`]
/// * [`Production::body`]
/// * [`System::parse_production`]
#[derive(Debug, Clone)]
pub struct Production {
    head: ProductionHead,
    body: Vec<ProductionBody>
}

impl Production {
    pub fn new(head: ProductionHead, body: ProductionBody) -> Self {
        Production {
            head,
            body: vec![body]
        }
    }

    #[inline]
    pub fn head(&self) -> &ProductionHead {
        &self.head
    }

    #[inline]
    pub fn body(&self) -> Result<&ProductionBody> {
        if self.body.is_empty() {
            return Err(Error::execution("Production has no bodies set"))
        }

        // Return the only instance. Chance does not matter here.
        if self.body.len() == 1 {
            return Ok(self.body.last().unwrap());
        }

        let total_chance : f32 = self.body.iter()
            .map(|b| b.chance.unwrap_or(0.0))
            .sum();

        if total_chance < 0.0 {
            return Err(Error::execution("chance should never be negative"));
        }

        if total_chance > 1.0 {
            return Err(Error::execution("total chance of production bodies should not be greater than 1.0"));
        }

        let remaining = self.body.iter().filter(|b| b.chance.is_derived()).count();
        let default_chance = if remaining == 0 {
            0_f32
        } else {
            (1.0_f32 - total_chance) / (remaining as f32)
        };

        let mut current = 0_f32;
        let random : f32 = thread_rng().gen_range(0.0..=1.0);

        for body in &self.body {
            current += body.chance.unwrap_or(default_chance);
            if random < current {
                return Ok(body);
            }
        }

        // All remaining chance given to last body.
        return Ok(self.body.last().unwrap());
    }

    /// Returns true iff this production's [`Production::head`] matches the given
    /// string's index position of the string.
    #[inline]
    pub fn matches(&self, string: &ProductionString, index: usize) -> bool {
        self.head().matches(string, index)
    }

    pub fn add_body(&mut self, body: ProductionBody) {
        self.body.push(body);
    }

    /// Adds all of the body elements from `other` into `self`.
    pub fn merge(&mut self, other: Self) {
        other.body.into_iter().for_each(|b| self.add_body(b));
    }
}

impl PartialEq for Production {
    fn eq(&self, other: &Self) -> bool {
        self.head().eq(other.head())
    }
}

impl Eq for Production { }

impl Hash for Production {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.head.hash(state);
    }
}

impl DisplaySystem for Production {
    fn format(&self, names: &HashMap<Token, String>) -> Result<String> {
        let head = self.head.format(names)?;
        let align_size = head.len() + 4;

        let mut output = String::new();
        output.push_str(&head);
        output.push_str(" -> ");

        let mut first = true;
        for body in &self.body {
            let tmp = body.format(names)?;

            if first {
                output.push_str(&tmp);
                first = false;
            } else {
                output.push('\n');
                output.push_str(" ".repeat(align_size).as_str());
                output.push_str(&tmp);
            }
        }

        Ok(output)
    }
}


/// A trait for collections that accept and store productions.
///
/// Design note: this does not use `&mut self` to allow greater
/// flexibility with sharing [`System`] and other implementers of this
/// trait across threads.
pub trait ProductionStore {
    fn add_production(&self, production: Production) -> Result<Production>;
}

impl ProductionStore for RefCell<Vec<Production>> {
    fn add_production(&self, production: Production) -> Result<Production> {
        let mut vec = self.borrow_mut();
        vec.push(production);
        vec.last().cloned().ok_or_else(|| Error::general("Unable to add production"))
    }
}