Creates assets in world from JSON

This commit is contained in:
7u83 2018-12-04 01:36:00 +01:00
parent 5a19f801b3
commit 6bb6d88371
15 changed files with 234 additions and 91 deletions

View File

@ -92,7 +92,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
String type_name; String type_name;
try { try {
type_name=a.getConstructor().newInstance().getTypeName(); type_name=a.getConstructor(World.class,JSONObject.class).newInstance(null,null).getTypeName();
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(AssetListPanel.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(AssetListPanel.class.getName()).log(Level.SEVERE, null, ex);

View File

@ -445,13 +445,7 @@ public class Globals {
} }
public static JSONObject getWorld(){
JSONObject world = new JSONObject();
world.put(PrefKeys.ASSETS, getAssets());
world.put(PrefKeys.EXCHANGES, getExchanges());
return world;
}
public static void clearAll() { public static void clearAll() {
putStrategies(new JSONObject()); putStrategies(new JSONObject());
putTraders(new JSONArray()); putTraders(new JSONArray());
@ -466,5 +460,13 @@ public class Globals {
loadString(s); loadString(s);
} }
public static JSONObject getWorld(){
JSONObject cfg = new JSONObject();
cfg.put(World.JKEYS.ASSETS, getAssets());
cfg.put(World.JKEYS.EXCHANGES, getExchanges());
return cfg;
}
} }

View File

@ -69,6 +69,7 @@ import opensesim.old_sesim.Exchange;
import opensesim.old_sesim.Scheduler; import opensesim.old_sesim.Scheduler;
import opensesim.util.XClassLoader; import opensesim.util.XClassLoader;
import opensesim.world.World;
/** /**
* *
@ -598,7 +599,7 @@ public class SeSimApplication extends javax.swing.JFrame {
void startSim() { void startSim() {
// World = new World(); World world = new World(Globals.getWorld());

View File

@ -129,16 +129,30 @@ public class Json {
} }
Class cls = f.getType(); Class cls = f.getType();
String name = null == imp.value() ? f.getName() : imp.value();
// JTextField
if (JTextField.class.isAssignableFrom(cls)) { if (JTextField.class.isAssignableFrom(cls)) {
try { try {
JTextField tf = (JTextField) f.get(o); JTextField tf = (JTextField) f.get(o);
String name = null == imp.value() ? f.getName() : imp.value();
tf.setText(jo.optString(name)); tf.setText(jo.optString(name));
} catch (IllegalArgumentException | IllegalAccessException ex1) { } catch (IllegalArgumentException | IllegalAccessException ex1) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1); Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1);
} }
continue; continue;
} }
// String
if (String.class.isAssignableFrom(cls)) {
try {
f.set(o, jo.optString(name));
} catch (IllegalArgumentException | IllegalAccessException ex1) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1);
}
continue;
}
} }
Method[] methods = o.getClass().getMethods(); Method[] methods = o.getClass().getMethods();

View File

@ -26,6 +26,8 @@
package opensesim.sesim.Assets; package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset; import opensesim.world.AbstractAsset;
import opensesim.world.World;
import org.json.JSONObject;
/** /**
* *
@ -33,6 +35,12 @@ import opensesim.world.AbstractAsset;
*/ */
public class CryptoCurrency extends AbstractAsset{ public class CryptoCurrency extends AbstractAsset{
public CryptoCurrency(World world, JSONObject cfg) {
super(world, cfg);
}
@Override @Override
public String getTypeName() { public String getTypeName() {
return "Crypto Currency"; return "Crypto Currency";

View File

@ -26,6 +26,8 @@
package opensesim.sesim.Assets; package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset; import opensesim.world.AbstractAsset;
import opensesim.world.World;
import org.json.JSONObject;
/** /**
* *
@ -33,6 +35,12 @@ import opensesim.world.AbstractAsset;
*/ */
public class CurrencyAsset extends AbstractAsset { public class CurrencyAsset extends AbstractAsset {
public CurrencyAsset(World world, JSONObject cfg) {
super(world, cfg);
}
@Override @Override
public String getTypeName() { public String getTypeName() {
return "Currency"; return "Currency";

View File

@ -27,12 +27,21 @@ package opensesim.sesim.Assets;
import javax.swing.JPanel; import javax.swing.JPanel;
import opensesim.world.AbstractAsset; import opensesim.world.AbstractAsset;
import opensesim.world.World;
import org.json.JSONObject;
/** /**
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class FurtureAsset extends AbstractAsset{ public class FurtureAsset extends AbstractAsset{
public FurtureAsset(World world, JSONObject cfg) {
super(world, cfg);
}
@Override @Override
public String getTypeName(){ public String getTypeName(){
return "Future"; return "Future";

View File

@ -26,6 +26,8 @@
package opensesim.sesim.Assets; package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset; import opensesim.world.AbstractAsset;
import opensesim.world.World;
import org.json.JSONObject;
/** /**
* *
@ -33,6 +35,12 @@ import opensesim.world.AbstractAsset;
*/ */
public class StockAssett extends AbstractAsset{ public class StockAssett extends AbstractAsset{
public StockAssett(World world, JSONObject cfg) {
super(world, cfg);
}
@Override @Override
public String getTypeName() { public String getTypeName() {
return "Stock"; return "Stock";

View File

@ -26,12 +26,20 @@
package opensesim.sesim.Assets; package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset; import opensesim.world.AbstractAsset;
import opensesim.world.World;
import org.json.JSONObject;
/** /**
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class WarrentAsset extends AbstractAsset{ public class WarrentAsset extends AbstractAsset{
public WarrentAsset(World world, JSONObject cfg) {
super(world, cfg);
}
@Override @Override
public String getTypeName(){ public String getTypeName(){
return "Warrent"; return "Warrent";

View File

@ -25,10 +25,11 @@
*/ */
package opensesim.world; package opensesim.world;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.JPanel; import javax.swing.JPanel;
import opensesim.gui.util.Json;
import opensesim.gui.util.Json.Import;
import opensesim.sesim.interfaces.Configurable; import opensesim.sesim.interfaces.Configurable;
import opensesim.util.idgenerator.Id; import opensesim.util.idgenerator.Id;
@ -39,9 +40,11 @@ import org.json.JSONObject;
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public abstract class AbstractAsset implements Configurable { public abstract class AbstractAsset {
World world;
private Id id;
private String symbol; private String symbol;
private String name; private String name;
private String description; private String description;
@ -49,18 +52,22 @@ public abstract class AbstractAsset implements Configurable {
/** /**
* Constructor * Constructor
* @param world
* @param cfg
*/ */
protected AbstractAsset() { public AbstractAsset(World world, JSONObject cfg) {
id = null; if (world == null)
return;
symbol = cfg.getString("symbol");
name = cfg.getString("name");
decimals = cfg.optInt("decimals",0);
this.world = world;
} }
public abstract String getTypeName(); public abstract String getTypeName();
public void setDecimals(int decimals) {
this.decimals = decimals;
}
public int getDecimals() { public int getDecimals() {
return decimals; return decimals;
@ -70,10 +77,6 @@ public abstract class AbstractAsset implements Configurable {
this.description = description; this.description = description;
} }
public Id getID() {
return id;
}
public String getSymbol() { public String getSymbol() {
return symbol; return symbol;
} }
@ -82,25 +85,20 @@ public abstract class AbstractAsset implements Configurable {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
public String getDescription() { public String getDescription() {
return description; return description;
} }
public boolean isCurrency(){
return false;
}
public boolean isAsset(){
return false;
}
public static final String JSON_ID ="id"; public boolean isCurrency() {
public static final String JSON_CLASS = "class"; return false;
}
public boolean isAsset() {
return false;
}
public static final String JSON_ID = "id";
public static final String JSON_CLASS = "class";
public static final String JSON_SYMBOL = "symbol"; public static final String JSON_SYMBOL = "symbol";
public static final String JSON_NAME = "name"; public static final String JSON_NAME = "name";
public static final String JSON_DESCRIPTION = "description"; public static final String JSON_DESCRIPTION = "description";
@ -118,9 +116,10 @@ public abstract class AbstractAsset implements Configurable {
} }
public static AbstractAsset create(World world, Class<AbstractAsset> cls, String symbol) throws Exception { /* public static AbstractAsset create(World world, Class<AbstractAsset> cls, String symbol) throws Exception {
AbstractAsset a = cls.newInstance(); AbstractAsset a = cls.newInstance();
if (world.assetsBySymbol.get(symbol) != null) { if (world.assetsBySymbol.get(symbol) != null) {
throw new java.lang.Exception("Can't create asset. Symbol '" + symbol + "' is already in use."); throw new java.lang.Exception("Can't create asset. Symbol '" + symbol + "' is already in use.");
} }
@ -129,13 +128,13 @@ public abstract class AbstractAsset implements Configurable {
a.symbol=symbol; a.symbol=symbol;
world.assetsById.put(a.id, a); world.assetsById.add(a);
world.assetsBySymbol.put(a.getSymbol(), a); world.assetsBySymbol.put(a.getSymbol(), a);
return a; return a;
} }
*/
public static AbstractAsset create(World world, JSONObject cfg){ /* public static AbstractAsset create(World world, JSONObject cfg){
AbstractAsset a; AbstractAsset a;
String class_name; String class_name;
Class cls; Class cls;
@ -160,9 +159,8 @@ public abstract class AbstractAsset implements Configurable {
} }
return null; return null;
} }
*/
@Override /* public JSONObject getConfig() {
public JSONObject getConfig() {
JSONObject cfg = new JSONObject(); JSONObject cfg = new JSONObject();
cfg.put(AbstractAsset.JSON_ID,id.toString()); cfg.put(AbstractAsset.JSON_ID,id.toString());
cfg.put(AbstractAsset.JSON_CLASS, this.getClass().getName()); cfg.put(AbstractAsset.JSON_CLASS, this.getClass().getName());
@ -173,15 +171,15 @@ public abstract class AbstractAsset implements Configurable {
return cfg; return cfg;
} }
@Override
public void putConfig(JSONObject cfg) { public void putConfig(JSONObject cfg) {
symbol = cfg.optString(AbstractAsset.JSON_SYMBOL); symbol = cfg.optString(AbstractAsset.JSON_SYMBOL);
decimals = cfg.optInt(AbstractAsset.JSON_DECIMALS, AbstractAsset.DECIMALS_DEFAULT); decimals = cfg.optInt(AbstractAsset.JSON_DECIMALS, AbstractAsset.DECIMALS_DEFAULT);
name = cfg.optString(AbstractAsset.JSON_NAME, ""); name = cfg.optString(AbstractAsset.JSON_NAME, "");
description = cfg.optString(AbstractAsset.JSON_DESCRIPTION); description = cfg.optString(AbstractAsset.JSON_DESCRIPTION);
} }
*/
public JPanel getEditGui(){ public JPanel getEditGui() {
return null; return null;
} }
} }

View File

@ -51,16 +51,15 @@ public class Exchange implements Configurable{
//private final HashMap<String, AssetPair> asset_pairs; //private final HashMap<String, AssetPair> asset_pairs;
private final HashMap<AssetPair,TradingEnv> asset_pairs; private final HashMap<AssetPair,TradingEnv> asset_pairs = new HashMap<>();
Exchange(World world, String symbol) { Exchange(World world, String symbol) {
asset_pairs = new HashMap<>();
this.world = world; this.world = world;
this.symbol=symbol; this.symbol=symbol;
} }
private void reset() { Exchange(World world, JSONObject cfg){
asset_pairs.clear();
} }
public String getName() { public String getName() {

View File

@ -31,14 +31,12 @@ import opensesim.sesim.AssetPair;
import opensesim.util.idgenerator.IDGenerator; import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.idgenerator.Id; import opensesim.util.idgenerator.Id;
/** /**
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class Order implements Comparable<Order> { public class Order implements Comparable<Order> {
@Override @Override
public int compareTo(Order o) { public int compareTo(Order o) {
{ {
@ -99,7 +97,7 @@ public class Order implements Comparable<Order> {
Order(World world, Account account, AssetPair pair, Type type, Order(World world, Account account, AssetPair pair, Type type,
double volume, double limit) { double volume, double limit) {
this.account = account; this.account = account;
this.type = type; this.type = type;
this.limit = limit; this.limit = limit;
@ -108,10 +106,10 @@ public class Order implements Comparable<Order> {
this.created = 0; this.created = 0;
this.status = OrderStatus.OPEN; this.status = OrderStatus.OPEN;
this.cost = 0; this.cost = 0;
// id = Order.idGenerator.getNext(); // id = Order.idGenerator.getNext();
this.world=world; this.world = world;
// id = world. // id = world.
id = null; id = world.orderIdGenerator.getNext();
} }
public Id getID() { public Id getID() {
@ -168,8 +166,8 @@ public class Order implements Comparable<Order> {
* @return Stock symbol * @return Stock symbol
*/ */
public String getStockSymbol() { public String getStockSymbol() {
// return stock.getSymbol(); // return stock.getSymbol();
return null; return null;
} }
} }

View File

@ -25,35 +25,35 @@
*/ */
package opensesim.world; package opensesim.world;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import opensesim.sesim.AssetPair; import opensesim.sesim.AssetPair;
import opensesim.sesim.interfaces.Configurable;
import opensesim.util.idgenerator.IDGenerator; import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.idgenerator.Id;
import opensesim.util.SeSimException; import opensesim.util.SeSimException;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class World implements Configurable { public class World {
public static final class JKEYS { public static final class JKEYS {
public static final String ASSETS = "assets";
public static final String ASSETS = "assets";
public static final String EXCHANGES = "exchanges"; public static final String EXCHANGES = "exchanges";
} }
HashMap<Id, AbstractAsset> assetsById = new HashMap<>(); HashSet<AbstractAsset> assetsById = new HashSet<>();
HashMap<String, AbstractAsset> assetsBySymbol = new HashMap<>(); HashMap<String, AbstractAsset> assetsBySymbol = new HashMap<>();
IDGenerator assetIdGenerator = new IDGenerator(); IDGenerator assetIdGenerator = new IDGenerator();
IDGenerator orderIdGenerator = new IDGenerator(); IDGenerator orderIdGenerator = new IDGenerator();
HashSet<AssetPair> assetPairs = new HashSet<>(); HashSet<AssetPair> assetPairs = new HashSet<>();
@ -62,14 +62,37 @@ public class World implements Configurable {
/** /**
* Create a World object. * Create a World object.
* *
* @param world * @param cfg
*/ */
public World(JSONObject world) { public World(JSONObject cfg) {
// Read assets
JSONObject jassets = cfg.getJSONObject(World.JKEYS.ASSETS);
for (String symbol : jassets.keySet()) {
AbstractAsset a = createAsset(jassets.getJSONObject(symbol));
assetsById.add(a);
assetsBySymbol.put(symbol, a);
}
}
private AbstractAsset createAsset(JSONObject cfg) {
AbstractAsset a;
String class_name;
Class<AbstractAsset> cls;
class_name = cfg.getString("type");
try {
cls = (Class<AbstractAsset>) Class.forName(class_name);
a = cls.getConstructor(World.class,JSONObject.class).newInstance(this,cfg);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return a;
} }
public Collection<AbstractAsset> getAssetCollection() { public Collection<AbstractAsset> getAssetCollection() {
return Collections.unmodifiableCollection(assetsById.values()); return Collections.unmodifiableCollection(assetsById);
} }
public Collection<AssetPair> getAssetPairsCollection() { public Collection<AssetPair> getAssetPairsCollection() {
@ -99,7 +122,7 @@ public class World implements Configurable {
static final String JSON_ASSET = "asset"; static final String JSON_ASSET = "asset";
static final String JSON_EXCHANGES = "exchanges"; static final String JSON_EXCHANGES = "exchanges";
@Override /*
public JSONObject getConfig() { public JSONObject getConfig() {
JSONObject cfg = new JSONObject(); JSONObject cfg = new JSONObject();
@ -132,12 +155,12 @@ public class World implements Configurable {
AbstractAsset.create(this, acfg); AbstractAsset.create(this, acfg);
} }
} }
*/
public AbstractAsset createAsset(Class cls, String symbol) throws Exception { /* public AbstractAsset createAsset(Class cls, String symbol) throws Exception {
return AbstractAsset.create(this, cls, symbol); return AbstractAsset.create(this, cls, symbol);
} }
*/
/* /*
static public JSONArray toJson() { static public JSONArray toJson() {
JSONArray all = new JSONArray(); JSONArray all = new JSONArray();

View File

@ -37,22 +37,22 @@ import static org.junit.Assert.*;
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class IDGeneratorTest { public class IDGeneratorTest {
public IDGeneratorTest() { public IDGeneratorTest() {
} }
@BeforeClass @BeforeClass
public static void setUpClass() { public static void setUpClass() {
} }
@AfterClass @AfterClass
public static void tearDownClass() { public static void tearDownClass() {
} }
@Before @Before
public void setUp() { public void setUp() {
} }
@After @After
public void tearDown() { public void tearDown() {
} }
@ -66,7 +66,7 @@ public class IDGeneratorTest {
IDGenerator instance = new IDGenerator(); IDGenerator instance = new IDGenerator();
instance.reset(); instance.reset();
// TODO review the generated test code and remove the default call to fail. // TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype."); // fail("The test case is a prototype.");
} }
/** /**
@ -76,12 +76,15 @@ public class IDGeneratorTest {
public void testGetNext() { public void testGetNext() {
System.out.println("getNext"); System.out.println("getNext");
IDGenerator instance = new IDGenerator(7L); IDGenerator instance = new IDGenerator(7L);
Id expResult = new Id<Long>(7L); Id expResult = new Id<Long>(7L);
Id result = instance.getNext(); Id result = instance.getNext();
assertEquals(expResult, result); assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype."); result = instance.getNext();
} expResult = new Id<Long>(8L);
assertEquals(expResult, result);
}
} }

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.world;
import java.util.Collection;
import opensesim.sesim.AssetPair;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class ExchangeTest {
public ExchangeTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
}