Debian Codegraph @

w4/model.c

raw
1 /* File: model.c
2 * Author: Jean Thierry-Mieg ([email protected])
3 * Copyright (C) J Thierry-Mieg and R Durbin, 1991
4 * -------------------------------------------------------------------
5 * Acedb is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
19 * -------------------------------------------------------------------
20 * This file is part of the ACEDB genome database package, written by
21 * Richard Durbin (MRC LMB, UK) [email protected], and
22 * Jean Thierry-Mieg (CRBM du CNRS, France) [email protected]
23 *
24 * Description:
25 ** Reads the models of the ACeDB program.
26 * Exported functions:
27 readModels
28 * HISTORY:
29 * Last edited: Nov 4 18:50 2002 (edgrif)
30 * * Oct 21 15:01 1998 (edgrif): Replace messout + exit() with messExit.
31 * Created: Wed Nov 6 13:27:39 1991 (mieg)
32 * CVS info: $Id: model.c,v 1.91 2004/04/22 21:45:24 mieg Exp $
33 *-------------------------------------------------------------------
34 */
35
36 #include <wh/acedb.h>
37 #include <wh/aceio.h>
38 #include <wh/lex.h>
39 #include <wh/pick.h>
40 #include <wh/bs_.h>
41 #include <wh/bindex.h>
42 #include <wh/cache.h> /* used just in duplicate model */
43 #include <wh/session.h> /* gain release write access */
44 #include <wh/query.h>
45 #include <wh/pick.h>
46 #include <wh/model.h>
47 #include <w4/parse_.h> /* for parseAceInProtected() */
48 #include <whooks/sysclass.h>
49 #include <whooks/systags.h>
50 #include <wh/dbpath.h>
51
52 /*
53 #define VERIFY_MODELS
54 */
55 /* the function verifyModels() has something to
56 * do with the ACEDB5 conversion */
57
58 /**************************************************/
59
60 static KEYSET checkModelSyntax (void) ;
61
62
63 #define MTEST FALSE
64
65 static VoidRoutine modelChangeRoutine = 0;
66
67 /**************************************************/
68
69 VoidRoutine modelChangeRegister (VoidRoutine func)
70 {
71 VoidRoutine old = modelChangeRoutine ;
72 modelChangeRoutine = func ;
73 return old ;
74 } /* modelChangeRegister */
75
76 /**************************************************/
77
78 BOOL isModel (KEY key) /* old models */
79 {
80 #if defined(ACEDB4)
81 return (KEYKEY(key) == 0) ;
82 #else
83 return (class(key) == _VModel || key == KEYMAKE (_VSession, 1)) ;
84 #endif
85 } /* isModel */
86
87 /**********************************************************************/
88
89 static int line ;
90 static Stack s1 = 0 ;
91
92 static void cleanVisibility (void)
93 { KEY dummy ;
94 OBJ obj ;
95 int i ;
96 KEYSET ks ;
97
98 if (!lexword2key("Class", &dummy, _VClass))
99 return ;
100 ks = query (0, "Find Class Visibility") ;
101
102 for (i = 0 ; i < keySetMax (ks) ; i ++)
103 { if ((obj = bsUpdate (keySet(ks,i))))
104 { if (bsFindTag (obj, _Visibility))
105 bsRemove (obj) ;
106 bsSave (obj) ;
107 }
108 }
109 i = 256 ;
110 while (i--)
111 pickList[i].visible = 0 ;
112
113 return;
114 } /* cleanVisibility */
115
116
117 static void cleanOldModels (void)
118 {
119 KEY key = 0 ;
120 OBJ obj ;
121 extern BOOL READING_MODELS ; /* from bssubs.c */
122
123 READING_MODELS = TRUE ;
124
125 while (lexNext (_VModel, &key))
126 { if (iskey(key)==2 && (obj = bsUpdate (key)))
127 bsKill (obj) ;
128 lexkill(key) ;
129 }
130 READING_MODELS = FALSE ;
131
132 return;
133 } /* cleanOldModels */
134
135 static int getLine (Array aNode, int *plevel) /* returns FALSE at eof */
136 {
137 static int pass = 1 ;
138 static FILE *fil = 0;
139 static ACETMP tmp_sysmodel = NULL;
140 static Array levelCount ;
141 static Stack sword ; /* holds arbitrarily long string */
142 char *cp, *word, buf[2], inQuotes = 0;
143 int c = 0, oldc = 0;
144 KEY dummy ;
145 int iNode, counter, inword, eow, eol, level=0 ;
146 #ifdef ACEDB4
147 int table ;
148 #endif
149
150 buf[1] = 0 ;
151
152 pass2:
153 iNode = counter = 0 ;
154 inword = eow = FALSE ;
155
156 if (!fil)
157 {
158 if (pass == 1)
159 {
160 Stack s ;
161
162 s = sysModels () ;
163 stackCursor (s, 0) ;
164
165 tmp_sysmodel = aceTmpCreate ("w", 0);
166 if (!tmp_sysmodel)
167 messExit ("Cannot continue without creating "
168 "temporary sysmodel file!");
169 while ((cp = stackNextText(s)))
170 aceOutPrint (aceTmpGetOutput(tmp_sysmodel), "%s\n", cp) ;
171 aceTmpClose (tmp_sysmodel);
172
173 stackDestroy (s) ;
174
175 fil = fopen (aceTmpGetFileName(tmp_sysmodel), "r");
176 if (!fil)
177 messExit ("Cannot reopen temporary sysmodel file %s (%s)",
178 aceTmpGetFileName(tmp_sysmodel), messSysErrorText()) ;
179 }
180 else
181 {
182 cp = dbPathStrictFilName("wspec", "models", "wrm", "r", 0) ;
183 if (cp)
184 {
185 fil = fopen (cp, "r");
186 messfree(cp);
187 }
188 else
189 fil = NULL;
190
191 if (!fil)
192 messExit ("Cannot open model file : wspec/models.wrm (%s)",
193 messSysErrorText()) ;
194 }
195
196 sword = stackCreate (64) ;
197 levelCount = arrayCreate (8,int) ;
198 array(levelCount,0,int) = 0 ;
199 line = 1 ;
200 }
201
202 arrayMax(aNode) = 0 ;
203 for (eol = FALSE ; !eol && !feof(fil) ; ++counter)
204 {
205 oldc = c ;
206 switch (c = (getc (fil) | inQuotes))
207 {
208 case '\r':
209 break; /* for windows, ignore carriage returns */
210 case '\t':
211 while (++counter%8) ;
212 --counter ; eow = TRUE ; break ;
213 case '/':
214 while((c = getc(fil)) && c != '\n' && !feof(fil) ) ;
215 /* empty line and fall through */
216 case '\n': case EOF:
217 eol = TRUE ; eow = TRUE ; inQuotes = FALSE ; break ;
218 case ' ':
219 eow = TRUE ; break ;
220 default: /* this includes the inQuotes modified characters */
221 c &= 127 ;
222 if (oldc != '\\' && c == '\"')
223 inQuotes = inQuotes ? 0 : 128 ; /* toggle */
224 if (!inword)
225 { if (!iNode) /* find level */
226 { for (level = 0 ; level < arrayMax(levelCount) ; level++)
227 if (arr(levelCount,level,int) >= counter)
228 break ;
229 if (level == arrayMax(levelCount) ||
230 arr(levelCount,level,int) > counter)
231 messcrash ("Model file: tree screwup line %d level %d",
232 line, level) ;
233 *plevel = level ;
234 }
235 array(levelCount,level,int) = counter ;
236 ++level ;
237 stackClear (sword) ;
238 inword = TRUE ; eow = FALSE ;
239 }
240 oldc = buf[0] = c ; /* a trick because push char fails on alpha */
241 catText (sword,buf) ;
242 }
243 if (inword && eow) /* end of word */
244 {
245 word = stackText(sword,0) ;
246
247 if ( *word == '@' ) /* a query */
248 { word++ ;
249 if (*word == '\"')
250 { char *cp ;
251 word++ ;
252 if (strlen(word))
253 { cp = word + strlen(word) - 1 ;
254 if (*cp == '\"')
255 *cp = 0 ;
256 }
257 }
258 if (lexMax (_VQuery) < 2) /* a hack: KEYKEY = 1 breaks treedisp */
259 lexaddkey("(NULL Query)", &dummy, _VQuery) ;
260 lexaddkey(word, arrayp(aNode,iNode,KEY), _VQuery) ;
261 }
262 #ifdef ACEDB4
263 else if ((*word == '?' || *word == '#') && /* Class pointer or type */
264 (table = pickMakeClass(word+1)))
265 /* Note that ?System is forbidden
266 * This is because implicit creation of a tag would create havock
267 */
268
269 { array(aNode,iNode,KEY) = KEYMAKE(table,*word == '#' ? 1 : 0) ;
270 if(!lexMax(table)) /* New class being defined */
271 lexaddkey(0,&dummy,table) ;
272 }
273 #else
274 else if (*word == '?') /* Class pointer */
275 {
276 pickMakeClass(word+1) ; /* declare the class */
277 if (iNode)
278 lexaddkey (word+1, &dummy, _VClass) ;
279 else /* Declaration of a new class */
280 {
281 *word = '#' ;
282 lexaddkey (word, &dummy, _VModel) ; /* interpret as a Model */
283 }
284 array(aNode,iNode,KEY) = dummy ;
285 }
286 else if (*word == '#') /* Constructed type */
287 {
288 lexaddkey (word, &dummy, _VModel) ;
289 array(aNode,iNode,KEY) = dummy ;
290 }
291 #endif
292 else
293 lexaddkey (word,arrayp(aNode,iNode,KEY),0) ;
294
295 ++iNode ;
296 inword = FALSE ;
297 }
298 }
299
300 #ifdef JUNK
301 if (MTEST) /* keep stderr here since it is for debugging */
302 { fprintf (stderr,"%d : ",*plevel) ;
303 for (iNode = 0 ; iNode < arrayMax(aNode) ; ++iNode)
304 fprintf (stderr,"%s ",name(arr(aNode,iNode,KEY))) ;
305 fprintf (stderr,"\n") ;
306 }
307 #endif
308
309 if (feof (fil))
310 {
311 fclose (fil) ;
312 fil = 0 ;
313 arrayDestroy (levelCount) ;
314 stackDestroy (sword) ;
315
316 if (pass == 1)
317 {
318 aceTmpDestroy (tmp_sysmodel);
319 pass = 2;
320 goto pass2 ;
321 }
322 pass = 1 ;
323 return FALSE ;
324 }
325
326 ++line ;
327
328 return TRUE ;
329 } /* getLine */
330
331 /*******************************************************/
332
333 static void getSubClasses(void)
334 {
335 KEY dummy ;
336 char *filename;
337 ACEIN subclasses_io;
338
339 if (lexword2key ("Class", &dummy, _VClass))
340 { /* fails during initial database construction */
341 KEYSET old =
342 query (0, "FIND Class Is_a_subclass_of OR Filter OR Mask") ;
343 OBJ obj ;
344 int i = keySetMax (old) ;
345
346 while (i--)
347 if ((obj = bsUpdate(keySet(old,i))))
348 {
349 if (bsFindTag (obj, _Is_a_subclass_of))
350 bsRemove(obj) ;
351 if (bsFindTag (obj, _Filter))
352 bsRemove(obj) ;
353 if (bsFindTag (obj, _Mask))
354 bsRemove(obj) ;
355 bsSave (obj) ;
356 }
357 }
358
359 filename = dbPathStrictFilName("wspec", "subclasses", "wrm", "r", 0) ;
360 if (filename)
361 {
362 subclasses_io = aceInCreateFromFile (filename, "r", "", 0);
363 if (subclasses_io)
364 parseAceInProtected (subclasses_io, 0);
365 /* will destroy subclasses_io */
366 messfree(filename);
367 }
368 return;
369 } /* getSubClasses */
370
371 /*******************************************************/
372
373 static void removeConstraints (void)
374 {
375 OBJ obj ;
376 KEY key = 0 ;
377
378 while (lexNext (_VClass, &key))
379 if ((obj = bsUpdate (key)))
380 { if (bsFindTag (obj, _Constraints))
381 bsRemove(obj) ;
382 bsSave (obj) ;
383 }
384
385 queryClearConstraints () ;
386
387 return;
388 } /* removeConstraints */
389
390 /*********/
391
392 static void getConstraints (void)
393 {
394 char *filename;
395 ACEIN constr_io;
396
397 removeConstraints () ;
398
399 /* check for file readability to avoid messerror */
400 filename = dbPathStrictFilName("wspec", "constraints", "wrm", "r", 0) ;
401 if (filename)
402 {
403 constr_io = aceInCreateFromFile (filename, "r", "", 0);
404 messfree(filename);
405 if (constr_io)
406 {
407 /* allow parsing data for protected classes */
408 parseAceInProtected (constr_io, 0) ; /* will destroy constr_io */
409 pickRegisterConstraints () ;
410 }
411 }
412
413 return;
414 } /* getConstraints */
415
416 /******************************************************************/
417 #ifdef ACEDB4
418
419 static void cleanUpModels (BS bs)
420 {
421 if (bs->right)
422 cleanUpModels (bs->right) ;
423 if (bs->down)
424 cleanUpModels (bs->down) ;
425 if (class(bs->key) > 1) /* not on tags */
426 { char buf[256] ;
427 switch (KEYKEY (bs->key))
428 {
429 case 0:
430 sprintf(buf, "?%s",className(bs->key)) ;
431 lexaddkey (buf, &bs->key, _VModel) ;
432 break ;
433 case 1:
434 sprintf(buf, "#%s",className(bs->key)) ;
435 lexaddkey (buf, &bs->key, _VModel) ;
436 break ;
437 }
438 }
439
440 return;
441 } /* cleanUpModels */
442
443 /******************************************************************/
444
445 static void aliasModels (void)
446 {
447 KEY k = 0, k1 ;
448 char *cp, buf [256] ;
449
450 while (lexNext(_VModel, &k))
451 { cp = name (k) ;
452 if (*cp == '#')
453 { sprintf(buf, "?%s",cp + 1) ;
454 if (lexword2key (buf, &k1, _VModel))
455 lexAlias (k, buf, FALSE, TRUE) ;
456 }
457 }
458
459 return;
460 } /* aliasModels */
461
462 /******************************************************************/
463
464 static void duplicateModel(OBJ Model)
465 {
466 KEY model ;
467 OBJ obj ;
468
469 if(class(Model->key) == _VModel) /* prevent loops */
470 return ;
471 lexaddkey(name(Model->key), &model, _VModel) ;
472 obj = bsUpdate(model) ;
473 bsTreePrune(obj->root) ;
474 obj->root = bsTreeCopy(Model->root) ;
475 obj->root->key = model ;
476 cacheMark(obj->x) ;
477 cleanUpModels (obj->root->right) ;
478 obj->flag |= OBJFLAG_TOUCHED ;
479 bsSave(obj) ;
480
481 return;
482 } /* duplicateModel */
483
484
485 /********************************************************************/
486 /* Verify that the obj0 model is the same as the Model class model */
487
488 static BOOL treeCompare(BS one, BS two)
489 {
490 int fault = 0;
491 static char buf[256]; /* recursive routine, save memory */
492
493 if (class(one->key) > 1 && (KEYKEY(one->key) == 0 || KEYKEY(one->key) == 1))
494 { sprintf(buf, "?%s",className(one->key)) ;
495 if (strcmp(buf, name(two->key)) != 0)
496 fault++;
497 }
498 else
499 { if (one->key != two->key)
500 fault++;
501 }
502
503 if (one->down)
504 { if (two->down)
505 { if (!treeCompare(one->down, two->down))
506 fault++;
507 }
508 else
509 fault++;
510 }
511 else
512 { if (two->down)
513 fault++;
514 }
515
516 if (one->right)
517 { if (two->right)
518 { if (!treeCompare(one->right, two->right))
519 fault++;
520 }
521 else
522 fault++;
523 }
524 else
525 { if (two->right)
526 fault++;
527 }
528
529 return (fault == 0);
530 } /* treeCompare */
531
532 #ifdef VERIFY_MODELS
533
534 static BOOL verifyModel(int class)
535 { KEY modelKey, zeroKey;
536 OBJ zero, model;
537 BOOL ok = TRUE;
538
539 if ((pickList[class].type == 'B') && pickList[class].model)
540 { zeroKey = KEYMAKE(class, 0);
541 modelKey = pickList[class].model;
542
543 zero = bsCreate(zeroKey);
544 model = bsCreate(modelKey);
545
546 if (zero && zero->root->right &&
547 model && model->root->right &&
548 !treeCompare(zero->root->right, model->root->right))
549 ok = FALSE;
550
551 bsDestroy(zero);
552 bsDestroy(model);
553 }
554
555 return ok;
556 } /* verifyModel */
557
558 void verifyModels(void)
559 {
560 int i;
561
562 for (i = 1 ; i < 256 ; ++i)
563 if (!verifyModel(i))
564 messout("Failed to verify class %s.\n", className(KEYMAKE(i,0)));
565
566 return;
567 } /* verifyModels */
568 #endif /* VERIFY_MODELS */
569
570 #else /* ACEDB4 */
571 static void duplicateSessionModel (OBJ Model)
572 {
573 KEY model ;
574 OBJ obj ;
575
576 lexaddkey ("SessionModel",&model,_VSession) ;
577 obj = bsUpdate(model) ;
578 bsTreePrune(obj->root) ;
579 obj->root = bsTreeCopy(Model->root) ;
580 obj->root->key = model ;
581 cacheMark(obj->x) ;
582 /* cleanUpModels (obj->root->right) ; */
583 obj->flag |= OBJFLAG_TOUCHED ;
584 bsSave(obj) ;
585
586 return;
587 } /* duplicateSessionModel */
588
589 #endif /* ACEDB4 */
590
591 /******************************************************************/
592
593 /* updates the models
594 * called by update.c and readModels()
595 */
596
597 BOOL getNewModels (void)
598 {
599 Array aNode,aDown ;
600 KEY key = 0, oldKey = 0 ;
601 OBJ obj = 0 ;
602 BS bs=0 ;
603 int i,level ;
604 int table = 0 ;
605 VoidRoutine previousCrashRoutine ;
606 extern BOOL READING_MODELS; /* from bssubs.c */
607
608 if (!isWriteAccess())
609 { messout ("Sorry, you do not have Write Access");
610 return FALSE ;
611 }
612
613 pickInit () ;
614
615 pickGetClassProperties(FALSE); /* see display.c for the reason for this. */
616 sysClassDisplayTypes(); /* and this. What a mess */
617
618 previousCrashRoutine = messcrashroutine ;
619 messcrashroutine = simpleCrashRoutine; /* don't allow crash recovery */
620
621 cleanOldModels () ;
622 cleanVisibility () ;
623 sysClassOptions () ;
624
625 aNode = arrayCreate (4,KEY) ;
626 aDown = arrayCreate (8,BS) ; /* available down pointers */
627 s1 = stackCreate(500) ; /* to store queries and so on */
628 pushText(s1, "dummy") ; /* to move stackMark(s1) away from 0 and 1 */
629 READING_MODELS = TRUE ;
630
631 while (getLine (aNode,&level))
632 if (arrayMax(aNode))
633 { i = 0 ;
634 key = arr(aNode,i,KEY) ;
635 if
636 #ifdef ACEDB4
637 (!KEYKEY(key) || KEYKEY(key) == 1) /* new class */
638 #else
639 /* (class(key) == _VClass || */ ( class(key) == _VModel)
640 #endif
641 { i++ ;
642 if (level)
643 messcrash ("%s starts line but not new class at line %d",
644 name(key), line) ;
645 if (obj) /* close it */
646 {
647 oldKey = bsKey (obj) ;
648 if (MTEST)
649 bsTreeDump (obj->root) ;
650 cacheMark (obj->x) ;
651 #ifdef ACEDB4
652 duplicateModel(obj) ;
653 #else
654 if (!strcmp (name(oldKey),"#Session"))
655 duplicateSessionModel (obj) ;
656 #endif
657 obj->flag |= OBJFLAG_TOUCHED ;
658 bsSave (obj) ;
659 #ifdef ACEDB4
660 bsMakePaths (table) ;
661 #else
662 bsMakePaths (oldKey) ;
663 #endif
664 }
665 #ifdef ACEDB4
666 table = class(key) ; key = KEYMAKE(table,0) ;
667 #else
668 table = KEYKEY(key) ;
669 #endif
670 obj = bsUpdate (key) ;
671 if (!obj)
672 messExit ("Can not form model for %s at line %d",
673 name(key), line) ;
674 bs = obj->root ;
675 bs->right = BSalloc() ; /* very first addition is right */
676 bs->right->up = bs ;
677 bs = bs->right ;
678 bs->key = arr (aNode,i++,KEY) ;
679 ++level ; /* hocus pocus */
680 array(aDown,level++,BS) = bs ;
681 }
682 else if (level <= 0 || level > arrayMax(aDown))
683 messExit ("Model screwup: impossible level line %d",
684 line) ;
685 else
686 { bs = arr(aDown,level,BS) ;
687 bs->down = BSalloc() ; /* first addition is down */
688 bs->down->up = bs ;
689 bs = bs->down ;
690 bs->key = arr (aNode,i++,KEY) ;
691 array(aDown,level++,BS) = bs ;
692 }
693
694 while (i < arrayMax(aNode))
695 { bs->right = BSalloc () ; /* rest are right */
696 bs->right->up = bs ;
697 bs = bs->right ;
698 bs->key = arr (aNode,i++,KEY) ;
699 array(aDown,level++,BS) = bs ;
700 }
701 arrayMax(aDown) = level ;
702 }
703
704 if (obj)
705 { if (MTEST)
706 bsTreeDump (obj->root) ;
707 cacheMark (obj->x) ;
708 #ifdef ACEDB4
709 duplicateModel (obj) ;
710 #endif
711 obj->flag |= OBJFLAG_TOUCHED ;
712 oldKey = bsKey (obj) ;
713 bsSave (obj) ;
714 #ifdef ACEDB4
715 bsMakePaths (table) ;
716 #else
717 bsMakePaths (oldKey) ;
718 #endif
719 }
720
721 getSubClasses() ;
722 getConstraints () ;
723 READING_MODELS = FALSE ;
724 arrayDestroy(aDown) ;
725 arrayDestroy(aNode) ;
726 stackDestroy(s1) ;
727 #ifdef ACEDB4
728 aliasModels () ;
729 #endif
730 pickCheckConsistency() ;
731
732
733 lexAlphaClassList (TRUE) ; /* re alpha order class names */
734 /* used to remake class lists etc. when models change */
735 if (modelChangeRoutine)
736 (*modelChangeRoutine)();
737
738 {
739 KEYSET ks = checkModelSyntax () ;
740 messdump ("Read models got %d models", keySetMax(ks)) ;
741 keySetDestroy (ks) ;
742 }
743
744 bIndexNewModels() ;
745
746
747 #ifdef VERIFY_MODELS
748 verifyModels();
749 #endif /* VERIFY_MODELS */
750
751 messcrashroutine = previousCrashRoutine;
752
753 return TRUE ;
754 } /* getNewModels */
755
756 /*******************************************************/
757 /***** Authorisation checks ****************************/
758
759 BOOL readModels (void)
760 {
761
762 /* To read models you must have admin access to the database. */
763 if (!sessionAdminUser("re-read the models"))
764 {
765 return FALSE ;
766 }
767
768 if (thisSession.session != 1)
769 /* if we're re-reading the models not during the bootstrapping
770 of an empty database we may need to ask a few more questions */
771 {
772 if (isWriteAccess())
773 {
774 if (messQuery ("You did not save the current session: "
775 "should I ?"))
776 sessionClose (TRUE) ;
777 else
778 return FALSE ; /* don't want to save, can't proceed */
779 }
780
781 if (!messQuery ("Watch out ! An erroneous modification of "
782 "the models may screw up the system.\n"
783 "Do you want to proceed?"))
784 return FALSE ;
785
786 /* we either got here without write access or
787 sessionClose dropped it ... */
788
789 if (!sessionGainWriteAccess ())
790 /* ... we are therefore guaranteed that the write access
791 will change. That will re-draw and change buttons/menus
792 if necessary */
793 {
794 messout("Sorry, you cannot gain write access. ",
795 "I cannot update the models.") ;
796 return FALSE ;
797 }
798 }
799
800 getNewModels() ;
801
802
803 return TRUE ; /* else it will have crashed */
804 } /* readModels */
805
806 /**********************************************************************/
807 /**************** syntax check - everything now done here *************/
808
809 typedef struct
810 { KEY fromModel ;
811 KEY fromTag ;
812 KEY toModel ;
813 KEY toTag ;
814 } XrefInfo ;
815
816 static Array xinfo ; /* all XREF info */
817
818 /* #define DEBUG 1 */
819 static int depth = 1 ;
820
821 static BOOL atRoot ;
822
823
824 /*
825
826 Two choices here, STRICT_TAGNAMES is
827
828 [a-zA-Z][a-zA-Z0-9_]*
829
830 without STRICT_TAGNAMES
831
832 [a-zA-Z0-9_]* and at least one char must be a-zA-Z
833
834 */
835
836 #ifdef STRICT_TAGNAMES
837
838 static BOOL validTagname(char *tag)
839 {
840 if (!isalpha(*tag++))
841 return FALSE;
842
843 while (isalnum(*tag) || (*tag == '_'))
844 tag++;
845
846 if (*tag)
847 return FALSE;
848
849 return TRUE;
850 }
851
852 #else
853
854 static BOOL validTagname(char *tag)
855 {
856 BOOL seenAlpha = FALSE;
857
858 while (isalnum((int)*tag) || (*tag == '_'))
859 {
860 if (isalpha((int)*tag))
861 seenAlpha = TRUE;
862 tag++;
863 }
864
865 if (*tag)
866 return FALSE;
867
868 return seenAlpha;
869 }
870
871 #endif
872
873 static void checkRecurse (KEY model, BS bs, BOOL inTags,
874 Associator tag2right)
875 {
876 char *zero = 0 ;
877
878 #ifdef DEBUG
879 { int i ;
880 for (i = 2*depth ; i-- ;)
881 putchar (' ') ;
882 printf ("%s\n", name(bs->key)) ;
883 }
884 #endif
885
886 if (atRoot)
887 atRoot = FALSE ;
888 else if (bs->key == _UNIQUE)
889 { if (bs->down)
890 messerror ("Model error: DOWN from UNIQUE in model %s", name(model)) ;
891 if (!bs->right)
892 messerror ("Model error: UNIQUE in model %s with nothing to the right",
893 name(model)) ;
894 }
895 else if (bs->key == _REPEAT)
896 { if (bs->right)
897 messerror ("Model error: something right of REPEAT in model %s",
898 name(model)) ;
899 if (bs->down)
900 messerror ("Model error: something down from REPEAT in model %s",
901 name(model)) ;
902 return ;
903 }
904 else if (bs->key == _COORD)
905 { if (bs->down)
906 messerror ("Model error: something down from COORD in model %s",
907 name(model)) ;
908 if (bs->up->key != _Int && bs->up->key != _Float)
909 messerror ("Model error: COORD not preceeded by Int or Float in model %s",
910 name(model)) ;
911 return ;
912 }
913 else if (class(bs->key) == _VQuery && !atRoot)
914 { if (bs->right)
915 messerror ("Model error: something right of attach query %s in model %s",
916 name(bs->key), name(model)) ;
917 if (bs->down)
918 messerror ("Model error: something down from attach query %s in model %s",
919 name(bs->key), name(model)) ;
920 return ;
921 }
922 else if (bs->key == _ANY)
923 { if (bs->down)
924 messerror ("Model error: ANY with something DOWN in model %s") ;
925 }
926 else if (class(bs->key) == _VModel) /* check subtypes */
927 { KEY key ;
928
929 lexword2key (name(bs->key), &key, _VModel) ;
930 if (key != bs->key) /* then bs->key is #..., a type */
931 { OBJ obj ;
932
933 if (bs->right)
934 messerror ("Model error: something right of subtype %s in model %s",
935 name(bs->key), name(model)) ;
936 if (bs->down)
937 messerror ("Model error: something down from subtype %s in model %s",
938 name(bs->key), name(model)) ;
939 if (!(obj = bsCreate (bs->key)))
940 messerror ("Model error: no model for subtype %s in model %s",
941 name(bs->key), name(model)) ;
942 else
943 { if (!obj->root->right)
944 messerror ("Model error: empty model for subtype %s in model %s",
945 name(bs->key), name(model)) ;
946 bsDestroy (obj) ;
947 }
948 }
949 inTags = FALSE ;
950 }
951 else if (bs->key < _LastN || class (bs->key))
952 inTags = FALSE ;
953 else /* a tag */
954 {
955 if (!validTagname(name(bs->key)))
956 messerror("Model error: invalid tag name %s in model %s",
957 name(bs->key), name(model)) ;
958 if (!inTags)
959 messerror ("Model error: tag %s outside rooted subtree in model %s",
960 name(bs->key), name(model)) ;
961 if (assFind (tag2right, zero + bs->key, 0))
962 messerror ("Model error: duplicate tag %s in model %s",
963 name(bs->key), name(model)) ;
964 else if (!bs->right || (bs->right->key == _UNIQUE && !bs->right->right))
965 assInsert (tag2right, zero + bs->key, zero) ;
966 else if (bs->right->key != _UNIQUE)
967 assInsert (tag2right, zero + bs->key, zero + bs->right->key) ;
968 else
969 assInsert (tag2right, zero + bs->key, zero + bs->right->right->key) ;
970 }
971
972 if (!bs->right) /* done */
973 return ;
974
975 if (bs->right->key == _XREF)
976 {
977 if (class(bs->key) != _VModel && class(bs->key) != _VClass)
978 messerror ("Model error: XREF from %s not a class in model %s",
979 name(bs->key), name(model)) ;
980 if (bs->right->down)
981 messerror ("Model error: DOWN from XREF %s in model %s",
982 name(bs->key), name(model)) ;
983 if (!bs->right->right || class(bs->right->right->key) ||
984 bs->right->right->key < _LastN)
985 { messerror ("Model error: no tag after XREF from %s in model %s",
986 name(bs->key), name(model)) ;
987 return ;
988 }
989 if (bs->right->right->down)
990 messerror ("Model error: down from tag after XREF from %s in model %s",
991 name(bs->key), name(model)) ;
992 { XrefInfo *inf = arrayp(xinfo, arrayMax(xinfo), XrefInfo) ;
993 inf->fromModel = model ;
994 if (bs->up->key == _UNIQUE)
995 inf->fromTag = bs->up->up->key ;
996 else
997 inf->fromTag = bs->up->key ;
998 #ifdef ACEDB4
999 inf->toModel = bs->key ;
1000 #else
1001 if (class(bs->key) == _VModel)
1002 inf->toModel = bs->key ;
1003 else
1004 lexword2key(messprintf("#%s",name(bs->key)), &(inf->toModel), _VModel) ;
1005 if (!inf->toModel) invokeDebugger() ;
1006 #endif
1007 inf->toTag = bs->right->right->key ;
1008 }
1009 bs = bs->right->right ; /* eat up XREF and tag */
1010 if (!bs->right)
1011 return ;
1012 }
1013
1014 bs = bs->right ;
1015 ++depth ;
1016 if (bs->down) do
1017 { if (bs->key < _LastN || class (bs->key))
1018 messerror ("Model error: non-tag %s in column in model %s",
1019 name(bs->key), name(model)) ;
1020 checkRecurse (model, bs, inTags, tag2right) ;
1021 } while ((bs = bs->down)) ;
1022 else
1023 checkRecurse (model, bs, inTags, tag2right) ;
1024 --depth ;
1025
1026 return;
1027 } /* checkRecurse */
1028
1029 static KEYSET checkModelSyntax (void)
1030 {
1031 int i ;
1032 KEY model ;
1033 OBJ obj ;
1034 Associator tag2right ;
1035 Array tagsInModel ; /* of Arrays */
1036 KEYSET ks = keySetCreate() ;
1037 char *zero = 0 , *cp ;
1038 int errorCount ;
1039
1040 tagsInModel = arrayCreate (256, Array) ;
1041 xinfo = arrayCreate (256, XrefInfo) ;
1042
1043 errorCount = messErrorCount() ;
1044
1045 for (i = 1 ; i < lexMax(_VModel) ; ++i)
1046 { model = KEYMAKE(_VModel, i) ;
1047 if (iskey(model) != 2)
1048 continue ;
1049 keySet(ks, keySetMax(ks)) = model ;
1050 obj = bsCreate (model) ;
1051 if (obj->root->right)
1052 { tag2right = array (tagsInModel, i, Associator) = assCreate () ;
1053 #ifdef DEBUG
1054 printf ("%s\n", name(model)) ;
1055 #endif
1056 atRoot = TRUE ;
1057 checkRecurse (model, obj->root, TRUE, tag2right) ;
1058 }
1059 bsDestroy (obj) ; /* mieg */
1060 }
1061
1062 for (i = 0 ; i < arrayMax (xinfo) ; ++i)
1063 { XrefInfo *inf = arrp(xinfo, i, XrefInfo) ;
1064 KEY toModel ;
1065 #ifdef DEBUG
1066 printf ("%d: in model %s, tag %s XREF's to tag %s in model %s\n",
1067 i, name(inf->fromModel), name(inf->fromTag), name(inf->toTag), name(inf->toModel)) ;
1068 #endif
1069 toModel = inf->toModel ;
1070 if ((tag2right = array (tagsInModel, KEYKEY(toModel), Associator)))
1071 { if (!assFind (tag2right, zero + inf->toTag, &cp))
1072 messerror ("Model error: In model %s, tag %s XREF's to tag %s in class %s\n"
1073 " can't find tag %s in model %s",
1074 name(inf->fromModel), name(inf->fromTag), name(inf->toTag), name(inf->toModel),
1075 name(inf->toTag), name(toModel)) ;
1076 else
1077 {
1078 /* there is a problem when using this paragraph because of XREF embedded in contructed types
1079 KEY backClass = cp - zero ;
1080
1081 if (strcasecmp (name(backClass) + 1, name(inf->fromModel) + 1))
1082 messerror ("Model error: In model %s, tag %s XREF's to tag %s in class %s\n"
1083 " reverse tag %s in model %s points to %s, not %s",
1084 name(inf->fromModel), name(inf->fromTag), name(inf->toTag), name(inf->toModel),
1085 name(inf->toTag), name(toModel), name(backClass), name(inf->fromModel)) ;
1086 */
1087 }
1088 }
1089 else
1090 messerror ("Model error: In model %s, tag %s XREF's to tag %s in class %s\n"
1091 " can't find model %s",
1092 name(inf->fromModel), name(inf->fromTag), name(inf->toTag), name(inf->toModel),
1093 name(toModel)) ;
1094 }
1095
1096
1097 /* Any errors in the model file and we exit..no save attempted. */
1098 if (messErrorCount() != errorCount) messExit ("Errors in model file") ;
1099
1100
1101 /* clean up memory */
1102 for (i = 1 ; i < lexMax(_VModel) ; ++i)
1103 assDestroy (array (tagsInModel, i, Associator)) ;
1104 arrayDestroy (xinfo) ;
1105 arrayDestroy (tagsInModel) ;
1106
1107 return ks ;
1108 } /* checkModelSyntax */
1109
1110
1111 /********************************************************/
1112
1113 static void showModelNodeJaq (ACEOUT fo, BS bs, int x, int *y,
1114 KEY lastTag, int dLastTag)
1115 {
1116 STORE_HANDLE h ;
1117
1118 if (!bs) return ;
1119 if (bs->key == _XREF)
1120 { if (!(bs = bs->right)) return ;
1121 if (!(bs = bs->right)) return ;
1122 }
1123 if (bs->key == _UNIQUE)
1124 if (!(bs = bs->right)) return ;
1125 if (bs->key == _REPEAT)
1126 return ;
1127
1128 h = handleCreate() ;
1129
1130 while (bs)
1131 { char *qname ;
1132 char *tname ;
1133
1134 if (x == 1 && *y == 1)
1135 qname = "NAME" ;
1136 else if (!class(bs->key) && bs->key > _LastN)
1137 { qname = strnew (messprintf ("EXISTS %s", name(bs->key)), h) ;
1138 lastTag = bs->key ;
1139 dLastTag = 0 ;
1140 }
1141 else
1142 { if (!lastTag)
1143 qname = strnew (messprintf ("[%d]", dLastTag+1), h) ;
1144 else if (dLastTag > 0)
1145 qname = strnew (messprintf ("%s[%d]", name(lastTag), dLastTag+1), h) ;
1146 else
1147 qname = name(lastTag) ;
1148 ++dLastTag ;
1149 }
1150
1151 if (!class(bs->key))
1152 tname = name(bs->key) ;
1153 else if (pickType(bs->key) == 'A')
1154 tname = strnew (messprintf ("@%s", className(bs->key)), h) ;
1155 else if (KEYKEY(bs->key) == 1)
1156 tname = strnew (messprintf ("#%s", className(bs->key)), h) ;
1157 else
1158 tname = name(bs->key) ;
1159
1160 #ifdef ACEDB4
1161 if (class(bs->key) == _VModel)
1162 aceOutPrint (fo, "%d %d |#%s|%s|\n", *y, x, name(bs->key)+1, qname) ;
1163 else
1164 #endif
1165 aceOutPrint (fo, "%d %d |%s|%s|\n", *y, x, tname, qname) ;
1166 showModelNodeJaq (fo, bs->right, x + strlen(tname) + 1, y, lastTag, dLastTag) ;
1167 if ((bs = bs->down))
1168 ++*y ;
1169 }
1170
1171 handleDestroy (h) ;
1172 } /* showModelNodeJaq */
1173
1174
1175 void showModelJaq (ACEOUT fo, KEY key) /* key is in class Model */
1176 {
1177 OBJ obj ;
1178 int x, y ;
1179
1180 #ifdef ACEDB4
1181 key = KEYMAKE(pickWord2Class(name(key)+1), 0) ;
1182 #endif /* ACEDB */
1183
1184 obj = bsCreate (key) ;
1185
1186 if (!obj) return ;
1187
1188 x = 1 ; y = 1 ;
1189 showModelNodeJaq (fo, obj->root, x, &y, 0, 0) ;
1190
1191 bsDestroy (obj) ;
1192
1193 return;
1194 } /* showModelJaq */
1195
1196 /********************************************************/
1197 /********************************************************/
1198
1199
1200
1201
1202