unbbayes.io
Class NetIO

java.lang.Object
  extended by unbbayes.io.NetIO
All Implemented Interfaces:
BaseIO

public class NetIO
extends Object
implements BaseIO

Manipulates input/output of NET files.

Version:
2.0
Author:
Rommel N. Carvalho, Michael S. Onishi, Mario Henrique Paes Vieira (mariohpv@bol.com.br), Shou Matsumoto

Field Summary
protected  long lineno
           
static String[] SUPPORTED_EXTENSIONS
          Single array containing "net"
 
Constructor Summary
NetIO()
           
 
Method Summary
protected  String formatString(String string)
           
 String getName()
          Gets the name of this I/O component.
protected  int getNext(StreamTokenizer st)
           
 String[] getSupportedFileExtensions(boolean isLoadOnly)
          Obtains an array of file extensions supported by this network window.
 String getSupportedFilesDescription(boolean isLoadOnly)
          Gets a description of supported file extensions, which may be shown to the user through file chooser's file filter to explain what file format are supported.
 Graph load(File input)
          Loads a NET format file using default node/network builder.
 ProbabilisticNetwork load(File input, IProbabilisticNetworkBuilder networkBuilder)
          Loads a NET format file using network builder
protected  void load(File input, SingleEntityNetwork net, IProbabilisticNetworkBuilder networkBuilder)
           
protected  void loadContinuousNodeDeclaration(StreamTokenizer st, SingleEntityNetwork net, IProbabilisticNetworkBuilder networkBuilder)
          If the current declaration is of type "continuous node", loads that node (creating new instances using networkBuilder) and adds it to net.
protected  void loadHierarchicTree(StringBuffer sb, DefaultMutableTreeNode root)
           
protected  void loadNetHeader(StreamTokenizer st, SingleEntityNetwork net)
          Start loading net{} header from .net specification file; iterating under loadNetHeaderBody(StreamTokenizer, SingleEntityNetwork) in order to treat each declaration.
protected  void loadNetHeaderBody(StreamTokenizer st, SingleEntityNetwork net)
          If a declaration inside net header is treatable, treat it.
protected  void loadNodeDeclaration(StreamTokenizer st, SingleEntityNetwork net, IProbabilisticNetworkBuilder networkBuilder)
          If the current declaration is of type "node" (or "decision" or "utility"), loads that node (creating new instances using networkBuilder) and adds it to net.
protected  void loadNodeDeclarationBody(StreamTokenizer st, Node node)
          Reads inside the node declaration.
protected  void loadPotentialDataContinuous(StreamTokenizer st, Node node)
          Loads potential declaration assuming it is declaring continuous distribution
protected  void loadPotentialDataOrdinal(StreamTokenizer st, Node node)
          Loads potential declaration's content assuming it is declaring ordinal "stateful" probability declaration
protected  void loadPotentialDeclaration(StreamTokenizer st, SingleEntityNetwork net)
          If the current declaration is "potential", treat that declaration and adds it to a node contained within net.
protected  void processTreeNode(TreeNode node, StringBuffer sb, TreeModel model)
           
protected  void readTillEOL(StreamTokenizer tokenizer)
          Reads and skips all tokens before next end of line token.
 void save(File output, Graph graph)
          Saves a network in basic NET file format.
protected  String saveHierarchicTree(HierarchicTree hierarchicTree)
           
protected  void saveNetHeader(PrintStream stream, SingleEntityNetwork net)
          Fills the PrintStream with net{} header, starting with "net {" declaration and closing with "}"
protected  void saveNetHeaderBody(PrintStream stream, SingleEntityNetwork net)
          Stores the content of net{[CONTENT]} header to a stream.
protected  void saveNodeDeclaration(PrintStream stream, Node node, SingleEntityNetwork net)
          Writes to a PrintStream a node/decision/utility{} declaration.
protected  void saveNodeDeclarationBody(PrintStream stream, Node node, SingleEntityNetwork net)
          Writes to a PrintStream the body of node/decision/utility{} declaration.
