Created
September 3, 2009 14:19
-
-
Save NicolasT/180325 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 */ | |
interface RootObject {} | |
class Machine implements RootObject { | |
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 implements RootObject { | |
private String name; | |
public Agent(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return this.name; | |
} | |
} | |
interface Applicable { | |
public RootObject apply(RootObject o); | |
} | |
class agent implements Applicable { | |
private Applicable child; | |
public agent() { | |
this.child = null; | |
} | |
public agent(Applicable a) { | |
this.child = a; | |
} | |
public RootObject apply(RootObject o) { | |
RootObject real_o = null; | |
if(this.child != null) | |
real_o = this.child.apply(o); | |
else | |
real_o = o; | |
Machine m = (Machine)real_o; | |
return m.getAgent(); | |
} | |
} | |
class parent implements Applicable { | |
private Applicable child; | |
public parent() { | |
this.child = null; | |
} | |
public parent(Applicable a) { | |
this.child = a; | |
} | |
public RootObject apply(RootObject o) { | |
RootObject real_o = null; | |
if(this.child != null) | |
real_o = this.child.apply(o); | |
else | |
real_o = o; | |
Machine m = (Machine)real_o; | |
return m.getParent(); | |
} | |
} | |
class DSL { | |
public static void main(String[] args) { | |
Applicable res = new agent(new parent(new parent())); | |
Machine m1 = new Machine(new Agent("agent1")); | |
Machine m2 = new Machine(new Agent("agent2"), m1); | |
Machine m3 = new Machine(new Agent("agent3"), m2); | |
Agent a = (Agent)res.apply(m3); | |
System.out.println("Name: " + a.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment