package aminePlatform.test.kernel; /******************************************************************* Amine - Artificial Intelligence platform for the development of Intelligent Systems. Copyright (C) 2004 AmineGroup. GNU Lesser General Public License This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 2.1 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *********************************************************************/ import javax.swing.*; import java.util.*; import java.awt.event.*; import aminePlatform.kernel.lexicons.*; import aminePlatform.kernel.ontology.*; import aminePlatform.util.cg.*; import aminePlatform.util.*; /** *
Title : LexiconSynonymsTest class
*Description : Test the Lexicon API
* @author Adil KABBAJ * @version 2 */ public class LexiconSynonymsTest extends JDialog { public LexiconSynonymsTest() { this.setTitle("LexiconSynonymsTest"); StringBuffer output = new StringBuffer(""); JScrollPane scrol; JTextArea text; try { Identifier root = new Identifier("Universal"); Identifier relationRoot = new Identifier("Relation"); Identifier mainLanguage = new Identifier("English"); Ontology ontology = new Ontology(root, relationRoot, mainLanguage); // Get the main lexicon to add, update or remove entries Lexicon mainLexicon = ontology.getMainLexicon(); // root cannot be removed (even if it has no children) output.append("Attempt to remove " + root + " :\n"); if (mainLexicon.removeEntry(root)) output.append("===> " + root + " has been removed \n"); else output.append("===> " + root + " has not been removed \n"); Identifier Action = new Identifier("Action"); Identifier Person = new Identifier("Person"); Identifier Object = new Identifier("Object"); Object[] identifiers = new Object[] {Action, Person, Object}; mainLexicon.addSynonym(new Identifier("Human"), Person); // Add three entries (for Action, Person and Object) to the mainLexicon // and relate them as subTypes of root mainLexicon.linkSubTypesToType(identifiers, root); // Different ways to add entries to a lexicon and relate them to other entries. Identifier Man = new Identifier("Man"); Identifier Woman = new Identifier("Woman"); identifiers = new Object[] {Man, Woman}; mainLexicon.linkSubTypesToType(identifiers, Person); mainLexicon.linkTypeToSuperType(new Identifier("Car"), Object); Identifier Bus = new Identifier("Bus"); mainLexicon.linkTypeToSuperType(Bus, Object); identifiers = new Object[] {new Identifier("Table"), new Identifier("Spoon")}; mainLexicon.linkSubTypesToType(identifiers, Object); //****************************************************************// // Get all the subType identifiers of a Type Identifiers and print them. // We consider the case of root type. // mainLexicon.getSubTypes(root) returns an Enumeration over all the subTypes // of root. Since the class Identifier has its toString() method, this later // is called implicitly by println(). output.append("Subtypes of " + root + " :\n"); for (Enumeration e = mainLexicon.getSubTypes(root); e.hasMoreElements();) { output.append("===> " + e.nextElement() + "\n"); } output.append("\n"); // A type that has subTypes (or individuals or situations) cannot be removed output.append("Attempt to remove " + Object + " which has subTypes :\n"); if (mainLexicon.removeEntry(Object)) output.append("===> " + Object + " has been removed \n"); else output.append("===> " + Object + " has not been removed \n"); // A type that has no subTypes (and no individuals and no situations) can be removed output.append("Attempt to remove " + Bus + " which has no children :\n"); if (mainLexicon.removeEntry(Bus)) output.append("===> " + Bus + " has been removed \n"); else output.append("===> " + Bus + " has not been removed \n"); output.append("Subtypes of " + root + " (after the remove) :\n"); for (Enumeration e = mainLexicon.getSubTypes(root); e.hasMoreElements();) { output.append("===> " + e.nextElement() + "\n"); } output.append("\n"); // Create a Lexicon for a new language : french for instance, and associate // it to the current ontology Identifier frenchLanguage = new Identifier("French"); Lexicon frenchLexicon = new Lexicon(frenchLanguage, ontology); // add synonyms for Person in french language identifiers = new Object[] {new Identifier("Persnne"), new Identifier("Humain")}; mainLexicon.addSynonyms(identifiers, frenchLanguage, Person); // replace the name of an identifier in a lexicon Identifier Personne = frenchLexicon.getIdentifier("Persnne"); frenchLexicon.replaceIdentifierName(Personne, "Personne"); // Remove the entry for Personne in frenchLexicon frenchLexicon.removeEntry(Personne); output.append("Synonyms of " + Person + " in french :\n"); for (Enumeration e = mainLexicon.getSynonyms(Person, frenchLanguage); e.hasMoreElements();) { output.append("===> " + e.nextElement() + "\n"); } text = new JTextArea(22, 30); text.setText(output.toString()); scrol = new JScrollPane(text); this.getContentPane().add(scrol); this.setLocation(100, 100); this.setSize(400, 450); this.show(); } catch (OntologyException ontExc) { output.append("OntologyException : " + ontExc.getMessage()); } catch (LexiconException lexExc) { output.append("LexiconException : " + lexExc.getMessage()); } } public static void main(String[] args) { LexiconSynonymsTest lexSynonymsTest = new LexiconSynonymsTest(); lexSynonymsTest.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(-1); } }); } }