Trader creator using JSON

This commit is contained in:
7u83 2018-12-12 19:58:10 +01:00
parent 4011aa4846
commit 9008eed00b
6 changed files with 31 additions and 12 deletions

View File

@ -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.in.editor=false
annotation.processing.processors.list=

View File

@ -594,7 +594,10 @@ public class SeSimApplication extends javax.swing.JFrame {
GodWorld world = new GodWorld(Globals.getWorld());
JSONObject cfg = new JSONObject("{"
+ "strategy: opensesim.trader.SimpleTrader"
+ "}");
world.createTrader(cfg);

View File

@ -27,6 +27,7 @@ package opensesim.trader;
import opensesim.world.AbstractTrader;
import opensesim.world.World;
import org.json.JSONObject;
/**
*
@ -40,12 +41,12 @@ public class SimpleTrader extends AbstractTrader{
}
SimpleTrader(World world){
super(world);
SimpleTrader(World world, JSONObject cfg){
super(world,cfg);
}
SimpleTrader(){
this(null);
this(null,null);
}

View File

@ -85,7 +85,7 @@ public abstract class AbstractTrader implements Trader {
this.name = name;
}
public AbstractTrader(World world) {
public AbstractTrader(World world, JSONObject cfg) {
this.world=world;
}

View File

@ -38,7 +38,7 @@ public class Account {
ConcurrentHashMap <AbstractAsset,Double> assets = new ConcurrentHashMap<>();
Trader owner;
Exchange exchange;
Exchange exchange=null;
public Map<AbstractAsset,Double> getAssets() {
return Collections.unmodifiableMap(assets);

View File

@ -208,7 +208,6 @@ public class GodWorld implements GetJson, World {
return Collections.unmodifiableCollection(assetPairs);
}
public void add(AssetPair pair) {
assetPairs.add(pair);
}
@ -307,15 +306,28 @@ public class GodWorld implements GetJson, World {
public World getWorld() {
return new RealWorld(this);
}
// --------------------------------------------------------------------
// Stuff belonging to traders
// --------------------------------------------------------------------
private final HashSet<Trader> traders = new HashSet<>();
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;
}
@ -324,5 +336,8 @@ public class GodWorld implements GetJson, World {
public Collection<Trader> getTradersCollection() {
return Collections.unmodifiableCollection(traders);
}
// --------------------------------------------------------------------
// Stuff belonging to accounts
// --------------------------------------------------------------------
}