gov.sandia.cognition.framework.io
Class CSVDefaultCognitiveModelLiteHandler

java.lang.Object
  extended by gov.sandia.cognition.framework.io.CSVDefaultCognitiveModelLiteHandler

public class CSVDefaultCognitiveModelLiteHandler
extends Object

The CSVDefaultCognitiveModelLiteHandler class implements a format handler for CSV files that specify a default setup for a CognitiveModelLite. It is implemented by calling static methods to parse a file or stream and then an object is instantiated to read from the stream to parse it into the proper model factory. It also supports writing to the CSV file format.

Since:
1.0
Author:
Justin Basilico, Kevin R. Dixon

Field Summary
static String FORMAT_NAME
          The name of the format that the file parses.
static double FORMAT_VERSION
          The current version that is parsed.
 
Constructor Summary
protected CSVDefaultCognitiveModelLiteHandler()
          Creates a new instance of CommaSeparatedValueHandler.
 
Method Summary
protected  void addLink(String fromName, String toName, double weight)
          Called when the "link" directive is seen.
protected  void addNode(String name)
          Called when the "node" directive is seen.
protected  DefaultSemanticLabel getLabel(String name)
          Takes a name and returns its DefaultSemanticLabel or null if there is none.
protected  CognitiveModuleFactory getMutableModuleFactory()
          Gets the sharable module factory that has been read in.
protected  DefaultSemanticNetwork getNetwork()
          Gets the underlying network.
protected  CognitiveModuleFactory getSharedModuleFactory()
          Gets the sharable module factory that has been read in.
protected  boolean parse(BufferedReader br)
          The main parsing function.
static CognitiveModelLiteFactory parseCSVToModelFactory(BufferedReader br, boolean mutable)
          Reads a default CognitiveModuleFactory from the given buffered reader and puts it in a default CognitiveModelFactory.
static CognitiveModelLiteFactory parseCSVToModelFactory(String fileName, boolean mutable)
          Reads a default CognitiveModuleFactory from the given file and puts it in a default CognitiveModelFactory.
static CognitiveModuleFactory parseCSVToModuleFactory(BufferedReader br, boolean mutable)
          Reads a default CognitiveModuleFactory from the given buffered reader and then returns it.
static CognitiveModuleFactory parseCSVToModuleFactory(String fileName, boolean mutable)
          Reads a default CognitiveModuleFactory from the given CSV file and returns it.
protected  double parseHeader(BufferedReader br)
          Parses the header and returns the version number.
protected  void setLabels(HashMap<String,DefaultSemanticLabel> labels)
          Sets the label mapping.
protected  void setNetwork(DefaultSemanticNetwork network)
          Set the network being parsed.
static void write(File file, SemanticNetwork network)
          Writes a SemanticNetwork to the given file using the CSV format.
static void write(PrintWriter pw, SemanticNetwork network)
          Writes a SemanticNetwork to the given stream using the CSV format.
static void write(String fileName, SemanticNetwork network)
          Writes a SemanticNetwork to the given file using the CSV format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FORMAT_NAME

public static final String FORMAT_NAME
The name of the format that the file parses.

See Also:
Constant Field Values

FORMAT_VERSION

public static final double FORMAT_VERSION
The current version that is parsed.

See Also:
Constant Field Values
Constructor Detail

CSVDefaultCognitiveModelLiteHandler

protected CSVDefaultCognitiveModelLiteHandler()
Creates a new instance of CommaSeparatedValueHandler.

Method Detail

parse

protected boolean parse(BufferedReader br)
                 throws CSVParseException,
                        IOException
The main parsing function. It reads through the CSV values in the stream and handles them.

Parameters:
br - The reader to read the CSV values from.
Returns:
True if the parsing was successful.
Throws:
CSVParseException - If there is an error in the CSV formatting.
IOException - If there is an IO error.

parseHeader

protected double parseHeader(BufferedReader br)
                      throws CSVParseException,
                             IOException
Parses the header and returns the version number. It throws an exception if there is any issue with the header so the returned version number is always valid.

Parameters:
br - The reader to read the CSV values from.
Returns:
True if the parsing was successful.
Throws:
CSVParseException - If there is an error in the CSV formatting.
IOException - If there is an IO error.

addNode

protected void addNode(String name)
                throws CSVParseException
Called when the "node" directive is seen. It adds a node to the network

Parameters:
name - The name of the node to add.
Throws:
CSVParseException - If there is an error in adding the node, such as it already existing.

