// Database Program // by Alan W. Biermann // January 1996 // Modified by D.Ramm Sep, 1999 // // //Inputs: In input mode, "facts" are read in three fields // noun phrase, relationship, noun phrase. For example, "John is a // boy." is separated into three parts, "John," "is," and "a boy." // These parts are entered into the program in three TextFields // // Input a fact. Enter three fields, the press "Enter" button // [John ] [is ] [a boy. ] // // In query mode, queries are read in the same // format as facts except that some fields may have a question // mark instead of data. // // Outputs: In query mode, the program prints into a TextArea all facts // that match the query on fields that do not have question marks. // // Method of operation: The facts are stored in three arrays // called noun1A, relationA, and noun2A. The k-th fact has its first, // second, and third fields in the k-th entries of, respectively, // noun1A, relationA, and noun2A. The program answers a query by // sequentially examining every stored fact and printing it if it // matches the query on the fields that do not contain a question // mark. import java.awt.*; import java.awt.event.*; public class DataBase extends java.applet.Applet implements ActionListener { TextField gN1, gRel, gN2, mInstruct, mInstruct2; String noun1A[], relationA[], noun2A[]; // Facts are stored in these TextArea mResults; Button bEnter, bAll, bFind; int last; // last tells how many facts are stored public void init() // postcondition: all class data members created, initialized, with // layout specified, listener activated for any buttons { last = -1; noun1A = new String[100]; relationA = new String[100]; noun2A = new String[100]; mInstruct = new TextField(70); mInstruct.setText( "Enter facts in the three fields below and press Enter button"); mInstruct2 = new TextField(70); mInstruct2.setText( "Enter search in the three fields above and press Find button"); gN1 = new TextField(25); gRel = new TextField(25); gN2 = new TextField(25); mResults = new TextArea(10, 60); bEnter = new Button("Enter"); bAll = new Button("All"); bFind = new Button("Find"); // register buttons to listeners bEnter.addActionListener(this); bAll.addActionListener(this); bFind.addActionListener(this); // specify layout add(mInstruct); add(gN1); add(gRel); add(gN2); add(bEnter); add(bAll); add(mInstruct2); add(bFind); add(mResults); } public void actionPerformed(ActionEvent event) // postcondition: Correct method his invoked to handle the processing // corresponding to a particular button that has been pressed { Object cause = event.getSource(); if (cause == bEnter) { InputFact(); } if (cause == bAll) //mainly for debugging { DumpData(); } if (cause == bFind) { Find(); } } void InputFact() // precondition: Facts have been entered in the TextFields: first noun // phrase in gN1, relation in gRel, and second noun phrase in gN2. // "Enter" button has been pressed. // postcondition: last has been incremented to point to the next empty // slots in the arrays. The information extracted from gN1, gRel, // and gN2 has been stored in noun1A[last], relationA[last], and // noun2A[last]. TextFields have been cleared. // known bugs: No explicit check is made to avoid exceeding array bounds. { last = last + 1; noun1A[last] = gN1.getText(); gN1.setText(""); relationA[last] = gRel.getText(); gRel.setText(""); noun2A[last] = gN2.getText(); gN2.setText(""); } void DumpData() // precondition: last facts have been stored in the String arrays // noun1A, relationA, and noun2A. "All" has been pressed. // postcondition: All information stored is displayed, in the same // order as entered, in TextArea mResults. { int k; mResults.setText("Dump of data\n"); k = 0; while (k <= last) { mResults.append(k + " " + noun1A[k] + " " + relationA[k] + " " + noun2A[k]+"\n"); k = k + 1; } } void Find() // precondition: last facts have been stored in the String arrays // noun1A, relationA, and noun2A. // Query facts or a ? have been entered into each of TextFields // gN1, gRel, and gN2. "Find" has been pressed. // postcondition: All information stored were compared to the query // facts read from TextFields. If QFCompare decided there is a // match, the facts are displayed in the TextArea mResults { String noun1, relation, noun2; int k; noun1 = gN1.getText(); relation = gRel.getText(); noun2 = gN2.getText(); mResults.setText("THE RELATED FACTS\n"); k = 0; while (k <= last) { if (QFCompare(noun1, relation, noun2, k) == 1) // 1 means match { mResults.append(noun1A[k] + " " + relationA[k] + " " + noun2A[k]+"\n"); } k = k + 1; } } int QFCompare(String noun1, String relation, String noun2, int k) // precondition: parameters noun1, relation, and noun2 contain // either a query fact of a "?". k is the index to properly // stored facts in arrays noun1A, relationA, and noun2A // postcondition: If for each of the three arrays, the k-th element // matches the corresponding query fact or the query fact is // a ?, QFCompare returns a 1 to indicate a match. Otherwise // returns a 0 { if ( (noun1.equals(noun1A[k]) || noun1.equals("?") ) && (relation.equals(relationA[k]) || relation.equals("?") ) && (noun2.equals(noun2A[k]) || noun2.equals("?") ) ) { return 1; // Use 1 to mean query matched database } else { return 0; // Use 0 to mean no match } } }