Skip to main content

riddle/
core.rs

1use crate::{
2    RiddleError,
3    env::{Atom, AtomId, BoolExpr, CommonEnv, Env, Object, ObjectId, Slot},
4    language::{Disjunction, execute},
5    parse_problem,
6    scope::{BoolType, Class, CommonScope, Field, Function, IntType, Predicate, RealType, Scope, StringType, Type},
7};
8use std::{
9    cell::RefCell,
10    collections::HashMap,
11    rc::{Rc, Weak},
12};
13
14pub trait Core: Scope + Env {
15    fn new_bool(&self, value: bool) -> Slot;
16    fn new_bool_var(&self) -> Slot;
17    fn new_int(&self, value: i64) -> Slot;
18    fn new_int_var(&self) -> Slot;
19    fn new_real(&self, num: i64, den: i64) -> Slot;
20    fn new_real_var(&self) -> Slot;
21    fn new_string(&self, value: &str) -> Slot;
22    fn new_string_var(&self) -> Slot;
23
24    fn sum(&self, sum: &[Slot]) -> Result<Slot, RiddleError>;
25    fn opposite(&self, term: Slot) -> Result<Slot, RiddleError>;
26    fn mul(&self, mul: &[Slot]) -> Result<Slot, RiddleError>;
27    fn div(&self, left: Slot, right: Slot) -> Result<Slot, RiddleError>;
28
29    fn assert(&self, term: Rc<BoolExpr>) -> bool;
30    fn new_var(&self, tp: Rc<dyn Class>, instances: &[ObjectId]) -> Result<Slot, RiddleError>;
31    fn new_disjunction(&self, disjunction: Disjunction);
32
33    fn new_object(&self, class: Rc<dyn Class>) -> ObjectId;
34    fn get_object(&self, id: ObjectId) -> Option<Rc<Object>>;
35    fn new_atom(&self, predicate: Rc<Predicate>, fact: bool, args: HashMap<String, Slot>) -> AtomId;
36    fn get_atom(&self, id: AtomId) -> Option<Rc<Atom>>;
37
38    fn bool_type(&self) -> Rc<BoolType> {
39        self.get_type("bool").expect("Core should have bool type").as_any().downcast::<BoolType>().expect("Core bool type should be BoolType")
40    }
41
42    fn int_type(&self) -> Rc<IntType> {
43        self.get_type("int").expect("Core should have int type").as_any().downcast::<IntType>().expect("Core int type should be IntType")
44    }
45
46    fn real_type(&self) -> Rc<RealType> {
47        self.get_type("real").expect("Core should have real type").as_any().downcast::<RealType>().expect("Core real type should be RealType")
48    }
49
50    fn string_type(&self) -> Rc<StringType> {
51        self.get_type("string").expect("Core should have string type").as_any().downcast::<StringType>().expect("Core string type should be StringType")
52    }
53}
54
55pub struct CommonCore {
56    scope: Rc<CommonScope>,
57    env: Rc<CommonEnv>,
58    objects: RefCell<Vec<Rc<Object>>>,
59    atoms: RefCell<Vec<Rc<Atom>>>,
60}
61
62impl CommonCore {
63    pub fn new(core: Weak<dyn Core>) -> Rc<Self> {
64        let c_core = Rc::new(CommonCore {
65            scope: Rc::new(CommonScope::new(core.clone(), None)),
66            env: Rc::new(CommonEnv::new(None)),
67            objects: RefCell::new(Vec::new()),
68            atoms: RefCell::new(Vec::new()),
69        });
70        c_core.add_type(Rc::new(BoolType::new(core.clone())));
71        c_core.add_type(Rc::new(IntType::new(core.clone())));
72        c_core.add_type(Rc::new(RealType::new(core.clone())));
73        c_core.add_type(Rc::new(StringType::new(core.clone())));
74        c_core
75    }
76
77    /// Parses and executes a RiDDLe problem in this core context.
78    ///
79    /// The parsed problem metadata is registered in the current scope,
80    /// then each statement is executed in order using this core scope
81    /// and environment.
82    ///
83    /// Returns an error if parsing or execution fails.
84    pub fn read(&self, riddle: &str) -> Result<(), RiddleError> {
85        let mut problem = parse_problem(riddle)?;
86        let statments = std::mem::take(&mut problem.statements);
87        self.scope.clone().add_problem(problem);
88        let scope: Rc<dyn Scope> = self.scope.clone();
89        for stmt in statments {
90            execute(&scope, self.env.clone(), &stmt)?;
91        }
92        Ok(())
93    }
94
95    /// Registers a type in the core type table under its declared name.
96    pub fn add_type(&self, tp: Rc<dyn Type>) {
97        self.scope.types.borrow_mut().insert(tp.name().to_string(), tp);
98    }
99
100    pub fn get_objects(&self) -> Vec<Rc<Object>> {
101        self.objects.borrow().clone()
102    }
103
104    pub fn get_object(&self, id: ObjectId) -> Option<Rc<Object>> {
105        self.objects.borrow().get(*id).cloned()
106    }
107
108    pub fn new_object(&self, class: Rc<dyn Class>) -> ObjectId {
109        let id = ObjectId(self.objects.borrow().len());
110        self.objects.borrow_mut().push(Rc::new(Object::new(id, class)));
111        id
112    }
113
114    pub fn get_atoms(&self) -> Vec<Rc<Atom>> {
115        self.atoms.borrow().clone()
116    }
117
118    pub fn get_atom(&self, id: AtomId) -> Option<Rc<Atom>> {
119        self.atoms.borrow().get(*id).cloned()
120    }
121
122    pub fn new_atom(&self, predicate: Rc<Predicate>, fact: bool, args: HashMap<String, Slot>) -> AtomId {
123        let id = AtomId(self.atoms.borrow().len());
124        self.atoms.borrow_mut().push(Rc::new(Atom::new(id, predicate, fact, args)));
125        id
126    }
127}
128
129impl Scope for CommonCore {
130    fn core(&self) -> Rc<dyn Core> {
131        self.scope.clone().core()
132    }
133
134    fn scope(&self) -> Option<Rc<dyn Scope>> {
135        None
136    }
137
138    fn get_fields(&self) -> Vec<Rc<Field>> {
139        self.scope.get_fields()
140    }
141
142    fn get_field(&self, name: &str) -> Option<Rc<Field>> {
143        self.scope.get_field(name)
144    }
145
146    fn get_function(&self, name: &str, types: &[Rc<dyn Type>]) -> Option<Rc<Function>> {
147        self.scope.get_function(name, types)
148    }
149
150    fn get_type(&self, name: &str) -> Option<Rc<dyn Type>> {
151        self.scope.get_type(name)
152    }
153
154    fn get_predicate(&self, name: &str) -> Option<Rc<Predicate>> {
155        self.scope.get_predicate(name)
156    }
157}
158
159impl Env for CommonCore {
160    fn parent(&self) -> Option<Rc<dyn Env>> {
161        None
162    }
163
164    fn get(&self, name: &str) -> Option<Slot> {
165        self.env.get(name)
166    }
167
168    fn set(&self, name: String, value: Slot) {
169        self.env.set(name, value);
170    }
171}
172
173#[cfg(test)]
174mod tests {
175    use super::*;
176    use crate::{env::Var, scope::arith_type};
177    use std::any::Any;
178
179    struct TestObject {
180        tp: Weak<dyn Type>,
181    }
182
183    impl TestObject {
184        fn new(var_type: Rc<dyn Type>) -> Self {
185            Self { tp: Rc::downgrade(&var_type) }
186        }
187    }
188
189    impl Var for TestObject {
190        fn var_type(&self) -> Rc<dyn Type> {
191            self.tp.upgrade().expect("Type should still exist")
192        }
193
194        fn as_any(self: Rc<Self>) -> Rc<dyn Any> {
195            self
196        }
197    }
198
199    struct TestCore {
200        core: Rc<CommonCore>,
201    }
202
203    impl TestCore {
204        fn new() -> Rc<Self> {
205            Rc::new_cyclic(|core| Self {
206                core: {
207                    let core: Weak<TestCore> = core.clone();
208                    CommonCore::new(core)
209                },
210            })
211        }
212
213        fn read(&self, riddle: &str) -> Result<(), RiddleError> {
214            self.core.read(riddle)
215        }
216    }
217
218    impl Core for TestCore {
219        fn new_bool(&self, _value: bool) -> Slot {
220            Slot::Primitive(Rc::new(TestObject::new(self.bool_type())))
221        }
222        fn new_bool_var(&self) -> Slot {
223            Slot::Primitive(Rc::new(TestObject::new(self.bool_type())))
224        }
225        fn new_int(&self, _value: i64) -> Slot {
226            Slot::Primitive(Rc::new(TestObject::new(self.int_type())))
227        }
228        fn new_int_var(&self) -> Slot {
229            Slot::Primitive(Rc::new(TestObject::new(self.int_type())))
230        }
231        fn new_real(&self, _num: i64, _den: i64) -> Slot {
232            Slot::Primitive(Rc::new(TestObject::new(self.real_type())))
233        }
234        fn new_real_var(&self) -> Slot {
235            Slot::Primitive(Rc::new(TestObject::new(self.real_type())))
236        }
237        fn new_string(&self, _value: &str) -> Slot {
238            Slot::Primitive(Rc::new(TestObject::new(self.string_type())))
239        }
240        fn new_string_var(&self) -> Slot {
241            Slot::Primitive(Rc::new(TestObject::new(self.string_type())))
242        }
243
244        fn sum(&self, sum: &[Slot]) -> Result<Slot, RiddleError> {
245            let tp = arith_type(self, sum)?;
246            Ok(Slot::Primitive(Rc::new(TestObject::new(tp))))
247        }
248        fn opposite(&self, term: Slot) -> Result<Slot, RiddleError> {
249            let tp = match term {
250                Slot::Primitive(var) => var.var_type(),
251                Slot::ObjectRef(id) => self.get_object(id).expect("Object should exist").class(),
252                Slot::AtomRef(id) => self.get_atom(id).expect("Atom should exist").predicate(),
253            };
254            Ok(Slot::Primitive(Rc::new(TestObject::new(tp))))
255        }
256        fn mul(&self, mul: &[Slot]) -> Result<Slot, RiddleError> {
257            let tp = arith_type(self, mul)?;
258            Ok(Slot::Primitive(Rc::new(TestObject::new(tp))))
259        }
260        fn div(&self, left: Slot, right: Slot) -> Result<Slot, RiddleError> {
261            let tp = arith_type(self, &[left, right])?;
262            Ok(Slot::Primitive(Rc::new(TestObject::new(tp))))
263        }
264
265        fn assert(&self, _term: Rc<BoolExpr>) -> bool {
266            true
267        }
268
269        fn new_var(&self, class: Rc<dyn Class>, instances: &[ObjectId]) -> Result<Slot, RiddleError> {
270            if instances.is_empty() {
271                return Err(RiddleError::InconsistencyError("Cannot create variable with no instances".into()));
272            }
273            Ok(Slot::Primitive(Rc::new(TestObject::new(class))))
274        }
275        fn new_disjunction(&self, _disjunction: Disjunction) {}
276
277        fn new_object(&self, class: Rc<dyn Class>) -> ObjectId {
278            self.core.new_object(class)
279        }
280        fn get_object(&self, id: ObjectId) -> Option<Rc<Object>> {
281            self.core.get_object(id)
282        }
283        fn new_atom(&self, predicate: Rc<Predicate>, fact: bool, args: HashMap<String, Slot>) -> AtomId {
284            self.core.new_atom(predicate, fact, args)
285        }
286        fn get_atom(&self, id: AtomId) -> Option<Rc<Atom>> {
287            self.core.get_atom(id)
288        }
289    }
290
291    impl Scope for TestCore {
292        fn core(&self) -> Rc<dyn Core> {
293            panic!("Core should not call scope core function")
294        }
295
296        fn scope(&self) -> Option<Rc<dyn Scope>> {
297            None
298        }
299
300        fn get_fields(&self) -> Vec<Rc<Field>> {
301            self.core.get_fields()
302        }
303
304        fn get_field(&self, _name: &str) -> Option<Rc<Field>> {
305            self.core.get_field(_name)
306        }
307
308        fn get_function(&self, name: &str, types: &[Rc<dyn Type>]) -> Option<Rc<Function>> {
309            self.core.get_function(name, types)
310        }
311
312        fn get_type(&self, name: &str) -> Option<Rc<dyn Type>> {
313            self.core.get_type(name)
314        }
315
316        fn get_predicate(&self, name: &str) -> Option<Rc<Predicate>> {
317            self.core.get_predicate(name)
318        }
319    }
320
321    impl Env for TestCore {
322        fn parent(&self) -> Option<Rc<dyn Env>> {
323            None
324        }
325
326        fn get(&self, name: &str) -> Option<Slot> {
327            self.core.get(name)
328        }
329
330        fn set(&self, name: String, value: Slot) {
331            self.core.set(name, value);
332        }
333    }
334
335    #[test]
336    fn create_core() {
337        let core = TestCore::new();
338        assert!(core.get_type("bool").is_some());
339        assert!(core.get_type("int").is_some());
340        assert!(core.get_type("real").is_some());
341        assert!(core.get_type("string").is_some());
342    }
343
344    #[test]
345    fn read_problem() {
346        let core = TestCore::new();
347        core.read("bool a, b, c; (a & b) | c;").expect("Failed to read problem with boolean variables and expression");
348    }
349}