addLink

protected void addLink(String fromName,
                       String toName,
                       double weight)
                throws CSVParseException
Called when the "link" directive is seen. It adds a link to the network

Parameters:
fromName - The node the link is from.
toName - The node the link is to.
weight - The weight for the link.
Throws:
CSVParseException - If there is an error in adding the link, such as one of the nodes not existing.

getSharedModuleFactory

protected CognitiveModuleFactory getSharedModuleFactory()
Gets the sharable module factory that has been read in.

Returns:
The module factory that has been read in.

getMutableModuleFactory

protected CognitiveModuleFactory getMutableModuleFactory()
Gets the sharable module factory that has been read in.

Returns:
The module factory that has been read in.

getLabel

protected DefaultSemanticLabel getLabel(String name)
Takes a name and returns its DefaultSemanticLabel or null if there is none.

Parameters:
name - The name of the label to get.
Returns:
The DefaultSemanticLabel associated with that name, if one exists, otherwise null.

getNetwork

protected DefaultSemanticNetwork getNetwork()
Gets the underlying network.

Returns:
The underlying semantic network.

setNetwork

protected void setNetwork(DefaultSemanticNetwork network)
Set the network being parsed.

Parameters:
network - The new network being parsed.

setLabels

protected void setLabels(HashMap<String,DefaultSemanticLabel> labels)
Sets the label mapping.

Parameters:
labels - The new label mapping.

parseCSVToModuleFactory

public static CognitiveModuleFactory parseCSVToModuleFactory(String fileName,
                                                             boolean mutable)
                                                      throws CSVParseException,
                                                             IOException
Reads a default CognitiveModuleFactory from the given CSV file and returns it.

Parameters:
fileName - The file to read from.
mutable - True to create a mutable module.
Returns:
The default CognitiveModuleFactory read from the file.
Throws:
CSVParseException - If the CSV file is not properly formatted.
IOException - If there is an IO error, such as the file not existing.

parseCSVToModuleFactory

public static CognitiveModuleFactory parseCSVToModuleFactory(BufferedReader br,
                                                             boolean mutable)
                                                      throws CSVParseException,
                                                             IOException
Reads a default CognitiveModuleFactory from the given buffered reader and then returns it.

Parameters:
br - The reader to read from.
mutable - True to create a mutable module.
Returns:
The default CognitiveModuleFactory read from the reader.
Throws:
CSVParseException - If the CSV file is not properly formatted.
IOException - If there is an IO error, such as the file not existing.

parseCSVToModelFactory

public static CognitiveModelLiteFactory parseCSVToModelFactory(String fileName,
                                                               boolean mutable)
                                                        throws CSVParseException,
                                                               IOException
Reads a default CognitiveModuleFactory from the given file and puts it in a default CognitiveModelFactory.

Parameters:
fileName - The file to read from.
mutable - True to create a mutable module.
Returns:
The default CognitiveModuleFactory read from the reader.
Throws:
CSVParseException - If the CSV file is not properly formatted.
IOException - If there is an IO error, such as the file not existing.

parseCSVToModelFactory

public static CognitiveModelLiteFactory parseCSVToModelFactory(BufferedReader br,
                                                               boolean mutable)
                                                        throws CSVParseException,
                                                               IOException
Reads a default CognitiveModuleFactory from the given buffered reader and puts it in a default CognitiveModelFactory.

Parameters:
br - The reader to read from.
mutable - True to create a mutable model factory.
Returns:
The default CognitiveModuleFactory read from the reader.
Throws:
CSVParseException - If the CSV file is not properly formatted.
IOException - If there is an IO error, such as the file not existing.

write

public static void write(String fileName,
                         SemanticNetwork network)
                  throws FileNotFoundException
Writes a SemanticNetwork to the given file using the CSV format.

Parameters:
fileName - The name of the file to write the network.
network - The network to write.
Throws:
FileNotFoundException - If the file cannot be written to.

write

public static void write(File file,
                         SemanticNetwork network)
                  throws FileNotFoundException
Writes a SemanticNetwork to the given file using the CSV format.

Parameters:
file - The file to write the network.
network - The network to write.
Throws:
FileNotFoundException - If the file cannot be written to.

write

public static void write(PrintWriter pw,
                         SemanticNetwork network)
Writes a SemanticNetwork to the given stream using the CSV format.

Parameters:
pw - The place to write the network.
network - The network to write.