com.cra.bnet.io
Interface BnetFormat

All Known Implementing Classes:
AbstractBnetFormat

public interface BnetFormat

A utility for reading and writing Bayesian networks in some format. Bayesian networks can be read from a generic input stream (provided by a Reader object) and written to a generic output stream (provided by a Writer object).

Note that implementations should not attempt to close the Reader or Writer objects. Closing is client's responsibility.


Method Summary
 String getDescription()
          Returns a short description of the format that this BnetFormat supports.
 String getError()
          Returns a description of the last error that occurred while reading or writing a Bayesian network.
 String getExtension()
          Returns the file extension that this BnetFormat supports.
 boolean isValid(BufferedRegExpReader reader)
          Returns true if the specified reader contains data understood by this BnetFormat and false if it is not supported.
 BayesianNetwork read(Reader reader)
          Returns the Bayesian network obtained from the specified reader.
 boolean write(BayesianNetwork network, Writer writer)
          Writes the specified Bayesian network to the specified writer and returns true if the Bayesian network was written successfully.
 

Method Detail

getDescription

public String getDescription()
Returns a short description of the format that this BnetFormat supports. The returned string will typically be used as the description for a file filter in a JFileChooser.

Returns:
a short description of the format that this BnetFormat supports.

getError

public String getError()
Returns a description of the last error that occurred while reading or writing a Bayesian network. If no errors occurred, this method returns null.

Returns:
a description of the last error that occurred or null if no error occurred.

getExtension

public String getExtension()
Returns the file extension that this BnetFormat supports. The returned string should not contain a period character, i.e. it should be "xbn" not ".xbn". The returned string will typically be used as the extension for a file filter in a JFileChooser.

Returns:
the file extension that this BnetFormat supports.

isValid

public boolean isValid(BufferedRegExpReader reader)
Returns true if the specified reader contains data understood by this BnetFormat and false if it is not supported. Implementations should typically examine the first few characters or lines provided by the reader and not attempt to parse all of the data. Implementations must also call reset on the specified reader before returning, so the reader can be re-used later.

A BufferedRegExpReader is used in this method so that any data read from it to test for validity can be buffered for later use (in the read method, for example). To use this method properly, follow this idiom (note that the BufferedRegExpReader is given to the read method, not the original Reader):

 Reader reader = ...;
 BnetFormat format = ...;
 BufferedRegExpReader buffered = new BufferedRegExpReader(reader);
 boolean valid = format.isValid(buffered);
 if (valid)
   BayesianNetwork network = format.read(buffered);
 

Parameters:
reader - input stream that provides a Bayesian network.
Returns:
true if the specified reader contains data understood by this BnetFormat and false if it is not supported.

read

public BayesianNetwork read(Reader reader)
Returns the Bayesian network obtained from the specified reader. If the Bayesian network could not be read for some reason, this method will return null. Clients may then call getError to obtain a description of the problem.

Parameters:
reader - input stream that provides a Bayesian network.
Returns:
the Bayesian network obtained from the specified reader.

write

public boolean write(BayesianNetwork network,
                     Writer writer)
Writes the specified Bayesian network to the specified writer and returns true if the Bayesian network was written successfully. If the Bayesian network could not be written for some reason, this method will return false. Clients may then call getError to obtain a description of the problem.

Parameters:
network - Bayesian network to write.
writer - output stream to write to.
Returns:
true if the Bayesian network was written successfully and false otherwise.