protected  void saveNodeLabelAndPosition(PrintStream stream, Node node)
          Stores node's label = "[LABEL]"; and position = ([X], [Y]); declarations inside "node" declaration's body
protected  void savePotentialDeclaration(PrintStream stream, Node node, SingleEntityNetwork net)
          Stores to PrintStream the potential{} declaration
protected  void savePotentialDeclarationBody(PrintStream stream, Node node, SingleEntityNetwork net)
          Stores to PrintStream the [BODY] of potential {[BODY]} declaration
 void setName(String name)
          Sets the name of this I/O component.
protected  void setUpHierarchicTree(SingleEntityNetwork net)
          Sets up the hierarchic tree after the network is completely loaded
protected  void setUpStreamTokenizer(StreamTokenizer st)
          Configures valid/invalid character ranges of stream tokenizer.
 boolean supports(File file, boolean isLoadOnly)
          Returns true if the file is supported by this IO class.
 boolean supports(String extension, boolean isLoadOnly)
          Checks if file extension is compatible to what this i/o expects.
protected  String unformatString(String string)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lineno

protected long lineno

SUPPORTED_EXTENSIONS

public static final String[] SUPPORTED_EXTENSIONS
Single array containing "net"

Constructor Detail

NetIO

public NetIO()
Method Detail

load

public Graph load(File input)
           throws LoadException,
                  IOException
Loads a NET format file using default node/network builder. In other words, this method returns exactly instances of ProbabilisticNetwork filled by DecisionNode, ProbabilisticNode and UtilityNode.

Specified by:
load in interface BaseIO
Parameters:
input - file to be read.
Returns:
loaded net.
Throws:
LoadException - when there were errors loading the network
IOException - in case there were errors when manipulating files.
See Also:
DefaultProbabilisticNodeBuilder

load

public ProbabilisticNetwork load(File input,
                                 IProbabilisticNetworkBuilder networkBuilder)
                          throws LoadException,
                                 IOException
Loads a NET format file using network builder

Parameters:
input - file to be read.
networkBuilder: - builder to be used in order to generate expected instances of probabilistic network, probabilistic nodes, decision nodes and utility nodes. This is useful if you want to reuse NetIO for networks/nodes which extends ProbabilisticNetwork, ProbabilisticNode, DecisionNode and UtilityNode (or else NetIO will be bound to those superclasses only).
Returns:
loaded net.
Throws:
LoadException - when there were errors loading the network
IOException - in case there were errors when manipulating files.
See Also:
IProbabilisticNetworkBuilder

save

public void save(File output,
                 Graph graph)
          throws FileNotFoundException
Saves a network in basic NET file format.

Specified by:
save in interface BaseIO
Parameters:
output - file where the net should be saved.
graph - network to be saved.
Throws:
FileNotFoundException

load

protected void load(File input,
                    SingleEntityNetwork net,
                    IProbabilisticNetworkBuilder networkBuilder)
             throws IOException,
                    LoadException
Throws:
IOException
LoadException

getNext

protected int getNext(StreamTokenizer st)
               throws IOException
Throws:
IOException

loadHierarchicTree

protected void loadHierarchicTree(StringBuffer sb,
                                  DefaultMutableTreeNode root)

saveHierarchicTree

protected String saveHierarchicTree(HierarchicTree hierarchicTree)

processTreeNode

protected void processTreeNode(TreeNode node,
                               StringBuffer sb,
                               TreeModel model)

readTillEOL

protected void readTillEOL(StreamTokenizer tokenizer)
                    throws IOException
Reads and skips all tokens before next end of line token.

Parameters:
tokenizer - Stream tokenizer
Throws:
IOException - EOF not found

formatString

protected String formatString(String string)

unformatString

protected String unformatString(String string)

setUpStreamTokenizer

protected void setUpStreamTokenizer(StreamTokenizer st)
                             throws IOException
Configures valid/invalid character ranges of stream tokenizer. By setting up using protected method, it becomes easier to extend this class

Parameters:
st: - StreamTokenizer to set up
Throws:
IOException

loadNetHeader

protected void loadNetHeader(StreamTokenizer st,
                             SingleEntityNetwork net)
                      throws IOException
Start loading net{} header from .net specification file; iterating under loadNetHeaderBody(StreamTokenizer, SingleEntityNetwork) in order to treat each declaration. loadNetHeaderBody(StreamTokenizer, SingleEntityNetwork) must ignore incompatible declaration.

Parameters:
st -
net -
Throws:
IOException

loadNetHeaderBody

protected void loadNetHeaderBody(StreamTokenizer st,
                                 SingleEntityNetwork net)
                          throws IOException
If a declaration inside net header is treatable, treat it. The currently compatible declarations are: name, node_size, tree, UnBBayes_Color_Utility, UnBBayes_Color_Decision, UnBBayes_Color_Probabilistic_Description, UnBBayes_Color_Probabilistic_Explanation. If declaration is not compatible, it will be ignored.

Parameters:
st -
net -
Throws:
IOException

loadNodeDeclaration

protected void loadNodeDeclaration(StreamTokenizer st,
                                   SingleEntityNetwork net,
                                   IProbabilisticNetworkBuilder networkBuilder)
                            throws IOException,
                                   LoadException
If the current declaration is of type "node" (or "decision" or "utility"), loads that node (creating new instances using networkBuilder) and adds it to net. If declaration is not "node" (or "decision" or "utility"), it will not move the token index from st.

Parameters:
st -
net -
networkBuilder -
Throws:
IOException
LoadException

loadContinuousNodeDeclaration

protected void loadContinuousNodeDeclaration(StreamTokenizer st,
                                             SingleEntityNetwork net,
                                             IProbabilisticNetworkBuilder networkBuilder)
                                      throws IOException,
                                             LoadException
If the current declaration is of type "continuous node", loads that node (creating new instances using networkBuilder) and adds it to net. If declaration is not "continuous node", it will not move the token index from st.

Parameters:
st -
net -
networkBuilder -
Throws:
IOException
LoadException

loadPotentialDeclaration

protected void loadPotentialDeclaration(StreamTokenizer st,
                                        SingleEntityNetwork net)
                                 throws IOException,
                                        LoadException
If the current declaration is "potential", treat that declaration and adds it to a node contained within net. If current declaration is not "potential", this method ignores it and remains the st untouched

Parameters:
st -
net -
Throws:
IOException
LoadException

setUpHierarchicTree

protected void setUpHierarchicTree(SingleEntityNetwork net)
Sets up the hierarchic tree after the network is completely loaded

Parameters:
net -

loadNodeDeclarationBody

protected void loadNodeDeclarationBody(StreamTokenizer st,
                                       Node node)
                                throws IOException,
                                       LoadException
Reads inside the node declaration. node {[READS THIS CONTENT]}

Parameters:
st -
node: - node to be filled
Throws:
IOException
LoadException

loadPotentialDataOrdinal

protected void loadPotentialDataOrdinal(StreamTokenizer st,
                                        Node node)
                                 throws LoadException,
                                        IOException
Loads potential declaration's content assuming it is declaring ordinal "stateful" probability declaration

Parameters:
st -
node -
Throws:
LoadException
IOException

loadPotentialDataContinuous

protected void loadPotentialDataContinuous(StreamTokenizer st,
                                           Node node)
                                    throws LoadException,
                                           IOException
Loads potential declaration assuming it is declaring continuous distribution

Parameters:
st -
node -
Throws:
LoadException
IOException

saveNetHeader

protected void saveNetHeader(PrintStream stream,
                             SingleEntityNetwork net)
Fills the PrintStream with net{} header, starting with "net {" declaration and closing with "}"

Parameters:
stream: - stream to write to
net: - network to be saved to stream

saveNetHeaderBody

protected void saveNetHeaderBody(PrintStream stream,
                                 SingleEntityNetwork net)
