Trader creator using JSON
This commit is contained in:
parent
4011aa4846
commit
9008eed00b
@ -1,4 +1,4 @@
|
|||||||
#Mon, 10 Dec 2018 11:28:07 +0100
|
#Wed, 12 Dec 2018 19:31:33 +0100
|
||||||
annotation.processing.enabled=true
|
annotation.processing.enabled=true
|
||||||
annotation.processing.enabled.in.editor=false
|
annotation.processing.enabled.in.editor=false
|
||||||
annotation.processing.processors.list=
|
annotation.processing.processors.list=
|
||||||
|
@ -594,7 +594,10 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
GodWorld world = new GodWorld(Globals.getWorld());
|
GodWorld world = new GodWorld(Globals.getWorld());
|
||||||
|
|
||||||
|
JSONObject cfg = new JSONObject("{"
|
||||||
|
+ "strategy: opensesim.trader.SimpleTrader"
|
||||||
|
+ "}");
|
||||||
|
world.createTrader(cfg);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ package opensesim.trader;
|
|||||||
|
|
||||||
import opensesim.world.AbstractTrader;
|
import opensesim.world.AbstractTrader;
|
||||||
import opensesim.world.World;
|
import opensesim.world.World;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -40,12 +41,12 @@ public class SimpleTrader extends AbstractTrader{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SimpleTrader(World world){
|
SimpleTrader(World world, JSONObject cfg){
|
||||||
super(world);
|
super(world,cfg);
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleTrader(){
|
SimpleTrader(){
|
||||||
this(null);
|
this(null,null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ public abstract class AbstractTrader implements Trader {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractTrader(World world) {
|
public AbstractTrader(World world, JSONObject cfg) {
|
||||||
this.world=world;
|
this.world=world;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class Account {
|
|||||||
ConcurrentHashMap <AbstractAsset,Double> assets = new ConcurrentHashMap<>();
|
ConcurrentHashMap <AbstractAsset,Double> assets = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
Trader owner;
|
Trader owner;
|
||||||
Exchange exchange;
|
Exchange exchange=null;
|
||||||
|
|
||||||
public Map<AbstractAsset,Double> getAssets() {
|
public Map<AbstractAsset,Double> getAssets() {
|
||||||
return Collections.unmodifiableMap(assets);
|
return Collections.unmodifiableMap(assets);
|
||||||
|
@ -208,7 +208,6 @@ public class GodWorld implements GetJson, World {
|
|||||||
return Collections.unmodifiableCollection(assetPairs);
|
return Collections.unmodifiableCollection(assetPairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void add(AssetPair pair) {
|
public void add(AssetPair pair) {
|
||||||
assetPairs.add(pair);
|
assetPairs.add(pair);
|
||||||
}
|
}
|
||||||
@ -311,11 +310,24 @@ public class GodWorld implements GetJson, World {
|
|||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
// Stuff belonging to traders
|
// Stuff belonging to traders
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
private final HashSet<Trader> traders = new HashSet<>();
|
private final HashSet<Trader> traders = new HashSet<>();
|
||||||
|
|
||||||
public Trader createTrader(JSONObject cfg) {
|
public Trader createTrader(JSONObject cfg) {
|
||||||
|
AbstractTrader trader;
|
||||||
|
String strategy = cfg.optString("strategy", null);
|
||||||
|
if (strategy == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Class cls;
|
||||||
|
try {
|
||||||
|
cls = (Class<Trader>) Class.forName(strategy);
|
||||||
|
trader = (AbstractTrader) cls.getConstructor(JSONObject.class).newInstance(cfg);
|
||||||
|
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
|
||||||
|
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -325,4 +337,7 @@ public class GodWorld implements GetJson, World {
|
|||||||
return Collections.unmodifiableCollection(traders);
|
return Collections.unmodifiableCollection(traders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
// Stuff belonging to accounts
|
||||||
|
// --------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user