;object-manager.rkt |
;对象管理器。 |
;每个对象包含属性标识聚集。 |
|
#lang racket |
|
(require "../arithmetic/guid.rkt") |
|
(provide |
make-object-manager) |
|
;对象散列表,每一个键值对代表一个对象。其: |
;键为对象标识值;值为对象属性标识值聚集。 |
(define objects (make-hash)) |
|
;界说对象管理器类: |
(define object-manager% |
(class object% |
(super-new) |
|
;创建对象: |
(define/public (create-object) |
(let ([id/o (create-guid-string)]) |
(hash-set! objects id/o (mutable-set)) |
id/o)) |
|
;销毁对象: |
(define/public (destroy-object id/o) |
(unless (hash-empty? objects) |
(hash-remove! objects id/o))) |
|
;取得对象散列表: |
(define/public (get-objects) |
objects) |
|
;取得属性聚集: |
(define/public (properties id/o) |
(hash-ref objects id/o (lambda () #f))) |
|
;添加属性: |
(define/public (add-property id/o id/p) |
(let ([ps (properties id/o)]) |
(when ps |
(set-add! (properties id/o) id/p)))) |
|
;移除属性: |
(define/public (remove-property id/o id/p) |
(let ([ps (properties id/o)]) |
(when ps |
(set-remove! (properties id/o) id/p)))) |
)) |
|
;创建对象管理器: |
(define (make-object-manager) |
(new object-manager%)) |
;property-manager.rkt |
;属性管理器。 |
;每个属性包含对象标识聚集。 |
|
#lang racket |
|
(provide make-property-manager) |
|
;属性散列表。其中: |
;键为属性标识值;值为对象标识聚集。 |
(define properties (make-hash)) |
|
;界说属性管理器类: |
(define property-manager% |
(class object% |
(super-new) |
|
;添加属性: |
(define/public (add-property id/p) |
(hash-set! properties id/p (mutable-set))) |
|
;删除属性: |
;删除属性需满足该属性没有对象需要。 |
(define/public (delete-property id/p) |
(when (set-empty? (objects id/p)) |
(hash-remove! properties id/p))) |
|
;取得对象聚集: |
(define/public (objects id/p) |
(hash-ref properties id/p (lambda () #f))) |
|
;添加对象: |
(define/public (add-object id/p id/o) |
(let ([os (objects id/p)]) |
(when os |
(set-add! os id/o)))) |
|
;移除对象: |
(define/public (remove-object id/p id/o) |
(let ([os (objects id/p)]) |
(when os |
(set-remove! os id/o))) |
(when (set-empty? (objects id/p)) |
(delete-property id/p))) |
)) |
|
;创建属性管理器对象: |
(define (make-property-manager) |
(new property-manager%)) |
;property-value-manager.rkt |
;属性值管理器。 |
|
#lang racket |
|
(require "../arithmetic/guid.rkt") |
|
(provide make-property-value-manager) |
|
;属性范例散列表,通过属性注册产生。其: |
;键为属性布局范例;值为属性值散列表。 |
;属性值散列表,通过添加产生,其键为属性标识值;值为属性值。 |
(define types (make-hash)) |
|
;界说属性值管理器类: |
(define property-value-manager% |
(class object% |
(super-new) |
|
;注册属性范例: |
(define/public (regist-type type) |
(hash-set! types type (make-hash))) |
|
;取得指定范例属性值散列表: |
(define/public (get-values type) |
(hash-ref types type (lambda () #f))) |
|
;添加属性值: |
;如果指定属性范例不存在,则注册该属性范例。 |
(define/public (add-value type value) |
(unless (has-type? type) |
(regist-type type)) |
(let ([values (get-values type)] |
[id/p (create-guid-string)]) |
(hash-set! values id/p value) |
id/p)) |
|
;销毁属性值: |
(define/public (destroy-value type id/p) |
(let ([values (get-values type)]) |
(unless (hash-empty? values) |
(hash-remove! values id/p)))) |
|
;取得属性值: |
(define/public (get-value type id/p) |
(let ([values (get-values type)]) |
(if values |
(hash-ref values id/p (lambda () #f)) |
#f))) |
|
;设置属性值: |
(define/public (set-value type id/p value) |
(let ([values (get-values type)]) |
(hash-set! values id/p value))) |
|
;查找属性值: |
;如果找到,返回属性id,否则返回#f. |
(define/public (find-value type value) |
(let ([values (get-values type)] |
[id/p #f]) |
(when values |
(hash-for-each |
values |
(lambda (k v) |
(when (equal? v value) |
(set! id/p k))))) |
id/p)) |
|
;判定属性是否为指定属性范例? |
(define/public (is-type? type id/p) |
(hash-has-key? (get-values type) id/p)) |
|
;判定是否存在指定属性范例? |
(define/public (has-type? type) |
(hash-has-key? types type)) |
)) |
|
;创建属性值管理器: |
(define (make-property-value-manager) |
(new property-value-manager%)) |
;action-manager.rkt |
;行为管理器。负责维护已注册的行为(action)。 |
|
#lang racket |
|
(require "../arithmetic/guid.rkt") |
|
(provide make-action-manager) |
|
;行为散列表。其: |
;键为行为类标识;值为行为类对象值。 |
(define actions (make-hash)) |
|
;界说行为管理器类: |
(define action-manager% |
(class object% |
(super-new) |
|
;注册行为: |
(define/public (regist-action type) |
(hash-set! actions type (new type))) |
|
;取得行为散列表: |
(define/public (get-actions) |
actions) |
|
;取得行为: |
(define/public (get-action type) |
(hash-ref actions type (lambda () #f))) |
|
;取得行为包含的对象聚集: |
(define/public (get-objects type) |
(let ([a (get-action type)]) |
(when a |
(send a get-objects)))) |
|
;添加对象: |
(define/public (add-object type id/o) |
(let ([a (get-action type)]) |
(when a |
(send a add-object id/o)))) |
|
;移除对象: |
(define/public (remove-object id/o) |
(hash-for-each |
actions |
(lambda (k v) |
(send v remove-object id/o)))) |
|
;判定范例是否存在: |
(define/public (has-type? type) |
(hash-has-key? actions type)) |
)) |
|
;创建行为管理器: |
(define (make-action-manager) |
(new action-manager%)) |
;coordinator.rkt |
;调和器。 |
;负责完成对象管理器、属性管理器、属性值管理器、行为管理器之间交叉的行为, |
;制止模块之间的交叉引用。 |
|
#lang racket |
|
(require "manager/object-manager.rkt" |
"manager/property-manager.rkt" |
"manager/property-value-manager.rkt" |
"manager/action-manager.rkt") |
|
(provide make-coordinator) |
|
;界说调和器类: |
(define coordinator% |
(class object% |
(super-new) |
|
(field |
[manager/o (make-object-manager)] |
[manager/p (make-property-manager)] |
[manager/pv (make-property-value-manager)] |
[manager/a (make-action-manager)]) |
|
;对象相关方法:----------------------------------------- |
;创建对象: |
(define/public (create-object) |
(send manager/o create-object)) |
|
;销毁对象: |
(define/public (destroy-object id/o) |
(send manager/o destroy-object id/o) |
(send manager/p remove-object id/o) |
(send manager/a remove-object id/o)) |
|
;取得对象散列表: |
(define/public (get-objects) |
(send manager/o get-objects)) |
|
;属性相关方法:------------------------------------------- |
;注册属性: |
(define/public (regist-property type) |
(unless (send manager/pv has-type? type) |
(send manager/pv regist-property type))) |
|
;添加属性: |
;如果value在属性列表中不存在,则添加。 |
(define/public (add-property id/o type value) |
(let ([id/p (send manager/pv find-value type value)]) |
(unless id/p |
(set! id/p (send manager/pv add-value type value)) |
(send manager/p add-property id/p)) |
(send manager/o add-property id/o id/p) |
(send manager/p add-object id/p id/o))) |
|
;移除属性: |
(define/public (remove-property id/o id/p) |
(send manager/o remove-property id/o id/p) |
(send manager/p remove-object id/p id/o)) |
|
;取得属性: |
(define/public (get-property id/o type) |
(let ([id/p #f]) |
(for ([id (send manager/o properties id/o)]) |
#:break (not (send manager/pv is-type? type id)) |
(set! id/p id)) |
id/p)) |
|
;取得属性值: |
(define/public (get-property-value type id/p) |
(send manager/pv get-value type id/p)) |
|
;更新属性: |
(define/public (update-property type id/o id/p value) |
(let ([id/v (send manager/pv find-value type value)] |
[n (set-count (send manager/p objects id/p))]) |
(cond |
;给定属性值已经存在,将该存在的属性设置给对象: |
[id/v |
(send manager/p remove-object id/p id/o) |
(send manager/p add-object id/v id/o) |
(send manager/o remove-property id/o id/p) |
(send manager/o add-property id/o id/v)] |
;属性仅属于一个对象,仅改变属性值: |
[(= n 1) |
(send manager/pv set-value type id/p value)] |
;属性属于多个对象,新创建属性给对象: |
[(> n 1) |
(let ([id/v (send manager/pv add-value type value)]) |
(remove-property id/o id/p) |
(send manager/p register-property id/v) |
(send manager/p add-object id/v id/o) |
(send manager/o add-property id/o id/p))]))) |
|
;行为相关方法:-------------------------------------------- |
;注册行为: |
(define/public (regist-action type) |
(unless (send manager/a has-type? type) |
(send manager/a regist-action type))) |
|
;添加行为: |
(define/public (add-action id/o type) |
(send manager/a add-object type id/o)) |
|
;取得行为散列表: |
(define/public (get-actions) |
(send manager/a get-actions)) |
|
;取得行为: |
(define/public (get-action type) |
(let ([as (get-actions)]) |
(if (hash-empty? as) |
#f |
(hash-ref as type)))) |
)) |
|
;创建调和器对象: |
(define (make-coordinator) |
(new coordinator%)) |