home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************
- * gnosis.c
- *
- * Prognosis Management Package
- *
- * Written by Paco Xander Nathan
- * ⌐1990, Motorola Inc. Public domain source code.
- ********************************************************************************/
-
- #include "applic.h"
- #include "window.h"
- #include "dialog.h"
- #include "error.h"
- #include "string.h"
- #include "text.h"
- #include "document.h"
- #include "neural.h"
-
- #include "test.h"
- #include "gnosis.h"
- #include "analysis.h"
-
-
- #define GNOS_BITS 181
-
-
- /* Local Data Structures
- */
-
-
- /* External Data Structures
- */
- GnosList
- gnosList = { 0, NULL, NULL };
-
-
- /* Local Function Prototypes
- */
- #ifdef PROTOTYPES
- #endif
-
-
- /* Allocate a new prognosis record and insert it at the head of the list
- */
- GnosPtr
- GnosNew ()
- {
- register GnosPtr gnosPtr = NULL;
-
- GnosSave();
-
- if (gnosPtr = (GnosPtr) ErrNewPtr(sizeof(GnosData))) {
- gnosPtr->next = gnosList.head;
- gnosList.head = gnosPtr;
- gnosList.count++;
-
- gnosPtr->textHdl = (Handle) ErrNewHandle(0L);
- }
-
- return gnosPtr;
- }
-
-
- void
- GnosDelete ()
- {
- register GnosPtr gnosPtr = gnosList.head;
-
- if (gnosList.open == gnosList.head)
- gnosList.head = gnosPtr->next;
- else {
- }
-
- gnosList.count--;
- gnosList.open = NULL;
-
- DisposHandle(gnosPtr->textHdl);
- DisposPtr(gnosPtr);
-
- WindSwitch(dPtrAnal, FALSE);
- WindSwitch(wPtrText, FALSE);
- }
-
-
- /* Read the set of prognosis from the opened resource file and display the
- * titles in the resource list window
- */
- void
- GnosRefresh ()
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(wPtrGnos);
- register ListHandle listHdl = infoPtr->item.list;
- register GnosPtr gnosPtr = gnosList.head;
- register Cell theCell;
- Str255 itemText;
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(wPtrGnos);
-
- /* Resize the list length
- */
- LDoDraw(FALSE, listHdl);
- LDelRow(0, 0, listHdl);
- theCell.h = 0;
- theCell.v = 0;
-
- for (theCell.v = 0; gnosPtr; gnosPtr = gnosPtr->next) {
- ApplSpinCurs(FALSE);
- StrPCpy(itemText, gnosPtr->title);
-
- LAddRow(1, 0, listHdl);
- LSetCell(itemText + 1, itemText[0], theCell, listHdl);
- }
-
- /* Switch to the resource list window and redraw it
- */
- InvalRect(&wPtrGnos->portRect);
- LDoDraw(TRUE, listHdl);
-
- if (!((WindowPeek) wPtrGnos)->visible)
- WindSwitch(wPtrGnos, TRUE);
-
- SetPort(savePort);
- }
-
-
- /* Open the indexed prognosis analysis
- */
- void
- GnosOpenAnal (theGnos)
- register WORD theGnos;
- {
- register GnosPtr gnosPtr = gnosList.head;
- register WORD theIndex;
-
- if ((theGnos >= 0) && (theGnos < gnosList.count)) {
- for (theIndex = gnosList.count - 1; theIndex > theGnos; theIndex--)
- gnosPtr = gnosPtr->next;
-
- gnosList.open = gnosPtr;
-
- AnalPutBuf(gnosPtr);
- WindSwitch(dPtrAnal, TRUE);
- }
- }
-
-
- /* Open the indexed prognosis description text
- */
- void
- GnosOpenText (WORD theGnos)
- {
- register GnosPtr gnosPtr = gnosList.head;
- register SignedByte hdlState;
- register WORD theIndex;
-
- if ((theGnos >= 0) && (theGnos < gnosList.count)) {
- for (theIndex = gnosList.count - 1; theIndex > theGnos; theIndex--)
- gnosPtr = gnosPtr->next;
-
- hdlState = HGetState(gnosPtr->textHdl);
- HLock(gnosPtr->textHdl);
-
- TextWindReset(wPtrText);
- TextPutText(*(gnosPtr->textHdl), GetHandleSize(gnosPtr->textHdl), wPtrText);
-
- HSetState(gnosPtr->textHdl, hdlState);
- WindSwitch(wPtrText, TRUE);
- }
- }
-
-
- /* Save the current prognosis
- */
- void
- GnosSave ()
- {
- register TEHandle teHdl = ((InfoPtr) GetWRefCon(wPtrText))->item.text.teHdl;
- register SignedByte hdlState;
-
- if (gnosList.open && !testUser) {
- /* First, read the analysis
- */
- if (((WindowPeek) dPtrAnal)->visible)
- AnalGetBuf(gnosList.open);
-
- /* Next, read the description text
- */
- if (((WindowPeek) wPtrText)->visible) {
- hdlState = HGetState(gnosList.open->textHdl);
- HLock(gnosList.open->textHdl);
-
- SetHandleSize(gnosList.open->textHdl, (Size) (*teHdl)->teLength);
- BlockMove(*(TEGetText(teHdl)), *gnosList.open->textHdl, (Size) (*teHdl)->teLength);
-
- HSetState(gnosList.open->textHdl, hdlState);
- }
-
- gnosList.open = NULL;
- }
- }
-
-
- /* Close all the prognosis
- */
- void
- GnosClose ()
- {
- register GnosPtr gnosPtr = gnosList.head;
-
- GnosSave();
-
- for (; gnosList.count--; ) {
- gnosList.head = gnosPtr->next;
- DisposHandle(gnosPtr->textHdl);
- DisposPtr(gnosPtr);
-
- gnosPtr = gnosList.head;
- }
-
- gnosList.count = 0;
- gnosList.head = NULL;
- gnosList.open = NULL;
-
- WindSwitch(wPtrGnos, FALSE);
- WindSwitch(dPtrAnal, FALSE);
- WindSwitch(wPtrText, FALSE);
- }
-
-
- /* Handle a click inside the prognosis list window
- */
- void
- GnosClick (theCell, dblClick)
- register Cell theCell;
- register Boolean dblClick;
- {
- /* First, save any current prognosis and update titles
- */
- GnosSave();
- GnosRefresh();
-
- /* Open the selected prognosis
- */
- GnosOpenAnal(theCell.v);
- GnosOpenText(theCell.v);
- }
-
-
- /* Convert from the character based gnos data pointer to the bitmap
- * required to condition the neural network
- */
- void
- GnosStuffBits (gnosPtr, gnosBits)
- register GnosPtr gnosPtr;
- register Boolean *gnosBits;
- {
- register WORD i, value, theBit = 0;
-
- for (i = MAXPAD * 2; --i;) {
- value = gnosPtr->color[0][i] - '0';
- gnosBits[theBit++] = (value & 0x01) ? TRUE : FALSE;
- gnosBits[theBit++] = (value & 0x02) ? TRUE : FALSE;
- gnosBits[theBit++] = (value & 0x04) ? TRUE : FALSE;
- }
-
- ApplSpinCurs(FALSE);
-
- for (i = MAXPAD * 2; --i;) {
- value = gnosPtr->pair[0][i] - '0';
- gnosBits[theBit++] = (value & 0x01) ? TRUE : FALSE;
- gnosBits[theBit++] = (value & 0x02) ? TRUE : FALSE;
- gnosBits[theBit++] = (value & 0x04) ? TRUE : FALSE;
- }
-
- ApplSpinCurs(FALSE);
-
- for (i = MAXPAD * 2; --i;) {
- switch (gnosPtr->mark[0][i]) {
- case '+':
- value = 0;
- break;
- case 'x':
- value = 1;
- break;
- case '=':
- value = 2;
- break;
- case '-':
- value = 3;
- break;
- default:
- break;
- }
-
- gnosBits[theBit++] = (value & 0x01) ? TRUE : FALSE;
- gnosBits[theBit++] = (value & 0x02) ? TRUE : FALSE;
- }
-
- ApplSpinCurs(FALSE);
-
- for (i = MAXPAD * 2; --i;) {
- switch (gnosPtr->anxiety[0][i]) {
- case ' ':
- value = 0;
- break;
- case 'A':
- value = 1;
- break;
- case 'C':
- value = 2;
- break;
- default:
- break;
- }
-
- gnosBits[theBit++] = (value & 0x01) ? TRUE : FALSE;
- gnosBits[theBit++] = (value & 0x02) ? TRUE : FALSE;
- }
-
- ApplSpinCurs(FALSE);
-
- for (i = MAXPAD * 2; --i;) {
- switch (gnosPtr->stress[0][i]) {
- case ' ':
- value = 0;
- break;
- case '1':
- value = 1;
- break;
- case '2':
- value = 2;
- break;
- default:
- break;
- }
-
- gnosBits[theBit++] = (value & 0x01) ? TRUE : FALSE;
- gnosBits[theBit++] = (value & 0x02) ? TRUE : FALSE;
- }
-
- ApplSpinCurs(FALSE);
-
- for (; theBit < GNOS_BITS; theBit++)
- gnosBits[theBit] = FALSE;
- }
-
-
- /* Teach the neural network using all the saved prognoses
- */
- void
- GnosLearn ()
- {
- register GnosPtr gnosPtr = gnosList.head;
- register WORD theGnos;
- Boolean gnosBits[GNOS_BITS];
-
- /* Clear any previous neural net and get a new one
- */
- NNInit();
-
- /* Now class, please study each chapter in your prognosis list
- */
- for (theGnos = 0; theGnos < gnosList.count; theGnos++, gnosPtr = gnosPtr->next) {
- ApplSpinCurs(FALSE);
- GnosStuffBits(gnosPtr, gnosBits);
- NNLearn(gnosBits, GNOS_BITS, theGnos);
- }
- }
-
-
- /* Use the neural network to infer saved prognoses
- */
- void
- GnosRecall ()
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(wPtrGnos);
- register ListHandle listHdl = infoPtr->item.list;
- register Cell theCell;
- Boolean gnosBits[GNOS_BITS];
- WORD theLen = 255;
- Str255 itemText;
- LONG theNum;
-
- /* Convert the current analysis into a network input vector, then activate
- */
- AnalReadWind();
- GnosStuffBits(&analGnosBuf, gnosBits);
- NNActivate(gnosBits, GNOS_BITS);
-
- /* Clear the certainty column
- */
- theCell.h = 1;
-
- for (theCell.v = (*listHdl)->dataBounds.bottom; theCell.v--; )
- LClrCell(theCell, listHdl);
-
- /* Report the certainty factor
- */
- sprintf((char *) itemText, "%5.2f%% certainty", nnCertainty);
- CtoPstr((char *) itemText);
-
- /* Reflect the index
- */
- theCell.h = 1;
- theCell.v = gnosList.count - (nnOutput + 1);
- LSetCell(itemText + 1, itemText[0], theCell, listHdl);
-
- /* Report the description text, but only in user mode...
- */
- if (testUser)
- GnosOpenText(theCell.v);
- else
- SelectWindow(wPtrGnos);
- }
-
-
- /* Execute the selected Prognosis menu item
- */
- void
- GnosMenu (theItem)
- register WORD theItem;
- {
- register InfoPtr infoPtr = (InfoPtr) GetWRefCon(wPtrGnos);
-
- switch (theItem) {
- case gnosNew:
- GnosNew();
- AnalStub();
- AnalWriteWind();
- AnalGetBuf(gnosList.head);
-
- GnosRefresh();
-
- GnosOpenAnal(gnosList.count - 1);
- GnosOpenText(gnosList.count - 1);
-
- if (!testUser)
- infoPtr->dirty = TRUE;
-
- break;
-
- case gnosDelete:
- GnosDelete();
- GnosRefresh();
-
- if (!testUser)
- infoPtr->dirty = TRUE;
-
- break;
-
- case gnosAnalyze:
- AnalReadWind();
- AnalReport();
-
- if (!testUser)
- infoPtr->dirty = TRUE;
-
- break;
-
- case gnosLearn:
- GnosSave();
- GnosRefresh();
- GnosLearn();
- break;
-
- case gnosRecall:
- GnosRecall();
- break;
-
- default:
- break;
- }
- }
-