com.cra.bnet.io
Class BnetFormatFactory

java.lang.Object
  |
  +--com.cra.bnet.io.BnetFormatFactory

public final class BnetFormatFactory
extends Object

A factory that provides BnetFormat implementations for clients to use. Note that this class is a singleton, so clients must use the getInstance method to obtain the sole instance.


Method Summary
 BnetFormat createFormat(BufferedRegExpReader reader)
          Returns a BnetFormat implementation that is capable of reading a Bayesian network from the specified reader.
 List getAllFormats()
          Returns an unmodifiable list of all registered formats.
static BnetFormatFactory getInstance()
          Returns the single instance of this factory.
 BayesianNetwork read(Reader reader)
          Returns the Bayesian network obtained from the specified reader or null if it could not be read.
 void registerFormat(BnetFormat format)
          Registers the specified format with this factory.
 void unregisterFormat(BnetFormat format)
          Unregisters the specified format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createFormat

public BnetFormat createFormat(BufferedRegExpReader reader)
Returns a BnetFormat implementation that is capable of reading a Bayesian network from the specified reader. This method checks all registered formats using their isValid methods, in the order returned by the getAllFormats method. If no suitable format is found, this method returns null.

A BufferedRegExpReader is required, rather than a more general Reader, so that any data read from it while checking validity can be buffered. An example of use is:

 Reader reader = ...;
 BnetFormatFactory factory = BnetFormatFactory.getInstance();
 BufferedRegExpReader buffered = new BufferedRegExpReader(reader);
 BnetFormat format = factory.createFormat(buffered);
 BayesianNetwork network = format.read(buffered);
 
Note that you must provide the BufferedRegExpReader object to the format's read method and not the original Reader object.

Note that after this method returns, the specified reader will still provide all of the original data, so it may be used elsewhere (i.e. given to the read method of a BnetFormat).

Note also clients are responsible for opening and closing the reader.

Parameters:
reader - input stream that provides a Bayesian network.
Returns:
a BnetFormat implementation that is capable of reading a Bayesian network from the specified reader or null if one is not found.

getAllFormats

public List getAllFormats()
Returns an unmodifiable list of all registered formats. The formats are returned in the order they are checked in the createFormat method for supporting the specified reader. All elements of the returned list are guaranteed to be of type BnetFormat.

Returns:
an unmodifiable list of all registered formats.

getInstance

public static BnetFormatFactory getInstance()
Returns the single instance of this factory.

Returns:
the single instance of this factory.

read

public BayesianNetwork read(Reader reader)
Returns the Bayesian network obtained from the specified reader or null if it could not be read. Use this method when you don't know what format the reader uses and you don't need access to the BnetFormat object. If you do need access to the BnetFormat object, use the createFormat method instead.

Parameters:
reader - input stream that contains a Bayesian network in some format.
Returns:
the Bayesian network obtained from the specified reader or null if it could not be read.

registerFormat

public void registerFormat(BnetFormat format)
Registers the specified format with this factory.

Parameters:
format - format to register.

unregisterFormat

public void unregisterFormat(BnetFormat format)
Unregisters the specified format.

Parameters:
format - format to unregister.