| 
									
										
										
										
											2009-06-26 16:25:50 -04:00
										 |  |  | USING: accessors arrays kernel models models.product monads | 
					
						
							| 
									
										
										
										
											2009-11-05 23:22:21 -05:00
										 |  |  | sequences sequences.extras shuffle ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 |  |  | FROM: syntax => >> ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | IN: models.combinators | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:42:31 -04:00
										 |  |  | TUPLE: multi-model < model important? ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | GENERIC: (model-changed) ( model observer -- )
 | 
					
						
							|  |  |  | : <multi-model> ( models kind -- model ) f swap new-model [ [ add-dependency ] curry each ] keep ;
 | 
					
						
							|  |  |  | M: multi-model model-changed over value>> [ (model-changed) ] [ 2drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-05-27 17:49:40 -04:00
										 |  |  | M: multi-model model-activated dup dependencies>> [ value>> ] find nip
 | 
					
						
							|  |  |  |    [ swap model-changed ] [ drop ] if* ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:42:31 -04:00
										 |  |  | : #1 ( model -- model' ) t >>important? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | IN: models | 
					
						
							|  |  |  | : notify-connections ( model -- )
 | 
					
						
							|  |  |  |     dup connections>> dup [ dup multi-model? [ important?>> ] [ drop f ] if ] find-all | 
					
						
							|  |  |  |     [ second tuck [ remove ] dip prefix ] each
 | 
					
						
							|  |  |  |     [ model-changed ] with each ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | IN: models.combinators | 
					
						
							| 
									
										
										
										
											2009-06-14 12:42:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | TUPLE: basic-model < multi-model ;
 | 
					
						
							|  |  |  | M: basic-model (model-changed) [ value>> ] dip set-model ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : merge ( models -- model ) basic-model <multi-model> ;
 | 
					
						
							|  |  |  | : 2merge ( model1 model2 -- model ) 2array merge ;
 | 
					
						
							|  |  |  | : <basic> ( value -- model ) basic-model new-model ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: filter-model < multi-model quot ;
 | 
					
						
							|  |  |  | M: filter-model (model-changed) [ value>> ] dip 2dup quot>> call( a -- ? ) | 
					
						
							|  |  |  |    [ set-model ] [ 2drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : filter-model ( model quot -- filter-model ) [ 1array \ filter-model <multi-model> ] dip >>quot ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-18 16:32:11 -04:00
										 |  |  | TUPLE: fold-model < multi-model quot base values ;
 | 
					
						
							|  |  |  | M: fold-model (model-changed) 2dup base>> =
 | 
					
						
							|  |  |  |     [ [ [ value>> ] [ [ values>> ] [ quot>> ] bi ] bi* swapd reduce* ] keep set-model ] | 
					
						
							|  |  |  |     [ [ [ value>> ] [ values>> ] bi* push ] | 
					
						
							|  |  |  |       [ [ [ value>> ] [ [ value>> ] [ quot>> ] bi ] bi* call( val oldval -- newval ) ] keep set-model ] 2bi
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | M: fold-model model-activated drop ;
 | 
					
						
							|  |  |  | : new-fold-model ( deps -- model ) fold-model <multi-model> V{ } clone >>values ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : fold ( model oldval quot -- model ) rot 1array new-fold-model swap >>quot | 
					
						
							| 
									
										
										
										
											2009-05-30 13:13:13 -04:00
										 |  |  |    swap >>value ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : fold* ( model oldmodel quot -- model ) over [ [ 2array new-fold-model ] dip >>quot ] | 
					
						
							| 
									
										
										
										
											2009-06-18 16:32:11 -04:00
										 |  |  |     dip [ >>base ] [ value>> >>value ] bi ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: updater-model < multi-model values updates ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 |  |  | M: updater-model (model-changed) [ tuck updates>> =
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  |    [ [ values>> value>> ] keep set-model ] | 
					
						
							| 
									
										
										
										
											2014-10-25 00:39:58 -04:00
										 |  |  |    [ drop ] if ] keep f swap value<< ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : updates ( values updates -- model ) [ 2array updater-model <multi-model> ] 2keep
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  |    [ >>values ] [ >>updates ] bi* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:42:31 -04:00
										 |  |  | SYMBOL: switch | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | TUPLE: switch-model < multi-model original switcher on ;
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:42:31 -04:00
										 |  |  | M: switch-model (model-changed) 2dup switcher>> =
 | 
					
						
							|  |  |  |    [ [ value>> ] dip over switch = [ nip [ original>> ] keep f >>on model-changed ] [ t >>on set-model ] if ] | 
					
						
							| 
									
										
										
										
											2009-05-31 22:49:55 -04:00
										 |  |  |    [ dup on>> [ 2drop ] [ [ value>> ] dip over [ set-model ] [ 2drop ] if ] if ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : switch-models ( model1 model2 -- model' ) swap [ 2array switch-model <multi-model> ] 2keep
 | 
					
						
							| 
									
										
										
										
											2009-06-10 17:15:02 -04:00
										 |  |  |    [ [ value>> >>value ] [ >>original ] bi ] [ >>switcher ] bi* ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | M: switch-model model-activated [ original>> ] keep model-changed ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 |  |  | : >behavior ( event -- behavior ) t >>value ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: mapped-model < multi-model model quot ;
 | 
					
						
							| 
									
										
										
										
											2009-05-25 16:28:05 -04:00
										 |  |  | : new-mapped-model ( model quot class -- mapped-model ) [ over 1array ] dip
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  |    <multi-model> swap >>quot swap >>model ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : <mapped> ( model quot -- model ) mapped-model new-mapped-model ;
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | M: mapped-model (model-changed) | 
					
						
							|  |  |  |     [ [ value>> ] [ quot>> ] bi* call( old -- new ) ] [ nip ] 2bi
 | 
					
						
							|  |  |  |     set-model ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: side-effect-model < mapped-model ;
 | 
					
						
							| 
									
										
										
										
											2009-06-21 22:08:24 -04:00
										 |  |  | M: side-effect-model (model-changed) [ value>> ] dip [ quot>> call( old -- ) ] 2keep set-model ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  | TUPLE: quot-model < mapped-model ;
 | 
					
						
							|  |  |  | M: quot-model (model-changed) nip [ quot>> call( -- b ) ] keep set-model ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: action-value < basic-model parent ;
 | 
					
						
							|  |  |  | : <action-value> ( parent value -- model ) action-value new-model swap >>parent ;
 | 
					
						
							|  |  |  | M: action-value model-activated dup parent>> dup activate-model model-changed ; ! a fake dependency of sorts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: action < multi-model quot ;
 | 
					
						
							|  |  |  | M: action (model-changed) [ [ value>> ] [ quot>> ] bi* call( a -- b ) ] keep value>> | 
					
						
							|  |  |  |    [ swap add-connection ] 2keep model-changed ;
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | : <action> ( model quot -- action-model ) [ 1array action <multi-model> ] dip >>quot dup f <action-value> >>value value>> ;
 | 
					
						
							| 
									
										
										
										
											2009-06-10 17:15:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 |  |  | TUPLE: collection < multi-model ;
 | 
					
						
							|  |  |  | : <collection> ( models -- product ) collection <multi-model> ;
 | 
					
						
							|  |  |  | M: collection (model-changed) | 
					
						
							| 
									
										
										
										
											2009-05-24 10:36:24 -04:00
										 |  |  |     nip
 | 
					
						
							|  |  |  |     dup dependencies>> [ value>> ] all?
 | 
					
						
							| 
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 |  |  |     [ dup [ value>> ] product-value swap set-model ] | 
					
						
							| 
									
										
										
										
											2009-05-24 11:05:17 -04:00
										 |  |  |     [ drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 |  |  | M: collection model-activated dup (model-changed) ;
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:42:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-21 22:08:24 -04:00
										 |  |  | ! for side effects | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | TUPLE: (when-model) < multi-model quot cond ;
 | 
					
						
							|  |  |  | : when-model ( model quot cond -- model ) rot 1array (when-model) <multi-model> swap >>cond swap >>quot ;
 | 
					
						
							|  |  |  | M: (when-model) (model-changed) [ quot>> ] 2keep
 | 
					
						
							| 
									
										
										
										
											2009-06-21 22:08:24 -04:00
										 |  |  |     [ value>> ] [ cond>> ] bi* call( a -- ? ) [ call( model -- ) ] [ 2drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-18 16:32:11 -04:00
										 |  |  | ! only used in construction | 
					
						
							| 
									
										
										
										
											2009-07-21 20:40:06 -04:00
										 |  |  | : with-self ( quot: ( model -- model ) -- model ) [ f <basic> dup ] dip call swap [ add-dependency ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-01 16:18:24 -04:00
										 |  |  | USE: models.combinators.templates | 
					
						
							| 
									
										
										
										
											2009-11-05 23:22:21 -05:00
										 |  |  | << { "$>" "<$" "fmap" } [ fmaps ] each >> |