Algernon Software

org.algernon.app
Class Java2Protege

java.lang.Object
  extended byorg.algernon.app.AlgernonApplication
      extended byorg.algernon.app.Java2Protege
All Implemented Interfaces:
Runnable

public class Java2Protege
extends AlgernonApplication
implements Runnable

Java2Protege converts a Java package to a Protege ontology. Input is a Java package name and the filename of a Protege project (.pprj file). The package must be on the classpath. If the Protege project exists, the classes are added to it. If it doesn't, a new knowledge base is created and saved to the given file. Þ The program first builds an ontology, then creates template slots on each class corresponding to all the get...() methods on each class. Enumerations are converted to Strings due to probably name conflicts. Any class starting or ending with 'Enum' or 'Enumeration' is assumed to be an enumeration.


Field Summary
(package private) static String CHILD_ERROR_STRING
           
(package private) static String DEFAULT_ROOT_CLASS
           
(package private)  String f_adviceFile
           
(package private)  AdviceTable f_adviceTable
           
(package private) static String[] f_adviceTableColumnNames
           
(package private)  Algernon f_algy
           
(package private)  FrameTree f_classTable
           
(package private)  SimpleTree f_classTree
           
(package private)  String f_errors
           
(package private)  boolean f_ignoreEnumerations
           
(package private)  AlgernonKB f_kb
           
(package private)  String f_kbFile
           
(package private)  String f_outputFile
           
(package private)  String f_package
           
(package private)  String f_saveInAlgernonFile
           
(package private)  SlotTable f_slotTable
           
(package private) static String[] f_slotTableColumnNames
           
(package private)  String f_topRoot
           
(package private)  J2PWindow f_window
           
static String HELP_URL
           
 
Fields inherited from class org.algernon.app.AlgernonApplication
 
Constructor Summary
Java2Protege()
           
 
Method Summary
 void addAdvice(String command, String type, String frameName, String replacement)
          Processes and stores the advice.
private  void checkForDuplicateFrames(String s)
          Adds an error to classes if they have the same name as the given slot.
private  void checkSlotDomainAndRange(J2PSlot slot)
           
private  String classesToAlgernon(FrameTree classTable, String topRoot)
          Creates Algernon taxonomy from stored information about class structure.
 String convertRelationRange(Class rangeType, String relation, Class domain)
          Converts a Java type to a Protege type.
 int countNameUses(String name)
          Returns the number of times the name is used.
 AdviceTable createAdviceTable()
           
private  void createClassDataStructures(SimpleTree classTree)
          Creates the data structures for storing potential classes.
private  void createClassDataStructuresAux(Class aClass, J2PClass superClass, SimpleTree classTree)
          Recursive method to create class and subclass data structures.
private  void createSlotDataStructures(SimpleTree classTree)
          Creates the data structures for storing potential slots.
private  void createSlotDataStructuresAux(Class domainClass)
          Helper method for createSlotDataStructures.
 SlotTable createSlotTable()
           
 void deleteAdvice(Vector values)
          See documentation below.
 void doCheckErrors(FrameTree f_classTable, SlotTable slotTable)
          Checks for errors (usually duplicate names) and stores errors on each frame.
 boolean doConversion(String packageName, String projectFile, String rootClass, String adviceFile)
          Performs the conversion.
 void doWriteAdviceFile(String filename)
          Writes the contents of the advice to a file.
 String doWriteAlgernonFile(String outFilename, String rootClass)
          Writes everything to an Algernon file.
 Advice getAdvice()
           
 Vector getAdviceColumnNames()
           
 String getAdviceFile()
           
private  String getAlgernonMetaTaxonomy()
           
 String getClassMappingInfo(J2PFrame aClass, FrameTree classTree)
          Returns a TELL statement that creates a mapping frame that stores the mapping between a Java name (Class or field) and a Protege Class.
 FrameTree getClassTable()
           
 String getKBFile()
           
 String getKBRoot()
           
private  String getMappingInfo(FrameTree classTable, SlotTable slotTable)
          Returns the mapping info to convert from Protege names to Java names.
 String getPackage()
           
 String getSaveInAlgernonFile()
          Returns the name of the file in which to save Algernon files.
 String getSlotMappingInfo(J2PSlot slot)
          Returns a TELL statement that creates a mapping frame that stores the mapping between a Java name (Class or field) and a Protege Slot.
 SlotTable getSlotTable()
           
 String getToolTipText(String className)
           
 void init(String[] args)
          Initializes variables from the command-line arguments.
 boolean isBuiltinRange(String range)
          returns true if the string represents an atomic slot range such as :INTEGER.
private  boolean isEnumeration(String name)
           
 boolean isValidPackage(String packageName)
           
static void main(String[] args)
          java org.algernon.app.Java2Protege package Protege-project-file advice-file
static String mappingOfSlot(J2PSlot slot)
          Returns the name of the mapping object in the KB that corresponds to this slot.
static String mappingOfSlot(String className, String slotName)
          Returns the name of the mapping object in the KB that corresponds to this slot.
 void newKB(String name)
          Creates a new KB for the system to use.
 void run()
          Performs the operations.
 void saveKB()
          Saves the kb.
private  void setDuplicateSlotNameErrorOnClasses(J2PSlot slot)
          Adds an error to classes if they have the same name as the given slot.
 void setKB(String projectFile)
          Sets the KB used with this program.
 void showMessage(String message, String title, int messageType)
          Displays an error message, using the GUI if possible.
private  String singularForm(String word)
           
private  String slotsToAlgernon(SlotTable slotTable)
          Returns a set of TELL statements that use :ADD-RELATION to create new slots.
private  String slotToAlgernonSlot(J2PSlot slot)
           
private  String treeBranchToTaxonomy(J2PFrame aClass, FrameTree classTree, String indent)
           
 
Methods inherited from class org.algernon.app.AlgernonApplication
getName, getVersionDate, getVersionMajor, getVersionMicro, getVersionMinor, getVersionString, getVersionType, getVersionURL
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HELP_URL

public static final String HELP_URL
See Also:
Constant Field Values

CHILD_ERROR_STRING

static String CHILD_ERROR_STRING

f_slotTableColumnNames

static String[] f_slotTableColumnNames

f_adviceTableColumnNames

static String[] f_adviceTableColumnNames

DEFAULT_ROOT_CLASS

static String DEFAULT_ROOT_CLASS

f_ignoreEnumerations

boolean f_ignoreEnumerations

f_package

String f_package

f_kbFile

String f_kbFile

f_adviceFile

String f_adviceFile

f_outputFile

String f_outputFile

f_saveInAlgernonFile

String f_saveInAlgernonFile

f_topRoot

String f_topRoot

f_classTable

FrameTree f_classTable

f_slotTable

SlotTable f_slotTable

f_adviceTable

AdviceTable f_adviceTable

f_kb

AlgernonKB f_kb

f_window

J2PWindow f_window

f_classTree

SimpleTree f_classTree

f_errors

String f_errors

f_algy

Algernon f_algy
Constructor Detail

Java2Protege

public Java2Protege()
Method Detail

init

public void init(String[] args)
Initializes variables from the command-line arguments.

Parameters:
args - (in order) package, Protege project file, advice file

main

public static void main(String[] args)
java org.algernon.app.Java2Protege package Protege-project-file advice-file

Parameters:
args -

run

public void run()
Performs the operations.

Specified by:
run in interface Runnable

doWriteAlgernonFile

public String doWriteAlgernonFile(String outFilename,
                                  String rootClass)
Writes everything to an Algernon file. First writes meta-taxonomy, then the taxonomy obtained from the Java API, then slot values.

Returns:
a String containing the full name of the file written to, or else null if an error occurred.

getMappingInfo

private String getMappingInfo(FrameTree classTable,
                              SlotTable slotTable)
Returns the mapping info to convert from Protege names to Java names. These statements are in Algernon Tell forms.

Parameters:
classTable -
slotTable -
Returns:
Tell forms that map the names.

getClassMappingInfo

public String getClassMappingInfo(J2PFrame aClass,
                                  FrameTree classTree)
Returns a TELL statement that creates a mapping frame that stores the mapping between a Java name (Class or field) and a Protege Class.


mappingOfSlot

public static String mappingOfSlot(J2PSlot slot)
Returns the name of the mapping object in the KB that corresponds to this slot. The name is composed of the slot name and the slot domain.

Parameters:
slot - The slot to be used to create the mapping name.
Returns:
A string to be used as the name of the mapping instance.

mappingOfSlot

public static String mappingOfSlot(String className,
                                   String slotName)
Returns the name of the mapping object in the KB that corresponds to this slot. The name is composed of the slot name and the slot domain.

Parameters:
className - the name of the class that has the slot.
slotName - the name of the slot to be used to create the mapping name.
Returns:
A string to be used as the name of the mapping instance.

getSlotMappingInfo

public String getSlotMappingInfo(J2PSlot slot)
Returns a TELL statement that creates a mapping frame that stores the mapping between a Java name (Class or field) and a Protege Slot.


getAlgernonMetaTaxonomy

private String getAlgernonMetaTaxonomy()

setKB

public void setKB(String projectFile)
           throws AlgernonStorageException
Sets the KB used with this program.

Throws:
AlgernonStorageException

newKB

public void newKB(String name)
Creates a new KB for the system to use.


saveKB

public void saveKB()
            throws AlgernonStorageException
Saves the kb.

Throws:
AlgernonStorageException

createClassDataStructures

private void createClassDataStructures(SimpleTree classTree)
Creates the data structures for storing potential classes.

Parameters:
classTree -

createClassDataStructuresAux

private void createClassDataStructuresAux(Class aClass,
                                          J2PClass superClass,
                                          SimpleTree classTree)
Recursive method to create class and subclass data structures.

Parameters:
aClass - Class to start with (root of subtree)
superClass - Superclass of aClass
classTree - Contains the tree mapping.

