Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.gp.vm.base.Program.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-06-08 08:26:06
 * Original Filename: org.dgpf.gp.vm.Program.java
 * Version          : 1.0.0
 * Last modification: 2006-07-15
 *                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.vm.base;

import org.dgpf.gp.Construct;
import org.sfc.math.Mathematics;
import org.sfc.text.Text;



/**
 * A program that can run on a ram.
 *
 * @author Thomas Weise
 */

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

  private static final long serialVersionUID = 1;

/**
 * The lable end.
 */

  private static  final char[]          LABEL_END = new char[] {':'' '};
/**
 * The internally appended chars.
 */

          static  final char[]          APPEND_CHRS = new char[]
       {' '' '' '' '' '' '' '' '' '' '' '' '};
/**
 * The append chars.
 */

          static  final char[]          APPEND  =
                (Text.LINE_BREAK + "  ").toCharArray();
  
  
/**
 * The program code.
 */

                      Instruction[][] m_code                ;
  
/**
 * Create a new program.
 * @param p_code                The instructions of the new program.
 */

  public  Program (final Instruction[][]  p_code)
    {
    this();    
    init(p_code);    
    }
  
/**
 * Create a new program by copying the data of an existing one.
 * @param p_program The program to copy from.
 */

  public  Program (final Program p_program)
    {
    this();
    init(p_program);
    }
  
/**
 * The parameterless constructor.
 */

  Program()
    {
    super();
    }
  
/**
 * The internal initializer.
 * @param p_code                The instructions of the new program.
 */

  private final void init(final Instruction[][]  p_code)
    {
    this.m_code        = p_code; 
    }
  
/**
 * Initialize a program by copying the data of an existing one.
 * @param p_program The program to copy from.
 */

  void  init  (final Program p_program)
    {
    init(p_program.m_code);
    }
  
  
/**
 * Store this objects content into a string builder.
 * @param p_sb  The string builder to store stuff into.
 */

  @Override
  public  void  to_string (final StringBuilder p_sb)
    {
    int             l_i, l_j, l_k, l_c, l_d, l_e;
    Instruction[][] l_a;
    Instruction[]   l_b;
        
    l_a = this.m_code;
    l_c = (l_a.length-1);
    for(l_i = 0; l_i <= l_c; l_i++)
      {
      p_sb.append(Conventions.PROC_PREFIX);
      p_sb.append(l_i);
      p_sb.append(APPEND);
      
      l_b = l_a[l_i];
      l_d = l_b.length;
      l_e = Mathematics.lg(l_d);
      l_d--;
      for(l_j = 0; l_j <= l_d; l_j++)
        {
        l_k = p_sb.length();
        p_sb.append(l_j);
        p_sb.insert(l_k, APPEND_CHRS, 0, (l_k+l_e-p_sb.length()));
        p_sb.append(LABEL_END);
        l_b[l_j].to_string(p_sb); 
        if(l_j < l_d) p_sb.append(APPEND);
        }
      
      if(l_i < l_c)
        {
        p_sb.append(Text.LINE_BREAK_CHARS);
        p_sb.append(Text.LINE_BREAK_CHARS);
        }
      }   
    }

/**
 * Obtain the count of procedures used by this program.
 * @return The count of procedures used by this program.
 */

  public  final int get_proc_count()
    {
    return this.m_code.length;
    }
  
/**
 * Obtain the count of instructions used by this program.
 * @return The count of instructions used by this program.
 */

  public  final int get_instr_count()
    {
    int             l_i, l_s;
    Instruction[][] l_a;
            
    l_s = 0;    
    l_a = this.m_code;
    for(l_i = (l_a.length-1); l_i >= 0; l_i--)
      {
      l_s += l_a[l_i].length;
      }
    
    return l_s;
    }
  
/**
 * Obtain the count of instructions used by the specified procedure.
 * @param p_proc  The procedure index.
 * @return The count of instructions used by the specified procedure.
 */

  public  final int get_instr_count(final int p_proc)
    {  
    return this.m_code[p_proc].length;
    }
  
  
/**
 * Count the instructions of a specific type.
 * @param p_class The instruction type.
 * @return  The count of instructions which are compatible to the class
 *          <code>p_class</code>.
 */

  public  final int get_count (final Class<? extends Instruction> p_class)
    {
    Instruction[][] l_ins;
    Instruction[]   l_ix;
    int             l_i, l_j, l_c;
    
    l_ins = this.m_code;
    l_c   = 0;
    for(l_i = (l_ins.length-1); l_i >= 0; l_i--)
      {
      l_ix = l_ins[l_i];
      for(l_j = (l_ix.length-1); l_j >= 0; l_j--)
        {
        if(p_class.isInstance(l_ix[l_j])) l_c++;
        }
      }
    
    return l_c;
    }
  
/**
 * Compare this object to another one.
 * @param p_o   The object to compare with.
 * @return  <code>true</code> if and only if this program is equal to
 *          <code>p_o</code>.
 */

  @Override
  public  boolean equals  (final Object p_o)
    {
    Instruction[][] l_cc1, l_cc2;
    Instruction[]   l_c1, l_c2;
    int             l_i, l_j;
    
    if(this == p_o) return true;
    
    if(p_o instanceof Program)
      {
      l_cc1 = ((Program)p_o).m_code;
      l_cc2 = this.m_code;
      
      l_i  = l_cc1.length;
      if(l_i != l_cc2.length) return false;
        
      for(--l_i; l_i >= 0; l_i--)
        {
        l_c1 = l_cc1[l_i];
        l_c2 = l_cc2[l_i];
        
        l_j  = l_c1.length;
        if(l_j != l_c2.length) return false;
        
        for(--l_j; l_j >= 0; l_j--)
          {
          if(!(l_c1[l_j].equals(l_c2[l_j]))) return false;
          }
        }
      
      return true;
      }
    
    return false;
    }
  
  
/**
 * Get a string that can be copy-pasted into a java file an that creates
 * this instruction.
 * @param p_b The string builder to add with. 
 */

  @Override
  public  void    to_creation_string (final StringBuilder p_b)
    {
    int             l_i, l_j;
    Instruction[][] l_i1;
    Instruction[]   l_i2;
        
    p_b.append("new Program(new Instruction[][]");
    p_b.append(APPEND);
    p_b.append('{');
    
    l_i1 = this.m_code;
    for(l_i = 0; l_i < l_i1.length; l_i++)
      {
      l_i2 = l_i1[l_i];
      p_b.append(Text.LINE_BREAK_CHARS);
      p_b.append(APPEND_CHRS, 0, 4);
      p_b.append('{');
      p_b.append(Text.LINE_BREAK_CHARS);
      
      for(l_j = 0; l_j < l_i2.length; l_j++)
        {
        p_b.append(APPEND_CHRS, 0, 4);
        l_i2[l_j].to_creation_string(p_b);
        p_b.append(',');
        p_b.append(Text.LINE_BREAK_CHARS);
        }
      
      p_b.append("    },");
      }
    
    p_b.setLength(p_b.length()-1);
    p_b.append(Text.LINE_BREAK_CHARS);
    p_b.append("  })");
    }
  
  }

File Information:

file name:Program.java
package:org.dgpf.gp.vm.base
qualified name:org.dgpf.gp.vm.base.Program.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gp/vm/base/Program.java
size:7.550 KB (7732 B)
uploaded: 2015-07-22 04:10:56 GMT+0000
last update: 2006-08-01 05:35:47 GMT+0000
last access: 2017-11-20 18:47:46 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-20 18:47:46 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo