Using InputStreamPersistence and Accessing a persisted network from within a jar

Dan's picture

I was wondering how to go about adding a persisted network to a jar file and then access it from within the jar. At the moment I am creating a persisted network...

final EncogPersistedCollection encog = new EncogPersistedCollection(
filename);
encog.create();
encog.add("network", network);

... adding this to a jar and then accessing it via...

EncogPersistedCollection encog = new EncogPersistedCollection(new InputStreamPersistence(this.getClass().getResourceAsStream(GNNCardClassifier.filename)));

network = (BasicNetwork)encog.find("network");

.. but am ending up with a nullPointer exception when i try to use network. Is there a better way to do this?

Thanks.

jeffheaton's picture

Encog core actually has an embedded text file that it reads to get data it needs for object names.

Encog actually has this built in. The EncogPersistedCollection works off of a "location", which is several different classes I have defined.

org.encog.persist.location

Normally you use FilePersistence. But, you can use ResourcePersistence to do that. Its really just a shorthand for what you were trying to do.

Here is how I get an input stream to my classlist.

final ResourcePersistence resource = new ResourcePersistence(
"org/encog/data/classes.txt");
final InputStream is = resource.createInputStream();

For what you want to do just pass the above "resource" variable directly into the constructor of EncogPersistedCollection. Of course this is read only. Encog has no ability to modify the JAR.

Jeff


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.