createSlotDataStructures

private void createSlotDataStructures(SimpleTree classTree)
Creates the data structures for storing potential slots.

Parameters:
classTree -

createSlotDataStructuresAux

private void createSlotDataStructuresAux(Class domainClass)
Helper method for createSlotDataStructures.


slotsToAlgernon

private String slotsToAlgernon(SlotTable slotTable)
Returns a set of TELL statements that use :ADD-RELATION to create new slots.


slotToAlgernonSlot

private String slotToAlgernonSlot(J2PSlot slot)

convertRelationRange

public String convertRelationRange(Class rangeType,
                                   String relation,
                                   Class domain)
Converts a Java type to a Protege type. Rules:
   1.  If the range is a collection class, returns the singular
       form of the relation name.  I.e.  Records returns Record.
   2.  long, short, int, integer become :INTEGER
   3.  float, double become :DOUBLE
   4.  boolean becomes :BOOLEAN
   5.  string becomes :STRING
   6.  EnumXyz or EnumerationXyz or XyzEnum or XyzEnumeration become :STRING.
   7.  Date becomes :STRING
 

Parameters:
rangeType - the Class of the method's return type.
relation - the name of the relation being worked on
Returns:
Returns a string containing the Protege type of something.

isEnumeration

private boolean isEnumeration(String name)

singularForm

private String singularForm(String word)

classesToAlgernon

private String classesToAlgernon(FrameTree classTable,
                                 String topRoot)
Creates Algernon taxonomy from stored information about class structure.

Parameters:
classTable - stored information
topRoot - Frame name to use as the root of the Taxonomy.

treeBranchToTaxonomy

private String treeBranchToTaxonomy(J2PFrame aClass,
                                    FrameTree classTree,
                                    String indent)

isBuiltinRange

public boolean isBuiltinRange(String range)
returns true if the string represents an atomic slot range such as :INTEGER.


isValidPackage

public boolean isValidPackage(String packageName)

doConversion

public boolean doConversion(String packageName,
                            String projectFile,
                            String rootClass,
                            String adviceFile)
                     throws J2PConversionError
Performs the conversion.

Parameters:
packageName -
projectFile -
adviceFile -
Returns:
true if the conversion succeeded, false otherwise.
Throws:
J2PConversionError

doCheckErrors

public void doCheckErrors(FrameTree f_classTable,
                          SlotTable slotTable)
Checks for errors (usually duplicate names) and stores errors on each frame.


checkForDuplicateFrames

private void checkForDuplicateFrames(String s)
Adds an error to classes if they have the same name as the given slot.

Parameters:
s - the name of a class.

countNameUses

public int countNameUses(String name)
Returns the number of times the name is used.

Parameters:
name - the name to check

checkSlotDomainAndRange

private void checkSlotDomainAndRange(J2PSlot slot)

setDuplicateSlotNameErrorOnClasses

private void setDuplicateSlotNameErrorOnClasses(J2PSlot slot)
Adds an error to classes if they have the same name as the given slot.

Parameters:
slot - the slot to check against the frames.

getPackage

public String getPackage()

getKBFile

public String getKBFile()

getAdviceFile

public String getAdviceFile()

getClassTable

public FrameTree getClassTable()

getSlotTable

public SlotTable getSlotTable()

getToolTipText

public String getToolTipText(String className)

createSlotTable

public SlotTable createSlotTable()

createAdviceTable

public AdviceTable createAdviceTable()

getAdvice

public Advice getAdvice()

getAdviceColumnNames

public Vector getAdviceColumnNames()

getSaveInAlgernonFile

public String getSaveInAlgernonFile()
Returns the name of the file in which to save Algernon files.


showMessage

public void showMessage(String message,
                        String title,
                        int messageType)
Displays an error message, using the GUI if possible.

Parameters:
message - The message to be displayed
messageType - JOptionPane.WARNING_MESSAGE, etc.

doWriteAdviceFile

public void doWriteAdviceFile(String filename)
                       throws IOException
Writes the contents of the advice to a file.

Parameters:
filename - the name of the file to save to.
Throws:
IOException

addAdvice

public void addAdvice(String command,
                      String type,
                      String frameName,
                      String replacement)
Processes and stores the advice.

Parameters:
type - Must be SLOT or CLASS, but is not case-sensitive.
frameName - Either ALL (uppercase) or the name of a frame (case-sensitive).
command - Not case-sensitive. Must be PREFIX, SUFFIX, CAPITALIZE, LOWERCASE, UPPERCASE or REPLACE
replacement - Case-sensitive. The new name for the slot or class, in a REPLACE command.

deleteAdvice

public void deleteAdvice(Vector values)
See documentation below. The returned Vector should contain the following:
  Command:   IGNORE, CAPITALIZE, etc.
  Type:      CLASS or SLOT
  frameName: a frame name
  replacement: a string or null
 
All items are strings or are null.

Parameters:
values - A Vector of strings.

getKBRoot

public String getKBRoot()

Algernon Software