Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/rule/Rule.java @ 9286

Last change on this file since 9286 was 6152, checked in by bfarka, 14 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 10.4 KB
Line 
1/*
2  Copyright 2006 by Sean Luke and George Mason University
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6
7
8package ec.rule;
9import ec.*;
10import ec.util.*;
11import java.io.*;
12
13/*
14 * Rule.java
15 *
16 * Created: Tue Feb 20 13:19:00 2001
17 * By: Liviu Panait and Sean Luke
18 */
19
20/**
21 * Rule is an abstract class for describing rules. It is abstract
22 * because it is supposed to be extended by different classes
23 * modelling different kinds of rules.
24 * It provides the reset abstract method for randomizing the individual.
25 * It also provides the mutate function for mutating an individual rule
26 * It also provides the clone function for cloning the rule.
27 *
28 * <p>You will need to implement some kind of artificial ordering between
29 * rules in a ruleset using the Comparable interface,
30 * so the ruleset can be sorted in such a way that it can be compared with
31 * another ruleset for equality.  You should also implement hashCode
32 * and equals
33 * in such a way that they aren't based on pointer information, but on actual
34 * internal features.
35 *
36 * <p>Every rule points to a RuleConstraints which handles information that
37 * Rule shares with all the other Rules in a RuleSet.
38
39 * <p>In addition to serialization for checkpointing, Rules may read and write themselves to streams in three ways.
40 *
41 * <ul>
42 * <li><b>writeRule(...,DataOutput)/readRule(...,DataInput)</b>&nbsp;&nbsp;&nbsp;This method
43 * transmits or receives a Rule in binary.  It is the most efficient approach to sending
44 * Rules over networks, etc.  The default versions of writeRule/readRule throw errors.
45 * You don't need to implement them if you don't plan on using read/writeRule.
46 *
47 * <li><b>printRule(...,PrintWriter)/readRule(...,LineNumberReader)</b>&nbsp;&nbsp;&nbsp;This
48 * approach transmits or receives a Rule in text encoded such that the Rule is largely readable
49 * by humans but can be read back in 100% by ECJ as well.  To do this, these methods will typically encode numbers
50 * using the <tt>ec.util.Code</tt> class.  These methods are mostly used to write out populations to
51 * files for inspection, slight modification, then reading back in later on.  <b>readRule</b>
52 * reads in a line, then calls <b>readRuleFromString</b> on that line.
53 * You are responsible for implementing readRuleFromString: the Code class is there to help you.
54 * The default version throws an error if called.
55 * <b>printRule</b> calls <b>printRuleToString<b>
56 * and printlns the resultant string. You are responsible for implementing the printRuleToString method in such
57 * a way that readRuleFromString can read back in the Rule println'd with printRuleToString.  The default form
58 * of printRuleToString() simply calls <b>toString()</b>
59 * by default.  You might override <b>printRuleToString()</b> to provide better information.   You are not required to implement these methods, but without
60 * them you will not be able to write Rules to files in a simultaneously computer- and human-readable fashion.
61 *
62 * <li><b>printRuleForHumans(...,PrintWriter)</b>&nbsp;&nbsp;&nbsp;This
63 * approach prints a Rule in a fashion intended for human consumption only.
64 * <b>printRuleForHumans</b> calls <b>printRuleToStringForHumans()<b>
65 * and printlns the resultant string.  The default form of this method just returns the value of
66 * <b>toString()</b>. You may wish to override this to provide more information instead.
67 * You should handle one of these methods properly
68 * to ensure Rules can be printed by ECJ.
69 * </ul>
70
71 <p><b>Parameters</b><br>
72 <table>
73 <tr><td valign=top><i>base</i>.<tt>constraints</tt><br>
74 <font size=-1>string</font></td>
75 <td valign=top>(name of the rule constraint)</td></tr>
76 </table>
77 
78 <p><b>Default Base</b><br>
79 rule.rule
80
81
82 * @author Liviu Panait and Sean luke
83 * @version 1.0
84 */
85public abstract class Rule implements Prototype, Comparable
86    {
87    public static final String P_RULE = "rule";
88    public static final String P_CONSTRAINTS = "constraints";
89    /**
90       An index to a RuleConstraints
91    */
92    public byte constraints;
93
94    /* Returns the Rule's constraints.  A good JIT compiler should inline this. */
95    public final RuleConstraints constraints(final RuleInitializer initializer)
96        {
97        return initializer.ruleConstraints[constraints];
98        }
99
100    /** Rulerates a hash code for this rule -- the rule for this is that the hash code
101        must be the same for two rules that are equal to each other genetically. */
102    public abstract int hashCode();
103   
104    /** Unlike the standard form for Java, this function should return true if this
105        rule is "genetically identical" to the other rule.  The default calls compareTo() */
106    public boolean equals( final Object other )
107        {
108        return compareTo(other) == 0;
109        }
110
111    /**
112       The reset method randomly reinitializes the rule.
113    */
114    public abstract void reset(final EvolutionState state, final int thread);
115
116    /**
117       Mutate the rule.  The default form just resets the rule.
118    */
119    public void mutate(final EvolutionState state, final int thread)
120        {
121        reset(state,thread);
122        }
123
124    /**
125       Nice printing.  The default form simply calls printRuleToStringForHumans and prints the result,
126       but you might want to override this.
127    */
128    public void printRuleForHumans( final EvolutionState state, final int log )
129        { printRuleForHumans(state, log, Output.V_VERBOSE); }
130               
131    /**
132       Nice printing.  The default form simply calls printRuleToStringForHumans and prints the result,
133       but you might want to override this.
134       @deprecated Verbosity no longer has an effect
135    */
136    public void printRuleForHumans( final EvolutionState state, final int log, final int verbosity )
137        { state.output.println(printRuleToStringForHumans(),log);}
138
139    /** Nice printing to a string. The default form calls toString().  */
140    public String printRuleToStringForHumans()
141        { return toString(); }
142       
143    /** Prints the rule to a string in a fashion readable by readRuleFromString.
144        The default form calls printRuleToString().
145        @deprecated */
146    public String printRuleToString(final EvolutionState state)
147        { return printRuleToString(); }
148       
149    /** Prints the rule to a string in a fashion readable by readRuleFromString.
150        The default form simply calls toString() -- you should just override toString()
151        if you don't need the EvolutionState. */
152    public String printRuleToString()
153        { return toString(); }
154       
155
156    /** Reads a rule from a string, which may contain a final '\n'.
157        Override this method.  The default form generates an error. */
158    public void readRuleFromString(final String string, final EvolutionState state)
159        { state.output.error("readRuleFromString(string,state) unimplemented in " + this.getClass()); }
160
161    /**
162       Prints the rule in a way that can be read by readRule().  The default form simply
163       calls printRuleToString(state).   Override this rule to do custom writing to the log,
164       or just override printRuleToString(...), which is probably easier to do.
165    */
166    public void printRule( final EvolutionState state, final int log )
167        { printRule(state, log, Output.V_VERBOSE); }
168
169    /**
170       Prints the rule in a way that can be read by readRule().  The default form simply
171       calls printRuleToString(state).   Override this rule to do custom writing to the log,
172       or just override printRuleToString(...), which is probably easier to do.
173       @deprecated Verbosity no longer has an effect
174    */
175    public void printRule( final EvolutionState state, final int log, final int verbosity )
176        { state.output.println(printRuleToString(state),log); }
177
178    /**
179       Prints the rule in a way that can be read by readRule().  The default form simply
180       calls printRuleToString(state).   Override this rule to do custom writing,
181       or just override printRuleToString(...), which is probably easier to do.
182    */
183    public void printRule( final EvolutionState state, final PrintWriter writer )
184        { writer.println(printRuleToString(state)); }
185
186    /**
187       Reads a rule printed by printRule(...).  The default form simply reads a line into
188       a string, and then calls readRuleFromString() on that line.  Override this rule to do
189       custom reading, or just override readRuleFromString(...), which is probably easier to do.
190    */
191    public void readRule(final EvolutionState state,
192        final LineNumberReader reader)
193        throws IOException
194        { readRuleFromString(reader.readLine(),state); }
195
196
197    /** Override this if you need to write rules out to a binary stream */
198    public void writeRule(final EvolutionState state,
199        final DataOutput dataOutput) throws IOException
200        {
201        state.output.fatal("writeRule(EvolutionState, DataOutput) not implemented in " + this.getClass());
202        }
203
204    /** Override this if you need to read rules in from a binary stream */
205    public void readRule(final EvolutionState state,
206        final DataInput dataInput) throws IOException
207        {
208        state.output.fatal("readRule(EvolutionState, DataInput) not implemented in " + this.getClass());
209        }
210
211
212    public Parameter defaultBase()
213        {
214        return RuleDefaults.base().push(P_RULE);
215        }
216
217    public Object clone()
218        {
219        try { return super.clone(); }
220        catch (CloneNotSupportedException e)
221            { throw new InternalError(); } // never happens
222        }
223
224
225    public void setup(EvolutionState state, Parameter base)
226        {
227        String constraintname = state.parameters.getString(
228            base.push( P_CONSTRAINTS ),defaultBase().push(P_CONSTRAINTS));
229        if (constraintname == null)
230            state.output.fatal("No RuleConstraints name given",
231                base.push( P_CONSTRAINTS ),defaultBase().push(P_CONSTRAINTS));
232
233        constraints = RuleConstraints.constraintsFor(constraintname,state).constraintNumber;
234        state.output.exitIfErrors();
235        }
236   
237    /** This function replaces the old gt and lt functions that Rule used to require
238        as it implemented the SortComparator interface.  If you had implemented those
239        old functions, you can simply implement this function as:
240       
241        <tt><pre>
242        public abstract int compareTo(Object o)
243        {
244        if (gt(this,o)) return 1;
245        if (lt(this,o)) return -1;
246        return 0;
247        }
248        </pre></tt>
249    */
250    public abstract int compareTo(Object o);
251    }
Note: See TracBrowser for help on using the repository browser.