Package ca.nengo.model.impl

Source Code of ca.nengo.model.impl.AbstractNode

/*
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
WARRANTY OF ANY KIND, either express or implied. See the License for the specific
language governing rights and limitations under the License.

The Original Code is "AbstractNode.java". Description:
"A base implementation of Node"

The Initial Developer of the Original Code is Bryan Tripp & Centre for Theoretical Neuroscience, University of Waterloo. Copyright (C) 2006-2008. All Rights Reserved.

Alternatively, the contents of this file may be used under the terms of the GNU
Public License license (the GPL License), in which case the provisions of GPL
License are applicable  instead of those above. If you wish to allow use of your
version of this file only under the terms of the GPL License and not to allow
others to use your version of this file under the MPL, indicate your decision
by deleting the provisions above and replace  them with the notice and other
provisions required by the GPL License.  If you do not delete the provisions above,
a recipient may use your version of this file under either the MPL or the GPL License.
*/

/*
* Created on 9-Mar-07
*/
package ca.nengo.model.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import ca.nengo.model.Node;
import ca.nengo.model.Origin;
import ca.nengo.model.SimulationException;
import ca.nengo.model.SimulationMode;
import ca.nengo.model.StructuralException;
import ca.nengo.model.Termination;
import ca.nengo.util.VisiblyMutable;
import ca.nengo.util.VisiblyMutableUtils;

/**
* A base implementation of Node.
*
* @author Bryan Tripp
*/
public abstract class AbstractNode implements Node {

  private static final long serialVersionUID = 1L;

  private String myName;
  private SimulationMode myMode;
  private final Map<String, Origin> myOrigins;
  private final Map<String, Termination> myTerminations;
  private String myDocumentation;
  private transient List<VisiblyMutable.Listener> myListeners;

  /**
   * @param name Name of Node
   * @param origins List of Origins from the Node
   * @param terminations List of Terminations onto the Node
   */
  public AbstractNode(String name, List<Origin> origins, List<Termination> terminations) {
    myName = name;
    myMode = SimulationMode.DEFAULT;

    myOrigins = new LinkedHashMap<String, Origin>(10);
    for (Origin o : origins) {
      myOrigins.put(o.getName(), o);
    }

    myTerminations = new LinkedHashMap<String, Termination>(10);
    for (Termination t : terminations) {
      myTerminations.put(t.getName(), t);
    }
  }

  /**
   * @see ca.nengo.model.Node#getMode()
   */
  public SimulationMode getMode() {
    return myMode;
  }

  /**
   * @see ca.nengo.model.Node#getName()
   */
  public String getName() {
    return myName;
  }

  /**
   * @param name The new name
   */
  public void setName(String name) throws StructuralException {
    VisiblyMutableUtils.nameChanged(this, getName(), name, myListeners);
    myName = name;
  }

  /**
   * @see ca.nengo.model.Node#getOrigin(java.lang.String)
   */
  public Origin getOrigin(String name) throws StructuralException {
    return myOrigins.get(name);
  }

  /**
   * @see ca.nengo.model.Node#getOrigins()
   */
  public Origin[] getOrigins() {
    return myOrigins.values().toArray(new Origin[0]);
  }

  /**
   * @see ca.nengo.model.Node#getTermination(java.lang.String)
   */
  public Termination getTermination(String name) throws StructuralException {
    return myTerminations.get(name);
  }

  /**
   * @see ca.nengo.model.Node#getTerminations()
   */
  public Termination[] getTerminations() {
    return myTerminations.values().toArray(new Termination[0]);
  }

  /**
   * Does nothing.
   *
   * @see ca.nengo.model.Node#run(float, float)
   */
  public abstract void run(float startTime, float endTime) throws SimulationException;

  /**
   * @see ca.nengo.model.Node#setMode(ca.nengo.model.SimulationMode)
   */
  public void setMode(SimulationMode mode) {
    myMode = mode;
  }

  /**
   * Does nothing.
   *
   * @see ca.nengo.model.Resettable#reset(boolean)
   */
  public abstract void reset(boolean randomize);

  /**
   * @see ca.nengo.model.Node#getDocumentation()
   */
  public String getDocumentation() {
    return myDocumentation;
  }

  /**
   * @see ca.nengo.model.Node#setDocumentation(java.lang.String)
   */
  public void setDocumentation(String text) {
    myDocumentation = text;
  }

  /**
   * @see ca.nengo.util.VisiblyMutable#addChangeListener(ca.nengo.util.VisiblyMutable.Listener)
   */
  public void addChangeListener(Listener listener) {
    if (myListeners == null) {
      myListeners = new ArrayList<Listener>(2);
    }
    myListeners.add(listener);
  }

  /**
   * @see ca.nengo.util.VisiblyMutable#removeChangeListener(ca.nengo.util.VisiblyMutable.Listener)
   */
  public void removeChangeListener(Listener listener) {
    myListeners.remove(listener);
  }

  /**
   * Performs a shallow copy. Origins and Terminations are not cloned, because generally they
   * will have to be reparameterized, at least to point to the new Node.
   */
  @Override
  public Node clone() throws CloneNotSupportedException {
    Node result = (Node) super.clone();
    return result;
  }

}
TOP

Related Classes of ca.nengo.model.impl.AbstractNode

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.