Logo
Distributed Genetic Programming Framework
print print

File org.sfc.meta.MetaInformationWriter.java

Here you can find all the information about the file org.sfc.meta.MetaInformationWriter.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-05-06 13:57:18
 * Original Filename: org.sfc.meta.MetaInformationWriter.java
 * Version          : 1.0.2
 * Last modification: 2006-07-14
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version DEFAULT_INDENT.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.sfc.meta;

import org.sfc.io.TextWriter;
import org.sfc.math.stochastic.statistics.StatisticInfo;
import org.sfc.text.Text;
import org.sfc.utils.Typesafe;


/**
 * This class provides helper routines to store meta-information into
 * a text writer.
 *
 * @author Thomas Weise
 */

public final class MetaInformationWriter
  {
  
/**
 * You cannot instantiate this class.
 */

  private MetaInformationWriter()
    {
    Typesafe.do_not_call();
    }
  
  
/**
 * Print this object's data in a human readable form to the destination
 * text writer using meta information.
 * @param p_object  The object to be written.
 * @param p_dest    The destination text writer.
 */

  public  static  final void  print_default (final Object     p_object,
                                             final TextWriter p_dest)
    {
    p_dest.write_linebreak();    
    p_dest.write_linebreak();
    
    if(p_object != null)
      {
      do_print_default(p_object, p_object.getClass(), p_dest);      
      }
    else p_dest.write_object(p_object);
    }
  
/**
 * Print this object's data in a human readable form to the destination
 * text writer using meta information.
 * @param p_object  The object to be written.
 * @param p_class   The class of the object.
 * @param p_dest    The destination text writer.
 */

  private static  final void  do_print_default(final Object     p_object,
                                               final Class      p_class,
                                               final TextWriter p_dest)
    {
    MetaInformation l_mi;
    PropertySet     l_ps;
    PropertySubSet  l_pps;
    boolean         l_b;
    int             l_i, l_j, l_k;
    
    l_mi = MetaInformation.get(p_class);
    if(l_mi != null)
      {
      l_ps = l_mi.get_properties();
      if(l_ps != null)
        {
        l_b = false;
        l_j = l_ps.size();
        for(l_i = 0; l_i < l_j; l_i++)
          {
          l_pps = l_ps.get(l_i);
          l_k   = l_pps.size();
          if(l_k > 0)
            {
            do_print_default2(p_object, l_pps, l_k, p_dest);
            l_b = true;
            }
          }
        if(l_b) return;    
        }
      }
    p_dest.write_object(p_object);
    }
  
/**
 * Print this object's data in a human readable form to the destination
 * text writer using meta information provided by a property subset.
 * @param p_object  The object to be written.
 * @param p_ss      The property subset to print.
 * @param p_ssc     The property subset length.
 * @param p_dest    The destination text writer.
 */

  private static final void do_print_default2(final Object         p_object,
                                              final PropertySubSet p_ss,
                                              final int            p_ssc,
                                              final TextWriter     p_dest)
    {
    int             l_i, l_k, l_l;
    Property<?>     l_p;
    PropertyPort<?> l_pp;
    String          l_s;
    
    p_dest.ensure_new_line();    
    p_dest.write('[');
    p_dest.write(p_ss.get_group().get_name());
    p_dest.write(']');   
    p_dest.inc_indent();
        
    for(l_i = 0; l_i < p_ssc; l_i++)
      {
      l_pp = p_ss.get(l_i);
      l_p  = l_pp.get_property();
      
      if(l_pp.is_array())
        {
        l_s = l_p.get_name();
        l_k = l_pp.get_count(p_object);    
        if(l_k > 0)
          {          
          for(l_l = 0; l_l < l_k; l_l++)
            {
            p_dest.ensure_new_line();
            p_dest.write(l_s);
            p_dest.write('[');
            p_dest.write_int(l_l);
            p_dest.write("]: ");
            p_dest.inc_indent();         
            do_print_default3(p_object, l_p, l_pp, p_dest, l_l, false);
            p_dest.dec_indent();
            }
          }
        else
          {
          p_dest.ensure_new_line();
          p_dest.write(l_s);
          p_dest.write(": []");
          }
        }
      else
        {
        do_print_default3(p_object, l_p, l_pp, p_dest, -1, true);
        }
      }    
    p_dest.dec_indent();    
    }
  
/**
 * Print this object's data in a human readable form to the destination
 * text writer using meta information provided by a property.
 * @param p_object  The object to be written.
 * @param p_p       The property to print.
 * @param p_pp      The property port to use.
 * @param p_dest    The destination text writer.
 * @param p_index   The index of the data to get.
 * @param p_full    <code>false</code> if we only print the stub of the
 *                  property.
 */

  private static final void do_print_default3(
                                  final Object          p_object,
                                  final Property<?>     p_p,
                                  final PropertyPort<?> p_pp,
                                  final TextWriter      p_dest,
                                  final int             p_index,
                                  final boolean         p_full)
    {
    Object        l_o;
    String        l_s;
    boolean       l_b;
    TimeProperty  l_t;
    long          l_l;
        
    if(p_index >= 0) l_o = p_pp.get(p_object, p_index);
    else             l_o = p_pp.get(p_object);
        
    if(l_o == null)
      {
      p_dest.write_linebreak();
      p_dest.write(p_p.get_name());
      p_dest.write(": null");
      }
    else
      {    
      if(MetaInformation.get(l_o.getClass()) != null)
        {
        if(p_index < 0)
          {
          p_dest.write_linebreak();
          p_dest.write(p_p.get_name());
          p_dest.write(": ");
          p_dest.inc_indent();
          }
        p_dest.write_linebreak();
        do_print_default(l_o, l_o.getClass(), p_dest);
        if(p_index < 0)p_dest.dec_indent();
        }
      else
        {      
        if(p_full)
          {
          p_dest.write_linebreak();
          p_dest.write(p_p.get_name());
          p_dest.write(": ");
          }
        
        if(l_o instanceof StatisticInfo)
          {
          print_stat_default(((StatisticInfo)l_o), p_dest);
          }
        else if(l_o instanceof Double)
          {
          p_dest.write_double(((Double)l_o).doubleValue());
          }
        else if(p_p instanceof TimeProperty)
          {
          l_t = Typesafe.cast(p_p);
          l_l = ((Long)l_o).longValue();
          p_dest.write_time(l_l, l_t.is_relative());
          p_dest.write(" (");
          p_dest.write_long(l_l);
          p_dest.write(')');
          }
        else
          {
          l_s = String.valueOf(l_o);
          l_b = Text.contains_linebreak(l_s);
          if(l_b)
            {
            p_dest.inc_indent();
            p_dest.write_linebreak();
            }
          p_dest.write(l_s);
          if(l_b) p_dest.dec_indent();
          }
        
        }
      }
    
    }
  
/**
 * Print the csv content for a statistic info pack.
 * @param p_info  The statistic info record to print.
 * @param p_dest  The destination to write to.
 *
 */

  private static  final void print_stat_default(final StatisticInfo p_info,
                                                final TextWriter    p_dest)
    {
    int l_c;

    p_dest.write("{cnt: ");
    p_dest.write_int(l_c = p_info.get_count());
    if(l_c > 0)
      {
      p_dest.write(", min: ");
      p_dest.write_double(p_info.get_minimum());
      p_dest.write(", med: ");
      p_dest.write_double(p_info.get_median());
      p_dest.write(", avg: ");
      p_dest.write_double(p_info.get_average());
      p_dest.write(", max: ");
      p_dest.write_double(p_info.get_maximum());
      if(l_c > 1)
        {
        p_dest.write(", var: ");
        p_dest.write_double(p_info.get_variance());
        }
      }
    p_dest.write('}');
    }
  
  

/**
 * Print a csv headline from the objects data.
 * @param p_object  The object to be written.
 * @param p_dest    The destination text writer.
 */

  public  static  final void  print_csv_head(final Object     p_object,
                                             final TextWriter p_dest)
    {    
    if(p_object != null)
      {
      do_print_csv_head(p_object, p_object.getClass(), p_dest, null);      
      }    
    }
  
  

/**
 * Print this object's csv head to the destination text writer using meta
 * information.
 * @param p_object  The object to be written.
 * @param p_class   The class of the object.
 * @param p_dest    The destination text writer.
 * @param p_prefix  A prefix to be appended.
 */

  private static  final void  do_print_csv_head(final Object     p_object,
                                                final Class      p_class,
                                                final TextWriter p_dest,
                                                final String     p_prefix)
    {
    MetaInformation l_mi;
    PropertySet     l_ps;
    PropertySubSet  l_pps;
    boolean         l_b;
    int             l_i, l_j, l_k;
    
    l_mi = MetaInformation.get(p_class);
    if(l_mi != null)
      {
      l_ps = l_mi.get_properties();
      if(l_ps != null)
        {
        l_b = false;
        l_j = l_ps.size();
        for(l_i = 0; l_i < l_j; l_i++)
          {
          l_pps = l_ps.get(l_i);
          l_k   = l_pps.size();
          if(l_k > 0)
            {
            do_print_csv_head2(p_object, l_pps, l_k, p_dest, p_prefix);
            l_b = true;
            }
          }
        if(l_b) return;    
        }
      }
    }


/**
 * Print this object's csv-head to the destination text writer using meta
 * information provided by a property subset.
 * @param p_object  The object to be written.
 * @param p_ss      The property subset to print.
 * @param p_ssc     The property subset length.
 * @param p_dest    The destination text writer.
 * @param p_prefix  The prefix to be appended.
 */

  private static final void do_print_csv_head2(final Object         p_object,
                                               final PropertySubSet p_ss,
                                               final int            p_ssc,
                                               final TextWriter     p_dest,
                                               final String         p_prefix)
    {
    int             l_i, l_k, l_l;
    Property<?>     l_p;
    PropertyPort<?> l_pp;
    String          l_s, l_z;
    Object          l_o;
    int             l_x;
    
    for(l_i = 0; l_i < p_ssc; l_i++)
      {
      l_pp = p_ss.get(l_i);
      l_p  = l_pp.get_property();
      
      l_s = l_p.get_name();
      if(p_prefix != null) l_s = p_prefix + l_s;
      l_x = ((MetaInformation.get(l_p.get_type()) != null) ? 1 :
            ((StatisticInfo.class.isAssignableFrom(l_p.get_type()) ? 2 : 
            ((l_p instanceof TimeProperty) ? 3 : 0))));
      
      if(l_pp.is_array())
        {
        l_k = l_pp.get_count(p_object);
        
        if(l_k > 0)
          {                   
          for(l_l = 0; l_l < l_k; l_l++)
            {     
            l_z = l_s + '[' + l_l + ']';
            switch(l_x)
              {
              case 1:
                {
                l_o = l_pp.get(p_object, l_l);
                do_print_csv_head(l_o, l_o.getClass(), p_dest,
                                  l_z + '.');
                break;                  
                }
              case 2:
                {
                print_stat_head(l_z, p_dest);
                break;
                }
              case 3:
                {
                if(!(p_dest.is_new_line())) p_dest.write_csv_separator();
                p_dest.write(l_z);
                p_dest.write("_java");
                p_dest.write_csv_separator();
                p_dest.write(l_z);
                break;
                }
              default:
                {
                if(!(p_dest.is_new_line())) p_dest.write_csv_separator();
                p_dest.write(l_z);
                break;
                }
              }
            }
          }
        }
      else
        {
        switch(l_x)
          {
          case 1:
            {
            l_o = l_pp.get(p_object);
            do_print_csv_head(l_o, l_o.getClass(), p_dest, l_s + '.');
            break;                  
            }
          case 2:
            {
            print_stat_head(l_s, p_dest);
            break;
            }
          case 3:
            {
            if(!(p_dest.is_new_line())) p_dest.write_csv_separator();
            p_dest.write(l_s);
            p_dest.write("_java");
            p_dest.write_csv_separator();
            p_dest.write(l_s);
            break;
            }
          default:
            {
            if(!(p_dest.is_new_line())) p_dest.write_csv_separator();
            p_dest.write(l_s);
            break;
            }
          }
        }
      }
    }
  

/**
 * Print the csv headline for a statistic info pack.
 * @param p_name  The name of the statistic info record.
 * @param p_dest  The destination text writer.
 *
 */

  private static final void print_stat_head(final String     p_name,
                                            final TextWriter p_dest)
    {
    p_dest.write_csv_separator();
    p_dest.write(p_name+"_cnt");
    p_dest.write_csv_separator();
    p_dest.write(p_name+"_min");
    p_dest.write_csv_separator();
    p_dest.write(p_name+"_med");
    p_dest.write_csv_separator();
    p_dest.write(p_name+"_avg");
    p_dest.write_csv_separator();
    p_dest.write(p_name+"_max");
    p_dest.write_csv_separator();
    p_dest.write(p_name+"_var");
    p_dest.write_csv_separator();
    p_dest.write(p_name+"_stddev");
    }

  
  
  

/**
 * Print a csv data from the objects data.
 * @param p_object  The object to be written.
 * @param p_dest    The destination text writer.
 */

  public  static  final void  print_csv_data(final Object     p_object,
                                             final TextWriter p_dest)
    {
    p_dest.ensure_new_line();  
    if(p_object != null)
      {
      do_print_csv_data(p_object, p_object.getClass(), p_dest);      
      }    
    }
  
  

/**
 * Print this object's csv data to the destination text writer using meta
 * information.
 * @param p_object  The object to be written.
 * @param p_class   The class of the object.
 * @param p_dest    The destination text writer.
 */

  private static  final void  do_print_csv_data(final Object     p_object,
                                                final Class      p_class,
                                                final TextWriter p_dest)
    {
    MetaInformation l_mi;
    PropertySet     l_ps;
    PropertySubSet  l_pps;
    boolean         l_b;
    int             l_i, l_j, l_k;
    
    l_mi = MetaInformation.get(p_class);
    if(l_mi != null)
      {
      l_ps = l_mi.get_properties();
      if(l_ps != null)
        {
        l_b = false;
        l_j = l_ps.size();
        for(l_i = 0; l_i < l_j; l_i++)
          {
          l_pps = l_ps.get(l_i);
          l_k   = l_pps.size();
          if(l_k > 0)
            {
            do_print_csv_data2(p_object, l_pps, l_k, p_dest);
            l_b = true;
            }
          }
        if(l_b) return;    
        }
      }
    }


/**
 * Print this object's csv-data to the destination text writer using meta
 * information provided by a property subset.
 * @param p_object  The object to be written.
 * @param p_ss      The property subset to print.
 * @param p_ssc     The property subset length.
 * @param p_dest    The destination text writer.
 */

  private static final void do_print_csv_data2(final Object         p_object,
                                               final PropertySubSet p_ss,
                                               final int            p_ssc,
                                               final TextWriter     p_dest)
    {
    int             l_i, l_k, l_l;
    long            l_z;
    Property<?>     l_p;
    PropertyPort<?> l_pp;
    String          l_s;
    Object          l_o;
    int             l_x;
    Class           l_c;
    TimeProperty    l_y;
        
    for(l_i = 0; l_i < p_ssc; l_i++)
      {
      l_pp = p_ss.get(l_i);
      l_p  = l_pp.get_property();
      
      if((l_x = ((MetaInformation.get(l_p.get_type()) != null) ? 1 :
        ((StatisticInfo.class.isAssignableFrom(l_c = l_p.get_type()) ? 2 :
        ((l_p instanceof TimeProperty) ? 4 :
        ((Boolean.class.isAssignableFrom(l_c) || (boolean.class == l_c)) ?
            6 : 
        ((Double.class.isAssignableFrom(l_c) || (double.class == l_c)) ? 3
            : 0))))))) == 4)        
        {
        l_y = Typesafe.cast(l_p);
        if(l_y.is_relative()) l_x = 5;
        }
      
      if(l_pp.is_array())
        {
        l_k = l_pp.get_count(p_object);
        
        if(l_k > 0)
          {                   
          for(l_l = 0; l_l < l_k; l_l++)
            {    
            l_o = l_pp.get(p_object, l_l);
            if((l_x != 1) && (!(p_dest.is_new_line())))
              {
              p_dest.write_csv_separator();
              }
            
            switch(l_x)
              {
              case 1:
                {                
                do_print_csv_data(l_o, l_o.getClass(), p_dest);
                break;                  
                }                            
              case 2:
                {
                print_stat_data(((StatisticInfo)l_o), p_dest);
                break;
                }
              case 3:
                {
                p_dest.write_double(((Double)l_o).doubleValue());
                break;
                }
              case 4:
              case 5:
                {
                l_z = (((Long)l_o).longValue());
                p_dest.write_long(l_z);
                p_dest.write_csv_separator();
                p_dest.write_time(l_z, (l_x==5));
                break;
                }
              case 6:
                {
                p_dest.write_boolean(((Boolean)l_o).booleanValue());
                break;
                }
              default:
                {
                l_s = String.valueOf(l_o);
                if(Text.contains_linebreak(l_s)) l_s = "[...]";
                p_dest.write(l_s);
                break;
                }
              }
            }
          }
        }
      else
        {          
        l_o = l_pp.get(p_object);
        if((l_x != 1) && (!(p_dest.is_new_line())))
          {
          p_dest.write_csv_separator();
          }
        
        switch(l_x)
          {
          case 1:
            {                
            do_print_csv_data(l_o, l_o.getClass(), p_dest);
            break;                  
            }                            
          case 2:
            {
            print_stat_data(((StatisticInfo)l_o), p_dest);
            break;
            }
          case 3:
            {
            p_dest.write_double(((Double)l_o).doubleValue());
            break;
            }
          case 4:
          case 5:
            {
            l_z = (((Long)l_o).longValue());
            p_dest.write_long(l_z);
            p_dest.write_csv_separator();
            p_dest.write_time(l_z, (l_x==5));
            break;
            }
          case 6:
            {
            p_dest.write_boolean(((Boolean)l_o).booleanValue());
            break;
            }
          default:
            {
            l_s = String.valueOf(l_o);
            if(Text.contains_linebreak(l_s)) l_s = "[...]";
            p_dest.write(l_s);
            break;
            }
          }
        }
      }
    }
  
  
/**
 * Print the csv content for a statistic info pack.
 * @param p_info  The statistic info record to print.
 * @param p_dest  The destination text writer.
 *
 */

  private   static final void print_stat_data(final StatisticInfo p_info,
                                              final TextWriter    p_dest)
    {
    int l_c;

    p_dest.write_int(l_c = p_info.get_count());
    p_dest.write_csv_separator();
    if(l_c > 0) p_dest.write_double(p_info.get_minimum());
    p_dest.write_csv_separator();
    if(l_c > 0) p_dest.write_double(p_info.get_median());
    p_dest.write_csv_separator();
    if(l_c > 0) p_dest.write_double(p_info.get_average());
    p_dest.write_csv_separator();
    if(l_c > 0) p_dest.write_double(p_info.get_maximum());
    p_dest.write_csv_separator();
    if(l_c > 1) p_dest.write_double(p_info.get_variance());
    p_dest.write_csv_separator();
    if(l_c > 1) p_dest.write_double(p_info.get_stddev());
    }

  }

File Information:

file name:MetaInformationWriter.java
package:org.sfc.meta
qualified name:org.sfc.meta.MetaInformationWriter.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/meta/MetaInformationWriter.java
size:21.867 KB (22392 B)
uploaded: 2015-07-22 04:11:12 GMT+0000
last update: 2006-07-14 03:37:19 GMT+0000
last access: 2018-01-23 19:34:30 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 2018-01-23 19:34:30 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo