Created
September 3, 2009 15:00
-
-
Save NicolasT/180345 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* POC code, abstract classes might be better */ | |
class Machine { | |
private Agent agent; | |
private Machine parent; | |
public Machine(Agent agent) { | |
this.agent = agent; | |
this.parent = null; | |
} | |
public Machine(Agent agent, Machine parent) { | |
this(agent); | |
this.parent = parent; | |
} | |
public Machine getParent() { | |
return this.parent; | |
} | |
public Agent getAgent() { | |
return this.agent; | |
} | |
} | |
class Agent { | |
private String name; | |
public Agent(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return this.name; | |
} | |
} | |
abstract class Applicable<I, O> { | |
// A child should be an applicable which converts an input type into | |
// another input type. I think. Need to check | |
private Applicable<I, I> child; | |
public Applicable() { | |
this.child = null; | |
} | |
public Applicable(Applicable<I, I> a) { | |
this(); | |
this.child = a; | |
} | |
abstract O do_apply(I obj); | |
public O apply(I obj) { | |
I real_i = null; | |
if(this.child != null) | |
real_i = this.child.apply(obj); | |
else | |
real_i = obj; | |
return do_apply(real_i); | |
} | |
} | |
class agent extends Applicable<Machine, Agent> { | |
public agent() { | |
super(); | |
} | |
public agent(Applicable<Machine, Machine> a) { | |
super(a); | |
} | |
Agent do_apply(Machine obj) { | |
return obj.getAgent(); | |
} | |
} | |
class parent extends Applicable<Machine, Machine> { | |
public parent() { | |
super(); | |
} | |
public parent(Applicable<Machine, Machine> a) { | |
super(a); | |
} | |
Machine do_apply(Machine obj) { | |
return obj.getParent(); | |
} | |
} | |
class DSL { | |
public static void main(String[] args) { | |
Machine m1 = new Machine(new Agent("agent1")); | |
Machine m2 = new Machine(new Agent("agent2"), m1); | |
Machine m3 = new Machine(new Agent("agent3"), m2); | |
System.out.println("Calculating name of parent's parent machine"); | |
agent res = new agent(new parent(new parent())); | |
Agent a = res.apply(m3); | |
System.out.println("Name: " + a.getName()); | |
System.out.println("Calculating agent name of m2"); | |
agent res2 = new agent(); | |
Agent b = res2.apply(m2); | |
System.out.println("Name: " + b.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment