-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrserve.dyalog
409 lines (370 loc) · 16.4 KB
/
rserve.dyalog
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
:Namespace RS
∇ r←init
wf←⊃⎕NPARTS #.RS{⍺{0=≢⍵:⍺.SALT_Data.SourceFile ⋄ ⍵}' '~⍨⊃⍵{⍵[⍸(⊂⍺){∨/⍺⍷⍵}¨⍵]}⊃¨5176⌶⍬}'rserve.dyalog'
#.settings←⎕JSON⊃⎕NGET wf,'settings.json'
∇
∇ r←start;a;o
:Access public
mac win bit64←∨/¨'Mac' 'Windows' '64'⍷¨⊂⊃'.'⎕WG'APLVersion'
wf←⊃⎕NPARTS #.RS{⍺{0=≢⍵:⍺.SALT_Data.SourceFile ⋄ ⍵}' '~⍨⊃⍵{⍵[⍸(⊂⍺){∨/⍺⍷⍵}¨⍵]}⊃¨5176⌶⍬}'rserve.dyalog'
:If 0=⎕NC'#.settings' ⋄ init ⋄ :EndIf
:If (mac⍱win) ⍝ linux
:If 0=≢⎕SH'pidof Rserve;exit 0'
o←⊂'library(Rserve)'
o,←⊂'Rserve(args="--no-save --RS-port ',(⍕#.settings.rserve.port),' --RS-conf ',wf,'Rserv.conf >~/Rserve.log")'
(⊂o)⎕NPUT(wf,'rserve.r')1
⎕SH'R CMD BATCH ',wf,'rserve.r'
:EndIf
:ElseIf win
:If 2>≢⎕CMD'tasklist /FI "ImageName eq Rserve.exe"'
⍝ a←⎕CMD'taskkill /IM Rserve.exe /F'
o←'Rserve(args="--no-save --slave --RS-workdir ',(('\\'⎕R'\\\\')wf),' --RS-port ',(⍕#.settings.rserve.port),'")'
a←⎕CMD(('/'⎕R'\\')'"',#.settings.r.home,'R.exe" CMD ',#.settings.rserve.home,o)'Hidden'
:EndIf
:ElseIf mac
∘ ⍝ my macbook is broken
:EndIf
∇
∇ r←kill
:Access public
mac win bit64←∨/¨'Mac' 'Windows' '64'⍷¨⊂⊃'.'⎕WG'APLVersion'
:Trap 0
:If bit64
⎕SH'kill -9 ',⍕⎕SH'pidof Rserve'
:ElseIf win
:If win
:If ⍬≢process
process.Kill''
:Else
a←⎕CMD'taskkill /IM Rserve.exe /F'
:EndIf
:EndIf
:EndIf
:EndTrap
∇
:class robject
⎕ML←1 ⋄ ⎕IO←1 ⋄ ⎕PP←34
:Field Public data←⍬
:field private _a←⊂''
:field private ga←⍬
:property keyed attributes
:access public
∇ r←Get args
:If args.IndexersSpecified
r←{0=≢⍵:⊂'' ⋄ ⍵[ga⍳⊃args.Indexers]}_a
:Else
⍝ r←{0=≢⍵:,⊂'' ⋄ ga{b←⍸~_a∊⊂'' ⋄ ⍺[b]{⍺ ⍵}¨⍵[b]}⍵}_a
r←(~_a∊⊂'')/ga
:EndIf
∇
∇ Set args
:If (⊂'')≡_a ⋄ _a←(⊂'')⍴⍨≢ga ⋄ :EndIf
_a[ga⍳⊃args.Indexers]←args.NewValue
∇
:endproperty
:property default value
:Access Public
∇ r←Get args
r←{1=≢data:,⍵ ⋄ ⍵}{attributes[⊂'class']≡⊂'factor':⍉attributes[⊂'levels'][⍵]
∨/({1=≡⊃⍵:⍵ ⋄ ⊃⍵}attributes[⊂'class'])∊⊂'table':⍵{~⊃attributes[⊂'dim']≡⊂'':⍵⍪⍉(⌽⊃attributes[⊂'dim'])⍴⍺ ⋄ ⍵,[0.5]⍺}⊃attributes[⊂'names']{⍺≢⊂'':⍺ ⋄ ⍵}attributes[⊂'dimnames']
~attributes[⊂'dim']≡⊂'':⍉(⌽⊃attributes[⊂'dim'])⍴⍵
attributes[⊂'class']≡⊂'ts':(↑(⍴⍵)↑,↑{(⊂⍳⍵[3]){⍵,¨⍺}¨⍵[1],⍵[1]+⍳⌊(⍵[2]-⍵[1])}⊃attributes[⊂'tsp']),⍵
∨/({1=≡⊃⍵:⍵ ⋄ ⊃⍵}attributes[⊂'class'])∊⊂'data.frame':(⊃attributes[⊂'names']){0≠⍴⍺:⍺⍪⍵ ⋄ ⍵}⍉↑{⎕IO←1 ⋄ 0::⍵ ⋄ (⊃⍵.attributes[⊂'levels'])[⍵.data]}¨{1=≡⍵:⊂⍵ ⋄ ⍵}⍵
}data
∇
:endproperty
∇ Make2 arg
:Access Public
:Implements Constructor
ga←#.settings.r.attributes
attributes[⊃⊃arg]←2⊃⊃arg
data←{1=≢⍵:,↑⍵ ⋄ ⍵}2⊃arg
∇
∇ Make0
:Access Public
:Implements Constructor
ga←#.settings.r.attributes
∇
:endclass
:Class Rserve
⍝ Interface from Dyalog APL to RServe
⍝ Currently asssumes existence of #.Conga
⎕ML←1 ⋄ ⎕IO←0 ⋄ ⎕PP←34
:Field Public CLT←'' ⍝ Public for debugging
:Field Private LittleEndian
:field private error←⍬
:field private command←⍬
:field private type←⍬
:field private sexp←⍬
:field private ga←⍬
:field private mac←0
:field private win←0
:field private bit64←0
:field private process←⍬
:field private timeout←⍬
prop←{83=⎕dr ⍵:⊂(⍺.name,⊂⍬)[⍺.code⍳⍵] ⋄ (⍺.code,0)[⍺.name⍳⊃⍵]}
:property keyed ERR
:access public
∇ r←get args
r←error prop args.Indexers
∇
:endproperty
:property keyed XT
:access public
∇ r←get args
r←sexp prop args.Indexers
∇
:endproperty
:property keyed CMD
:access public
∇ r←get args
r←command prop args.Indexers
∇
:endproperty
:property keyed DT
:access public
∇ r←get args
r←type prop args.Indexers
∇
:endproperty
split←{((≢⍵)⍴⍺↑1)⊂⍵} ⍝ thanks VMJ for pimping my code
shape←{(⍺,⍨(≢⍵)÷⍺)⍴⍵}
IntToBytes←{⎕FR←(⍺=8)⊃645 1287 ⋄ ⍺↑⎕UCS 80 ⎕DR(×⍵)×((2*(8×⍺))-1)⌊|⍵}
b2i←{(⍺⍴256)⊥⌽⍵}
ld←{⍵,⍨3 IntToBytes≢⍵}
evalOut←{0,⍨DT[⊂'STRING'],ld{⍵↑⍨4×⌈(≢⍵)÷4}10,⍨⎕UCS ⍵}
strOut←{DT[⊂'STRING'],ld{⍵↑⍨4×⌈(≢⍵)÷4}0,⍨⎕UCS ⍵}
∇ o←{void}eval s;b;d;dh;hdr;r;s;t;xt;z
:Access Public
s←{1=≡⍵:,⊂⍵ ⋄ ⍵}s
b←{CMD[⊂{⍵:{⍵≡0:'voidEval' ⋄ 'eval'}void ⋄ 'eval'}2=⎕NC'void']{z←DRC.Send CLT(∊{4 IntToBytes ⍵}¨⍺(≢⍵)0 0) ⋄ SendWait ⍵}evalOut ⍵}¨s
o←{1=≢⍵:⊃⍵ ⋄ ⍵}{(⍵≡,⊂⍬)∨('Err'≡3↑⍵)∨(1=≡⍵)∨1=≢∪¯1↑¨⍕¨⎕DR¨⍵:⍵ ⋄ object ⍵}¨decode¨b
:If 0=≢o ⋄ ⎕EX'o' ⋄ :EndIf
∇
∇ o←object i;attr;b;data;in;xt
attr←(⊂'')⍴⍨≢ga ⋄ data←⍬ ⋄ xt←⊃i ⋄ i←1↓i
:If xt∊128+XT['ARRAY_INT' 'ARRAY_DOUBLE' 'ARRAY_STR'] ⋄ i←⊃i ⋄ :EndIf
:For in :In i
:If (xt=128+XT[⊂'VECTOR'])
:AndIf 0>≡in
:AndIf ∨/{⍵≡⊃⊃in}¨128+XT['ARRAY_INT' 'ARRAY_DOUBLE']
data,←object in
in←⍬
:EndIf
:While 0≠≢in
:If (1<|≡in)∨(1<≢in)
:If ∨/b←ga∊in[1]
attr[⍸b]←⊂(⊃in){'row.names'≡⍵:{¯2147483648≡⊃⍵:1+⍳|1⊃⍵ ⋄ ⍵}⍺ ⋄ ⍺}1⊃in
in←2↓in
:Else
data,←{xt∊128+XT[⊂'ARRAY_STR']:⍵ ⋄ ⊂⍵}in ⋄ in←⍬
:EndIf
:Else
data,←in ⋄ in←⍬
:EndIf
:EndWhile
:EndFor
o←⎕NEW robject((ga attr)data)
∇
∇ o←decode i;h;s;xt
o←⍬ ⋄ h←16↑i ⋄ i←16↓i
:If 1≠⊃h
o←('Error '),⍕{⍬≡⊃⍵:2⊃h ⋄ ⍵}⊃ERR[3⊃h] ⋄ →0
:EndIf
:While 0≠≢i
o,←(⊃i)SEXPin(4↓i)↑⍨l←3 b2i i[1 2 3] ⋄ i←(4+l)↓i
:End
:If 1=⍴o ⋄ o←⊃o ⋄ :EndIf
∇
∇ o←s set i;o;d
:Access public
i←{(83=⎕DR ⍵)∧0=⍴⍴i:,⍵ ⋄ ⍵}i ⍝ atom
o←⍬ ⋄ (s i)←s{((2>≡⍵)∧326≠⎕DR ⍵)∨((1=≢⍵)∧326=⎕DR ⍵):(,⊂⍺)(,⊂⍵) ⋄ ⍺ ⍵}i
d←s{total←{326=⎕DR ⍵:≢⍵.data ⋄ 0}⍵ ⋄ (strOut ⍺),DT[⊂'SEXP'],ld(SEXPout ⍵)}¨i
o←⊃¨{z←DRC.Send CLT(∊({4 IntToBytes ⍵}¨CMD[⊂'setSEXP'],(≢⍵)0 0))
SendWait ⍵}¨d
∇
∇ o←{ty}SEXPout i;a;at;b;d;dim;dt;in;ts;ty
:If 0=⎕NC'ty' ⋄ ty←⎕DR i ⋄ :EndIf
:Select ty
:Case 80
ty←XT[⊂'ARRAY_STR']
:CaseList 83 163 323
ty←XT[⊂'ARRAY_INT']
:Case 645
ty←XT[⊂'ARRAY_DOUBLE']
:Case 326
:If 80≡⊃∪⎕DR¨i
ty←XT[⊂'ARRAY_STR']
:EndIf
:EndSelect
:Select ty
:CaseList XT['SYMNAME' 'ARRAY_STR']
o←ty,ld{⍵,1⍴⍨4-4|≢⍵}∊{0,⍨⎕UCS ⍵}¨i
:CaseList XT[⊂'ARRAY_INT']
:If 1=≢⍴i ⍝ vector
o←XT[⊂'ARRAY_INT'],ld ⎕UCS 80 ⎕DR⊃0 323 ⎕DR∊{1=≢⍵:⊃⍵ ⋄ ⍵}i
:Else ⍝ matrix
dim←⌽⍴i ⋄ d←⎕UCS 80 ⎕DR⊃0 323 ⎕DR∊i
li←XT[⊂'SYMNAME'],1↓strOut'dim'
li←li,⍨XT[⊂'ARRAY_STR'],ld∊4 IntToBytes¨dim
li←li,⍨XT[⊂'LIST_TAG'],3 IntToBytes≢li
o←(128+XT[⊂'ARRAY_INT']),ld li,d
:EndIf
:Case 326 ⍝ robjects
ts←∨/i.attributes[⊂'class']∊⊂'ts'
:If ts
d←XT[⊂'ARRAY_DOUBLE']SEXPout¨{1=≡⍵:,⊂⍵ ⋄ ⍵}i.data
:Else
d←SEXPout¨{1=≡⍵:,⊂⍵ ⋄ ⍵}i.data
:EndIf
at←↓{⍵,[0.5]i.attributes[⍵]}i.attributes[]
:If (⊂'')≢i.attributes[⊂'row.names']
:AndIf {⎕IO←1 ⋄ ⍵≡⍳¯1↑⍵}⊃i.attributes[⊂'row.names']
b←¯1↑⊃i.attributes[⊂'row.names']
at[{⍵⍳⊂'row.names'}0⊃¨at]←⊂(⊂'row.names'),⊂¯2147483648 b
:EndIf
a←∊{{(⊃⍵)SEXPout(1↓⍵)}¨({
(XT['ARRAY_INT' 'ARRAY_DOUBLE' 'ARRAY_STR']⊃⍨323 645 80⍳⎕DR∊⍵),{
1=≡⍵:⊂⍵ ⋄ ⍵}⍵}1⊃⍵)(XT[⊂'SYMNAME'],⍵[0])}¨at
:If (⍴d)<total
d←∊d ⋄ dt←⊃d
o←(128+dt),ld(4↓d),⍨XT[⊂'LIST_TAG'],ld a
:Else
o←(128+XT[⊂'VECTOR']),ld(∊d),⍨XT[⊂'LIST_TAG'],ld a
:EndIf
:CaseList XT[⊂'ARRAY_DOUBLE']
:If 1=≢⍴i
o←XT[⊂'ARRAY_DOUBLE'],ld ⎕UCS 80 ⎕DR{645≠⎕DR ⍵:⊃0 645 ⎕DR ⍵ ⋄ ⍵}∊i
:Else
dim←⌽⍴i ⋄ d←⎕UCS 80 ⎕DR∊i
li←XT[⊂'SYMNAME'],1↓strOut'dim'
li←li,⍨XT[⊂'ARRAY_INT'],ld∊4 IntToBytes¨dim
li←li,⍨XT[⊂'LIST_TAG'],3 IntToBytes≢li
o←(128+XT[⊂'ARRAY_DOUBLE']),ld li,d
:End
:Else
∘
:EndSelect
∇
∇ o←t SEXPin i;a;b;class;d;dh;dim;dt;hdr;ii;in;levels;names;out;row;s;save;st;ty;xt;bb
d←⍬ ⋄ o←t{(⍺≡10)∧0≡+/⍵:⎕NULL ⋄ ⍬}i
:While (0≠≢i)∧(0≠+/i)
xt←⊃i ⋄ s←(3 b2i i[1 2 3]) ⋄ i←4↓i ⋄ ii←s↑i
:If 0=≢i ⋄ o←1 ⋄ →0 ⋄ :EndIf
:Select xt
:Case 0
:Case XT[⊂'VECTOR']
d←⊃xt SEXPin ii
:Case XT[⊂'SYMNAME']
d←⎕UCS{⍵/⍨(~⍵∊1)∧⌽∨\~0∊⍨⌽⍵}ii
:CaseList XT['LIST_TAG' 'LANG_TAG']
d←xt SEXPin ii
:If t∊128+XT['ARRAY_INT' 'ARRAY_DOUBLE']
(a b)←(t-160)⊃(323 4)(645 8)
d←d(∊a ⎕DR ⎕UCS b shape s↓i) ⋄ s←≢i
:ElseIf t∊128+XT[⊂'ARRAY_STR']
d←d(⎕UCS¨{⍵⊆⍨~⍵∊0}{⍵↓⍨-1+0⍳⍨⌽⍵}s↓i) ⋄ s←≢i
:EndIf
:Case XT[⊂'CLOS']
d←xt SEXPin ii
:Case XT[⊂'LANG_NOTAG']
d←xt SEXPin ii
:CaseList XT[⊂'UNKNOWN'],128+XT[⊂'UNKNOWN']
→0
:Case XT[⊂'ARRAY_INT']
d←∊323 ⎕DR ⎕UCS 4 shape ii
:Case XT[⊂'ARRAY_DOUBLE']
:Trap 11
a←8 shape ii
d←∊645 ⎕DR ⎕UCS a
:Else
b←(0 0 0 0 0 0 240 127)⍷a ⍝ infinity
b∨←(0 0 0 0 0 0 240 255)⍷a ⍝ -infinity
b∨←(0 0 0 0 0 0 248 127)⍷a ⍝ NaN
b∨←(162 7 0 0 0 0 240 127)⍷a ⍝ NA_real_
bb←∨/b
d←∊645 ⎕DR ⎕UCS(~bb)⌿a
d←(~bb)\d ⋄ (bb/d)←⎕NULL
:End
:Case XT[⊂'ARRAY_STR']
d←{1=≢⍵:⊃⍵ ⋄ ⍵}⎕UCS¨{⍵⊆⍨~⍵∊0}{⍵↓⍨-1+0⍳⍨⌽⍵}ii
:Case XT[⊂'ARRAY_BOOL'] ⍝ logical
d←4↓ii~255 ⋄ ((d∊2)/d)←⎕NULL ⍝ NA
:Case XT[⊂'ARRAY_CPLX']
:Trap 11
a←8 shape ii
d←∊645 ⎕DR ⎕UCS a
:Else
b←(0 0 0 0 0 0 240 127)⍷a ⍝ infinity
b∨←(0 0 0 0 0 0 240 255)⍷a ⍝ -infinity
b∨←(0 0 0 0 0 0 248 127)⍷a ⍝ NaN
b∨←(162 7 0 0 0 0 240 127)⍷a ⍝ NA_real_/NA_complex_
bb←∨/b
d←{a←2÷⍨≢⍵ ⋄ (a↑⍵)+(a↓⍵)ׯ1*0.5}∊645 ⎕DR ⎕UCS(~bb)⌿a
d←(~bb)\d ⋄ (bb/d)←⎕NULL
:End
:Case 128 ⋄ →0
:CaseList 128+XT['S4' 'VECTOR' 'ARRAY_INT' 'ARRAY_DOUBLE' 'ARRAY_STR']
d←xt,(xt SEXPin ii)
:Else
∘
:EndSelect
:If 0≠≢d ⋄ o,←⊂d ⋄ i←s↓i ⋄ :EndIf
:EndWhile
∇
∇ o←SendWait d;z;done;length
:If 0≠0⊃z←DRC.Send CLT d
('Send failed: ',,⍕z)⎕SIGNAL 11
:EndIf
o←⍬ ⋄ done←0 ⋄ length←¯1
:Repeat
:If 0=0⊃z←DRC.Wait CLT timeout
d←3⊃z
:If length=¯1 ⍝ First block
length←16+4 b2i d[4+⍳4]
o←d
:Else
o,←d
:EndIf
done←length≤⍴o
:Else
done←1
o←z
:EndIf
:Until done
⍝ KSL 1.01
∇
∇ Make;a;b;f;h;out;p;r;rc;step;z;si;c;wf
:Access public
:Implements constructor
:If 0=⎕NC'#.settings'
RS.init
:EndIf
error←#.settings.r.error ⋄ command←#.settings.r.command
sexp←#.settings.r.sexp ⋄ type←#.settings.r.type
ga←#.settings.r.attributes
timeout←#.settings.rserve.timeout
:If 0=⎕NC'RS.DRC'
:If 0=⎕NC'#.Conga' ⋄ 'Conga'#.⎕CY'conga' ⋄ :EndIf
DRC←#.Conga.Init''
:EndIf
:If 0=0⊃z←DRC.Clt''#.settings.rserve.address #.settings.rserve.port'Raw'⊣step←'Connection'
CLT←1⊃z
:AndIf 0=0⊃z←DRC.Wait CLT⊣step←'Wait for confirmation'
:AndIf 32=≢h←⎕UCS 3⊃z⊣step←'Check R header'
:AndIf 'Rsrv'≡4↑h⊣step←'Check R header for protocol'
:AndIf 'QAP1'≡h[8+⍳4]⊣step←'Check R header for transfer protocol'
⍝ Then you should check authentication protocol
:Else
('Failed at step ',step,': ',,⍕z)⎕SIGNAL 11
:EndIf
∇
∇ UnMake;a
:Implements destructor
:Trap 0 ⍝ Ignore errors in teardown
:If 0≠≢DRC.Names'' ⋄ {}DRC.Close¨DRC.Names'' ⋄ :EndIf
:EndTrap
∇
:endclass
:EndNamespace