More structured FID representation.

This commit is contained in:
Tony Garnock-Jones 2018-04-08 07:58:17 +01:00
parent 3c70496688
commit fe6b895d8a
1 changed files with 41 additions and 36 deletions

View File

@ -13,14 +13,18 @@
(require "pattern.rkt") (require "pattern.rkt")
(require "bag.rkt") (require "bag.rkt")
;; An `ActorID` uniquely identifies an actor in a `Dataspace`.
;;
;; A `FID` is a Facet ID, uniquely identifying both a facet and its ;; A `FID` is a Facet ID, uniquely identifying both a facet and its
;; ancestry in a `Dataspace`. ;; ancestry in a `Dataspace`.
;; ;;
;; FID = (Listof Nat) ;; FID = (facet-id ActorID (Listof Nat))
;; ;;
;; The first `Nat` in the list is the unique identifier for this (struct facet-id (actor path) #:prefab)
;; facet; the `cdr` of the list is the FID of its parent facet. No two ;;
;; `FID`s in a `Dataspace` have the same first `Nat`. ;; The first `Nat` in `path` is the unique identifier for this facet;
;; the `cdr` of the list is the `path` its parent facet. No two `FID`s
;; in a `Dataspace` have the same first `Nat` in their `path`.
;; A `Dataspace` is a ... TODO ;; A `Dataspace` is a ... TODO
@ -33,7 +37,7 @@
(struct dataspace ([next-id #:mutable] ;; Nat (struct dataspace ([next-id #:mutable] ;; Nat
routing-table ;; Skeleton routing-table ;; Skeleton
facets ;; (MutableHash FID Facet) facets ;; (MutableHash FID Facet)
actors ;; (MutableHash FID Any) ;; maps FID to actor name actors ;; (MutableHash ActorID Any) ;; maps ActorID to actor name
assertions ;; (Bagof Assertion) assertions ;; (Bagof Assertion)
dataflow ;; DataflowGraph dataflow ;; DataflowGraph
pending-scripts ;; (MutableVectorof (Queueof (-> Any))) pending-scripts ;; (MutableVectorof (Queueof (-> Any)))
@ -41,7 +45,7 @@
) )
#:transparent) #:transparent)
(struct facet (id ;; FID (struct facet (fid ;; FID
endpoints ;; (MutableHash EID Endpoint) endpoints ;; (MutableHash EID Endpoint)
[stop-scripts #:mutable] ;; (Listof Script) -- IN REVERSE ORDER [stop-scripts #:mutable] ;; (Listof Script) -- IN REVERSE ORDER
[children #:mutable] ;; (Setof FID) [children #:mutable] ;; (Setof FID)
@ -59,7 +63,7 @@
;; it on access. ;; it on access.
(struct field-handle (name ;; Symbol (struct field-handle (name ;; Symbol
id ;; Nat id ;; Nat
owner ;; FID owner ;; ActorID
[value #:mutable] ;; Any [value #:mutable] ;; Any
) )
#:methods gen:custom-write #:methods gen:custom-write
@ -135,36 +139,38 @@
(set-dataspace-next-id! ds (+ id 1)) (set-dataspace-next-id! ds (+ id 1))
id)) id))
;; TODO: remove??
(define (fid-parent fid) (define (fid-parent fid)
(cdr fid)) (match-define (facet-id actor (cons _ path)) fid)
(facet-id actor path))
(define (generate-fid! ds parent-fid) (define (generate-fid! ds parent-fid)
(cons (generate-id! ds) parent-fid)) (match-define (facet-id actor path) parent-fid)
(facet-id actor (cons (generate-id! ds) path)))
(define (actor-fid? fid)
(null? (fid-parent fid)))
(define (fid->actor-fid fid)
(if (actor-fid? fid)
fid
(fid->actor-fid (fid-parent fid))))
(define (fid-ancestor? fid maybe-ancestor) (define (fid-ancestor? fid maybe-ancestor)
(and (pair? fid) ;; empty fid lists obviously no ancestors at all! (and (equal? (facet-id-actor fid) (facet-id-actor maybe-ancestor))
(or (equal? fid maybe-ancestor) (let ((b-path (facet-id-path maybe-ancestor)))
(fid-ancestor? (cdr fid) maybe-ancestor)))) (let walk ((a-path (facet-id-path fid)))
(and (pair? a-path) ;; empty fid lists obviously no ancestors at all!
(or (equal? a-path b-path)
(walk (cdr a-path))))))))
(define (has-parent? fid)
(pair? (cdr (facet-id-path fid))))
(define (add-actor! ds name boot-proc initial-assertions) (define (add-actor! ds name boot-proc initial-assertions)
(define actor-fid (generate-fid! ds '())) (define actor-id (generate-id! ds))
(hash-set! (dataspace-actors ds) actor-fid name) (define root-fid (facet-id actor-id (list actor-id))) ;; TODO: bad convention. Use a fresh Nat, don't reuse actor-id in the path
(hash-set! (dataspace-actors ds) actor-id name)
(for [(a initial-assertions)] (for [(a initial-assertions)]
(match (bag-change! (dataspace-assertions ds) a 1) (match (bag-change! (dataspace-assertions ds) a 1)
['absent->present (add-assertion! (dataspace-routing-table ds) a)] ['absent->present (add-assertion! (dataspace-routing-table ds) a)]
;; 'absent->absent and 'present->absent absurd ;; 'absent->absent and 'present->absent absurd
['present->present (void)])) ;; i.e. no visible change ['present->present (void)])) ;; i.e. no visible change
(add-facet! ds #f actor-fid (lambda () (add-facet! ds #f root-fid (lambda ()
(boot-proc) (boot-proc)
(for [(a initial-assertions)] (dataspace-retract! ds a))))) (for [(a initial-assertions)] (dataspace-retract! ds a)))))
(define (lookup-facet ds fid) (define (lookup-facet ds fid)
(hash-ref (dataspace-facets ds) fid #f)) (hash-ref (dataspace-facets ds) fid #f))
@ -176,7 +182,7 @@
(current-facet-id fid) (current-facet-id fid)
(in-script? script?)) (in-script? script?))
(with-handlers ([(lambda (e) (not (exn:break? e))) (with-handlers ([(lambda (e) (not (exn:break? e)))
(lambda (e) (terminate-actor! ds (fid->actor-fid fid)))]) ;; TODO: tracing (lambda (e) (terminate-actor! ds (facet-id-actor fid)))]) ;; TODO: tracing
body ... body ...
(void))))) (void)))))
@ -258,14 +264,15 @@
'() '()
(set))) (set)))
(hash-set! (dataspace-facets ds) fid f) (hash-set! (dataspace-facets ds) fid f)
(when (pair? parent-fid) (when (has-parent? fid)
(define pf (lookup-facet ds parent-fid)) (define pf (lookup-facet ds parent-fid))
(when pf (set-facet-children! pf (set-add (facet-children pf) fid)))) (when pf (set-facet-children! pf (set-add (facet-children pf) fid))))
(with-current-facet [ds fid #f] (with-current-facet [ds fid #f]
(boot-proc)) (boot-proc))
(schedule-script!* ds (lambda () (schedule-script!* ds (lambda ()
(when (and (facet-live? ds fid) (when (and (facet-live? ds fid)
(or (and (pair? parent-fid) (not (facet-live? ds parent-fid))) (or (and (has-parent? fid)
(not (facet-live? ds parent-fid)))
(facet-live-but-inert? ds fid))) (facet-live-but-inert? ds fid)))
(terminate-facet! ds fid))))) (terminate-facet! ds fid)))))
@ -295,11 +302,9 @@
(when h (dataspace-unsubscribe! ds h)))))) (when h (dataspace-unsubscribe! ds h))))))
;; Abruptly terminates an entire actor, without running stop-scripts etc. ;; Abruptly terminates an entire actor, without running stop-scripts etc.
(define (terminate-actor! ds actor-fid) (define (terminate-actor! ds actor-id)
(when (not (actor-fid? actor-fid)) (hash-remove! (dataspace-actors ds) actor-id)
(error 'terminate-actor! "Attempt to terminate non-actor FID ~a" actor-fid)) (let abort-facet! ((fid (facet-id actor-id (list actor-id)))) ;; TODO: ugh
(hash-remove! (dataspace-actors ds) actor-fid)
(let abort-facet! ((fid actor-fid))
(define f (lookup-facet ds fid)) (define f (lookup-facet ds fid))
(when f (when f
(hash-remove! (dataspace-facets ds) fid) (hash-remove! (dataspace-facets ds) fid)
@ -312,7 +317,7 @@
(when f (when f
(define parent-fid (fid-parent fid)) (define parent-fid (fid-parent fid))
(when (pair? parent-fid) (when (has-parent? fid)
(define pf (lookup-facet ds parent-fid)) (define pf (lookup-facet ds parent-fid))
(when pf (set-facet-children! pf (set-remove (facet-children pf) fid)))) (when pf (set-facet-children! pf (set-remove (facet-children pf) fid))))
@ -335,7 +340,7 @@
#:priority *gc-priority* #:priority *gc-priority*
ds ds
(lambda () (lambda ()
(if (pair? parent-fid) (if (has-parent? fid)
(when (facet-live-but-inert? ds parent-fid) (terminate-facet! ds parent-fid)) (when (facet-live-but-inert? ds parent-fid) (terminate-facet! ds parent-fid))
(terminate-actor! ds fid)))))) (terminate-actor! ds fid))))))
@ -415,7 +420,7 @@
(lambda () (lambda ()
(define current-value (field-handle 'current-value (define current-value (field-handle 'current-value
(generate-id! (current-dataspace)) (generate-id! (current-dataspace))
(fid->actor-fid (current-facet-id)) (facet-id-actor (current-facet-id))
0)) 0))
(add-endpoint! (current-dataspace) (add-endpoint! (current-dataspace)
'stop-when-ten 'stop-when-ten