-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommunication.nls
140 lines (114 loc) · 4.24 KB
/
communication.nls
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
;;; File to be included in NetLogo Mutliagent Models
;;; Communication for NetLogo Multiagent models
;;; Includes primitives for message creation and handling in NetLogo
;;; Original Version for Netlogo 2 (2005) I. Sakellariou
;;; Adapted to NetLogo 4 (2008) I. Sakellariou
;;; Requirements
;;; All agents that are able to communicate MUST have a declated -own variable incoming-queue.
;;; This is the variable to which all messages are recorded. So, in your model if there is a breed of turtles
;;; which you desire to communicate, then you should have a BREED-own [incoming-queue] declaration (along with any other
;;; variables that you wish to include in your model.
;;; MAKE SURE that when you create the variables you set its values to empty list ([]).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;; COMMUNICATION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; MESSAGE PROCESSING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sending Messages
;; (One man's send is another man's receive..)
;; The second commented out line presents an alternative send implementation.
;; The commented out line represents an alternative method.
;; Problem: What if the agent I am sending the message is "killed"
;; Solution: Nothing Happens. Could yield an error message Alternative: create a safe send.
to send [msg]
let recipients get-receivers msg
let recv 0
foreach recipients [
set recv turtle (read-from-string ?)
if recv != nobody [without-interruption [ask recv [receive msg]]] ;; read-from-string is required to convert the string to number
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Message reception deals with updating incoming-queue
to receive [msg]
if show_messages [show msg]
set incoming-queue lput msg incoming-queue
end
;; This reporter returns the next message in the list and removes it from the queue.
to-report get-message
if empty? incoming-queue [report "no_message"]
let nextmsg first incoming-queue
remove-msg
report nextmsg
end
;; This reporter returns the next message in the list WITHOUT removimg it from the queue.
to-report get-message-no-remove
if empty? incoming-queue [report "no_message"]
report first incoming-queue
end
;; Explicit remove-msg.
;; This is needed since reporters *cannot* change a variable's values (apparently).
to remove-msg
set incoming-queue but-first incoming-queue
end
;; broadcasting to all agents of breed t-breed
to broadcast-to [t-breed msg]
foreach [who] of t-breed [
send add-receiver ? msg
]
end
;; Creating Messages and adding the sender.
to-report create-message [performative]
report (list performative (word "sender:" who) )
end
to-report create-reply [performative msg]
let msgOut 0
set msgOut create-message performative
set msgOut add-receiver (get-sender msg) msgOut
report msgOut
end
;; Accesing information on Messages
;; Reports the sender of a message
to-report get-sender [msg]
report remove "sender:" first (filter [not is-number? ? and member? "sender:" ?] msg)
;;report item ((position "sender:" msg) + 1) msg
end
;; Reports (returns) the content of a message
to-report get-content [msg]
report item (position "content:" msg + 1) msg
end
;; Reports the list of receivers of a message
to-report get-receivers [msg]
report map [remove "receiver:" ?] filter [not is-number? ? and member? "receiver:" ?] msg
end
;; reports the message performative.
to-report get-performative [msg]
report first msg
end
;;; ADDING FIELDS TO A MESSAGE
;; Adding a sender to a message.
to-report add-sender [sender msg]
report add msg "sender:" sender
end
;; add a receiver
to-report add-receiver [receiver msg]
report add msg "receiver:" receiver
end
;; adding multiple recipients
to-report add-multiple-receivers [receivers msg]
foreach receivers
[
set msg add-receiver ? msg
]
report msg
end
;; Adding content to a message
to-report add-content [content msg]
report add msg "content:" content
end
;; Primitive Add command
to-report add [msg field value]
ifelse field = "content:"
[report lput value lput field msg]
[report lput (word field value) msg]
end