forked from isislovecruft/discern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
2787 lines (2435 loc) · 94.5 KB
/
graph.c
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* File: graph.c
*
* X interface for DISCERN
*
* Copyright (C) 1994 Risto Miikkulainen
*
* This software can be copied, modified and distributed freely for
* educational and research purposes, provided that this notice is included
* in the code, and the author is acknowledged in any materials and reports
* that result from its use. It may not be used for commercial purposes
* without expressed permission from the author.
*
* $Id: graph.c,v 1.68 1994/09/22 05:06:52 risto Exp risto $
*/
#include <stdio.h>
#include <math.h>
#include <setjmp.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/AsciiText.h>
#include "defs.h"
#include "Gwin.h"
#include "globals.c"
/************ graphics constants *************/
/* color parameters */
#define UNITCOLORS 0 /* chooses bryw colorscale */
#define WEIGHTCOLORS 1 /* chooses gbryw colorscale (not used) */
#define MINCOLORS 10 /* if fewer free colors, use existing */
#define MAXCOLORS 256 /* max # of colors */
#define MAXCOLGRAN 65535 /* granularity of color values */
/* colormap entry */
typedef struct RGB
{
short red, green, blue; /* hold possible rgb values in XColor struct */
}
RGB;
/* graphics separator constants */
#define HIDSP 1 /* separation of hidden layers,pixels */
#define VERSP 3 /* generic vertical separation */
#define HORSP 3 /* generic horizontal separation */
#define BOXSP 1 /* vertical space around boxed text */
#define HFMSP 3 /* separation of hfm boxes */
#define PREVSP (3 * HORSP) /* indentation of prev hidden layers */
/* displaying labels */
#define MORELABEL "more.." /* if # of labels > maxlabels, this is shown */
#define NWINS NMODULES /* number of windows */
/* text constants in hfminp */
#define HFMINP_REPS "slot-representations" /* input data begins in hfminp */
/******************** Function prototypes ************************/
/* global functions */
#include "prototypes.h"
extern unsigned sleep __P ((unsigned seconds));
/* functions local to this file */
static void handle_events __P((void));
static void runstop_callback __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void toggle_callback __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void Read_command __P((Widget w, XEvent *ev, String *params,
Cardinal *num_params));
static void clear_callback __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void quit_callback __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void init_rgbtab_noweights __P((void));
static void init_rgbtab_bw __P((void));
static void alloc_col __P((void));
static void clean_color_map __P((int k));
static void create_colormap __P((void));
static int createGC __P((Window New_win, GC *New_GC, Font fid, Pixel FGpix,
Pixel theBGpix));
static XFontStruct *loadFont __P((char fontName[]));
static void common_resize __P((int modi, Widget w));
static void init_common_proc_display_params __P((int modi, Widget w));
static void expose_sentpars __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void expose_storypars __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void expose_sentgen __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void expose_storygen __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void expose_cueformer __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void expose_answerprod __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void common_proc_resize __P((int modi));
static void resize_sentpars __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void resize_storypars __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void resize_storygen __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void resize_sentgen __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void resize_cueformer __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void resize_answerprod __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void display_labels __P((int modi, int x, int y,
char *labels[], int nitem));
static void display_boxword __P((int modi, int x, int y, int width, int height,
char wordchars[], int index,
XFontStruct *fontStruct, GC currGC));
static int imin __P((int a, int b));
static int imax __P((int a, int b));
static void init_hfm_display_params __P((int modi, Widget w));
static void expose_hfm __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void resize_hfm __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void hfmmouse_handler __P ((Widget w, XtPointer client_data,
XEvent *p_event));
static void topmouse_handler __P ((int x, int y));
static void midmouse_handler __P ((int x, int y));
static void botmouse_handler __P ((int x, int y));
static int compressed_lines __P ((int indices[], int nindices,
int low, int high));
static void display_top_box __P((int modi, int nnet, int x, int y, int width,
int height, XFontStruct *fontstruct,
FMUNIT units[MAXTOPNET][MAXTOPNET]));
static void display_mid_box __P((int modi, int nnet, int x, int y, int width,
int height, XFontStruct *fontstruct,
FMUNIT units[MAXMIDNET][MAXMIDNET]));
static void display_bot_box __P((int modi, int nnet, int x, int y, int width,
int height, XFontStruct *fontstruct,
FMUNIT units[MAXBOTNET][MAXBOTNET],
double lweights[MAXBOTNET][MAXBOTNET]
[MAXBOTNET][MAXBOTNET]));
static void init_lex_display_params __P((int modi, Widget w, int nnet,
int nwords, WORDSTRUCT words[],
int nrep,
FMUNIT units[MAXLSNET][MAXLSNET]));
static void expose_lex __P((Widget w, XtPointer units, XtPointer call_data));
static void resize_lex __P((Widget w, XtPointer client_data,
XtPointer call_data));
static void lexsemmouse_handler __P ((Widget w, XtPointer client_data,
XEvent *p_event));
static void display_assocweights __P ((int srcmodi,
FMUNIT srcunits[MAXLSNET][MAXLSNET],
int nsrcnet, WORDSTRUCT *srcwords,
int nsrcrep, int nsrcwords, int tgtmodi,
FMUNIT tgtunits[MAXLSNET][MAXLSNET],
int ntgtnet, WORDSTRUCT *tgtwords,
int ntgtrep, int ntgtwords,
int x, int y,
double assoc[MAXLSNET][MAXLSNET]
[MAXLSNET][MAXLSNET]));
static void display_title __P((int modi, char name[]));
static void display_log __P((int modi));
static void frameRectangle __P((int modi, int x, int y, int width, int height,
int colorindex));
static void labelbox __P((int modi, int x, int y, int width, int height,
double value, char labels[][MAXWORDL + 1],
int labelcount, XFontStruct *fontstruct,
GC fGC, GC bGC));
static void collect_uniq_labels __P((char labels[][MAXWORDL + 1],
int *count, char label[], int maxlabels));
static void collect_labels __P((char labels[][MAXWORDL + 1], int *count,
char label[], int maxlabels));
static int newlabel __P((char labels[][MAXWORDL + 1], int count, char *label));
static int trans_to_color __P((double value, int map));
static void drawLine __P((int modi, int x1, int y1, int x2, int y2, GC currGC,
int width));
static void clearRectangle __P((int modi, int x, int y, int width,
int height));
static void fillRectangle __P((int modi, int x, int y, int width, int height,
int colorindex));
static void drawRectangle __P((int modi, int x, int y, int width, int height));
static void drawText __P((int modi, int x, int y, char text[], GC currGC));
static void drawoverText __P((int modi, int x, int y, char text[], GC currGC));
/******************** static variables *********************/
static XColor colors[MAXCOLORS];
static int actual_color_range; /* number of colors in use */
static int fewesttopi, fewesttopj; /* coordinates for the hfm label */
/* button, command, and graphics widgets */
static Widget
runstop, step, clear, quit, /* buttons */
command, /* command input widget */
sentpars, storypars, storygen, sentgen, cueformer,
answerprod, hfm, lex, sem; /* network graphics widgets */
/* useful pointers to windows */
static Window
theMain, runstopWin, commandWin, Win[NWINS]; /* network graphics windows */
/* text constants to be displayed */
static char
/* labels of the case role assemblies */
*caselabels[] =
{"AGENT", "ACT", "RECIPIENT", "PAT-ATTR", "PATIENT", "LOCATION"},
/* labels of the script slot assemblies */
*slotlabels[] =
{"SCRIPT", "TRACK", "ACTOR", "ITEM", "PLACE", "ROLE1", "ROLE2"},
/* names of the network modules */
*titles[] =
{"SENTENCE PARSER", "STORY PARSER", "STORY GENERATOR", "SENTENCE GENERATOR",
"CUE FORMER", "ANSWER PRODUCER", "EPISODIC MEMORY", "unused", "LEXICAL MAP",
"unused", "SEMANTIC MAP", "unused"}; /* input and output lexical maps */
/* are combined into one window */
/* so their indices are unused */
/* graphics contexts */
static GC
titleGC, /* window name */
logGC, /* log line: item and error */
asmGC, /* text in assemblies */
asmerrorGC, /* errors in assemblies (smaller font) */
hfmfGC, tracefGC, lexfGC,
semfGC, /* foreground labels on maps */
hfmbGC, tracebGC, lexbGC,
sembGC, /* background (reverse) labels */
linefGC, linebGC, /* trace lines, foreground and back */
clearGC, /* for clearing areas */
boxGC, /* for drawing network boxes */
activityGC; /* for displaying activations */
/* corresponding fonts */
static XFontStruct
*titlefontStruct, *logfontStruct, *asmfontStruct, *asmerrorfontStruct,
*hfmfontStruct, *tracefontStruct, *lexfontStruct, *semfontStruct;
/* Array of rgb values that represents a linear color spectrum */
static RGB rgbtab[MAXCOLORS];
/* the unit value colormap */
static short cmap[MAXCOLORS];
/* actions */
static XtActionsRec command_actions[] =
{ /* need to read the command line */
{ "read_command", Read_command } /* after user presses return */
}; /* so define it as an action */
/********************* general initialization ***********************/
void
display_init ()
/* initialize the X display */
{
Arg args[3];
char s[MAXSTRL + 1];
Pixel theBGpix; /* background color */
Dimension borderwidth,
height, width, /* various widget heights and widths given */
tot_width; /* computed total width of display */
printf ("Initializing graphics...\n");
XtSetArg (args[0], XtNborderWidth, &borderwidth);
XtGetValues (main_widget, args, 1); /* get the current border width */
/* create a form with no space between widgets */
XtSetArg (args[0], XtNdefaultDistance, 0);
form = XtCreateManagedWidget ("form", formWidgetClass, main_widget, args, 1);
/* the command button for running and stopping the simulation */
runstop = XtCreateManagedWidget ("runstop", commandWidgetClass,
form, args, 1);
/* toggle switch: if on, the simulation will stop after each propagation */
step = XtCreateManagedWidget ("step", toggleWidgetClass, form, args, 1);
/* the command button for clearing the networks */
clear = XtCreateManagedWidget ("clear", commandWidgetClass, form, args, 1);
/* the command button for exiting the program */
quit = XtCreateManagedWidget ("quit", commandWidgetClass, form, args, 1);
/* create command line widget: */
/* first get the height from runstop */
XtSetArg (args[0], XtNheight, &height);
/* then figure out the total width of the display by */
/* getting the widths of each widget, adding them up, and subtracting
from the total width of the display */
XtSetArg (args[1], XtNwidth, &width);
XtGetValues (runstop, args, 2);
tot_width = width + 2 * borderwidth;
XtSetArg (args[0], XtNwidth, &width);
XtGetValues (step, args, 1);
tot_width += width + 2 * borderwidth;
XtSetArg (args[0], XtNwidth, &width);
XtGetValues (clear, args, 1);
tot_width += width + 2 * borderwidth;
XtSetArg (args[0], XtNwidth, &width);
XtGetValues (quit, args, 1);
tot_width += width + 2 * borderwidth;
XtSetArg (args[0], XtNheight, height);
XtSetArg (args[1], XtNwidth,
2 * data.netwidth + 2 * borderwidth - tot_width);
/* display the name of the current inputfile in the command line widget */
sprintf (s, "file %s", current_inpfile);
XtSetArg (args[2], XtNstring, s);
command = XtCreateManagedWidget ("command", asciiTextWidgetClass,
form, args, 3);
/* create the proc network displays */
/* get the size from resourses */
XtSetArg (args[0], XtNwidth, data.netwidth);
XtSetArg (args[1], XtNheight, data.procnetheight);
sentpars = XtCreateManagedWidget ("sentpars", gwinWidgetClass,
form, args, 2);
storypars = XtCreateManagedWidget ("storypars", gwinWidgetClass,
form, args, 2);
cueformer = XtCreateManagedWidget ("cueformer", gwinWidgetClass,
form, args, 2);
sentgen = XtCreateManagedWidget ("sentgen", gwinWidgetClass,
form, args, 2);
storygen = XtCreateManagedWidget ("storygen", gwinWidgetClass,
form, args, 2);
answerprod = XtCreateManagedWidget ("answerprod", gwinWidgetClass,
form, args, 2);
/* create the hfm and lex displays */
/* take the height from resources; width is the same for all */
XtSetArg (args[1], XtNheight, data.lexnetheight);
lex = XtCreateManagedWidget ("lex", gwinWidgetClass, form, args, 2);
sem = XtCreateManagedWidget ("sem", gwinWidgetClass, form, args, 2);
XtSetArg (args[1], XtNheight, data.hfmnetheight);
hfm = XtCreateManagedWidget ("hfm", gwinWidgetClass, form, args, 2);
/* callbacks: what to do when a button is pressed */
XtAddCallback (runstop, XtNcallback, runstop_callback, NULL);
XtAddCallback (step, XtNcallback, toggle_callback, NULL);
XtAddCallback (clear, XtNcallback, clear_callback, NULL);
XtAddCallback (quit, XtNcallback, quit_callback, NULL);
/* network callbacks: redrawing the state */
XtAddCallback (sentpars, XtNexposeCallback, expose_sentpars, NULL);
XtAddCallback (storypars, XtNexposeCallback, expose_storypars, NULL);
XtAddCallback (cueformer, XtNexposeCallback, expose_cueformer, NULL);
XtAddCallback (sentgen, XtNexposeCallback, expose_sentgen, NULL);
XtAddCallback (storygen, XtNexposeCallback, expose_storygen, NULL);
XtAddCallback (answerprod, XtNexposeCallback, expose_answerprod, NULL);
XtAddCallback (hfm, XtNexposeCallback, expose_hfm, NULL);
XtAddCallback (lex, XtNexposeCallback, expose_lex, lunits);
XtAddCallback (sem, XtNexposeCallback, expose_lex, sunits);
/* network callbacks for resizing */
XtAddCallback (sentpars, XtNresizeCallback, resize_sentpars, NULL);
XtAddCallback (storypars, XtNresizeCallback, resize_storypars, NULL);
XtAddCallback (cueformer, XtNresizeCallback, resize_cueformer, NULL);
XtAddCallback (sentgen, XtNresizeCallback, resize_sentgen, NULL);
XtAddCallback (storygen, XtNresizeCallback, resize_storygen, NULL);
XtAddCallback (answerprod, XtNresizeCallback, resize_answerprod, NULL);
XtAddCallback (hfm, XtNresizeCallback, resize_hfm, NULL);
XtAddCallback (lex, XtNresizeCallback, resize_lex, NULL);
XtAddCallback (sem, XtNresizeCallback, resize_lex, NULL);
/* add the command reading action in the table */
XtAppAddActions (app_con, command_actions, XtNumber (command_actions));
/* network event handlers for mouse clicks */
XtAddEventHandler (hfm, ButtonPressMask, FALSE,
(XtEventHandler) hfmmouse_handler, NULL);
XtAddEventHandler (lex, ButtonPressMask, FALSE,
(XtEventHandler) lexsemmouse_handler, NULL);
XtAddEventHandler (sem, ButtonPressMask, FALSE,
(XtEventHandler) lexsemmouse_handler, NULL);
/* figure out the display type and allocate colors */
create_colormap ();
/* put the display on screen */
XtRealizeWidget (main_widget);
theMain = XtWindow (main_widget);
runstopWin = XtWindow (runstop);
commandWin = XtWindow (command);
/* all keyboard events should go to the command line window */
XtSetKeyboardFocus (main_widget, command);
/* calculate some network geometry variables */
init_common_proc_display_params (SENTPARSMOD, sentpars);
init_common_proc_display_params (STORYPARSMOD, storypars);
init_common_proc_display_params (STORYGENMOD, storygen);
init_common_proc_display_params (SENTGENMOD, sentgen);
init_common_proc_display_params (CUEFORMMOD, cueformer);
init_common_proc_display_params (ANSWERPRODMOD, answerprod);
/* and label the feature map units */
init_hfm_display_params (HFMWINMOD, hfm);
init_lex_display_params (LEXWINMOD, lex, nlnet, nlwords,
lwords, nlrep, lunits);
init_lex_display_params (SEMWINMOD, sem, nsnet, nlwords,
swords, nsrep, sunits);
/* use nlwords even on sem map so that internal symbols are not mapped */
/* set a common font for all buttons and command line */
XtSetArg (args[0], XtNfont, loadFont (data.commandfont));
XtSetValues (runstop, args, 1);
XtSetValues (step, args, 1);
XtSetValues (clear, args, 1);
XtSetValues (quit, args, 1);
XtSetValues (command, args, 1);
/* load the other fonts */
titlefontStruct = loadFont (data.titlefont);
logfontStruct = loadFont (data.logfont);
asmfontStruct = loadFont (data.asmfont);
asmerrorfontStruct = loadFont (data.asmerrorfont);
hfmfontStruct = loadFont (data.hfmfont);
tracefontStruct = loadFont (data.tracefont);
lexfontStruct = loadFont (data.lexfont);
semfontStruct = loadFont (data.semfont);
/* figure out space needed for the title and labels */
titleboxhght = titlefontStruct->ascent + titlefontStruct->descent
+ 2 * BOXSP;
asmboxhght = asmfontStruct->ascent + asmfontStruct->descent + 2 * BOXSP;
/* get the background color */
XtSetArg (args[0], XtNbackground, &theBGpix);
XtGetValues (main_widget, args, 1);
/* create graphics context for all fonts */
createGC (theMain, &titleGC, titlefontStruct->fid, data.textColor, theBGpix);
createGC (theMain, &logGC, logfontStruct->fid, data.textColor, theBGpix);
createGC (theMain, &asmGC, asmfontStruct->fid, data.textColor, theBGpix);
createGC (theMain, &asmerrorGC, asmerrorfontStruct->fid, data.textColor,
theBGpix);
/* these are foreground colors */
createGC (theMain, &hfmfGC, hfmfontStruct->fid, data.textColor, theBGpix);
createGC (theMain, &tracefGC, tracefontStruct->fid, data.textColor,theBGpix);
createGC (theMain, &lexfGC, lexfontStruct->fid, data.textColor, theBGpix);
createGC (theMain, &semfGC, semfontStruct->fid, data.textColor, theBGpix);
/* and these are background (when the label color needs to be reversed) */
createGC (theMain, &hfmbGC, hfmfontStruct->fid, theBGpix, theBGpix);
createGC (theMain, &tracebGC, tracefontStruct->fid, theBGpix, theBGpix);
createGC (theMain, &lexbGC, lexfontStruct->fid, theBGpix, theBGpix);
createGC (theMain, &sembGC, semfontStruct->fid, theBGpix, theBGpix);
/* foreground and background colors for trace map weights */
createGC (theMain, &linefGC, logfontStruct->fid, data.latweightColor,
theBGpix);
createGC (theMain, &linebGC, logfontStruct->fid, theBGpix, theBGpix);
/* clearing areas */
createGC (theMain, &clearGC, logfontStruct->fid, theBGpix, theBGpix);
/* network boxes */
createGC (theMain, &boxGC, logfontStruct->fid, data.netColor, theBGpix);
/* generic context for displaying unit activity */
createGC (theMain, &activityGC, logfontStruct->fid, theBGpix, theBGpix);
/* calculate all network geometries and put them on screen */
resize_sentpars (sentpars, NULL, NULL);
resize_storypars (storypars, NULL, NULL);
resize_storygen (storygen, NULL, NULL);
resize_sentgen (sentgen, NULL, NULL);
resize_cueformer (cueformer, NULL, NULL);
resize_answerprod (answerprod, NULL, NULL);
resize_hfm (hfm, NULL, NULL);
resize_lex (lex, NULL, NULL);
resize_lex (sem, NULL, NULL);
printf ("Graphics initialization complete.\n");
}
/********************* event handler ***********************/
static void
handle_events ()
/* event handling loop */
/* we need this instead of XtMainLoop because the main task is to run
the simulation, and only occasionally check for events */
{
XEvent theEvent;
while (XtAppPending (app_con))
/* as long as there are unprocessed events */
{
XtAppNextEvent (app_con, &theEvent);
if (!(theEvent.type == Expose && theEvent.xexpose.count > 0))
/* only process the last expose event */
{
XtDispatchEvent (&theEvent);
XFlush (theDisplay);
}
}
}
void
wait_and_handle_events ()
/* this is called after each main step of the simulation
to stop and wait for "run" if stepping, and to deal with
any other possible events before displaying simulation results */
{
if (stepping)
wait_for_run (); /* wait until user is ready */
else
/* slow down the display this many seconds */
sleep ((unsigned) abs (data.delay));
handle_events (); /* process all pending events before proceeding
to display the simulation results */
}
/********************* button callback routines ***********************/
static void
runstop_callback (w, client_data, call_data)
/* "Run/Stop" button press handler:
if simulation is running, stop it; otherwise, continue simulation.
from wherever handle_events was called */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
if (simulator_running)
wait_for_run ();
else
start_running ();
}
void
wait_for_run ()
/* stop the simulation and start processing events */
{
Arg args[1];
simulator_running = FALSE; /* process events */
XtSetArg (args[0], XtNlabel, "Run"); /* change label on "Run/Stop" button */
XtSetValues (runstop, args, 1);
XFlush (theDisplay);
while (!simulator_running) /* process events until Run pressed */
{
/* Process one event if any or block */
XtAppProcessEvent (app_con, XtIMAll);
/* Process more events, but don't block */
handle_events ();
}
}
void
start_running ()
/* set up to continue or start simulation */
{
Arg args[1];
simulator_running = TRUE; /* allow exit from wait_for_run */
XtSetArg (args[0], XtNlabel, "Stop"); /* change label on "Run/Stop" button */
XtSetValues (runstop, args, 1);
}
static void
toggle_callback (w, client_data, call_data)
/* "Step" button press handler: turn stepping on or off */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
stepping = !stepping;
}
static void
Read_command (w, ev, params, num_params)
/* this action is executed after the user presses return:
read a command from the command widget and process it */
/* standard action parameters, not used here */
Widget w; XEvent *ev; String *params; Cardinal *num_params;
{
Arg args[1];
String str = NULL; /* command string */
char commandstr[MAXSTRL + 1], /* command and */
rest[MAXSTRL + 1]; /* its parameters */
/* get the command line from screen */
XtSetArg (args[0], XtNstring, &str);
XtGetValues (command, args, 1);
if (strlen (str))
{
/* chop it into command and params */
printf ("%s\n", str);
sscanf (str, "%s", commandstr);
strcpy (rest, str + strlen (commandstr));
/* clear the command widget */
XtSetArg (args[0], XtNstring, "");
XtSetValues (command, args, 1);
process_command (NULL, commandstr, rest);
}
else
printf ("\n%s", promptstr);
}
static void
clear_callback (w, client_data, call_data)
/* "Clear" button press handler: clear the display and network activations,
start a fresh command processing loop */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
extern jmp_buf loop_stories_env;
printcomment ("\n", "Clearing networks", "\n");
clear_networks_display ();
nohfmmouseevents = FALSE; /* allow mouse events again */
nolexmouseevents = FALSE;
longjmp (loop_stories_env, 0);
}
void
clear_networks_display ()
/* clear the network activations, traces, logs, and the display */
{
int modi;
/* clear the processing modules and their display windows */
for (modi = 0; modi < NPROCMODULES; modi++)
{
proc_clear_network (modi);
XClearArea (theDisplay, Win[modi], 0, 0, 0, 0, True);
}
/* clear episodic memory and its display */
hfm_clear_values ();
hfm_clear_prevvalues ();
clear_traces ();
sprintf (net[HFMWINMOD].log, "%s", "");
XClearArea (theDisplay, Win[HFMWINMOD], 0, 0, 0, 0, True);
/* clear the lexical map and its display */
lex_clear_values (lunits, nlnet);
lex_clear_prevvalues (lunits, nlnet);
sprintf (net[LEXWINMOD].log, "%s", "");
XClearArea (theDisplay, Win[LEXWINMOD], 0, 0, 0, 0, True);
/* clear the semantic map and its display */
lex_clear_values (sunits, nsnet);
lex_clear_prevvalues (sunits, nsnet);
sprintf (net[SEMWINMOD].log, "%s", "");
XClearArea (theDisplay, Win[SEMWINMOD], 0, 0, 0, 0, True);
/* update the output immediately before data changes */
XFlush (theDisplay);
handle_events ();
}
static void
quit_callback (w, client_data, call_data)
/* "Quit" button press handler: exit the program */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
close_display ();
exit (EXIT_NORMAL);
}
void
close_display ()
/* free fonts and close the display */
{
XFreeFont (theDisplay, titlefontStruct);
XFreeFont (theDisplay, logfontStruct);
XFreeFont (theDisplay, asmfontStruct);
XFreeFont (theDisplay, asmerrorfontStruct);
XFreeFont (theDisplay, hfmfontStruct);
XFreeFont (theDisplay, tracefontStruct);
XFreeFont (theDisplay, lexfontStruct);
XFreeFont (theDisplay, semfontStruct);
XCloseDisplay (theDisplay);
}
/********************* colormap allocation ***********************/
static void
create_colormap ()
/* allocate colors for activation values: depending on the display type
and available colors, allocate a continuous spectrum the best you can */
{
if (!(visual->class == GrayScale || visual->class == StaticGray))
/* we have color display */
init_rgbtab_noweights ();
else
/* black and white screen */
init_rgbtab_bw ();
alloc_col ();
}
static void
init_rgbtab_noweights ()
/* calculates values for the linear color spectrum black-red-yellow-white
and stores them in rbgtab */
{
double multiplier;
int rangeby3, i;
/* divide the range into three sections:
black-red, red-yellow, yellow-white */
rangeby3 = MAXCOLORS / 3;
multiplier = ((double) MAXCOLGRAN) / rangeby3;
for (i = 0; i < MAXCOLORS; i++)
{
rgbtab[i].green = 0;
rgbtab[i].blue = 0;
if (i < rangeby3)
rgbtab[i].red = (int) (i * multiplier);
else
/* second section: red to yellow */
{
rgbtab[i].red = MAXCOLGRAN;
if (i < 2 * rangeby3)
rgbtab[i].green = (int) ((i - rangeby3) * multiplier);
else
/* third section: yellow to white */
{
rgbtab[i].green = MAXCOLGRAN;
rgbtab[i].blue = (int) ((i - 2 * rangeby3) * multiplier);
}
}
}
}
static void
init_rgbtab_bw ()
/* calculates values for the linear gray scale and stores them in rbgtab */
{
double multiplier;
int i;
/* straight scale from black to white */
multiplier = ((double) MAXCOLGRAN) / MAXCOLORS;
for (i = 0; i < MAXCOLORS; i++)
rgbtab[i].green = rgbtab[i].blue = rgbtab[i].red
= (int) (i * multiplier);
}
static void
alloc_col ()
/* allocate colors from rgbtab until no more free cells,
using existing colors if they match what we want */
{
int start = MAXCOLORS / 2; /* starting offset in an alloc sweep */
int d = MAXCOLORS; /* increment in an alloc sweep */
int j, /* index to the linear spectrum */
k; /* number of colors allocated */
for (j = 0; j < MAXCOLORS; j++)
cmap[j] = NONE;
k = 0;
/* add colors to cmap, keep them uniformly distributed in the range,
and gradually make the spectrum more refined */
while (d > 1)
{
/* add colors to cmap with d as the current distance
between new colors in the spectrum and start as the first location */
j = start;
while (j < MAXCOLORS) /* completed a sweep of new colors */
{
colors[k].flags = DoRed | DoGreen | DoBlue; /* use all planes */
colors[k].red = rgbtab[j].red;
colors[k].green = rgbtab[j].green;
colors[k].blue = rgbtab[j].blue;
cmap[j] = k;
if (XAllocColor (theDisplay, colormap, &(colors[k])) == 0)
{
cmap[j] = NONE;
clean_color_map(k);
return;
}
k++; /* allocated one new color */
j += d; /* next location in the spectrum */
}
d /= 2; /* set up a tighter distance */
start /= 2; /* start lower in the spectrum */
}
clean_color_map(k); /* set # of colors, clean up */
}
static void
clean_color_map(k)
/* set the number of colors, print message, and clean up the map */
int k; /* number of colors allocated */
{
int i, m; /* counters for cleaning up cmap */
/* colors[k-1] is the last valid colorcell */
actual_color_range = k;
if (actual_color_range < MINCOLORS)
{
fprintf (stderr, "Warning: obtained only %d colors\n", k);
fprintf (stderr, "(consider using a private colormap)\n");
}
else
printf ("Obtained %d colors.\n", k);
/* clean up cmap; move all entries to the beginning */
m = 0;
while (m < MAXCOLORS && cmap[m] != NONE)
++m;
for (i = m + 1; i < MAXCOLORS; i++)
if (cmap[i] == NONE) /* no colorcell */
continue;
else
cmap[m] = cmap[i], ++m;
}
/********************* GCs, fonts, resizing ***********************/
static int
createGC (New_win, New_GC, fid, theFGpix, theBGpix)
/* create a new graphics context for the given window with given
font and foreground and background colors */
Window New_win; /* window for the GC */
GC *New_GC; /* return pointer to the created GC here */
Font fid; /* font id */
Pixel theFGpix, theBGpix; /* foreground and background colors */
{
XGCValues GCValues; /* graphics context parameters; not used */
*New_GC = XCreateGC (theDisplay, New_win, (unsigned long) 0, &GCValues);
if (*New_GC == 0) /* could not create */
return (FALSE);
else
{
/* set font, foreground and background */
XSetFont (theDisplay, *New_GC, fid);
XSetForeground (theDisplay, *New_GC, theFGpix);
XSetBackground (theDisplay, *New_GC, theBGpix);
return (TRUE);
}
}
static XFontStruct *
loadFont (fontName)
/* load a given font */
char fontName[]; /* name of the font */
{
XFontStruct *fontStruct; /* return font here */
if ((fontStruct = XLoadQueryFont (theDisplay, fontName)) == NULL)
{
fprintf (stderr, "Cannot load font: %s, using fixed\n", fontName);
if ((fontStruct = XLoadQueryFont (theDisplay, "fixed")) == NULL)
{
fprintf (stderr, "Cannot load fixed font\n");
exit (EXIT_X_ERROR);
}
}
return (fontStruct);
}
static void
common_resize (modi, w)
/* when resizing any net, first get the new window width and height */
int modi; /* module number */
Widget w; /* and its widget */
{
Arg args[2];
Dimension width, height;
/* get the current width and height from the server */
XtSetArg (args[0], XtNwidth, &width);
XtSetArg (args[1], XtNheight, &height);
XtGetValues (w, args, 2);
/* and store them for further calculations */
net[modi].width = width;
net[modi].height = height;
}
/********************* processing module operations ***********************/
static void
init_common_proc_display_params (modi, w)
/* initialize a number of parameters common for all processing modules */
int modi; /* module number */
Widget w; /* and its widget */
{
int i;
Win[modi] = XtWindow (w); /* get a pointer to the window */
/* calculate the number of slots at the input and output */
for (i = 0; i < ninputs[modi]; i++)
inputs[modi][i] = BLANKINDEX; /* initially all blank words */
for (i = 0; i < noutputs[modi]; i++)
targets[modi][i] = BLANKINDEX; /* initially all blank words */
/* number of assembly columns on the display */
if (modi == ANSWERPRODMOD) /* answer producer has its input assemblies */
net[modi].columns = nslot; /* in two layers: story rep is longer */
else
net[modi].columns = imax (ninputs[modi], noutputs[modi]);
}
/********************* expose event handlers */
static void
expose_sentpars (w, client_data, call_data)
/* expose event handler for sentence parser: redraw the window */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
int modi = SENTPARSMOD;
XClearWindow (theDisplay, Win[modi]);
display_title (modi, titles[modi]);
display_labels (modi, net[modi].tgtx,
net[modi].tgty + net[modi].uhght + asmboxhght,
caselabels, ncase);
display_current_proc_net (modi);
}
static void
expose_storypars (w, client_data, call_data)
/* expose event handler for story parser: redraw the window */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
int modi = STORYPARSMOD;
XClearWindow (theDisplay, Win[modi]);
display_title (modi, titles[modi]);
display_labels (modi, net[modi].inpx, titleboxhght,
caselabels, ncase);
display_labels (modi, net[modi].tgtx,
net[modi].tgty + net[modi].uhght + asmboxhght,
slotlabels, nslot);
display_current_proc_net (modi);
}
static void
expose_storygen (w, client_data, call_data)
/* expose event handler for story generator: redraw the window */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
int modi = STORYGENMOD;
XClearWindow (theDisplay, Win[modi]);
display_title (modi, titles[modi]);
display_labels (modi, net[modi].inpx,
net[modi].inpy + net[modi].uhght + asmboxhght,
slotlabels, nslot);
display_labels (modi, net[modi].outx, titleboxhght,
caselabels, ncase);
display_current_proc_net (modi);
}
static void
expose_sentgen (w, client_data, call_data)
/* expose event handler for sentence generator: redraw the window */
/* standard callback parameters, not used here */
Widget w; XtPointer client_data, call_data;
{
int modi = SENTGENMOD;
XClearWindow (theDisplay, Win[modi]);
display_title (modi, titles[modi]);
display_labels (modi, net[modi].inpx,
net[modi].inpy + net[modi].uhght + asmboxhght,
caselabels, ncase);
display_current_proc_net (modi);
}
static void
expose_cueformer (w, client_data, call_data)
/* expose event handler for cue former: redraw the window */
/* standard callback parameters, not used here */