Stores the content of net{[CONTENT]} header to a stream. the informations are: node_size, name, tree, UnBBayes_Color_Probabilistic_Description, UnBBayes_Color_Probabilistic_Explanation, UnBBayes_Color_Utility, UnBBayes_Color_Decision.

Parameters:
stream -
net -

saveNodeDeclaration

protected void saveNodeDeclaration(PrintStream stream,
                                   Node node,
                                   SingleEntityNetwork net)
Writes to a PrintStream a node/decision/utility{} declaration. node|decision|utility [NAME] {[CONTENT]}

Parameters:
stream -
node -
net -

saveNodeDeclarationBody

protected void saveNodeDeclarationBody(PrintStream stream,
                                       Node node,
                                       SingleEntityNetwork net)
Writes to a PrintStream the body of node/decision/utility{} declaration. node|decision|utility [NAME] {[THIS_CONTENT_IS_WRITTEN]}

Parameters:
stream -
node -
net -

savePotentialDeclaration

protected void savePotentialDeclaration(PrintStream stream,
                                        Node node,
                                        SingleEntityNetwork net)
Stores to PrintStream the potential{} declaration

Parameters:
stream -
node -
net -

savePotentialDeclarationBody

protected void savePotentialDeclarationBody(PrintStream stream,
                                            Node node,
                                            SingleEntityNetwork net)
Stores to PrintStream the [BODY] of potential {[BODY]} declaration

Parameters:
stream -
node -
net -

saveNodeLabelAndPosition

protected void saveNodeLabelAndPosition(PrintStream stream,
                                        Node node)
Stores node's label = "[LABEL]"; and position = ([X], [Y]); declarations inside "node" declaration's body

Parameters:
stream -
node -

supports

public boolean supports(String extension,
                        boolean isLoadOnly)
Checks if file extension is compatible to what this i/o expects.

Parameters:
extension -
isLoadOnly -
Returns:
See Also:
supports(File, boolean)

getSupportedFileExtensions

public String[] getSupportedFileExtensions(boolean isLoadOnly)
Description copied from interface: BaseIO
Obtains an array of file extensions supported by this network window. The file extensions should come without the dot

Specified by:
getSupportedFileExtensions in interface BaseIO
Parameters:
isLoadOnly - : if set to true, it should consider file extensions for file loading (input). If set to false, it should consider both saving and loading. Note that not every module/plugin can implement both loading and saving, and this parameter may separate such special behaviors.
Returns:

getSupportedFilesDescription

public String getSupportedFilesDescription(boolean isLoadOnly)
Description copied from interface: BaseIO
Gets a description of supported file extensions, which may be shown to the user through file chooser's file filter to explain what file format are supported. E.g. "Net (.net), XMLBIF(.xml), UnBBayes File (.ubf)"

Specified by:
getSupportedFilesDescription in interface BaseIO
Parameters:
isLoadOnly - : if set to true, it should consider file extensions for file loading (input). If set to false, it should consider both saving and loading. Note that not every module/plugin can implement both loading and saving, and this parameter may separate such special behaviors.

supports

public boolean supports(File file,
                        boolean isLoadOnly)
Description copied from interface: BaseIO
Returns true if the file is supported by this IO class. It may be implemented as simple extension check. False otherwise.

Specified by:
supports in interface BaseIO
Parameters:
file - : the file to analyze extension.
isLoadOnly - : if set to true, it should consider file extensions for file loading (input). If set to false, it should consider both saving and loading. Note that not every I/O class can implement both loading and saving, and this parameter may separate such special behaviors.
Returns:

getName

public String getName()
Description copied from interface: BaseIO
Gets the name of this I/O component. This name may be displayed to a user when there is a need to choose a specific I/O class to use.

Specified by:
getName in interface BaseIO
Returns:
the name

setName

public void setName(String name)
Description copied from interface: BaseIO
Sets the name of this I/O component. This name may be displayed to a user when there is a need to choose a specific I/O class to use.

Specified by:
setName in interface BaseIO
Parameters:
name - the name to set


Copyright © 2001-2010 University of Brasilia - UnB. All Rights Reserved.