📄 network.java
字号:
/*************************************************************************
This program is copyrighted. Please refer to COPYRIGHT.txt for the
copyright notice.
This file is part of JavaNNS.
JavaNNS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
JavaNNS is distributed in the hope that it will be useful,
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JavaNNS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*************************************************************************/
package javanns;
/*---------------------------- imports ---------------------------------------*/
import java.io.* ;
import java.util.* ;
import java.util.jar.*;
import javax.swing.* ;
import java.awt.* ;
// imports for NetTrainer:
import java.awt.event.*;
/*----------------------- class declaration ----------------------------------*/
public class Network extends LASAdapter {
public static final boolean under_construction = false;
Snns snns;
KernelInterface ki;
NetworkAccessory accessory;
Vector listeners = new Vector();
Vector training_listeners = new Vector();
Function[] functions = new Function[Function.differentTypes];
double[][] parameters = new double[Function.differentTypes][5];
private String net_name = "default";
static String libName = System.mapLibraryName(Snns.LIBRARY_NAME);
private boolean is_ready = false;
// for creating new links :
private Unit[] source_units;
// for unit selection :
private boolean[] selection_flags;
private int selectedUnitsCount = 0;
// layers :
Layers layers;
// maximal x coordinate of an unit:
private int[] max_coord = new int[]{ -1, -1 };
/*-------------------------- constructor -------------------------------------*/
public Network( Snns snns ) throws Exception{
this.snns = snns;
if( !loadKernel()) throw
new Exception("The system couldn磘 find the library SNNS_jkr\nJavaNNS couldn磘 start.");
layers = new Layers( this );
new NetChangeListener();
/*
String name;
int i, fnNo = ki.getNoOfFunctions();
ki.functionName = "";
System.out.println("Number of functions: "+fnNo);
for( i=0; i<fnNo; i++ ){
ki.getFuncInfo( i );
name = ki.functionName;
ki.getFuncParamInfo( name, i );
System.out.println( "Name: "+name );
System.out.println( "Type: "+ki.functionType );
System.out.println( "Parameter: in["+ki.function_inputs+"], out["+ki.function_outputs+"]");
}
*/
}
/**
* method tries to load the library
* it first tries to find it at the place specified by the properties entry
* if this wasn磘 possible, an dialog is shown to ask the user, whether he
* knows the place of the library or where it is to put. In the last case
* the method extracts the library to the specified path from the jar-file, if
* this could be found
*/
boolean loadKernel(){
ki = new KernelInterface();
String dll_path = snns.properties.getProperty(JavaNNSProperties.LIBRARY_PATH_KEY);
try { ki.loadLibrary(dll_path); return true; }
catch(UnsatisfiedLinkError e) {
DLLPathDialog dpd = new DLLPathDialog(snns, dll_path, libName);
dll_path = dpd.getPath();
if(dll_path == null) return false;
try {
ki.loadLibrary(dll_path);
setDLLPathProperty(dll_path);
return true;
}
catch(UnsatisfiedLinkError e2) {
if(! extractLibraryFromJar(dll_path)) return false;
try{
ki.loadLibrary(dll_path);
setDLLPathProperty(dll_path);
}
catch(UnsatisfiedLinkError ex) { return false; }
return true;
}
}
}
/**
* Extracts the native kernel library from the
* JAR containing the JavaNNS distribution and
* stores it in the specified path.
*
* @param path folder where to store the library
* @return true if the library was successfuly extracted
*/
boolean extractLibraryFromJar(String dll_path) {
File jar = new File(Snns.JAR_FILE_NAME);
if( !jar.exists() ) throw new
UnsatisfiedLinkError("Couldn磘 find the JavaNNS.jar file");
try {
JarFile jf = new JarFile(jar);
Enumeration entries = jf.entries();
JarEntry je = null;
while(entries.hasMoreElements()) {
je = (JarEntry)entries.nextElement();
if(je.getName().equals(libName)) break;
}
if(je == null) {
System.out.println(
"Could not find the kernel library " + libName +
"in the jar file.\n" +
"Probaly a wrong release."
);
return false;
}
InputStream is = jf.getInputStream(je);
FileOutputStream fos =
new FileOutputStream(dll_path + File.separatorChar + libName);
byte[] buffer = new byte[16384];
for(int read = is.read(buffer); read > -1; read = is.read(buffer))
fos.write(buffer, 0, read);
is.close();
fos.flush();
fos.close();
return true;
}
catch(Exception ex) {
ex.printStackTrace();
return false;
}
}
void setDLLPathProperty(String dll_path){
if( snns.properties.editable() ){
snns.properties.setProperty(JavaNNSProperties.LIBRARY_PATH_KEY,dll_path);
try{ snns.properties.save(); }
catch(IOException ex){}
}
}
/*---------------------- listeners & events ----------------------------------*/
/**
* adds a new network listener
* this listener gets infos about all network events
*
* @param listener
*/
public void addListener( NetworkListener l ){
if( !listeners.contains(l) ) listeners.add(l);
}
public void addTrainingListener(NetworkListener l){
if( !training_listeners.contains(l) ) training_listeners.add(l);
}
/**
* method removes a certain listener from the listener list
*/
public void removeListener( NetworkListener l ){
listeners.remove(l);
training_listeners.remove(l);
}
/**
* sends infos to all listeners
*
* @param type the id of the event
*/
public void fireEvent( int type ){
fireEvent( type, null );
}
/**
* sends infos to all listeners
*
* @param type the id of the event
* @param the argument
*/
public void fireEvent( int type, Object arg ){
fireEvent( new NetworkEvent( this, type, arg ) );
}
/**
* sends infos to all listeners
*
* @param the event
*/
public void fireEvent( NetworkEvent evt ){
if( under_construction ) System.out.println("Network.fireEvent(): "+evt.getMessage() );
/*
for( int i=listeners[ evt.id ].size()-1; i>-1; i-- )
( (NetworkListener)listeners[ evt.id ].get( i ) ).networkChanged( evt );
*/
for( int i=0; i < listeners.size(); i++ ){
NetworkListener nl = (NetworkListener)listeners.get(i);
if( under_construction ) System.out.println("next recipient: " + nl.getClass() );
nl.networkChanged( evt );
}
if( under_construction ) System.out.println("Network.fireEvent Ende");
}
/*---------------- methods concerning the whole network ----------------------*/
/**
* method returns the name of the network
*/
public String getName(){ return net_name; }
/**
* method sets a new name to the network
*/
public String setName( String name ){
return setName( name, true );
}
/**
* method sets a new name to the network
*/
private String setName( String name, boolean fire_event ){
String old_name = net_name;
net_name = name;
if( fire_event && !net_name.equals( old_name ) )
fireEvent( NetworkEvent.NETWORK_NAME_CHANGED, old_name );
return old_name;
}
/**
* method returns the number of units
*/
public int getNumberOfUnits(){
if( under_construction ) System.out.println("Network.getNumberOfUnits()");
return ki.getNoOfUnits();
}
/**
* method returns the number of input units
*/
public int getNumberOfInputUnits(){
return ki.getNoOfInputUnits();
}
/**
* method returns an array of input units
*/
public Unit[] getInputUnits(){
Unit[] units = new Unit[ ki.getNoOfInputUnits() ];
int index = 0;
for( int no = 1; no < ki.getNoOfUnits() + 1; no++ )
if( ki.getUnitTType( no ) == UnitTTypes.INPUT )
units[ index++ ] = new Unit( ki, no );
return units;
}
/**
* method returns number of output units
*/
public int getNumberOfOutputUnits(){
return ki.getNoOfOutputUnits();
}
/**
* method returns an array of output units
*/
public Unit[] getOutputUnits(){
Unit[] units = new Unit[ ki.getNoOfOutputUnits() ];
int index = 0;
for( int no = 1; no < ki.getNoOfUnits() + 1; no++ )
if( ki.getUnitTType( no ) == UnitTTypes.OUTPUT )
units[ index++ ] = new Unit( ki, no );
return units;
}
/**
* method updates the network with the current update function
*/
public void updateNet() throws Exception{
ki.updateNet();
fireEvent( NetworkEvent.NETWORK_UPDATED );
}
/*-------------------------- functions & parameters --------------------------*/
/**
* method sets a function
*
* @param function
* @param parameters
*/
public void setFunction( Function f, double[] p) throws Exception{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -