codesimian
Class Exec

java.lang.Object
  extended by codesimian.CS
      extended by codesimian.DefaultCS
          extended by codesimian.Exec
All Implemented Interfaces:
CodeSimian, java.io.Serializable
Direct Known Subclasses:
BeforeExec, Exec.Calc, Exec.CalcFuel, Exec.CalcFuelNan, Exec.CalcFuelNanNoPrev, Exec.CalcFuelNoPrev, Exec.CalcNan, Exec.CalcNanNoPrev, Exec.CalcNoPrev, Exec.Fuel, Exec.FuelNan, Exec.FuelNanNoPrev, Exec.FuelNoPrev, Exec.Nan, Exec.NanNoPrev, Exec.NoPrev, Exec.SetPrevExecButNeverUseIt, Static.DefaultExec

public abstract class Exec
extends DefaultCS

An EXEC is a wrapper class for CS.DForProxy().

A subclass can do whatever you want to CodeSimian language, but the default does things like measure costs and end infinite loops caused by cycles in a graph of code. Other default (but not yet instantiated) options include to only return DForProxy() or to only prevent infinite loops or neither or both etc.

This is a wrapper class for the execution of an other CS.
Use someExec.setP(0,x) then someExec.D() to execute x, instead of calling x.D() directly.

DForProxy() and D() are reversed. In a normal CS, D() calls DForProxy() but never the reverse. Here, DForProxy() calls D(), and D() does the work, but external code should call D() directly and never call DForProxy().

Instead of targetCS.DForProxy(), normal CSs must call targetCS.D(), which calls targetCS.exec.DForProxy(targetCS). The first exec is an instance of this class. The second is this class's DForProxy() function.

That allows code to easily be inserted between all CodeSimian operations, between parent and child. Any CS that ever calls any CS's DForProxy() must override execHacker() to return true.

Has inner classes: some implememtations of Exec interface, combining the same few options in different combinations.

It is faster to use these inner classes like function-pointers than to use if's to check if each option is enabled every execution (in 1 unified DForProxy()), and that would not be as extendable.

See Also:
Serialized Form

Nested Class Summary
static class Exec.Calc
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if:
-- execMe.myFuel() <= 0
If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
This class automatically generated by Exec.javaCode(null, true, false, false, true)
static class Exec.CalcFuel
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if either of:
-- execMe.myFuel() <= 0
-- execMe.fuel().D() <= 0.0
If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
-- decreases execMe.fuel() by execMe.cost()
This class automatically generated by Exec.javaCode(null, true, true, false, true)
static class Exec.CalcFuelNan
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if any of:
-- execMe.myFuel() <= 0
-- execMe.fuel().D() <= 0.0
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
-- decreases execMe.fuel() by execMe.cost()
This class automatically generated by Exec.javaCode(null, true, true, true, true)
static class Exec.CalcFuelNanNoPrev
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if any of:
-- execMe.myFuel() <= 0
-- execMe.fuel().D() <= 0.0
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
If execMe is executed, CHANGES:
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
-- decreases execMe.fuel() by execMe.cost()
WARNING: Sometimes returns prevExec, but never sets it. PrevExec might always be 0.
This class automatically generated by Exec.javaCode(null, true, true, true, false)
static class Exec.CalcFuelNoPrev
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if either of:
-- execMe.myFuel() <= 0
-- execMe.fuel().D() <= 0.0
If execMe is executed, CHANGES:
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
-- decreases execMe.fuel() by execMe.cost()
WARNING: Sometimes returns prevExec, but never sets it. PrevExec might always be 0.
This class automatically generated by Exec.javaCode(null, true, true, false, false)
static class Exec.CalcNan
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if either of:
-- execMe.myFuel() <= 0
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
This class automatically generated by Exec.javaCode(null, true, false, true, true)
static class Exec.CalcNanNoPrev
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if either of:
-- execMe.myFuel() <= 0
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
If execMe is executed, CHANGES:
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
WARNING: Sometimes returns prevExec, but never sets it. PrevExec might always be 0.
This class automatically generated by Exec.javaCode(null, true, false, true, false)
static class Exec.CalcNoPrev
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if:
-- execMe.myFuel() <= 0
If execMe is executed, CHANGES:
-- decrements execMe.myFuel (unless it equals CS.ALWAYS_CALCULATE)
WARNING: Sometimes returns prevExec, but never sets it. PrevExec might always be 0.
This class automatically generated by Exec.javaCode(null, true, false, false, false)
static class Exec.Fuel
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if:
-- execMe.fuel().D() <= 0.0
If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
-- decreases execMe.fuel() by execMe.cost()
This class automatically generated by Exec.javaCode(null, false, true, false, true)
static class Exec.FuelNan
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if either of:
-- execMe.fuel().D() <= 0.0
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
-- decreases execMe.fuel() by execMe.cost()
This class automatically generated by Exec.javaCode(null, false, true, true, true)
static class Exec.FuelNanNoPrev
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if either of:
-- execMe.fuel().D() <= 0.0
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
If execMe is executed, CHANGES:
-- decreases execMe.fuel() by execMe.cost()
WARNING: Sometimes returns prevExec, but never sets it. PrevExec might always be 0.
This class automatically generated by Exec.javaCode(null, false, true, true, false)
static class Exec.FuelNoPrev
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if:
-- execMe.fuel().D() <= 0.0
If execMe is executed, CHANGES:
-- decreases execMe.fuel() by execMe.cost()
WARNING: Sometimes returns prevExec, but never sets it. PrevExec might always be 0.
This class automatically generated by Exec.javaCode(null, false, true, false, false)
static class Exec.Nan
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if:
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
This class automatically generated by Exec.javaCode(null, false, false, true, true)
static class Exec.NanNoPrev
          CS execMe = this.P(0); Normally returns execMe.DForProxy(), but returns execMe.prevD() if:
-- execMe.DForProxy() returns NaN, infinity, or -infinity.
WARNING: Sometimes returns prevExec, but never sets it. PrevExec might always be 0.
This class automatically generated by Exec.javaCode(null, false, false, true, false)
static class Exec.NoPrev
          CS execMe = this.P(0); This class automatically generated by Exec.javaCode(null, false, false, false, false)
static class Exec.SetPrevExecButNeverUseIt
          CS execMe = this.P(0); If execMe is executed, CHANGES:
-- sets execMe's prevExec to value of execMe.DForProxy()
WARNING: Sets prevExec, but never returns or uses it. If you do not prevD() externally, this wastes cpu time.
This class automatically generated by Exec.javaCode(SetPrevExecButNeverUseIt, false, false, false, true)
static class Exec.SubclassDescription
          describes a subclass of Exec that you are searching for.
 
Field Summary
static java.lang.String[] subclassNameLimitCalcExec
          part of the (future) name of of automatically generated subclasses of Exec that limit DForProxy() by myFuel.
static java.lang.String[] subclassNameLimitFuel
          {"", "Fuel"}
static java.lang.String[] subclassNameLimitValidNumber
          {"", "Nan"}
static java.lang.String[] subclassNameSetsPrevExec
          {"NoPrev", ""}
 
Fields inherited from class codesimian.CS
DESCRIPTION, END, EXECPROXY, FUEL, HEAP, JAVACODE, MYFUEL, NAME, NEWINSTANCE, NULL, PARENT, PARSEPRIORITY, PREV, TESTER
 
Constructor Summary
Exec()
           
 
Method Summary
static void addSubclass(java.lang.Class subclassOfExec)
          Add a new subclass of Exec at any time.
For any subDesc where subDesc.equals(subclassInstance.getClassDescription()),
Exec.getSubclass(subDesc) returns subclassInstance.getClass().
 java.lang.String detailString()
           
 double DForProxy()
          reversed.
 Exec.SubclassDescription getClassDescription()
           
static java.lang.Class getSubclass(Exec.SubclassDescription sd)
          Returns a subclass of Exec, probably automatically generated by javaCode(), with the specified options.
static Exec instantiateSubclass(Exec.SubclassDescription sd)
          returns a new instance of the subclass you describe, or null if class for that description not found
static java.lang.String javaCode(java.lang.String name, boolean limitCalcExec, boolean limitFuel, boolean limitValidNumber, boolean setsPrevExec)
           
static java.lang.String javaCode(java.lang.String newClassName, Exec.SubclassDescription sd)
          Returns a string of Java code to be inserted in this class as inner classes, between "//START-GENERATED-CODE" and "//END-GENERATED-CODE".
 java.lang.String keyword()
          return "exec."+getClass().getName();
 java.lang.Object L(java.lang.Class castToThisType)
          Optionally execute this CS, and definitely try to CAST it to the specified Java type.
 java.lang.Object L(int startIndex, java.lang.Class castToThisType, int indexQuantity)
          same as L(Class) except only uses a subset of param indexs.
