2015-03-02 16:10:11 +00:00
|
|
|
|
#lang racket/base
|
2016-04-07 07:42:54 +00:00
|
|
|
|
;; Core implementation of Incremental Syndicate.
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(provide (struct-out message)
|
2015-03-06 11:21:50 +00:00
|
|
|
|
(except-out (struct-out quit) quit)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct-out quit-dataspace)
|
2015-03-06 11:21:50 +00:00
|
|
|
|
(rename-out [quit <quit>])
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(except-out (struct-out spawn) spawn)
|
|
|
|
|
(rename-out [spawn <spawn>])
|
|
|
|
|
(struct-out transition)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct-out dataspace)
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
2015-09-07 20:05:06 +00:00
|
|
|
|
(struct-out seal)
|
2016-05-06 21:35:15 +00:00
|
|
|
|
sealof
|
2015-09-07 20:05:06 +00:00
|
|
|
|
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(all-from-out "patch.rkt")
|
|
|
|
|
|
2016-03-12 16:54:31 +00:00
|
|
|
|
;; imported from trie.rkt:
|
2015-03-05 14:54:12 +00:00
|
|
|
|
?
|
|
|
|
|
wildcard?
|
|
|
|
|
?!
|
|
|
|
|
(struct-out capture)
|
2016-01-22 02:55:41 +00:00
|
|
|
|
pretty-print-trie
|
|
|
|
|
trie->pretty-string
|
|
|
|
|
trie-non-empty?
|
|
|
|
|
trie-empty?
|
|
|
|
|
trie-empty
|
2015-03-05 14:54:12 +00:00
|
|
|
|
projection->pattern
|
2016-03-12 16:54:31 +00:00
|
|
|
|
projection-arity
|
2016-01-22 02:55:41 +00:00
|
|
|
|
trie-project
|
|
|
|
|
trie-project/set
|
|
|
|
|
trie-project/set/single
|
2016-01-18 22:33:26 +00:00
|
|
|
|
project-assertions
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
|
|
|
|
event?
|
|
|
|
|
action?
|
2016-03-01 21:45:29 +00:00
|
|
|
|
match-event
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
|
|
|
|
meta-label?
|
|
|
|
|
|
2015-03-05 16:39:57 +00:00
|
|
|
|
prepend-at-meta
|
2015-03-05 14:54:12 +00:00
|
|
|
|
assert
|
|
|
|
|
retract
|
|
|
|
|
sub
|
|
|
|
|
unsub
|
|
|
|
|
pub
|
|
|
|
|
unpub
|
|
|
|
|
|
2015-03-06 11:21:50 +00:00
|
|
|
|
(rename-out [make-quit quit])
|
2016-04-07 07:42:54 +00:00
|
|
|
|
make-dataspace
|
|
|
|
|
spawn-dataspace
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(rename-out [spawn-process spawn])
|
2015-03-06 13:23:36 +00:00
|
|
|
|
spawn/stateless
|
2016-04-07 07:42:54 +00:00
|
|
|
|
make-spawn-dataspace
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
2015-03-06 15:49:39 +00:00
|
|
|
|
transition-bind
|
|
|
|
|
sequence-transitions
|
2015-12-11 02:16:06 +00:00
|
|
|
|
sequence-transitions*
|
|
|
|
|
sequence-transitions0
|
|
|
|
|
sequence-transitions0*
|
2015-03-06 15:49:39 +00:00
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
dataspace-handle-event
|
2015-03-16 14:38:32 +00:00
|
|
|
|
clean-transition
|
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
pretty-print-dataspace)
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(require racket/set)
|
|
|
|
|
(require racket/match)
|
|
|
|
|
(require (only-in racket/list flatten))
|
|
|
|
|
(require "functional-queue.rkt")
|
2016-03-12 16:54:31 +00:00
|
|
|
|
(require "trie.rkt")
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(require "patch.rkt")
|
2015-03-04 16:16:18 +00:00
|
|
|
|
(require "trace.rkt")
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(require "mux.rkt")
|
2015-05-11 22:25:21 +00:00
|
|
|
|
(require "pretty.rkt")
|
2015-03-04 14:45:16 +00:00
|
|
|
|
(module+ test (require rackunit))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
2015-03-04 16:16:18 +00:00
|
|
|
|
;; Events = Patches ∪ Messages
|
|
|
|
|
(struct message (body) #:prefab)
|
|
|
|
|
|
2015-03-02 16:10:11 +00:00
|
|
|
|
;; Actions ⊃ Events
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(struct spawn (boot) #:prefab)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct quit-dataspace () #:prefab) ;; NB. An action. Compare (quit), a Transition.
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
;; A Behavior is a ((Option Event) Any -> Transition): a function
|
|
|
|
|
;; mapping an Event (or, in the #f case, a poll signal) and a
|
|
|
|
|
;; Process's current state to a Transition.
|
|
|
|
|
;;
|
|
|
|
|
;; A Transition is either
|
|
|
|
|
;; - #f, a signal from a Process that it is inert and need not be
|
|
|
|
|
;; scheduled until some Event relevant to it arrives; or,
|
|
|
|
|
;; - a (transition Any (Constreeof Action)), a new Process state to
|
2016-04-07 07:42:54 +00:00
|
|
|
|
;; be held by its Dataspace and a sequence of Actions for the Dataspace
|
2015-03-02 16:10:11 +00:00
|
|
|
|
;; to take on the transitioning Process's behalf.
|
2015-11-17 19:01:04 +00:00
|
|
|
|
;; - a (quit (Option Exn) (Constreeof Action)), signalling that the
|
|
|
|
|
;; Process should never again be handed an event, and that any
|
|
|
|
|
;; queued actions should be performed, followed by the sequence
|
|
|
|
|
;; of Actions given, and then the process should be
|
|
|
|
|
;; garbage-collected. The optional Exn is only used for
|
|
|
|
|
;; debugging purposes; #f means normal termination.
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(struct transition (state actions) #:transparent)
|
2015-11-17 19:01:04 +00:00
|
|
|
|
(struct quit (exn actions) #:prefab)
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
;; A PID is a Nat.
|
|
|
|
|
;; A Label is a PID or 'meta.
|
|
|
|
|
|
|
|
|
|
;; VM private states
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct dataspace (mux ;; Multiplexer
|
|
|
|
|
pending-action-queue ;; (Queueof (Cons Label (U Action 'quit)))
|
|
|
|
|
runnable-pids ;; (Setof PID)
|
|
|
|
|
behaviors ;; (HashTable PID Behavior)
|
|
|
|
|
states ;; (HashTable PID Any)
|
|
|
|
|
)
|
2015-05-11 22:25:21 +00:00
|
|
|
|
#:transparent
|
2016-04-01 23:53:46 +00:00
|
|
|
|
#:methods gen:syndicate-pretty-printable
|
|
|
|
|
[(define (syndicate-pretty-print w [p (current-output-port)])
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(pretty-print-dataspace w p))])
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
2015-09-07 20:05:06 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
;; Seals are used by protocols to prevent the routing tries from
|
|
|
|
|
;; examining internal structure of values.
|
|
|
|
|
|
|
|
|
|
(struct seal (contents)) ;; NB. Neither transparent nor prefab
|
|
|
|
|
|
2016-05-06 21:35:15 +00:00
|
|
|
|
;; contract -> contract
|
|
|
|
|
(define ((sealof c) x)
|
|
|
|
|
(and (seal? x) (c (seal-contents x))))
|
|
|
|
|
|
2015-03-02 16:10:11 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
2015-03-04 16:16:18 +00:00
|
|
|
|
(define (event? x) (or (patch? x) (message? x)))
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define (action? x) (or (event? x) (spawn? x) (quit-dataspace? x)))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
2016-03-01 21:45:29 +00:00
|
|
|
|
(define-syntax-rule (match-event e clause ...)
|
|
|
|
|
(match e
|
|
|
|
|
clause ...
|
|
|
|
|
[_ #f]))
|
|
|
|
|
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(define (prepend-at-meta pattern level)
|
|
|
|
|
(if (zero? level)
|
|
|
|
|
pattern
|
|
|
|
|
(at-meta (prepend-at-meta pattern (- level 1)))))
|
|
|
|
|
|
|
|
|
|
(define (observe-at-meta pattern level)
|
|
|
|
|
(if (zero? level)
|
2016-03-12 16:54:31 +00:00
|
|
|
|
(pattern->trie '<observe-at-meta> (observe pattern))
|
2016-01-22 02:55:41 +00:00
|
|
|
|
(trie-union
|
2016-03-12 16:54:31 +00:00
|
|
|
|
(pattern->trie '<observe-at-meta> (observe (prepend-at-meta pattern level)))
|
|
|
|
|
(pattern->trie '<observe-at-meta> (at-meta (embedded-trie (observe-at-meta pattern (- level 1))))))))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
|
|
|
|
(define (assert pattern #:meta-level [level 0])
|
2016-03-12 16:54:31 +00:00
|
|
|
|
(patch (pattern->trie '<assert> (prepend-at-meta pattern level)) trie-empty))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(define (retract pattern #:meta-level [level 0])
|
2016-03-12 16:54:31 +00:00
|
|
|
|
(patch trie-empty (pattern->trie '<retract> (prepend-at-meta pattern level))))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
|
|
|
|
(define (sub pattern #:meta-level [level 0])
|
2016-03-12 16:54:31 +00:00
|
|
|
|
(patch (observe-at-meta pattern level) trie-empty))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(define (unsub pattern #:meta-level [level 0])
|
2016-03-12 16:54:31 +00:00
|
|
|
|
(patch trie-empty (observe-at-meta pattern level)))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
|
|
|
|
(define (pub pattern #:meta-level [level 0]) (assert (advertise pattern) #:meta-level level))
|
|
|
|
|
(define (unpub pattern #:meta-level [level 0]) (retract (advertise pattern) #:meta-level level))
|
|
|
|
|
|
2015-03-02 16:10:11 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
2015-12-03 20:53:07 +00:00
|
|
|
|
(define (general-transition? v)
|
2016-03-01 21:45:29 +00:00
|
|
|
|
(or (not v) (transition? v) (quit? v) (void? v)))
|
2015-12-03 20:53:07 +00:00
|
|
|
|
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(define (ensure-transition v)
|
2015-12-03 20:53:07 +00:00
|
|
|
|
(if (general-transition? v)
|
2015-03-02 16:10:11 +00:00
|
|
|
|
v
|
2016-03-01 21:45:29 +00:00
|
|
|
|
(raise (exn:fail:contract (format "Expected transition, quit, #f or (void); got ~v" v)
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(current-continuation-marks)))))
|
|
|
|
|
|
|
|
|
|
(define (clean-transition t)
|
2015-03-06 11:21:50 +00:00
|
|
|
|
(match t
|
|
|
|
|
[#f #f]
|
2015-11-17 19:01:04 +00:00
|
|
|
|
[(quit exn actions) (quit exn (clean-actions actions))]
|
2016-03-01 21:45:29 +00:00
|
|
|
|
[(transition state actions) (transition state (clean-actions actions))]
|
|
|
|
|
[(? void?) #f]))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define (clean-actions actions)
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(filter (lambda (x) (and (action? x) (not (patch-empty? x)))) (flatten actions)))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
2015-03-04 14:45:16 +00:00
|
|
|
|
(define (send-event e pid w)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define behavior (hash-ref (dataspace-behaviors w) pid #f))
|
|
|
|
|
(define old-state (hash-ref (dataspace-states w) pid #f))
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(if (not behavior)
|
|
|
|
|
w
|
2015-03-21 16:30:10 +00:00
|
|
|
|
(begin
|
|
|
|
|
(trace-process-step e pid behavior old-state)
|
|
|
|
|
(invoke-process pid
|
|
|
|
|
(lambda () (clean-transition (ensure-transition (behavior e old-state))))
|
|
|
|
|
(match-lambda
|
|
|
|
|
[#f w]
|
2015-11-17 19:01:04 +00:00
|
|
|
|
[(and q (quit exn final-actions))
|
|
|
|
|
(trace-process-step-result e pid behavior old-state exn q)
|
2015-12-03 20:53:32 +00:00
|
|
|
|
(enqueue-actions (disable-process pid exn w) pid (append final-actions
|
|
|
|
|
(list 'quit)))]
|
2015-03-21 16:30:10 +00:00
|
|
|
|
[(and t (transition new-state new-actions))
|
|
|
|
|
(trace-process-step-result e pid behavior old-state #f t)
|
|
|
|
|
(enqueue-actions (mark-pid-runnable (update-state w pid new-state) pid)
|
|
|
|
|
pid
|
|
|
|
|
new-actions)])
|
|
|
|
|
(lambda (exn)
|
|
|
|
|
(trace-process-step-result e pid behavior old-state exn #f)
|
|
|
|
|
(enqueue-actions (disable-process pid exn w) pid (list 'quit)))))))
|
2015-03-16 14:38:32 +00:00
|
|
|
|
|
|
|
|
|
(define (update-state w pid s)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct-copy dataspace w [states (hash-set (dataspace-states w) pid s)]))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
|
|
|
|
(define (send-event/guard delta pid w)
|
|
|
|
|
(if (patch-empty? delta)
|
|
|
|
|
w
|
|
|
|
|
(send-event delta pid w)))
|
|
|
|
|
|
|
|
|
|
(define (disable-process pid exn w)
|
2015-03-06 11:21:50 +00:00
|
|
|
|
(when exn
|
|
|
|
|
(log-error "Process ~a died with exception:\n~a"
|
|
|
|
|
(cons pid (trace-pid-stack))
|
|
|
|
|
(exn->string exn)))
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct-copy dataspace w
|
|
|
|
|
[behaviors (hash-remove (dataspace-behaviors w) pid)]
|
|
|
|
|
[states (hash-remove (dataspace-states w) pid)]))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define (invoke-process pid thunk k-ok k-exn)
|
|
|
|
|
(define-values (ok? result)
|
2015-03-04 16:16:18 +00:00
|
|
|
|
(call-in-trace-context
|
|
|
|
|
pid
|
|
|
|
|
(lambda ()
|
|
|
|
|
(with-handlers ([(lambda (exn) #t) (lambda (exn) (values #f exn))])
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(values #t (with-continuation-mark 'minimart-process pid (thunk)))))))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(if ok?
|
|
|
|
|
(k-ok result)
|
|
|
|
|
(k-exn result)))
|
|
|
|
|
|
|
|
|
|
(define (mark-pid-runnable w pid)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct-copy dataspace w [runnable-pids (set-add (dataspace-runnable-pids w) pid)]))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define (enqueue-actions w label actions)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(struct-copy dataspace w
|
2015-03-02 16:10:11 +00:00
|
|
|
|
[pending-action-queue
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(queue-append-list (dataspace-pending-action-queue w)
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(for/list [(a actions)] (cons label a)))]))
|
|
|
|
|
|
2015-11-17 19:01:04 +00:00
|
|
|
|
(define (make-quit #:exception [exn #f] . actions)
|
|
|
|
|
(quit exn actions))
|
2015-03-06 11:21:50 +00:00
|
|
|
|
|
2016-01-28 01:46:24 +00:00
|
|
|
|
(define-syntax spawn-process
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
[(_ #:name name-exp behavior-exp initial-state-exp initial-action-tree-exp)
|
|
|
|
|
(spawn (lambda ()
|
2016-01-28 01:53:37 +00:00
|
|
|
|
(list (let ((name name-exp)
|
|
|
|
|
(beh behavior-exp))
|
|
|
|
|
(if name (procedure-rename beh name) beh))
|
2016-01-28 01:46:24 +00:00
|
|
|
|
(transition initial-state-exp initial-action-tree-exp))))]
|
|
|
|
|
[(_ behavior-exp initial-state-exp initial-action-tree-exp)
|
|
|
|
|
(spawn (lambda ()
|
|
|
|
|
(list behavior-exp
|
|
|
|
|
(transition initial-state-exp initial-action-tree-exp))))]))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
|
2015-12-03 20:53:07 +00:00
|
|
|
|
(define-syntax-rule (spawn/stateless behavior-exp initial-action-tree-exp)
|
2015-03-06 13:23:36 +00:00
|
|
|
|
(spawn-process (stateless-behavior-wrap behavior-exp)
|
|
|
|
|
(void)
|
2015-12-03 20:53:07 +00:00
|
|
|
|
initial-action-tree-exp))
|
2015-03-06 13:23:36 +00:00
|
|
|
|
|
|
|
|
|
(define ((stateless-behavior-wrap b) e state)
|
|
|
|
|
(match (b e)
|
|
|
|
|
[#f #f]
|
|
|
|
|
[(? quit? q) q]
|
|
|
|
|
[actions (transition state actions)]))
|
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define-syntax-rule (spawn-dataspace boot-action ...)
|
|
|
|
|
(make-spawn-dataspace (lambda () (list boot-action ...))))
|
2015-03-04 14:45:16 +00:00
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define (make-dataspace boot-actions)
|
|
|
|
|
(dataspace (mux)
|
|
|
|
|
(list->queue (for/list ((a (in-list (clean-actions boot-actions)))) (cons 'meta a)))
|
|
|
|
|
(set)
|
|
|
|
|
(hash)
|
|
|
|
|
(hash)))
|
2015-03-04 14:45:16 +00:00
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define (make-spawn-dataspace boot-actions-thunk)
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(spawn (lambda ()
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(list dataspace-handle-event
|
|
|
|
|
(transition (make-dataspace (boot-actions-thunk)) '())))))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define (transition-bind k t0)
|
2015-03-06 11:21:50 +00:00
|
|
|
|
(match t0
|
|
|
|
|
[#f (error 'transition-bind "Cannot bind from transition #f with continuation ~v" k)]
|
2015-11-17 19:01:04 +00:00
|
|
|
|
[(quit _ _) t0]
|
2015-03-06 11:21:50 +00:00
|
|
|
|
[(transition state0 actions0)
|
|
|
|
|
(match (k state0)
|
|
|
|
|
[#f t0]
|
2015-11-17 19:01:04 +00:00
|
|
|
|
[(quit exn actions1) (quit exn (cons actions0 actions1))]
|
2015-03-06 11:21:50 +00:00
|
|
|
|
[(transition state1 actions1) (transition state1 (cons actions0 actions1))])]))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define (sequence-transitions t0 . steps)
|
2015-12-09 23:59:33 +00:00
|
|
|
|
(sequence-transitions* t0 steps))
|
|
|
|
|
|
|
|
|
|
(define (sequence-transitions* t0 steps)
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(foldl transition-bind t0 steps))
|
|
|
|
|
|
2015-12-09 23:59:33 +00:00
|
|
|
|
(define (sequence-transitions0 state0 . steps)
|
|
|
|
|
(sequence-transitions0* state0 steps))
|
|
|
|
|
|
|
|
|
|
(define (sequence-transitions0* state0 steps)
|
|
|
|
|
(match steps
|
|
|
|
|
['() #f]
|
|
|
|
|
[(cons step rest)
|
|
|
|
|
(match (step state0)
|
|
|
|
|
[#f (sequence-transitions0* state0 rest)]
|
|
|
|
|
[(? quit? q) q]
|
|
|
|
|
[(? transition? t) (sequence-transitions* t rest)])]))
|
|
|
|
|
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(define (inert? w)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(and (queue-empty? (dataspace-pending-action-queue w))
|
|
|
|
|
(set-empty? (dataspace-runnable-pids w))))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define (dataspace-handle-event e w)
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(if (or e (not (inert? w)))
|
|
|
|
|
(sequence-transitions (transition w '())
|
|
|
|
|
(inject-event e)
|
2015-03-05 14:54:12 +00:00
|
|
|
|
perform-actions
|
|
|
|
|
(lambda (w) (or (step-children w) (transition w '()))))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(step-children w)))
|
|
|
|
|
|
|
|
|
|
(define ((inject-event e) w)
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(transition (match e
|
|
|
|
|
[#f w]
|
|
|
|
|
[(? patch? delta) (enqueue-actions w 'meta (list (lift-patch delta)))]
|
|
|
|
|
[(message body) (enqueue-actions w 'meta (list (message (at-meta body))))])
|
|
|
|
|
'()))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define (perform-actions w)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(for/fold ([wt (transition (struct-copy dataspace w [pending-action-queue (make-queue)]) '())])
|
|
|
|
|
((entry (in-list (queue->list (dataspace-pending-action-queue w)))))
|
2015-10-24 01:49:21 +00:00
|
|
|
|
#:break (quit? wt) ;; TODO: should a quit action be delayed until the end of the turn?
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(match-define [cons label a] entry)
|
2015-03-21 16:30:10 +00:00
|
|
|
|
(trace-internal-action label a (transition-state wt))
|
2015-03-04 16:16:18 +00:00
|
|
|
|
(define wt1 (transition-bind (perform-action label a) wt))
|
2015-03-21 16:30:10 +00:00
|
|
|
|
(trace-internal-action-result label a (transition-state wt) wt1)
|
2015-03-04 16:16:18 +00:00
|
|
|
|
wt1))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define ((perform-action label a) w)
|
|
|
|
|
(match a
|
2015-03-05 14:54:12 +00:00
|
|
|
|
[(spawn boot)
|
|
|
|
|
(invoke-process 'booting
|
|
|
|
|
(lambda ()
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(match (boot)
|
2015-12-03 20:53:07 +00:00
|
|
|
|
[(and results (list (? procedure?) (? general-transition?)))
|
2015-03-16 14:38:32 +00:00
|
|
|
|
results]
|
|
|
|
|
[other
|
|
|
|
|
(error 'spawn
|
|
|
|
|
"Spawn boot procedure must yield boot spec; received ~v"
|
|
|
|
|
other)]))
|
|
|
|
|
(lambda (results)
|
2015-12-03 20:53:07 +00:00
|
|
|
|
(match-define (list behavior initial-transition) results)
|
|
|
|
|
(create-process w behavior initial-transition))
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(lambda (exn)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(log-error "Spawned process in dataspace ~a died with exception:\n~a"
|
2015-03-05 14:54:12 +00:00
|
|
|
|
(trace-pid-stack)
|
|
|
|
|
(exn->string exn))
|
|
|
|
|
(transition w '())))]
|
2015-03-06 11:21:50 +00:00
|
|
|
|
['quit
|
2016-01-18 19:29:48 +00:00
|
|
|
|
(define-values (new-mux _label delta delta-aggregate)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(mux-remove-stream (dataspace-mux w) label))
|
2015-12-04 16:21:13 +00:00
|
|
|
|
;; behavior & state in w already removed by disable-process
|
|
|
|
|
(deliver-patches w new-mux label delta delta-aggregate)]
|
2016-04-07 07:42:54 +00:00
|
|
|
|
[(quit-dataspace)
|
2015-10-23 23:49:30 +00:00
|
|
|
|
(make-quit)]
|
2015-03-02 16:10:11 +00:00
|
|
|
|
[(? patch? delta-orig)
|
2015-12-04 16:21:13 +00:00
|
|
|
|
(define-values (new-mux _label delta delta-aggregate)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(mux-update-stream (dataspace-mux w) label delta-orig))
|
2015-12-04 16:21:13 +00:00
|
|
|
|
(deliver-patches w new-mux label delta delta-aggregate)]
|
2015-03-04 16:16:18 +00:00
|
|
|
|
[(and m (message body))
|
2015-12-04 16:21:13 +00:00
|
|
|
|
(when (observe? body)
|
|
|
|
|
(log-warning "Stream ~a sent message containing query ~v"
|
|
|
|
|
(cons label (trace-pid-stack))
|
|
|
|
|
body))
|
|
|
|
|
(if (and (not (meta-label? label)) ;; it's from a local process, not envt
|
|
|
|
|
(at-meta? body)) ;; it relates to envt, not local
|
|
|
|
|
(transition w (message (at-meta-claim body)))
|
|
|
|
|
(transition (for/fold [(w w)]
|
2016-04-07 07:42:54 +00:00
|
|
|
|
[(pid (in-list (mux-route-message (dataspace-mux w) body)))]
|
2015-12-04 16:21:13 +00:00
|
|
|
|
(send-event m pid w))
|
|
|
|
|
'()))]))
|
2015-03-16 14:38:32 +00:00
|
|
|
|
|
2015-12-03 20:53:07 +00:00
|
|
|
|
(define (create-process w behavior initial-transition)
|
|
|
|
|
(if (not initial-transition)
|
2015-12-04 15:40:02 +00:00
|
|
|
|
(transition w '()) ;; Uh, ok
|
2015-12-03 20:53:07 +00:00
|
|
|
|
(let ()
|
|
|
|
|
(define-values (postprocess initial-actions)
|
|
|
|
|
(match (clean-transition initial-transition)
|
|
|
|
|
[(and q (quit exn initial-actions0))
|
|
|
|
|
(values (lambda (w pid)
|
|
|
|
|
(trace-process-step-result 'boot pid behavior (void) exn q)
|
|
|
|
|
(disable-process pid exn w))
|
|
|
|
|
(append initial-actions0 (list 'quit)))]
|
|
|
|
|
[(and t (transition initial-state initial-actions0))
|
|
|
|
|
(values (lambda (w pid)
|
|
|
|
|
(trace-process-step-result 'boot pid behavior (void) #f t)
|
|
|
|
|
(mark-pid-runnable (update-state w pid initial-state) pid))
|
|
|
|
|
initial-actions0)]))
|
|
|
|
|
(define-values (initial-patch remaining-initial-actions)
|
|
|
|
|
(match initial-actions
|
|
|
|
|
[(cons (? patch? p) rest) (values p rest)]
|
2016-03-12 16:54:31 +00:00
|
|
|
|
[other (values patch-empty other)]))
|
2015-12-04 16:21:13 +00:00
|
|
|
|
(define-values (new-mux new-pid delta delta-aggregate)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(mux-add-stream (dataspace-mux w) initial-patch))
|
|
|
|
|
(let* ((w (struct-copy dataspace w
|
|
|
|
|
[behaviors (hash-set (dataspace-behaviors w)
|
2015-12-03 20:53:07 +00:00
|
|
|
|
new-pid
|
|
|
|
|
behavior)]))
|
2015-12-04 15:40:02 +00:00
|
|
|
|
(w (enqueue-actions (postprocess w new-pid) new-pid remaining-initial-actions)))
|
2015-12-04 16:21:13 +00:00
|
|
|
|
(deliver-patches w new-mux new-pid delta delta-aggregate)))))
|
2015-12-03 20:53:07 +00:00
|
|
|
|
|
2015-12-04 16:21:13 +00:00
|
|
|
|
(define (deliver-patches w new-mux acting-label delta delta-aggregate)
|
|
|
|
|
(define-values (patches meta-action)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(compute-patches (dataspace-mux w) new-mux acting-label delta delta-aggregate))
|
|
|
|
|
(transition (for/fold [(w (struct-copy dataspace w [mux new-mux]))]
|
2015-12-04 16:21:13 +00:00
|
|
|
|
[(entry (in-list patches))]
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(match-define (cons label event) entry)
|
|
|
|
|
(send-event/guard event label w))
|
|
|
|
|
meta-action))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
|
|
|
|
|
(define (step-children w)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define runnable-pids (dataspace-runnable-pids w))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
(if (set-empty? runnable-pids)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
#f ;; dataspace is inert.
|
|
|
|
|
(transition (for/fold [(w (struct-copy dataspace w [runnable-pids (set)]))]
|
2015-03-04 14:45:16 +00:00
|
|
|
|
[(pid (in-set runnable-pids))]
|
|
|
|
|
(send-event #f pid w))
|
2015-03-02 16:10:11 +00:00
|
|
|
|
'())))
|
2015-03-04 14:45:16 +00:00
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(define (pretty-print-dataspace w [p (current-output-port)])
|
|
|
|
|
(match-define (dataspace mux qs runnable behaviors states) w)
|
|
|
|
|
(fprintf p "DATASPACE:\n")
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(fprintf p " - ~a queued actions\n" (queue-length qs))
|
|
|
|
|
(fprintf p " - ~a runnable pids ~a\n" (set-count runnable) (set->list runnable))
|
2015-12-11 02:21:24 +00:00
|
|
|
|
(fprintf p " - ~a live processes\n" (hash-count states))
|
|
|
|
|
(fprintf p " - ")
|
2016-04-01 23:53:46 +00:00
|
|
|
|
(display (indented-port-output 3 (lambda (p) (syndicate-pretty-print mux p)) #:first-line? #f) p)
|
2016-01-24 05:03:32 +00:00
|
|
|
|
(newline p)
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(for ([pid (set-union (hash-keys (mux-interest-table mux)) (hash-keys states))])
|
|
|
|
|
(fprintf p " ---- process ~a, behavior ~v, STATE:\n" pid (hash-ref behaviors pid #f))
|
|
|
|
|
(define state (hash-ref states pid #f))
|
2016-04-01 23:53:46 +00:00
|
|
|
|
(display (indented-port-output 6 (lambda (p) (syndicate-pretty-print state p))) p)
|
2015-03-16 14:38:32 +00:00
|
|
|
|
(newline p)
|
|
|
|
|
(fprintf p " process ~a, behavior ~v, CLAIMS:\n" pid (hash-ref behaviors pid #f))
|
|
|
|
|
(display (indented-port-output 6 (lambda (p)
|
2016-01-22 02:55:41 +00:00
|
|
|
|
(pretty-print-trie (mux-interests-of mux pid) p)))
|
2015-03-16 14:38:32 +00:00
|
|
|
|
p)
|
|
|
|
|
(newline p)))
|
|
|
|
|
|
2015-03-04 14:45:16 +00:00
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
(require racket/pretty)
|
|
|
|
|
|
|
|
|
|
(define (step* w)
|
|
|
|
|
(let loop ((w w) (actions '()))
|
|
|
|
|
(pretty-print w)
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(match (dataspace-handle-event #f w)
|
2015-11-17 19:01:04 +00:00
|
|
|
|
[#f (values w #f (flatten actions))]
|
|
|
|
|
[(quit exn new-actions) (values w exn (flatten (cons actions new-actions)))]
|
2015-03-04 14:45:16 +00:00
|
|
|
|
[(transition new-w new-actions) (loop new-w (cons actions new-actions))])))
|
|
|
|
|
|
2016-04-07 07:42:54 +00:00
|
|
|
|
(step* (make-dataspace '()))
|
2015-03-04 14:45:16 +00:00
|
|
|
|
)
|
2016-04-07 08:06:59 +00:00
|
|
|
|
|
|
|
|
|
;;; Local Variables:
|
|
|
|
|
;;; eval: (put 'match-event 'scheme-indent-function 1)
|
|
|
|
|
;;; eval: (put 'match-event 'racket-indent-function 1)
|
|
|
|
|
;;; End:
|