Java:从输入文件读取,然后将数据移入子类 [英] Java: reading from an input file, then moving data into subclasses

查看:77
本文介绍了Java:从输入文件读取,然后将数据移入子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模拟项目的早期阶段,当该程序运行时,应将其读入一个文件(如以下文件),然后应通过该程序处理每个神经元并进行突触.突触有两种类型,未命名突触和命名突触.上面的突触X是连接神经元A和B的命名突触.未命名的突触在名称字段中有一个破折号.神经元和突触名称是非数字的.命名的任何突触都可能是次级突触的目标.上面的突触B X是将神经元B连接到(主要)突触X的次要突触.可以命名或未命名的突触具有两个子类:主要和次要.

Early stages of a simulation project, when this program runs, its supposed to read in a file such as the one below, then it should process each neuron and synapse through the program. There are two types of synapse, unnamed synapses and named ones. Synapse X above is a named synapse connecting neurons A and B. Unnamed synapses have a single dash in the name field. Neuron and synapse names be non-numeric. Any synapse that is named may be the target of a secondary synapse. The synapse B X above is a secondary synapse that connects neuron B to (primary) synapse X. A synapse can be named or unnamed has two subclasses: Primary and Secondary.

我的问题:该代码是一个学期项目的一部分,前几部分并不困难,但是我不确定子类.如何在突触行中进行扫描,以确定哪些属于子类,哪些属于子类?我应该对InitializeNetwork方法做什么还是在别的地方需要其他东西?

My question: This code is part of a semester project, the first few parts were not difficult, but I'm unsure about subclasses. How do I scan in the synapse line and determine which belongs in subclass Primary and which ones belong in Secondary? Should I do something to the InitializeNetwork method or do I need something somewhere else?

示例输入文件:*

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();
    }
}

推荐答案

我会问您是否真的要在这里使用继承.使用合成可能是一个更好的主意,在合成中,突触的行为由组成它的类定义.查看策略模式,它可能最终会比某些继承层次结构更好地工作.

I would question if you really want to use inheritance here. It might be a better idea to use composition, where the behavior of synapses are defined by the classes that compose it. Look into the Strategy Pattern, it will probably end up working much better than some inheritance hierarchy.

http://www.tutorialspoint.com/design_pattern/strategy_pattern.htm

这篇关于Java:从输入文件读取,然后将数据移入子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