Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:从输入文件读取数据,然后将数据移动到子类中_Java_Polymorphism_Subclass_Simulation - Fatal编程技术网

Java:从输入文件读取数据,然后将数据移动到子类中

Java:从输入文件读取数据,然后将数据移动到子类中,java,polymorphism,subclass,simulation,Java,Polymorphism,Subclass,Simulation,在模拟项目的早期阶段,当这个程序运行时,它应该读入一个文件,比如下面的文件,然后它应该通过这个程序处理每个神经元和突触。有两种类型的突触,未命名突触和命名突触。上面的突触X是连接神经元a和B的命名突触。未命名的突触在名称字段中有一个破折号。神经元和突触的名称不能是数字。任何被命名的突触都可能是次级突触的目标。上面的突触Bx是连接神经元B和(初级)突触X的次级突触。一个突触可以命名或不命名,它有两个亚类:初级和次级 我的问题:这段代码是一个学期项目的一部分,前几部分并不难,但我不确定子类。我如何扫

在模拟项目的早期阶段,当这个程序运行时,它应该读入一个文件,比如下面的文件,然后它应该通过这个程序处理每个神经元和突触。有两种类型的突触,未命名突触和命名突触。上面的突触X是连接神经元a和B的命名突触。未命名的突触在名称字段中有一个破折号。神经元和突触的名称不能是数字。任何被命名的突触都可能是次级突触的目标。上面的突触Bx是连接神经元B和(初级)突触X的次级突触。一个突触可以命名或不命名,它有两个亚类:初级和次级

我的问题:这段代码是一个学期项目的一部分,前几部分并不难,但我不确定子类。我如何扫描突触线,确定哪些属于初级亚类,哪些属于次级亚类?我应该对InitializeNetwork方法做些什么,还是需要在其他地方做些什么

示例输入文件:

neuron A 1.0 0.95
neuron B 1.0 0.0
synapse X A B 1.2  0.5
synapse - B A 0.3 -0.5
synapse - B X 0.3  0.5
我目前掌握的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;

// Utility classes

/** Error reporting methods
 */
class Errors {
    static void fatal( String message ) {
        System.err.println( "Fatal error: " + message );
        System.exit( 1 );
    }
    static void warning( String message ) {
        System.err.println( "Error: " + message );
    }
}

/** Input scanning support methods
 */
class ScanSupport {
    /** Force there to be a line end here, complain if not
     */
    static void lineEnd( Scanner sc, String message ) {
        String skip = sc.nextLine();
        if (!"".equals( skip )) {
            // Bug:  do we want to allow comments here
            Errors.warning( message + " -- expected a newline" );
        }
        // Bug:  what if sc.nextLine() was illegal (illegal state)
    }

    /** Get the next float, or complain if there isn't one
     */
    static String nextName( Scanner sc, String message ) {
        if (sc.hasNext( "[a-zA-Z]\\w*" )) {
            return sc.next();
        } else {
            Errors.warning( message + " -- expected a name" );
            return null;
        }
    }

    /** Get the next float, or complain if there isn't one
     */
    static float nextFloat( Scanner sc, String message ) {
        if (sc.hasNextFloat()) {
            return sc.nextFloat();
        } else {
            Errors.warning( message + " -- expected a number" );
            return 99.99f;
        }
    }
}

// Simulation classes

/** Neurons are the vertices in the neuron network
 *  @see Synapse
 */
class Neuron {
    String name;            // name of this neuron
    private float threshold;    // voltage at which the neuron fires
    private float voltage;      // voltage at the given time
    private float time;     // (see above)

    private LinkedList <Synapse> synapses;  // the outputs of this neuron

    public class IllegalNameException extends Exception {}

    // initializer
    public Neuron( Scanner sc ) throws IllegalNameException {
        // scan and process one neuron
        name = ScanSupport.nextName( sc, "Neuron ??" );
        if (name == null) {
            sc.nextLine();
            throw new IllegalNameException();
        }
        if (NeuronNetwork.findNeuron( name ) != null) {
            Errors.warning(
                "Neuron " + name + " -- duplicate declaration"
            );
            sc.nextLine();
            throw new IllegalNameException();
        }
        threshold = ScanSupport.nextFloat( sc, "Neuron " + name );
        voltage = ScanSupport.nextFloat( sc, "Neuron " + name );
        time = 0.0f;
        ScanSupport.lineEnd( sc, "Neuron " + name );
    }

    // other methods
    public String toString() {
        return (
            "Neuron " +
            name +
            " " +
            threshold +
            " " +
            voltage
        );
    }
}

/** Synapses join neurons
 *  @see Neuron
 */
class Synapse {
    Neuron source;
    Neuron destination;
    Float delay;
    Float strength;

    // name is source destination

    public Synapse( Scanner sc ) {
        // scan and process one synapse
        String sourceName = ScanSupport.nextName( sc, "Synapse ??" );
        String dstName = ScanSupport.nextName( sc,
            "Synapse " +
            ( sourceName != null ? sourceName : "??" ) +
            " ??"
        );
        delay = ScanSupport.nextFloat( sc,
            "Synapse " +
            ( sourceName != null ? sourceName : "??" ) +
            " " +
            ( dstName != null ? dstName : "??" ) +
            " ??"
        );
        strength = ScanSupport.nextFloat( sc,
            "Synapse " +
            ( sourceName != null ? sourceName : "??" ) +
            " " +
            ( dstName != null ? dstName : "??" ) +
            " " + delay + " ??"
        );
        ScanSupport.lineEnd( sc,
            "Synapse " +
            ( sourceName != null ? sourceName : "??" ) +
            " " +
            ( dstName != null ? dstName : "??" ) +
            delay + " " + strength
        );

        // check correctness of fields
        source = NeuronNetwork.findNeuron( sourceName );
        if (source == null) {
            Errors.warning(
                "Synapse " +
                ( sourceName != null ? sourceName : "??" ) +
                " " +
                ( dstName != null ? dstName : "??" ) +
                " -- no such source"
            );
        }
        destination = NeuronNetwork.findNeuron( dstName );
        if (destination == null) {
            Errors.warning(
                "Synapse " +
                ( sourceName != null ? sourceName : "??" ) +
                " " +
                ( dstName != null ? dstName : "??" ) +
                " -- no such destination"
            );
        }
        if (delay < 0.0f) {
            Errors.warning(
                "Synapse " +
                ( sourceName != null ? sourceName : "??" ) +
                " " +
                ( dstName != null ? dstName : "??" ) +
                " " + delay + " " + strength +
                " -- illegal negative delay"
            );
            delay = 99.99f;
        }
    }

    // other methods
    public String toString() {
        return (
            "Synapse " +
            ( source != null ? source.name : "---" ) +
            " " +
            ( destination != null ? destination.name : "---" ) +
            " " + delay + " " + strength
        );
    }
}

/** NeuronNetwork is the main class that builds the whole model
 *  @see Neuron
 *  @see Synapse
 */
public class NeuronNetwork {

    // the sets of all neurons and all synapses
    static LinkedList <Neuron> neurons
        = new LinkedList <Neuron> ();
    static LinkedList <Synapse> synapses
        = new LinkedList <Synapse> ();

    /** Look up s in neurons, find that Neuron if it exists
     *  return null if not.
     */
    public static Neuron findNeuron( String s ) {
        for (Neuron n: neurons) {
            if (n.name.equals(s)) {
                return n;
            }
        }
        return null;
    }

    /** Initialize the neuron network by scanning its description
     */
    static void initializeNetwork( Scanner sc ) {
        while (sc.hasNext()) {
            String command = sc.next();
            if ("neuron".equals( command )) {
                try {
                    neurons.add( new Neuron( sc ) );
                } catch (Neuron.IllegalNameException e) {
                    // no action required
                }
            } else if ("synapse".equals( command )) {
                synapses.add( new Synapse( sc ) );
            } else {
                Errors.warning( command + " -- what is that" );
                sc.nextLine();
            }
        }
    }

    /** Print out the neuron network from the data structure
     */
    static void printNetwork() {
        for (Neuron n:neurons) {
            System.out.println( n.toString() );
        }
        for (Synapse s:synapses) {
            System.out.println( s.toString() );
        }
    }

    /** Main program
     * @see initializeNetwork
     */
    public static void main(String[] args) {
        try {
            if (args.length < 1) {
                Errors.fatal( "-- missing file name" );
            }
            if (args.length > 1) {
                Errors.fatal( "-- too many arguments" );
            }
            initializeNetwork( new Scanner(new File(args[0])) );
        } catch (FileNotFoundException e) {
            Errors.fatal( "" + args[0] + " -- file not found" );
        }
        printNetwork();
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.LinkedList;
导入java.util.Scanner;
//实用程序类
/**错误报告方法
*/
类错误{
静态无效致命(字符串消息){
System.err.println(“致命错误:+消息”);
系统出口(1);
}
静态无效警告(字符串消息){
System.err.println(“错误:+消息”);
}
}
/**输入扫描支持方法
*/
类扫描支持{
/**强迫这里有一条线,如果没有,就抱怨
*/
静态无效lineEnd(扫描仪sc、字符串消息){
字符串skip=sc.nextLine();
如果(!“”.equals(跳过)){
//Bug:我们想在这里允许评论吗
错误。警告(消息+“--应为换行符”);
}
//错误:如果sc.nextLine()是非法的(非法状态),该怎么办
}
/**获取下一个浮动,如果没有,则抱怨
*/
静态字符串nextName(扫描仪sc、字符串消息){
如果(sc.hasNext(“[a-zA-Z]\\w*”){
返回sc.next();
}否则{
错误。警告(消息+“--应为名称”);
返回null;
}
}
/**获取下一个浮动,如果没有,则抱怨
*/
静态浮点nextFloat(扫描仪sc、字符串消息){
if(sc.hasNextFloat()){
返回sc.nextFloat();
}否则{
错误。警告(消息+“--应为数字”);
返回99.99f;
}
}
}
//模拟班
/**神经元是神经元网络中的顶点
*@见突触
*/
类神经元{
字符串名称;//此神经元的名称
私有浮点阈值;//神经元触发的电压
专用浮动电压;//给定时间的电压
专用浮动时间;//(见上文)
私有LinkedList突触;//此神经元的输出
公共类IllegalNameException扩展异常{}
//初始值设定项
公共神经元(扫描程序sc)引发非法名称异常{
//扫描并处理一个神经元
name=ScanSupport.nextName(sc,Neuron??);
if(name==null){
sc.nextLine();
抛出新的非法名称异常();
}
if(NeuronNetwork.findNeuron(name)!=null){
错误。警告(
“神经元”+名称+“--重复声明”
);
sc.nextLine();
抛出新的非法名称异常();
}
阈值=扫描支持.nextFloat(sc,“神经元”+名称);
电压=扫描支持.nextFloat(sc,“神经元”+名称);
时间=0.0f;
ScanSupport.lineEnd(sc,“神经元”+名称);
}
//其他方法
公共字符串toString(){
返回(
“神经元”+
名字+
" " +
门槛+
" " +
电压
);
}
}
/**突触连接神经元
*@见神经元
*/
类突触{
神经元源;
神经元终点;
浮动延迟;
浮力;
//名称是源目的地
公共突触(扫描仪sc){
//扫描和处理一个突触
字符串sourceName=ScanSupport.nextName(sc,“Synapse??);
字符串dstName=ScanSupport.nextName(sc,
“突触”+
(sourceName!=null?sourceName:“??”)+
" ??"
);
延迟=扫描支持.nextFloat(sc,
“突触”+
(sourceName!=null?sourceName:“??”)+
" " +
(dstName!=null?dstName:“??”)+
" ??"
);
强度=扫描支持。下一个浮动(sc,
“突触”+
(sourceName!=null?sourceName:“??”)+
" " +
(dstName!=null?dstName:“??”)+
“+延迟+”??”
);
扫描支持。lineEnd(sc,
“突触”+
(sourceName!=null?sourceName:“??”)+
" " +
(dstName!=null?dstName:“??”)+
延迟+强度
);
//检查字段的正确性
source=NeuronNetwork.findNeuron(sourceName);
if(source==null){
错误。警告(
“突触”+
(sourceName!=null?sourceName:“??”)+
" " +
(dstName!=null?dstName:“??”)+
“--没有这种来源”
);
}
目的地=NeuronNetwork.findNeuron(dstName);
如果(目标==null){
错误。警告(
“突触”+
(sourceName!=null?sourceName:“??”)+
" " +
(dstName!=null?dstName:“??”)+
“--没有这样的目的地”