Like many other L functions, allows optimizations of converting CSs to specific Object types.
Example: new S("abcdefg").L(2,String.class,3) returns "cde".
 java.lang.Object LForProxy(java.lang.Class castToThisType)
          reversed
 java.lang.Object LForProxy(int startIndex, java.lang.Class castToThisType, int indexQuantity)
          reversed
 boolean limitCalcExec()
          true if this.DForProxy(f) chooses to execute f or not depending on if f.myFuel is positive.
 boolean limitFuel()
          true if this.DForProxy(f) chooses to execute f or not depending on the fuel contained in f's fuel() object.
 boolean limitValidNumber()
          true if this.DForProxy(f) chooses to execute f or not depending on if f.DForProxy() is NaN, -infinity, or infinity.
 boolean setsPrevExec()
          true if this.DForProxy(f) always sets (or returns) f's prevExec.
 
Methods inherited from class codesimian.DefaultCS
B, C, countP, decrementMyFuel, deleteP, description, F, fuel, getExec, getObject, heap, I, indexP, indexPName, insertB, insertC, insertD, insertF, insertI, insertJ, insertL, insertL, insertL1, insertP, insertS, insertZ, J, javaCode, minP, myFuel, name, newInstance, objectToCS, objectToCSArray, objectToCSArray, P, prevD, prevL, PType, S, setB, setC, setCountP, setD, setD, setExec, setF, setFuel, setI, setJ, setL, setL, setL, setL1, setMyFuel, setName, setObject, setP, setPrevExec, setS, setZ, start, toString, V, Z
 
Methods inherited from class codesimian.CS
addP, addP, addP, addP, addP, BForProxy, CForProxy, clone, cost, D, deleteP, FForProxy, IForProxy, isIllusion, javaCode, JForProxy, L, L, L, maxD, maxP, minD, overwrites, parent, parsePriority, PB, PC, PD, PF, PI, PJ, PL, prevB, prevC, prevF, prevI, prevJ, prevS, prevZ, proxyOf, PS, PZ, reflect, reflect, setB, setC, setCost, setDescription, setF, setHeap, setI, setJ, setL, setL, setParent, setParsePriority, setProxyOf, setPType, setS, setTester, setZ, SForProxy, tester, VForProxy, voidReflect, ZForProxy
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

subclassNameLimitCalcExec

public static java.lang.String[] subclassNameLimitCalcExec
part of the (future) name of of automatically generated subclasses of Exec that limit DForProxy() by myFuel. If limitCalcExec()==true, use index 1, else index 0.
Changing my value only changes inner subclass names if you copy/paste them to Exec.java.
{"", "Calc"} <-- my initial value


subclassNameLimitFuel

public static java.lang.String[] subclassNameLimitFuel
{"", "Fuel"}


subclassNameLimitValidNumber

public static java.lang.String[] subclassNameLimitValidNumber
{"", "Nan"}


subclassNameSetsPrevExec

public static java.lang.String[] subclassNameSetsPrevExec
{"NoPrev", ""}

Constructor Detail

Exec

public Exec()
Method Detail

limitCalcExec

public boolean limitCalcExec()
true if this.DForProxy(f) chooses to execute f or not depending on if f.myFuel is positive. If positive, decrements it and returns the exec value. Else returns prevExec. Default: FALSE


limitFuel

public boolean limitFuel()
true if this.DForProxy(f) chooses to execute f or not depending on the fuel contained in f's fuel() object. Executing depletes that fuel by f.cost(). If positive, any amount of fuel can be consumed one time, but not if its negative first.
If it can not execute, returns prevExec. Default: FALSE


limitValidNumber

public boolean limitValidNumber()
true if this.DForProxy(f) chooses to execute f or not depending on if f.DForProxy() is NaN, -infinity, or infinity. If it is one of those 3, returns prevExec. Else returns exec normally. Default: FALSE


setsPrevExec

public boolean setsPrevExec()
true if this.DForProxy(f) always sets (or returns) f's prevExec. Default: TRUE


L

public java.lang.Object L(java.lang.Class castToThisType)
Description copied from class: CS
Optionally execute this CS, and definitely try to CAST it to the specified Java type. If fail, throw a ClassCastException.

Should I change this from throwing a CSCastException to return null instead?

It is preferred not to return CSs from this function. If you have a CS to return, the standard is to put it in param0... setP(0,returnValue), where the function caller should get the output from... P(0).

The functions: V L Z B C S I J F and D, usually EXECUTE this CS and CAST its value to the specified type.
L does not have to execute this CS, but all the others do. The other difference is L can throw a CSCastException (extends ClassCastException), which is a RuntimeException so you do not have to use try/catch if you trust it.

