Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gp.automaton.base.Program.java

Here you can find all the information about the file org.dgpf.gp.automaton.base.Program.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2005 Thomas Weise
 *
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2005-12-28 09:44:58
 * Original Filename: org.dgpf.automaton.base.Program.java
 * Version          : 2.2.1
 * Last modification: 2006-05-08
 *                by: Thomas Weise
 *
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */


package org.dgpf.gp.automaton.base;

import org.dgpf.gp.automaton.expressions.Constant;
import org.dgpf.gp.automaton.instructions.IfJump;
import org.sfc.math.Mathematics;
import org.sfc.text.Text;
import org.sfc.utils.HashCode;


/**
 * A set of instructions that can be executed by an org.dgpf.automaton.
 * Like in the real world, a program is the blueprint of an activity,
 * telling what should be done and how.
 * The automaton is the "running instance" of a program, like a process is
 * in real world.
 *
 * @author Thomas Weise
 * @see Automaton
 */

public class Program extends AutomatonBase
  {
/**
 * The serial version uid.
 */

  private static  final long            serialVersionUID  = 1;

/**
 * The append chars.
 */

  private static  final String          APPEND  = Text.LINE_BREAK + "  ";
/**
 * The append chars.
 */

  private static  final String          GOTO_   =    GOTO + '0';
  
/**
 * The dummy instruction set.
 */

  private static  final Instruction[] DUMMY = new Instruction[]
       { new IfJump(Constant.CTRUE, 0) };


/**
 * The internal code hash.
 */

                  final Instruction[] m_code  ;
/**
 * The static complexity of the program.
 */

                  final int           m_complexity  ;
/**
 * The memory size needed by this program.
 */

                  final int           m_memory_size ;

/**
 * Create a new set of code instructions.
 * @param p_code        The code constructs to store.
 * @param p_complexity  The static complexity.
 * @param p_memory_size The count of memory cells needed by this org.dgpf.automaton.
 */

  Program(final Instruction[] p_code,
          final int           p_complexity,
          final int           p_memory_size)
    {
    super();
    this.m_code         = p_code;
    this.m_complexity   = ((p_complexity > 0) ? p_complexity
                           : ProgramAccessor.calculate_complexity(p_code));
    this.m_memory_size  = p_memory_size;
    }

/**
 * Create a new set of code instructions.
 * @param p_min_mem_size  The minimum memory size to allocate for this
 *                        program.
 * @param p_code          The code constructs to store.
 */

  public Program(      Instruction[] p_code,
                 final int           p_min_mem_size)
    {
    this( ((p_code != null) &&
           (p_code.length > 0)) ? p_code : (p_code = DUMMY), -1,
         Math.max(1, Math.max(p_min_mem_size,
         ProgramAccessor.calculate_memory_size(p_code))));
    }

/**
 * Returns the count of instructions in the code. This is the length
 * of the program.
 * @return  The count of instructions, i.e. the program's length.
 */

  public  final int get_instruction_count ()
    {
    return this.m_code.length;
    }


/**
 * Returns the complexity of this element.
 * That is the count of cpu ticks that would be consumed if every single
 * sub-statement of the element was executed once.
 * @return The static complexity of this element.
 */

  @Override
  public  final int   get_complexity  ()
    {
    return this.m_complexity;
    }


/**
 * Obtain the count of memory cells this programm needs.
 * @return  The count of memory cells this programm needs.
 */

  @Override
  public  final int   get_memory_size ()
    {
    return this.m_memory_size;
    }

/**
 * Store this objects content into a string builder.
 * @param p_sb  The string builder to store stuff into.
 */

  @Override
  public final  void  to_string (final StringBuilder p_sb)
    {
    int           l_i, l_nw, l_d, l_j;
    Instruction[] l_code;

    p_sb.append("static complexity: ");
    p_sb.append(this.m_complexity);
    p_sb.append(Text.LINE_BREAK_CHARS);
    p_sb.append("memory size:       ");
    p_sb.append(this.m_memory_size);
//    p_sb.append(Text.LINE_BREAK_CHARS);
    p_sb.append(APPEND);
    
    l_code = this.m_code;
    l_nw   = Mathematics.lg(l_code.length);
    
    for(l_i = 0; l_i < l_code.length; l_i++)
      {
      l_d = p_sb.length();
      p_sb.append(l_i);
      for(l_j = (l_d+l_nw-p_sb.length()); l_j > 1; l_j--)
                                                     p_sb.insert(l_d, ' ');
      p_sb.append(": ");
      l_code[l_i].to_string(p_sb);
      p_sb.append(APPEND);
      }

    for(; l_nw >= 0; l_nw--) p_sb.append(' ');
    p_sb.append(GOTO_);
    }

/**
 * This method has been declared abstract so you must override it. For
 * every instruction or expression, a hash code has to be provided.
 * @return  The hash code of the construct.
 * @see Object#hashCode()
 */

  @Override
  public  final int     hashCode  ()
    {
    return HashCode.combine_hash_codes(this.getClass().hashCode(),
           HashCode.object_array_hash_code(this.m_code));
    }

/**
 * Compares two objects for equality.
 * @param p_compare The object to compare with.
 * @return  <code>true</code> if and only if <code>p_compare</code>
 *          represents the same program code as this object.
 */

  @Override
  public  boolean equals  (final Object p_compare)
    {
    if(this == p_compare) return true;

    return ((p_compare instanceof Program) &&
             ProgramAccessor.equals_code( ((Program)p_compare).m_code,
                                          this.m_code));
    }
  }

File Information:

file name:Program.java
package:org.dgpf.gp.automaton.base
qualified name:org.dgpf.gp.automaton.base.Program.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/automaton/base/Program.java
size:6.321 KB (6473 B)
uploaded: 2015-07-22 04:10:55 GMT+0000
last update: 2006-05-08 13:38:45 GMT+0000
last access: 2017-11-18 23:07:11 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-18 23:07:11 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo