Creates assets in world from JSON
This commit is contained in:
parent
5a19f801b3
commit
6bb6d88371
@ -92,7 +92,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
|
||||
String type_name;
|
||||
|
||||
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) {
|
||||
Logger.getLogger(AssetListPanel.class.getName()).log(Level.SEVERE, null, ex);
|
||||
|
@ -445,12 +445,6 @@ 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() {
|
||||
putStrategies(new JSONObject());
|
||||
@ -467,4 +461,12 @@ public class Globals {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static JSONObject getWorld(){
|
||||
JSONObject cfg = new JSONObject();
|
||||
cfg.put(World.JKEYS.ASSETS, getAssets());
|
||||
cfg.put(World.JKEYS.EXCHANGES, getExchanges());
|
||||
return cfg;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -69,6 +69,7 @@ import opensesim.old_sesim.Exchange;
|
||||
import opensesim.old_sesim.Scheduler;
|
||||
|
||||
import opensesim.util.XClassLoader;
|
||||
import opensesim.world.World;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -598,7 +599,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
|
||||
void startSim() {
|
||||
|
||||
// World = new World();
|
||||
World world = new World(Globals.getWorld());
|
||||
|
||||
|
||||
|
||||
|
@ -129,16 +129,30 @@ public class Json {
|
||||
}
|
||||
|
||||
Class cls = f.getType();
|
||||
String name = null == imp.value() ? f.getName() : imp.value();
|
||||
|
||||
// JTextField
|
||||
if (JTextField.class.isAssignableFrom(cls)) {
|
||||
try {
|
||||
JTextField tf = (JTextField) f.get(o);
|
||||
String name = null == imp.value() ? f.getName() : imp.value();
|
||||
|
||||
tf.setText(jo.optString(name));
|
||||
} catch (IllegalArgumentException | IllegalAccessException ex1) {
|
||||
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1);
|
||||
}
|
||||
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();
|
||||
|
@ -26,6 +26,8 @@
|
||||
package opensesim.sesim.Assets;
|
||||
|
||||
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 CryptoCurrency(World world, JSONObject cfg) {
|
||||
super(world, cfg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return "Crypto Currency";
|
||||
|
@ -26,6 +26,8 @@
|
||||
package opensesim.sesim.Assets;
|
||||
|
||||
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 CurrencyAsset(World world, JSONObject cfg) {
|
||||
super(world, cfg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return "Currency";
|
||||
|
@ -27,12 +27,21 @@ package opensesim.sesim.Assets;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import opensesim.world.AbstractAsset;
|
||||
import opensesim.world.World;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class FurtureAsset extends AbstractAsset{
|
||||
|
||||
public FurtureAsset(World world, JSONObject cfg) {
|
||||
super(world, cfg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getTypeName(){
|
||||
return "Future";
|
||||
|
@ -26,6 +26,8 @@
|
||||
package opensesim.sesim.Assets;
|
||||
|
||||
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 StockAssett(World world, JSONObject cfg) {
|
||||
super(world, cfg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return "Stock";
|
||||
|
@ -26,12 +26,20 @@
|
||||
package opensesim.sesim.Assets;
|
||||
|
||||
import opensesim.world.AbstractAsset;
|
||||
import opensesim.world.World;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class WarrentAsset extends AbstractAsset{
|
||||
|
||||
public WarrentAsset(World world, JSONObject cfg) {
|
||||
super(world, cfg);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getTypeName(){
|
||||
return "Warrent";
|
||||
|
@ -25,10 +25,11 @@
|
||||
*/
|
||||
package opensesim.world;
|
||||
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.swing.JPanel;
|
||||
import opensesim.gui.util.Json;
|
||||
import opensesim.gui.util.Json.Import;
|
||||
import opensesim.sesim.interfaces.Configurable;
|
||||
import opensesim.util.idgenerator.Id;
|
||||
|
||||
@ -39,9 +40,11 @@ import org.json.JSONObject;
|
||||
*
|
||||
* @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 name;
|
||||
private String description;
|
||||
@ -49,19 +52,23 @@ public abstract class AbstractAsset implements Configurable {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param world
|
||||
* @param cfg
|
||||
*/
|
||||
protected AbstractAsset() {
|
||||
id = null;
|
||||
public AbstractAsset(World world, JSONObject cfg) {
|
||||
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 void setDecimals(int decimals) {
|
||||
this.decimals = decimals;
|
||||
}
|
||||
|
||||
public int getDecimals() {
|
||||
return decimals;
|
||||
}
|
||||
@ -70,10 +77,6 @@ public abstract class AbstractAsset implements Configurable {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Id getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
@ -82,25 +85,20 @@ public abstract class AbstractAsset implements Configurable {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public boolean isCurrency(){
|
||||
return false;
|
||||
}
|
||||
public boolean isAsset(){
|
||||
public boolean isCurrency() {
|
||||
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_ID = "id";
|
||||
public static final String JSON_CLASS = "class";
|
||||
public static final String JSON_SYMBOL = "symbol";
|
||||
public static final String JSON_NAME = "name";
|
||||
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();
|
||||
|
||||
|
||||
if (world.assetsBySymbol.get(symbol) != null) {
|
||||
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;
|
||||
|
||||
|
||||
world.assetsById.put(a.id, a);
|
||||
world.assetsById.add(a);
|
||||
world.assetsBySymbol.put(a.getSymbol(), a);
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
public static AbstractAsset create(World world, JSONObject cfg){
|
||||
*/
|
||||
/* public static AbstractAsset create(World world, JSONObject cfg){
|
||||
AbstractAsset a;
|
||||
String class_name;
|
||||
Class cls;
|
||||
@ -160,9 +159,8 @@ public abstract class AbstractAsset implements Configurable {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getConfig() {
|
||||
*/
|
||||
/* public JSONObject getConfig() {
|
||||
JSONObject cfg = new JSONObject();
|
||||
cfg.put(AbstractAsset.JSON_ID,id.toString());
|
||||
cfg.put(AbstractAsset.JSON_CLASS, this.getClass().getName());
|
||||
@ -173,15 +171,15 @@ public abstract class AbstractAsset implements Configurable {
|
||||
return cfg;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void putConfig(JSONObject cfg) {
|
||||
symbol = cfg.optString(AbstractAsset.JSON_SYMBOL);
|
||||
decimals = cfg.optInt(AbstractAsset.JSON_DECIMALS, AbstractAsset.DECIMALS_DEFAULT);
|
||||
name = cfg.optString(AbstractAsset.JSON_NAME, "");
|
||||
description = cfg.optString(AbstractAsset.JSON_DESCRIPTION);
|
||||
}
|
||||
|
||||
public JPanel getEditGui(){
|
||||
*/
|
||||
public JPanel getEditGui() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -51,16 +51,15 @@ public class Exchange implements Configurable{
|
||||
|
||||
//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) {
|
||||
asset_pairs = new HashMap<>();
|
||||
|
||||
this.world = world;
|
||||
this.symbol=symbol;
|
||||
}
|
||||
|
||||
private void reset() {
|
||||
asset_pairs.clear();
|
||||
Exchange(World world, JSONObject cfg){
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
@ -31,14 +31,12 @@ import opensesim.sesim.AssetPair;
|
||||
import opensesim.util.idgenerator.IDGenerator;
|
||||
import opensesim.util.idgenerator.Id;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class Order implements Comparable<Order> {
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(Order o) {
|
||||
{
|
||||
@ -108,10 +106,10 @@ public class Order implements Comparable<Order> {
|
||||
this.created = 0;
|
||||
this.status = OrderStatus.OPEN;
|
||||
this.cost = 0;
|
||||
// id = Order.idGenerator.getNext();
|
||||
this.world=world;
|
||||
// id = world.
|
||||
id = null;
|
||||
// id = Order.idGenerator.getNext();
|
||||
this.world = world;
|
||||
// id = world.
|
||||
id = world.orderIdGenerator.getNext();
|
||||
}
|
||||
|
||||
public Id getID() {
|
||||
@ -168,8 +166,8 @@ public class Order implements Comparable<Order> {
|
||||
* @return Stock symbol
|
||||
*/
|
||||
public String getStockSymbol() {
|
||||
// return stock.getSymbol();
|
||||
return null;
|
||||
// return stock.getSymbol();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,36 +25,36 @@
|
||||
*/
|
||||
package opensesim.world;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import opensesim.sesim.AssetPair;
|
||||
import opensesim.sesim.interfaces.Configurable;
|
||||
import opensesim.util.idgenerator.IDGenerator;
|
||||
import opensesim.util.idgenerator.Id;
|
||||
import opensesim.util.SeSimException;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class World implements Configurable {
|
||||
public class World {
|
||||
|
||||
public static final class JKEYS {
|
||||
|
||||
public static final String ASSETS = "assets";
|
||||
public static final String EXCHANGES = "exchanges";
|
||||
}
|
||||
|
||||
HashMap<Id, AbstractAsset> assetsById = new HashMap<>();
|
||||
HashSet<AbstractAsset> assetsById = new HashSet<>();
|
||||
HashMap<String, AbstractAsset> assetsBySymbol = new HashMap<>();
|
||||
IDGenerator assetIdGenerator = new IDGenerator();
|
||||
IDGenerator orderIdGenerator = new IDGenerator();
|
||||
|
||||
|
||||
HashSet<AssetPair> assetPairs = new HashSet<>();
|
||||
|
||||
ArrayList<Exchange> exchanges = new ArrayList<>();
|
||||
@ -62,14 +62,37 @@ public class World implements Configurable {
|
||||
/**
|
||||
* 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() {
|
||||
return Collections.unmodifiableCollection(assetsById.values());
|
||||
return Collections.unmodifiableCollection(assetsById);
|
||||
}
|
||||
|
||||
public Collection<AssetPair> getAssetPairsCollection() {
|
||||
@ -99,7 +122,7 @@ public class World implements Configurable {
|
||||
static final String JSON_ASSET = "asset";
|
||||
static final String JSON_EXCHANGES = "exchanges";
|
||||
|
||||
@Override
|
||||
/*
|
||||
public JSONObject getConfig() {
|
||||
JSONObject cfg = new JSONObject();
|
||||
|
||||
@ -132,12 +155,12 @@ public class World implements Configurable {
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
/*
|
||||
static public JSONArray toJson() {
|
||||
|
||||
JSONArray all = new JSONArray();
|
||||
|
@ -66,7 +66,7 @@ public class IDGeneratorTest {
|
||||
IDGenerator instance = new IDGenerator();
|
||||
instance.reset();
|
||||
// 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.");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,8 +80,11 @@ public class IDGeneratorTest {
|
||||
Id expResult = new Id<Long>(7L);
|
||||
Id result = instance.getNext();
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
64
test/opensesim/world/ExchangeTest.java
Normal file
64
test/opensesim/world/ExchangeTest.java
Normal 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() {
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user