No pattern of L()'s behavior is guaranteed.
The L's of some some CSs are more predictable than others, often written about in javadoc.
If it casts correctly, it could fail later. If it fails many times, and you try again, it could still succeed.

Example: new int[7][6][5].getClass().getName() returns "[[[I". These 9 function names (not 10 because excludes Void) are also used by a very important part of Java (but not as function names): java.lang.Class.name() .

EXAMPLES:
JButton b = (JButton) new ButtonCS().L(Component.class); //JButton inherits from Component
int fiveFiftyFive = new N(555).I();
double charValues[] = (double[]) new S("arraySize11").L(double[].class);

Overrides:
L in class CS
See Also:
CS.Z(), CS.B(), CS.C(), CS.S(), CS.I(), CS.J(), CS.F(), CS.D()

LForProxy

public java.lang.Object LForProxy(java.lang.Class castToThisType)
reversed

Overrides:
LForProxy in class DefaultCS
See Also:
execProxy(), setExecProxy(CS)

L

public java.lang.Object L(int startIndex,
                          java.lang.Class castToThisType,
                          int indexQuantity)
Description copied from class: CS
same as L(Class) except only uses a subset of param indexs.
Like many other L functions, allows optimizations of converting CSs to specific Object types.
Example: new S("abcdefg").L(2,String.class,3) returns "cde".

Overrides:
L in class CS

LForProxy

public java.lang.Object LForProxy(int startIndex,
                                  java.lang.Class castToThisType,
                                  int indexQuantity)
reversed

Overrides:
LForProxy in class DefaultCS
See Also:
CS.L(int,Class,int)

keyword

public java.lang.String keyword()
return "exec."+getClass().getName();

Overrides:
keyword in class DefaultCS
See Also:
CS.parent(), CS.newInstance(), CS.name()

DForProxy

public double DForProxy()
reversed. FastExec would normally call exec, but this DForProxy() calls D().

This, combined with the subclasses implementing fastExec instead of exec,
does not prevent an instance of X from replacing an Exec,
where X is a subclass of CS but not a subclass of Exec.
A non-Exec that whose execHacker()==false can do the job of an Exec.
This execHacker stuff is just for efficiency.

Specified by:
DForProxy in class DefaultCS
See Also:
execHacker()

detailString

public java.lang.String detailString()

getSubclass

public static java.lang.Class getSubclass(Exec.SubclassDescription sd)
Returns a subclass of Exec, probably automatically generated by javaCode(), with the specified options.

Subclasses should have been automatically generated for every valid combination, by javaCode().
4/05 working on this... If you add more subclasses to this file, they will be automatically found (by using java.lang.reflect).

Returns null if not found.


addSubclass

public static void addSubclass(java.lang.Class subclassOfExec)
Add a new subclass of Exec at any time.
For any subDesc where subDesc.equals(subclassInstance.getClassDescription()),
Exec.getSubclass(subDesc) returns subclassInstance.getClass().


getClassDescription

public Exec.SubclassDescription getClassDescription()

instantiateSubclass

public static Exec instantiateSubclass(Exec.SubclassDescription sd)
returns a new instance of the subclass you describe, or null if class for that description not found


javaCode

public static java.lang.String javaCode(java.lang.String newClassName,
                                        Exec.SubclassDescription sd)
Returns a string of Java code to be inserted in this class as inner classes, between "//START-GENERATED-CODE" and "//END-GENERATED-CODE".

If you change this function, get the output of javaCode(options) for every combination of options and copy/paste them to the bottom of this file. If there are too many combinations, only copy/paste the 10000 (or choose your own limit) generated classes that are most likely to be used.

The number of inner classes is exponential compared to the number of options.
If there are 10 boolean options, there must be 2^10 = 1024 subclasses to cover all combinations.

If Name is null (which is usually best), calculates a name from the options given.

Might generate classes that do not make sense, depending on what each option means. Some combinations might not make sense together, like dont setPrevExec but do limitFuel, which makes Fuel return 0 if the fuel is gone because prevExec was not updated. It should have returned the previous calculated fuel value, before the fuel ran out. Unexpected or illogical generated subclasses should include a bold warning in their javadoc.


javaCode

public static java.lang.String javaCode(java.lang.String name,
                                        boolean limitCalcExec,
                                        boolean limitFuel,
                                        boolean limitValidNumber,
                                        boolean setsPrevExec)