forked from isislovecruft/discern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1729 lines (1553 loc) · 53.3 KB
/
main.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: main.c
*
* Main story processing loop, initializations, and statistics 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: main.c,v 1.62 1994/09/20 10:46:59 risto Exp $
*/
#include <stdio.h>
#include <math.h>
#include <setjmp.h>
#include <stdlib.h>
#include <strings.h>
#include <signal.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Cardinals.h>
#include "defs.h"
#define DEFINE_GLOBALS /* so global variables get defined here only */
#include "globals.c"
/************ general constants *************/
/* commands */
#define C_READ "read"
#define C_READ_AND_PARAPHRASE "read-and-paraphrase"
#define C_QUESTION "question"
#define C_TEXT_QUESTION "text-question"
#define C_CLEAR_NETWORKS "clear-networks"
#define C_STOP "stop"
#define C_FILE "file"
#define C_ECHO "echo"
#define C_INIT_STATS "init-stats"
#define C_PRINT_STATS "print-stats"
#define C_LIST_PARAMS "list-params"
#define C_QUIT "quit"
#define C_LREPFILE "lrepfile"
#define C_SREPFILE "srepfile"
#define C_PROCFILE "procfile"
#define C_HFMFILE "hfmfile"
#define C_HFMINPFILE "hfminpfile"
#define C_LEXFILE "lexfile"
#define C_DISPLAYING "displaying"
#define C_NSLOT "nslot"
#define C_NCASE "ncase"
#define C_CHAIN "chain"
#define C_WITHLEX "withlex"
#define C_WITHHFM "withhfm"
#define C_BABBLING "babbling"
#define C_PRINT_MISTAKES "print_mistakes"
#define C_LOG_LEXICON "log_lexicon"
#define C_IGNORE_STOPS "ignore_stops"
#define C_DELAY "delay"
#define C_TOPSEARCH "topsearch"
#define C_MIDSEARCH "midsearch"
#define C_TRACENC "tracenc"
#define C_TSETTLE "tsettle"
#define C_EPSILON "epsilon"
#define C_ALIVEACT "aliveact"
#define C_MINACT "minact"
#define C_MAXACT "maxact"
#define C_GAMMAEXC "gammaexc"
#define C_GAMMAINH "gammainh"
#define C_WITHINERR "withinerr"
#define C_COMMENT_CHAR '#'
/* keywords expected in the input files */
#define SENTDELIM "." /* sentence delimiter in input files */
#define QUESTDELIM "?" /* question delimiter in input files */
#define BLANKLABEL "_" /* blank rep symbol in input files */
#define REPS_INST "instances" /* reps instance list */
#define PROCSIMU_REPS "word-representations" /* reps, procsimu word rep */
#define PROCSIMU_REPSIZE "nwordrep" /* reps size */
/* option keywords */
#define OPT_HELP "-help"
#define OPT_GRAPHICS "-graphics"
#define OPT_NOGRAPHICS "-nographics"
#define OPT_OWNCMAP "-owncmap"
#define OPT_NOOWNCMAP "-noowncmap"
#define OPT_DELAY "-delay"
/* other name defaults */
#define APP_CLASS "Discern" /* class of this application */
#define DEFAULT_INITFILENAME "init" /* initialization file */
#define DEFAULT_INPUTFILENAME "input-example" /* input file */
#define SEMANTIC_KEYWORD "semantic" /* name of semantic reps */
#define LEXICAL_KEYWORD "lexical" /* name of lexical reps */
/******************** Function prototypes ************************/
/* global functions */
#include "prototypes.h"
extern int strcasecmp __P ((const char *s1, const char *s2));
/* functions local to this file */
static void create_toplevel_widget __P ((int argc, char **argv));
static void process_remaining_arguments __P ((int argc, char **argv));
static void process_display_options __P ((int argc, char **argv));
static void process_nodisplay_options __P ((int *argc, char **argv));
static char *get_option __P ((XrmDatabase db,
char *app_name, char *app_class,
char *res_name, char *res_class));
static void usage __P ((char *app_name));
static void loop_stories __P((void));
static void process_inpfile __P((FILE *fp));
static void init_system __P((void));
static int system_initialized __P((char *commandstr));
static void reps_init __P((char *lexsem, char *repfile, WORDSTRUCT words[],
int *nwords, int *nrep));
static void read_story_data __P((FILE *fp));
static void read_qa_data __P((FILE *fp));
static void read_text_question __P((char *rest));
static void list_params __P((void));
static void sent2indices __P ((SENTSTRUCT *sent, char rest[]));
static int text2floats __P((double itemarray[], int nitems, char nums[]));
static int wordindex __P((char wordstring[]));
static void read_int_par __P((char *rest, char *commandstr, int *variable));
static void read_float_par __P((char *rest, char *commandstr,
double *variable));
static char *rid_sspace __P((char rest[]));
/******************** static variables ******************** */
/* space for the lexica (including blank word -1) */
static WORDSTRUCT
lwordsarray[MAXWORDS + 1],
swordsarray[MAXWORDS + 1];
/* define the geometry of the display */
static String fallback_resources[] =
{
"*runstop.left: ChainLeft",
"*runstop.right: ChainLeft",
"*runstop.top: ChainTop",
"*runstop.bottom: ChainTop",
"*step.fromHoriz: runstop",
"*step.left: ChainLeft",
"*step.right: ChainLeft",
"*step.top: ChainTop",
"*step.bottom: ChainTop",
"*clear.fromHoriz: step",
"*clear.left: ChainLeft",
"*clear.right: ChainLeft",
"*clear.top: ChainTop",
"*clear.bottom: ChainTop",
"*quit.fromHoriz: clear",
"*quit.left: ChainLeft",
"*quit.right: ChainLeft",
"*quit.top: ChainTop",
"*quit.bottom: ChainTop",
"*command.fromHoriz: quit",
"*command.left: ChainLeft",
"*command.right: ChainRight",
"*command.top: ChainTop",
"*command.bottom: ChainTop",
"*sentpars.fromVert: runstop",
"*sentpars.top: ChainTop",
"*storypars.fromVert: sentpars",
"*cueformer.fromVert: storypars",
"*lex.fromVert: cueformer",
"*sem.fromVert: lex",
"*sentgen.fromHoriz: sentpars",
"*sentgen.fromVert: runstop",
"*sentgen.top: ChainTop",
"*storygen.fromHoriz: storypars",
"*storygen.fromVert: sentgen",
"*answerprod.fromHoriz: cueformer",
"*answerprod.fromVert: storygen",
"*hfm.fromHoriz: lex",
"*hfm.fromVert: answerprod",
/* define the color defaults */
"*foreground: white",
"*background: black",
"*borderColor: white",
/* commands can be entered by hitting return */
"*command*translations: #override\\n\
<Key>Return: read_command()",
"*command*editType: edit",
NULL
};
/* these are the possible command line options */
static XrmOptionDescRec options[] =
{
{OPT_HELP, ".help", XrmoptionNoArg, "true"},
{OPT_GRAPHICS, ".bringupDisplay", XrmoptionNoArg, "true"},
{OPT_NOGRAPHICS, ".bringupDisplay", XrmoptionNoArg, "false"},
{OPT_OWNCMAP, ".owncmap", XrmoptionNoArg, "true"},
{OPT_NOOWNCMAP, ".owncmap", XrmoptionNoArg, "false"},
{OPT_DELAY, ".delay", XrmoptionSepArg, NULL},
};
/* the default values for the application-specific resources;
see defs.h for component descriptions */
static XtResource resources[] =
{
{"bringupDisplay", "BringupDisplay", XtRBoolean, sizeof (Boolean),
XtOffset (RESOURCE_DATA_PTR, bringupdisplay), XtRImmediate,
(XtPointer) True},
{"owncmap", "Owncmap", XtRBoolean, sizeof (Boolean),
XtOffset (RESOURCE_DATA_PTR, owncmap), XtRImmediate, (XtPointer) False},
{"delay", "Delay", XtRInt, sizeof (int),
XtOffset (RESOURCE_DATA_PTR, delay), XtRString, "0"},
{"netwidth", "Netwidth", XtRDimension, sizeof (Dimension),
XtOffset (RESOURCE_DATA_PTR, netwidth), XtRString, "512"},
{"procnetheight", "Procnetheight", XtRDimension, sizeof (Dimension),
XtOffset (RESOURCE_DATA_PTR, procnetheight), XtRString, "140"},
{"hfmnetheight", "Hfmnetheight", XtRDimension, sizeof (Dimension),
XtOffset (RESOURCE_DATA_PTR, hfmnetheight), XtRString, "514"},
{"lexnetheight", "Lexnetheight", XtRDimension, sizeof (Dimension),
XtOffset (RESOURCE_DATA_PTR, lexnetheight), XtRString, "255"},
{"tracelinescale", "Tracelinescale", XtRFloat, sizeof (float),
XtOffset (RESOURCE_DATA_PTR, tracelinescale), XtRString, "1.5"},
{"tracewidthscale", "Tracewidthscale", XtRFloat, sizeof (float),
XtOffset (RESOURCE_DATA_PTR, tracewidthscale), XtRString, "0.01"},
{"reversevalue", "Reversevalue", XtRFloat, sizeof (float),
XtOffset (RESOURCE_DATA_PTR, reversevalue), XtRString, "0.3"},
{"textColor", "TextColor", XtRPixel, sizeof (Pixel),
XtOffset (RESOURCE_DATA_PTR, textColor), XtRString, "green"},
{"netColor", "NetColor", XtRPixel, sizeof (Pixel),
XtOffset (RESOURCE_DATA_PTR, netColor), XtRString, "red"},
{"latweightColor", "LatweightColor", XtRPixel, sizeof (Pixel),
XtOffset (RESOURCE_DATA_PTR, latweightColor), XtRString, "white"},
{"commandfont", "Commandfont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, commandfont), XtRString, "7x13bold"},
{"titlefont", "Titlefont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, titlefont), XtRString, "8x13bold"},
{"logfont", "Logfont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, logfont), XtRString, "6x10"},
{"asmfont", "Asmfont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, asmfont), XtRString, "6x10"},
{"asmerrorfont", "Asmerrorfont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, asmerrorfont), XtRString, "5x8"},
{"hfmfont", "Hfmfont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, hfmfont), XtRString, "7x13"},
{"tracefont", "Tracefont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, tracefont), XtRString, "5x8"},
{"lexfont", "Lexfont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, lexfont), XtRString, "5x8"},
{"semfont", "Semfont", XtRString, sizeof (String),
XtOffset (RESOURCE_DATA_PTR, semfont), XtRString, "5x8"},
/* command line options */
{"help", "Help", XtRBoolean, sizeof (Boolean),
XtOffset (RESOURCE_DATA_PTR, help), XtRImmediate, (XtPointer) False},
};
/* interrupt handling */
/* for AIX ^C handling, we need to use setsig instead of signal */
/* and call sigrelse before longjmp (see below) */
#if defined(_AIX) || defined(___AIX)
extern void sigset __P ((int Signal, void (*Function) ()));
#define signal(a,b) sigset(a,b)
extern int sigrelse __P ((int Signal));
#endif
jmp_buf loop_stories_env; /* jump here from after interrupt */
void sig_handler ()
/* handles control-c by jumping to loop-stories like clear-networks */
{
#if defined(_AIX) || defined(___AIX)
sigrelse (SIGINT);
#endif
longjmp (loop_stories_env, 1);
}
/********************* main command processing ******************************/
void
main (argc, argv)
/* initialize X display, system parameters, and start the command loop */
int argc;
char **argv;
{
int xargc; /* saved argc & argv */
char **xargv;
int i;
/* save command line args so we can parse them later */
xargc = argc;
xargv = (char **) XtMalloc (argc * sizeof (char *));
for (i = 0; i < argc; i++)
xargv[i] = argv[i];
/* try to open the display */
XtToolkitInitialize ();
app_con = XtCreateApplicationContext ();
XtAppSetFallbackResources (app_con, fallback_resources);
theDisplay = XtOpenDisplay (app_con, NULL, NULL, APP_CLASS,
options, XtNumber (options), &argc, argv);
if (theDisplay != NULL)
/* create the top-level widget and get application resources */
create_toplevel_widget(xargc, xargv);
else
fprintf (stderr, "No display: running in text mode.\n");
process_remaining_arguments (argc, argv);
init_system ();
loop_stories ();
exit (EXIT_NORMAL);
}
static void
loop_stories ()
/* process commands from a file or from the keyboard */
{
if (setjmp (loop_stories_env))
{
/* longjmp gets here */
}
else
{
/* return from setjmp */
}
printf ("%s", promptstr);
if (displaying)
while (TRUE)
{
/* if the Xdisplay is up, process events until the user hits "Run" */
/* user hitting "Quit" will terminate the program */
wait_for_run ();
/* user hit "Run"; start reading commands from current input file */
process_command (NULL, "file", current_inpfile);
}
else
/* no Xdisplay; read commands from the keyboard */
process_inpfile (stdin);
}
static void
process_inpfile (fp)
/* read commands from an input file */
FILE *fp; /* pointer to the input file */
{
int c; /* temporarily holds one char */
char
commandstr[MAXSTRL + 1], /* command string */
rest[MAXSTRL + 1]; /* parameters */
while ((c = fgetc (fp)) != EOF)
{
/* reprint the prompt if return from the keyboard */
if (c == '\n' && fp == stdin)
{
printf ("%s", promptstr);
continue;
}
/* otherwise ignore white space */
if (c == ' ' || c == '\t' || c == '\n')
continue;
/* there is real input; process it as a command */
ungetc (c, fp);
fscanf (fp, "%s", commandstr);
fgetline (fp, rest, MAXSTRL);
process_command (fp, commandstr, rest);
}
}
void
process_command (fp, commandstr, rest)
/* process one command */
FILE *fp; /* file where command was read from; NULL=display */
char *commandstr, *rest; /* command and its parameters */
{
double bestvalue, foo; /* activation of the max responding unit */
char s[MAXSTRL + 1], ss[MAXSTRL + 1];
FILE *fp2; /* pointer to a new input file */
if (!system_initialized (commandstr))
{
fprintf (stderr, "Initialization incomplete or out of order\n");
exit (EXIT_DATA_ERROR);
}
/* these commands are meaningful only in files and from stdin */
else if (fp == NULL && (!strcmp (commandstr, C_READ) ||
!strcmp (commandstr, C_READ_AND_PARAPHRASE) ||
!strcmp (commandstr, C_QUESTION)))
{
sprintf (s, "command %s ignored in display mode, sorry", commandstr);
printcomment ("", s, "\n");
}
else if (!strcmp (commandstr, C_READ))
/* read in a story, maybe store in the memory, do not paraphrase */
{
printcomment ("", "Read story", "\n");
read_story_data (fp); /* read in the story data */
parse_story (); /* process it through the networks */
if (withhfm)
/* store in the memory */
presentmem (PARATASK, STOREMOD, story.slots, &foo);
if (babbling)
putchar ('\n');
}
else if (!strcmp (commandstr, C_READ_AND_PARAPHRASE))
/* read in a story, maybe store in the memory, paraphrase */
{
printcomment ("", "Read and paraphrase story", "\n");
read_story_data (fp); /* read in the story data */
parse_story (); /* process it through the networks */
if (withhfm)
/* store in the memory */
presentmem (PARATASK, STOREMOD, story.slots, &foo);
gener_story (); /* generate a paraphrase */
if (babbling)
putchar ('\n');
}
else if (!strcmp (commandstr, C_QUESTION))
/* parse and answer a question */
{
printcomment ("", "Answer question", "\n");
read_qa_data (fp); /* read in the question/answer data */
formcue (); /* parse the question and form cue */
if (withhfm)
/* retrieve the appropriate story */
presentmem (QATASK, RETMOD, qa.slots, &bestvalue);
if (!(withhfm && bestvalue < aliveact)) /* if one found */
produceanswer (); /* form an answer and output it */
if (babbling)
putchar ('\n');
}
else if (!strcmp (commandstr, C_TEXT_QUESTION))
/* process a question that the user typed in */
{
printcomment ("", "Answer text question", "\n");
text_question = TRUE; /* answer processing done without a target */
read_text_question (rest);/* read in the question data */
formcue (); /* parse the question and form cue */
if (withhfm)
/* retrieve the appropriate story */
presentmem (QATASK, RETMOD, qa.slots, &bestvalue);
if (!(withhfm && bestvalue < aliveact)) /* if one found */
produceanswer (); /* form an answer and output it */
text_question = FALSE;
if (babbling)
putchar ('\n');
}
else if (!strcmp (commandstr, C_CLEAR_NETWORKS))
/* clear the traces and clean the screen */
{
printcomment ("", "Clearing networks", "\n");
if (displaying)
clear_networks_display (); /* this will clear traces and more */
else
clear_traces ();
}
else if (!strcmp (commandstr, C_STOP))
/* stop the simulation and wait for "Run" or return */
{
if (!ignore_stops) /* stopping can be turned off */
{
if (displaying)
printcomment ("", "Stopping (click Run to continue)", "\n");
else
printcomment ("", "Stopping (hit Return to continue)", "");
if (displaying)
wait_for_run (); /* wait for "Run" click */
else
while (getchar () != '\n');
}
else
printcomment ("", "Stop command ignored", "\n");
}
else if (!strcmp (commandstr, C_FILE))
/* read commands from a file */
{
sscanf (rest, "%s", s); /* read the filename */
if (open_file (s, "r", &fp2, NOT_REQUIRED)) /* if open succeeeds */
{
if (fp == NULL || fp == stdin) /* currently reading commands */
{ /* from the Xdisplay or keyboard */
if (displaying)
start_running (); /* change from interactive */
sprintf (current_inpfile, "%s", s); /* into reading from file */
}
sprintf (ss, "Reading input from %s...", s);
printcomment ("", ss, "\n");
process_inpfile (fp2); /* process commands from the file */
fclose (fp2);
sprintf (ss, "Finished reading input from %s.", s);
printcomment ("", ss, "\n");
if (displaying && fp == NULL)
simulator_running = FALSE; /* stay in the event handler */
}
}
else if (!strcmp (commandstr, C_ECHO))
/* print text on log output */
{
if (babbling || print_mistakes) /* only if not turned off */
if (strlen (rest))
printf ("%s%s%s\n", BEGIN_COMMENT, rest + 1, END_COMMENT);
}
else if (!strcmp (commandstr, C_INIT_STATS))
/* zero out the stats variables */
{
printcomment ("", "Initializing statistics", "\n");
init_stats ();
}
else if (!strcmp (commandstr, C_PRINT_STATS))
/* print the performance statistics on the log output */
{
print_stats ();
}
else if (!strcmp (commandstr, C_LIST_PARAMS))
/* list current values of parameters, input files */
{
list_params ();
}
else if (!strcmp (commandstr, C_QUIT))
/* exit from the program */
{
if (displaying)
close_display ();
exit (EXIT_NORMAL);
}
else if (!strcmp (commandstr, C_LREPFILE))
/* read in the lexical representations */
{
sscanf (rest, "%s", lrepfile); /* get the filename */
reps_init (LEXICAL_KEYWORD, lrepfile, lwords, &nlwords, &nlrep);
if (displaying)
display_new_lex_labels(LEXWINMOD, nlnet, nlwords,
lwords, nlrep, lunits);
}
else if (!strcmp (commandstr, C_SREPFILE))
/* read in the semantic representations */
{
sscanf (rest, "%s", srepfile); /* get the filename */
reps_init (SEMANTIC_KEYWORD, srepfile, swords, &nswords, &nsrep);
if (displaying)
display_new_lex_labels(SEMWINMOD, nsnet, nswords,
swords, nsrep, sunits);
}
else if (!strcmp (commandstr, C_PROCFILE))
/* read in the processing modules */
{
sscanf (rest, "%s", procfile); /* get the filename */
proc_init ();
}
else if (!strcmp (commandstr, C_HFMFILE))
/* read in the hierarchical feature maps */
{
sscanf (rest, "%s", hfmfile); /* get the filename */
hfm_init ();
}
else if (!strcmp (commandstr, C_HFMINPFILE))
/* read in the labels for hierarchical feature map display */
{
sscanf (rest, "%s", hfminpfile); /* get the filename */
if (displaying)
display_new_hfm_labels();
}
else if (!strcmp (commandstr, C_LEXFILE))
/* read in the proc modules */
{
sscanf (rest, "%s", lexfile); /* get the filename */
lex_init ();
}
else if (!strcmp (commandstr, C_DISPLAYING))
/* start or close the X window display */
{
read_int_par (rest, commandstr, &displaying); /* get int value */
if (displaying)
{
if (form == NULL) /* display does not exist at all.. */
{
printcomment ("", "Clearing networks", "\n");
display_init (); /* ..so bring it up from scratch */
}
else /* display exists */
XtMapWidget (main_widget); /* so make sure it is on screen */
}
else if (!displaying && form != NULL) /* display exists */
{
XtUnmapWidget (main_widget); /* so hide it (but don't destroy) */
}
/* decide what to do when user hits ^C */
if (displaying)
/* do not catch control-c */
signal (SIGINT, SIG_DFL);
else
/* catch control-c */
signal (SIGINT, sig_handler);
longjmp (loop_stories_env, 1);
}
else if (commandstr[0] == C_COMMENT_CHAR); /* a comment, ignore */
else if (!strcmp (commandstr, C_NSLOT))
/* set the number of script representation slots */
{
read_int_par (rest, commandstr, &nslot); /* get int value */
if (nslot > MAXSLOT || nslot <= 0)
{
fprintf (stderr, "Value of nslot exceeds array size\n");
exit (EXIT_SIZE_ERROR);
}
nslotrep = nslot * nsrep; /* set also number of units in rep */
}
else if (!strcmp (commandstr, C_NCASE))
/* set number of case representation assemblies */
{
read_int_par (rest, commandstr, &ncase); /* get int value */
if (ncase > MAXCASE || ncase <= 0)
{
fprintf (stderr, "Value of ncase exceeds array size\n");
exit (EXIT_SIZE_ERROR);
}
ncaserep = ncase * nsrep; /* set also number of units in rep */
}
else if (!strcmp (commandstr, C_CHAIN))
/* whether input for a net is taken from output of another or cleaned up */
read_int_par (rest, commandstr, &chain);
else if (!strcmp (commandstr, C_WITHLEX))
/* whether lexicon is included in the simulation */
read_int_par (rest, commandstr, &withlex);
else if (!strcmp (commandstr, C_WITHHFM))
/* whether episodic memory is included in the simulation */
read_int_par (rest, commandstr, &withhfm);
else if (!strcmp (commandstr, C_BABBLING))
/* whether log output is verbose */
read_int_par (rest, commandstr, &babbling);
else if (!strcmp (commandstr, C_PRINT_MISTAKES))
/* whether errors are printed at log output even when not babbling */
read_int_par (rest, commandstr, &print_mistakes);
else if (!strcmp (commandstr, C_LOG_LEXICON))
/* whether lexicon translation is printed in the log output */
read_int_par (rest, commandstr, &log_lexicon);
else if (!strcmp (commandstr, C_IGNORE_STOPS))
/* whether stop commands in an input file are ignored */
read_int_par (rest, commandstr, &ignore_stops);
else if (!strcmp (commandstr, C_DELAY))
/* sleep this many seconds at each handle_events */
read_int_par (rest, commandstr, &data.delay);
else if (!strcmp (commandstr, C_TOPSEARCH))
/* search threshold at the script level */
read_float_par (rest, commandstr, &topsearch);
else if (!strcmp (commandstr, C_MIDSEARCH))
/* search threshold at the track level */
read_float_par (rest, commandstr, &midsearch);
else if (!strcmp (commandstr, C_TRACENC))
/* size of the trace on a trace map */
read_int_par (rest, commandstr, &tracenc);
else if (!strcmp (commandstr, C_TSETTLE))
/* number of settling iterations in trace maps */
read_int_par (rest, commandstr, &tsettle);
else if (!strcmp (commandstr, C_EPSILON))
/* min activation for a trace to be stored on a unit */
read_float_par (rest, commandstr, &epsilon);
else if (!strcmp (commandstr, C_ALIVEACT))
/* min activation for a trace to be retrieved */
read_float_par (rest, commandstr, &aliveact);
else if (!strcmp (commandstr, C_MINACT))
/* lower threshold for trace map sigmoid */
read_float_par (rest, commandstr, &minact);
else if (!strcmp (commandstr, C_MAXACT))
/* upper threshold for trace map sigmoid */
read_float_par (rest, commandstr, &maxact);
else if (!strcmp (commandstr, C_GAMMAEXC))
/* excitatory lateral connection weight on trace feature maps */
read_float_par (rest, commandstr, &gammaexc);
else if (!strcmp (commandstr, C_GAMMAINH))
/* inhibitory lateral connection weight on trace feature maps */
read_float_par (rest, commandstr, &gammainh);
else if (!strcmp (commandstr, C_WITHINERR))
/* statistics collected within this close to the correct value */
read_float_par (rest, commandstr, &withinerr);
else
{
sprintf (s, "Command %s not recognized", commandstr);
printcomment ("", s, "\n");
}
/* if interactive, print out the prompt after processing each command */
if (fp == stdin || fp == NULL)
printf ("%s", promptstr);
}
/********************* initializations ******************************/
/**************** X interface, command line */
static void
create_toplevel_widget (argc, argv)
/* retrieve resources, create a colormap, and start the top-level widget */
int argc;
char **argv;
{
Widget dummy; /* dummy top-level widget */
Arg args[10];
int scr; /* temporary screen (for obtaining colormap) */
int n = 0; /* argument counter */
/* Create a dummy top-level widget to retrieve resources */
/* (necessary to get the right netcolor etc with owncmap) */
dummy = XtAppCreateShell (NULL, APP_CLASS, applicationShellWidgetClass,
theDisplay, NULL, ZERO);
XtGetApplicationResources (dummy, &data, resources,
XtNumber (resources), NULL, ZERO);
scr = DefaultScreen (theDisplay);
visual = DefaultVisual (theDisplay, scr);
/* Select colormap; data.owncmap was specified in resources
or as an option */
if (data.owncmap)
{
colormap = XCreateColormap (theDisplay, DefaultRootWindow (theDisplay),
visual, AllocNone);
XtSetArg (args[n], XtNcolormap, colormap);
n++;
}
else
colormap = DefaultColormap (theDisplay, scr);
XtDestroyWidget (dummy);
/* Create the real top-level widget */
XtSetArg (args[n], XtNargv, argv);
n++;
XtSetArg (args[n], XtNargc, argc);
n++;
main_widget = XtAppCreateShell (NULL, APP_CLASS, applicationShellWidgetClass,
theDisplay, args, n);
XtGetApplicationResources (main_widget, &data, resources,
XtNumber (resources), NULL, ZERO);
}
static void
process_remaining_arguments (argc, argv)
/* parse nongraphics options, simufile and inputfile, setup displaying */
int argc;
char **argv;
{
int i;
/* if opendisplay was successful, all options were parsed into "data" */
/* otherwise, we have to get the options from command-line argument list */
if (theDisplay != NULL)
process_display_options (argc, argv);
else
process_nodisplay_options (&argc, argv);
/* figure out the initfile and input file names */
sprintf (initfile, "%s", "");
sprintf (current_inpfile, "%s", "");
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
{
fprintf (stderr, "Unknown option %s\n", argv[i]);
usage (argv[0]);
exit (EXIT_COMMAND_ERROR);
}
else if (!strlen (initfile)) /* first argument is initfile */
sprintf (initfile, "%s", argv[i]);
else if (!strlen (current_inpfile)) /* second is inputfile */
sprintf (current_inpfile, "%s", argv[i]);
else
{
fprintf (stderr, "Too many arguments\n");
usage (argv[0]);
exit (EXIT_COMMAND_ERROR);
}
if (!strlen (initfile)) /* if no initfile given */
sprintf (initfile, "%s", DEFAULT_INITFILENAME);
if (!strlen (current_inpfile)) /* if no inputfile given */
sprintf (current_inpfile, "%s", DEFAULT_INPUTFILENAME);
}
static void
process_display_options (argc, argv)
/* get the non-graphics-related options from the "data" structure */
int argc;
char **argv;
{
/* quick user help */
if (data.help)
{
usage (argv[0]);
exit (EXIT_NORMAL);
}
}
static void
process_nodisplay_options (argc, argv)
/* get the non-graphics-related options from the command string */
int *argc;
char **argv;
{
char *res_str; /* string value of an option */
XrmDatabase db = NULL; /* resource database for options */
XrmParseCommand (&db, options, XtNumber (options), APP_CLASS, argc, argv);
/* quick user help */
res_str = get_option (db, argv[0], APP_CLASS, "help", "Help");
if (res_str != NULL)
if (!strcasecmp ("true", res_str))
{
usage (argv[0]);
exit (EXIT_NORMAL);
}
}
static char *
get_option (db, app_name, app_class, res_name, res_class)
/* return the pointer to the string value of the resource */
XrmDatabase db; /* resource database */
char *res_name, *res_class, /* resource name and class */
*app_name, *app_class; /* application name and class */
{
XrmValue value; /* value of the resource */
char *type, /* resource type */
name[MAXSTRL + 1], class[MAXSTRL + 1];/* full name and class of resource */
sprintf (name, "%s.%s", app_name, res_name);
sprintf (class, "%s.%s", app_class, res_class);
XrmGetResource(db, name, class, &type, &value);
return (value.addr);
}
static void
usage (app_name)
/* print out the list of options and arguments */
char *app_name; /* name of the program */
{
char s[MAXSTRL + 1];
sprintf(s, "%s %s", OPT_DELAY, "<sec>");
fprintf (stderr, "Usage: %s [options] [init file] [input file]\n\
where the options are\n\
%-20s Prints this message\n\
%-20s Bring up graphics display\n\
%-20s Text output only\n\
%-20s Use a private colormap\n\
%-20s Use the existing colormap\n\
%-20s Delay in updating the screen (in seconds)\n",
app_name, OPT_HELP, OPT_GRAPHICS, OPT_NOGRAPHICS,
OPT_OWNCMAP, OPT_NOOWNCMAP, s);
}
/********************* system setup */
static void
init_system ()
/* set up blanks, read configuration commands from the init file,
set up graphics if displaying */
{
FILE *fp; /* pointer to the init file */
setbuf (stdout, NULL); /* output one word at a time */
lwords = lwordsarray + 1; /* reserve index -1 for the blank */
swords = swordsarray + 1; /* for lexical and semantic words.. */
init_stats_blanks (); /* ..and for the statics tables */
printf ("Initializing DISCERN from %s:\n", initfile);
open_file (initfile, "r", &fp, REQUIRED);
process_inpfile (fp); /* read commands from initfile */
fclose (fp);
/* decide whether to bring up display */
if (theDisplay && data.bringupdisplay)
displaying = TRUE;
else
displaying = FALSE;
/* initialize graphics */
if (displaying)
display_init ();
if (!displaying)
/* catch control-c; equivalent to the "clear" button in graphics mode */
signal (SIGINT, sig_handler);
printf ("System initialization complete.\n");
}
static int
system_initialized (commandstr)
/* check whether the initialization commands are given in the right order */
char *commandstr; /* command */
{
/* these commands can appear anywhere */
if (strcmp (commandstr, C_ECHO) && strcmp (commandstr, C_BABBLING) &&
strcmp (commandstr, C_LIST_PARAMS) && strcmp (commandstr, C_QUIT) &&
commandstr[0] != C_COMMENT_CHAR)
/* first we need the data sizes */
if (nslot <= 0 || ncase <= 0)
{
if (strcmp (commandstr, C_NSLOT) && strcmp (commandstr, C_NCASE))
return (FALSE);
}
/* next the representation files */
else if (!strlen (lrepfile) || !strlen (srepfile))
{
if (strcmp (commandstr, C_LREPFILE) && strcmp (commandstr, C_SREPFILE))
return (FALSE);
}
/* then the simulation files */
else if (!strlen (procfile) || !strlen (hfmfile) ||
!strlen (hfminpfile) || !strlen (lexfile))
{
if (strcmp (commandstr, C_PROCFILE) && strcmp (commandstr, C_HFMFILE)&&
strcmp (commandstr, C_HFMINPFILE) && strcmp (commandstr,C_LEXFILE))
return (FALSE);
}
return (TRUE);
}
/********************* reps */
static void
reps_init (lexsem, repfile, words, nwords, nrep)
/* read the word labels and representations from a file */
/* this is called once for lexical and once for semantic words */
char *lexsem, /* either "lexical" or "semantic" */
*repfile; /* name of the representation file */
WORDSTRUCT words[]; /* the lexicon data structure */
int *nwords, /* return number of words */
*nrep; /* return representation dimension */
{
int i;
FILE *fp;
char instancestring[MAXSTRL + 1],/* temporarily holds list of instances */
wordstring[MAXSTRL + 1], /* temporarily holds the word */
repstring[MAXSTRL + 1]; /* temporarily holds the representations */
/* first set up the blank word */
sprintf (words[BLANKINDEX].chars, "%s", BLANKLABEL);
sprintf (words[BLANKINDEX].chars, "%s", BLANKLABEL);
for(i = 0; i < MAXREP; i++) /* make sure its rep is all-0 */
words[BLANKINDEX].rep[i] = 0.0;
printf ("Reading %s representations from %s...", lexsem, repfile);
open_file (repfile, "r", &fp, REQUIRED);
if (!strcmp (lexsem, SEMANTIC_KEYWORD))
/* if this is semantic file, read the list of instances and hold it */
{
/* find the instances */
read_till_keyword (fp, REPS_INST, REQUIRED);
fgetline (fp, instancestring, MAXSTRL);
}
/* get representation size */
read_till_keyword (fp, PROCSIMU_REPSIZE, REQUIRED);
fscanf (fp, "%d", nrep);
if (*nrep > MAXREP || *nrep <= 0)
{
fprintf (stderr, "%s exceeds array size\n", PROCSIMU_REPSIZE);
exit (EXIT_SIZE_ERROR);
}
/* find the representations */
read_till_keyword (fp, PROCSIMU_REPS, REQUIRED);
for (i = 0; i < MAXWORDS + 1; i++)
{
if (fscanf (fp, "%s", wordstring) == EOF) /* read label */
break;
else if (i >= MAXWORDS)
{
fprintf (stderr, "Number of %s words exceeds array size\n",
lexsem);
exit (EXIT_SIZE_ERROR);
}
else if (strlen (wordstring) > MAXWORDL)
{
fprintf (stderr, "Length of word %s exceeds array size\n",
wordstring);
exit (EXIT_SIZE_ERROR);
}
sprintf(words[i].chars, "%s", wordstring);