-
Notifications
You must be signed in to change notification settings - Fork 5
/
dsim.clj
1214 lines (726 loc) · 30.3 KB
/
dsim.clj
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns dvlopt.dsim
"Idiomatic, purely-functional discrete event simulation.
A transition is pure stepwise function gradually modifying some arbitrary state map. Transitions are part of the state itself
and are located under the `transition-key` key. They can be organized in arbitrarily nested maps. It is both common and desired
for transitions to mirror the data they act upon :
{dvlopt.dsim/transition-key {:asteroids {42 {:x ...
:y ...}}}
:asteroids {42 {:x 450
:y 1420}}}
This pattern is so common that in this example, [:asteroids 42 :x] would be called the `data-path` of the :x transition of
asteroid 42. Such a transition accepts 3 arguments: a state map, its data-path, and a step. It returns a new state which, although
not enforced, should somehow modify the :x value of asteroid 42.
For doing so, a transition is created by providing an `on-step` function which also accepts 3 arguments: a state map, the data-path,
and a percentage of completion. This percentage depends on the first step of the transition, how many steps it lasts, and the
current step: (current-step - first-step) / n-steps.
After reaching 100%, if it was provided in the first place, the `on-complete` function of the transition is called. It accepts
4 arguments: the current state map, the data-path, the completion step, and the current step. It is useful when action must be
taken after a transition, for instance for creating a new one. If some steps are missed or skipped, the completion step and the
current step will not match. Hence it is useful to provide both. Completed transitions are removed automatically.
Cf. `infinite`
`once`
`repeating`
A poly-transition is a higher-order transition composed of several transitions. At the end of each sub-transition, the poly-transition
takes care of creating the next one at the right moment. Hence, it would be easy to animate asteroid 42 to sequentially move in
different directions, or to sequentially rotate in some complex manner. It is also trivial to create nested poly-transitions.
Cf. `poly`
`poly-infinite`
`poly-repeating`
Scaling a percentage to a value such as the :x position of an asteroid is facilitated by using `scale` and `fn-scale`. It is often
needed for a transition to behave non-linearly. This can be simply done by modifying the percentage of completion, which is a linear
progression, to be non-linear. For example, if an asteroid has to move faster and faster along the :x axis from 500 to 1000 pixels in a
100 steps starting from step 0:
(dvlopt.dsim/once 0
100
(let [scale' (dvlopt.dsim/fn-scale 500
1000)]
(fn on-step [state data-path percent]
(assoc-in state
data-path
(scale' (Math/pow percent
2))))))
The most straightforward way to add or remove transitions to a state is by using `merge-transitions`. A series of helpers for `on-step`
and `on-complete` functions is provided. For example, improving the last example and removing the asteroid when done :
(dvlopt.dsim/once 0
100
(dsim/fn-mirror-percent (comp (dvlopt.dsim/fn-scale 500
1000)
#(Math/pow %
2)))
dsim/remove-pre-data)
The most basic way of moving a state to some step is done by using `move`. `move-seq` facilitates the process of iteratively moving
through a sequence of steps. However, the most useful way is probably `move-events` which also takes into account events happening at
some particular steps, each modifying the state is some way. Any non-trivial simulation involves such events."
{:author "Adam Helinski"})
;;;;;;;;;; For keeping alphabetical or logical order
(declare poly-infinite
poly-repeating
transition-key
transition-path)
;;;;;;;;;; Utilities
(defn dissoc-in
"Deep dissoc, natural counterpart of Clojure's `assoc-in`.
Empty maps are removed.
Ex. (dissoc-in {:a {:b 42}
:c :ok}
[:a :b])
=> {:c :ok}"
[hmap [k & ks :as path]]
(if-not ks
(dissoc hmap
k)
(let [hmap-rest (dissoc-in (get hmap
k)
ks)]
(if (empty? hmap-rest)
(dissoc hmap
k)
(assoc hmap
k
hmap-rest)))))
(defn deep-merge
"Deep merges two maps."
[hmap-1 hmap-2]
(merge-with (fn select-value [v-1 v-2]
(if (and (map? v-1)
(map? v-2))
(deep-merge v-1
v-2)
v-2))
hmap-1
hmap-2))
(defn last-step
"Simplify provides the last step of a transition given its first-step and the number of steps it lasts."
[first-step n-steps]
(+ first-step
(dec n-steps)))
(defn millis->n-steps
"Computes the number of steps needed for completing a transition in `millis` milliseconds for a phenomenon,
such as the frame-rate, happening `hz` per second.
Ex. Computing the number of frames needed in order to last 2000 milliseconds with a frame-rate of 60.
(millis->n-steps 2000
60)
=> 120"
[millis hz]
(long (Math/round (double (* (/ hz
1000)
millis)))))
;;;;;;;;;; Scaling percents and values
(defn- -scale-percent
;; Scale a percent value to an arbitrary range.
[scaled-a scaled-delta percent]
(+ (* percent
scaled-delta)
scaled-a))
(defn- -scale
;; Scale an arbitrary value to another range.
[scaled-a scaled-delta a delta x]
(-scale-percent scaled-a
scaled-delta
(/ (- x
a)
delta)))
(defn scale
"3 args : scales a `percent` value to a value between `scaled-a` and `scaled-b`.
5 args : scales the `x` value between `a` and `b` to be between `scaled-a` and `scaled-b`.
Ex. (scale 0
1000
0.5)
=> 500
(scale 0
1000
0
100
50)
=> 500"
([scaled-a scaled-b percent]
(-scale-percent scaled-a
(- scaled-b
scaled-a)
percent))
([scaled-a scaled-b a b x]
(-scale scaled-a
(- scaled-b
scaled-a)
a
(- b
a)
x)))
(defn fn-scale
"Exactly like `scale` but does not accept a value to scale. Instead, returns a function which does so.
Particularly useful when working with the percentage of completion of transitions."
([scaled-a scaled-b]
(let [scaled-delta (- scaled-b
scaled-a)]
(fn scale-percent
([percent]
(-scale-percent scaled-a
scaled-delta
percent))
([_state _data-path percent]
(scale-percent percent)))))
([scaled-a scaled-b a b]
(let [delta (- b
a)
scaled-delta (- scaled-b
scaled-a)]
(fn scale'
([x]
(-scale scaled-a
scaled-delta
a
delta
x))
([_state _data-path x]
(scale' x))))))
;;;;;;;;;; Helpers for transitions and state management
(defn fn-assoc-data
"Returns a function assoc'ing the given data at the data-path of a transition.
Useful when some steps might be skipped but it is needed for a transition to reach 100%. For instance,
during a live animation, a frame will probably not be drawn at the exact millisecond a transition should
complete but some milliseconds later. The returned function can be used as an `on-complete` function
so that the state will always reflect the last step of such a transition."
[data]
(fn assoc-data
([state data-path]
(assoc-in state
data-path
data))
([state data-path _step]
(assoc-data state
data-path))
([state data-path _completion-step _step]
(assoc-data state
data-path))))
(defn fn-mirror
"Given an `on-step` function returning some arbitrary value instead of a new state, returns an `on-step`
function assoc'ing this value at the data-path in the state.
Idiomatic."
[map-percent]
(fn mirror-on-step [state data-path percent]
(assoc-in state
data-path
(map-percent state
data-path
percent))))
(defn fn-mirror-percent
"Behaves just like `fn-mirror` but the function provided in the first place simply maps a percent value to
an arbitrary one instead of being an `on-step` function.
Small convenient helper when the current state and data-path are not needed."
[map-only-percent]
(fn-mirror (fn only-percent [_state _data-path percent]
(map-only-percent percent))))
(defn- -in-transition?
;; Checks if there are any transitions.
[subtree]
(or (and (map? subtree)
(not (empty? subtree)))
(some? subtree)))
(defn in-transition?
"Is the given state or some part of it currently in transition?"
([state]
(-in-transition? (get state
transition-key)))
([state data-path]
(-in-transition? (get-in state
(transition-path data-path)))))
(defn merge-transitions
"Deep merges the provided - often nested - map of transitions in the given state.
Very useful for adding or removing several transitions at once. Indeed, nil values are simply removed when moving the state."
[state transitions]
(update state
transition-key
deep-merge
transitions))
(defn pipe-complete
"Given a collection of `on-complete` functions, returns an `on-complete` function piping arguments into this collection.
Nil values are simply filtered-out."
[on-completes]
(let [on-completes' (filterv some?
on-completes)]
(case (count on-completes')
0 nil
1 (first on-completes')
2 (let [[on-complete-1
on-complete-2] on-completes']
(fn piped-on-complete [state data-path completion-step step]
(-> state
(on-complete-1 data-path
completion-step
step)
(on-complete-2 data-path
completion-step
step))))
(fn reduce-on-complete [state data-path completion-step step]
(reduce (fn next-on-complete [state' local-on-complete]
(local-on-complete state'
data-path
completion-step
step))
state
on-completes')))))
(defn remove-data
"Uses `dissoc-in` for removing what is at some data-path.
More useful when used as an `on-complete` function and the data needs to be cleaned once the transition completes."
([state data-path]
(dissoc-in state
data-path))
([state data-path _percent]
(remove-data state
data-path))
([state data-path _completion-step _step]
(remove-data state
data-path)))
(defn remove-transition
"Removes a transition given the data-path."
([state data-path]
(dissoc-in state
(transition-path data-path)))
([state data-path _percent]
(remove-transition state
data-path))
([state data-path _completion-step _step]
(remove-transition state
data-path)))
(defn remove-pre-data
"A vast majority of modeling involves some form of entities. It is also very common for such entities to be removed once all
their transitions completes, meaning they cannot evolve anymore. This function, used as an `on-complete` function, does exactly that.
For instance, modeling asteroids as {:asteroids {42 {:x 542
:y 1000}}} having :x and :y transitions.
Once it cannot move anymore, an asteroid must be cleaned (ie. removed from the state). By providing this function as an `on-complete`
function to every :x and :y transition garantees that. It will use `dissoc-in` for removing [:asteroids 42] once it does not have
any transitions anymore."
([state data-path]
(let [subtree-path (drop-last data-path)]
(if (in-transition? state
subtree-path)
state
(dissoc-in state
subtree-path))))
([state data-path _percent]
(remove-pre-data state
data-path))
([state data-path _completion-step _step]
(remove-pre-data state
data-path)))
(def transition-key
"All transitions belonging to a state must be under this key."
::transitions)
(defn transition-path
"Given a data-path, returns a transition-path"
[data-path]
(cons transition-key
data-path))
(defn without-transitions
"Returns the given state without its transitions."
[state]
(dissoc state
transition-key))
;;;;;;;;;; Creating transitions
(defn- -complete-transition
;; Completes a mono-transitions.
[state data-path completion-step step transition on-complete]
(let [state' (remove-transition state
data-path)]
(if on-complete
(on-complete state'
data-path
completion-step
step)
state')))
(defn infinite
"Returns a transition endlessly repeating cycles of `n-steps` steps.
Obviously, it does not need an `on-complete` function."
[first-step n-steps on-step]
(let [last-cycle-step (dec n-steps)]
(fn infinite-transition [state data-path step]
(if (>= step
first-step)
(on-step state
data-path
(double (/ (rem (- step
first-step)
n-steps)
last-cycle-step)))
state))))
(defn fn-infinite
"Returns a function returning an infinite transition.
Useful for poly-transitions.
Cf. `infinite`
`poly`"
[n-steps on-step]
(fn make-infinite
([state first-step]
(make-infinite state
first-step
nil))
([_state first-step _on-complete]
(infinite first-step
n-steps
on-step))))
(defn once
"Returns a transition lasting `n-steps` steps."
([first-step n-steps on-step]
(once first-step
n-steps
on-step
nil))
([first-step n-steps on-step on-complete]
(let [last-step' (last-step first-step
n-steps)
delta-steps (- last-step'
first-step)]
(fn once-transition [state data-path step]
(if (>= step
first-step)
(if (<= step
last-step')
(on-step state
data-path
(double (/ (- step
first-step)
delta-steps)))
(-complete-transition state
data-path
(inc last-step')
step
once-transition
on-complete))
state)))))
(defn fn-once
"Returns a function returning a transition.
Useful for poly-transitions.
Cf. `once`
`poly`"
([n-steps on-step]
(fn-once n-steps
on-step
nil))
([n-steps on-step on-complete]
(fn make-once
([state first-step]
(make-once state
first-step
nil))
([_state first-step on-complete-2]
(once first-step
n-steps
on-step
(pipe-complete [on-complete
on-complete-2]))))))
(defn- -validate-n-times
;; Ensures repeating transitions happen more than once.
[n-times]
(when (<= n-times
1)
(throw (IllegalArgumentException. "`n-times` must be > 1"))))
(defn repeating
"Returns a transition repeating `n-steps` steps `n-times` times."
([first-step n-times n-steps on-step]
(repeating first-step
n-times
n-steps
on-step
nil))
([first-step n-times n-steps on-step on-complete]
(-validate-n-times n-times)
(let [last-cycle-step (dec n-steps)]
(fn repeating-transition [state data-path step]
(if (>= step
first-step)
(let [delta-first (- step
first-step)]
(if (< (quot delta-first
n-steps)
n-times)
(on-step state
data-path
(double (/ (rem delta-first
n-steps)
last-cycle-step)))
(-complete-transition state
data-path
(+ first-step
(* n-times
n-steps))
step
repeating-transition
on-complete)))
state)))))
(defn fn-repeating
"Returns a function returning a repeating transition.
Useful for poly-transitions.
Cf. `repeating`
`poly`"
([n-times n-steps on-step]
(fn-repeating n-times
n-steps
on-step
nil))
([n-times n-steps on-step on-complete]
(fn make-repeating
([state first-step]
(make-repeating state
first-step
nil))
([_state first-step on-complete-2]
(repeating first-step
n-times
n-steps
on-step
(pipe-complete [on-complete
on-complete-2]))))))
(defn- -assoc-next-transition
;; Assoc'es the given transition and also realizes it for the given step.
[state data-path step transition]
(transition (assoc-in state
(transition-path data-path)
transition)
data-path
step))
(defn poly
"Returns a poly-transition which will follow a collection of functions producing transitions.
Those functions will be provided with the state at the moment the transition is created, the first-step
of this transition and an `on-complete` function which must not be ignored. This `on-complete` function
ensures that the next transition, if there is one, will be created."
([state first-step fn-transitions]
(poly state
first-step
fn-transitions
nil))
([state first-step fn-transitions on-complete]
(when-some [fn-transition (first fn-transitions)]
(fn-transition state
first-step
(fn on-complete' [state' data-path completion-step step]
(if-some [next-transition (poly state'
completion-step
(rest fn-transitions)
on-complete)]
(-assoc-next-transition state'
data-path
step
next-transition)
(if on-complete
(on-complete state'
data-path
completion-step
step)
state')))))))
(defn fn-poly
"Returns a function returning a poly-transition.
Useful for nested poly-transitions.
Cf. `poly`"
([fn-transitions]
(fn-poly fn-transitions
nil))
([fn-transitions on-complete]
(fn make-poly
([state first-step]
(make-poly state
first-step
nil))
([state first-step on-complete-2]
(poly state
first-step
fn-transitions
(pipe-complete [on-complete
on-complete-2]))))))
(defn- -poly-infinite
;; Helper for `poly-infinite`.
[state first-step all-fn-transitions fn-transitions]
(when-some [fn-transition (first fn-transitions)]
(fn-transition state
first-step
(fn endless-cycle [state' data-path completion-step step]
(-assoc-next-transition state'
data-path
step
(or (-poly-infinite state'
completion-step
all-fn-transitions
(rest fn-transitions))
(poly-infinite state'
completion-step
all-fn-transitions)))))))
(defn poly-infinite
"Union of `infinite` and `poly`. Returns a poly-transition endlessly repeating."
[state first-step fn-transitions]
(-poly-infinite state
first-step
fn-transitions
fn-transitions))
(defn fn-poly-infinite
"Returns a function returning an infinite poly-transition.
Useful for nested poly-transitions.
Cf. `poly`
`poly-infinite`"
[fn-transitions]
(fn make-poly-infinite
([state first-step]
(make-poly-infinite state
first-step
nil))
([state first-step _on-complete]
(poly-infinite state
first-step
fn-transitions))))
(defn- -poly-repeating
;; Helper for `poly-repeating`.
[state first-step n-times all-fn-transitions fn-transitions on-complete]
(when-some [fn-transition (first fn-transitions)]
(let [n-times' (dec n-times)]
(fn-transition state
first-step
(fn repeating-cycle [state' data-path completion-step step]
(if-some [next-transition (or (-poly-repeating state'
completion-step
n-times
all-fn-transitions
(rest fn-transitions)
on-complete)
(when (> n-times'
0)
(-poly-repeating state'
completion-step