22
33pub mod index;
44
5- use crate :: * ;
65use super :: * ;
76use crate :: atom:: * ;
8- use crate :: atom:: subexpr:: split_expr;
97
108use std:: fmt:: Debug ;
119use std:: collections:: HashSet ;
@@ -15,9 +13,6 @@ pub use index::{ALLOW_DUPLICATION, NO_DUPLICATION};
1513
1614// Grounding space
1715
18- /// Symbol to concatenate queries to space.
19- pub const COMMA_SYMBOL : Atom = sym ! ( "," ) ;
20-
2116/// In-memory space which can contain grounded atoms.
2217// TODO: Clone is required by C API
2318#[ derive( Clone ) ]
@@ -75,7 +70,7 @@ impl<D: DuplicationStrategy> GroundingSpace<D> {
7570 /// assert_eq!(space.query(&sym!("C")), BindingsSet::empty());
7671 /// ```
7772 pub fn add ( & mut self , atom : Atom ) {
78- // log::debug!("GroundingSpace::add(): self: {:? }, atom: {:? }", self as *const GroundingSpace , atom);
73+ log:: debug!( "GroundingSpace::add: { }, atom: {}" , self , atom) ;
7974 self . index . insert ( atom. clone ( ) ) ;
8075 self . common . notify_all_observers ( & SpaceEvent :: Add ( atom) ) ;
8176 }
@@ -97,7 +92,7 @@ impl<D: DuplicationStrategy> GroundingSpace<D> {
9792 /// assert_eq!(space.query(&sym!("A")), BindingsSet::empty());
9893 /// ```
9994 pub fn remove ( & mut self , atom : & Atom ) -> bool {
100- // log::debug!("GroundingSpace::remove(): self: {:? }, atom: {:? }", self as *const GroundingSpace , atom);
95+ log:: debug!( "GroundingSpace::remove: { }, atom: {}" , self , atom) ;
10196 let is_removed = self . index . remove ( atom) ;
10297 if is_removed {
10398 self . common . notify_all_observers ( & SpaceEvent :: Remove ( atom. clone ( ) ) ) ;
@@ -152,42 +147,20 @@ impl<D: DuplicationStrategy> GroundingSpace<D> {
152147 /// assert_eq!(result, bind_set![{x: sym!("B")}]);
153148 /// ```
154149 pub fn query ( & self , query : & Atom ) -> BindingsSet {
155- match split_expr ( query) {
156- // Cannot match with COMMA_SYMBOL here, because Rust allows
157- // it only when Atom has PartialEq and Eq derived.
158- Some ( ( sym @ Atom :: Symbol ( _) , args) ) if * sym == COMMA_SYMBOL => {
159- args. fold ( BindingsSet :: single ( ) ,
160- |mut acc, query| {
161- let result = if acc. is_empty ( ) {
162- acc
163- } else {
164- acc. drain ( 0 ..) . flat_map ( |prev| -> BindingsSet {
165- let query = matcher:: apply_bindings_to_atom_move ( query. clone ( ) , & prev) ;
166- let mut res = self . query ( & query) ;
167- res. drain ( 0 ..)
168- . flat_map ( |next| next. merge ( & prev) )
169- . collect ( )
170- } ) . collect ( )
171- } ;
172- log:: debug!( "query: current result: {:?}" , result) ;
173- result
174- } )
175- } ,
176- _ => self . single_query ( query) ,
177- }
150+ complex_query ( query, |query| self . single_query ( query) )
178151 }
179152
180153 /// Executes simple `query` without sub-queries on the space.
181154 fn single_query ( & self , query : & Atom ) -> BindingsSet {
182- log:: debug!( "single_query: query: {}" , query) ;
155+ log:: debug!( "GroundingSpace:: single_query: {} query: {}" , self , query) ;
183156 let mut result = BindingsSet :: empty ( ) ;
184157 let query_vars: HashSet < & VariableAtom > = query. iter ( ) . filter_type :: < & VariableAtom > ( ) . collect ( ) ;
185158 for bindings in self . index . query ( query) {
186159 let bindings = bindings. narrow_vars ( & query_vars) ;
187160 log:: trace!( "single_query: push result: {}" , bindings) ;
188161 result. push ( bindings) ;
189162 }
190- log:: debug!( "single_query: result: {:?}" , result) ;
163+ log:: debug!( "GroundinSpace:: single_query: {} result: {}" , self , result) ;
191164 result
192165 }
193166
@@ -249,7 +222,7 @@ impl PartialEq for GroundingSpace {
249222 }
250223}
251224
252- impl Debug for GroundingSpace {
225+ impl < D : DuplicationStrategy > Debug for GroundingSpace < D > {
253226 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
254227 match & self . name {
255228 Some ( name) => write ! ( f, "GroundingSpace-{name} ({self:p})" ) ,
@@ -258,7 +231,7 @@ impl Debug for GroundingSpace {
258231 }
259232}
260233
261- impl Display for GroundingSpace {
234+ impl < D : DuplicationStrategy > Display for GroundingSpace < D > {
262235 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
263236 match & self . name {
264237 Some ( name) => write ! ( f, "GroundingSpace-{name}" ) ,
0 commit comments