Removed the "masterkey thing"

We will use proxies from RealWorld to GodWorld
This commit is contained in:
7u83 2018-12-08 11:11:08 +01:00
parent 137c6af75d
commit 6d24eb67b9
27 changed files with 423 additions and 374 deletions

View File

@ -1,4 +1,4 @@
#Fri, 07 Dec 2018 20:30:03 +0100
#Sat, 08 Dec 2018 11:08:46 +0100
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -31,10 +31,8 @@ import java.awt.Window;
import opensesim.world.AbstractAsset;
import opensesim.gui.util.EscDialog;
import opensesim.gui.Globals;
import opensesim.gui.util.Json;
import opensesim.world.World;
import opensesim.world.WorldAdm;
import opensesim.world.GodWorld;
import org.json.JSONObject;
/**
@ -43,7 +41,7 @@ import org.json.JSONObject;
*/
public class AssetEditorDialog extends EscDialog {
WorldAdm worldadm;
GodWorld worldadm;
/**
@ -196,7 +194,7 @@ public class AssetEditorDialog extends EscDialog {
}//GEN-LAST:event_cancelButtonActionPerformed
static public boolean runDialog(Window parent, WorldAdm worldadm, JSONObject o, JSONObject old) {
static public boolean runDialog(Window parent, GodWorld worldadm, JSONObject o, JSONObject old) {
AssetEditorDialog d = new AssetEditorDialog(parent);
d.worldadm = worldadm;

View File

@ -40,8 +40,9 @@ import opensesim.gui.util.Json;
import opensesim.gui.util.Json.Export;
import opensesim.gui.util.Json.Import;
import opensesim.util.SeSimException;
import opensesim.world.World;
import opensesim.world.WorldAdm;
import opensesim.world.GodWorld;
import opensesim.world.RealWorld;
import org.json.JSONObject;
/**
@ -228,7 +229,7 @@ public class AssetEditorPanel extends javax.swing.JPanel {
decimalsField.setValue(Integer.parseInt(d));
}
@Export(World.JKEYS.ASSET_TYPE)
@Export(GodWorld.JKEYS.ASSET_TYPE)
public String getType() {
int selected = assetTypesComboBox.getSelectedIndex();
//return asset_types.get(selected).getName();
@ -237,7 +238,7 @@ public class AssetEditorPanel extends javax.swing.JPanel {
String type;
@Import(World.JKEYS.ASSET_TYPE)
@Import(GodWorld.JKEYS.ASSET_TYPE)
public void setType(String type) {
this.type=type;
@ -249,7 +250,7 @@ public class AssetEditorPanel extends javax.swing.JPanel {
AbstractAsset a;
try {
a = ac.getConstructor(World.class, JSONObject.class).newInstance(null, null);
a = ac.getConstructor(RealWorld.class, JSONObject.class).newInstance(null, null);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
return;
@ -274,12 +275,12 @@ public class AssetEditorPanel extends javax.swing.JPanel {
}
public boolean save(WorldAdm worldadm ){
public boolean save(GodWorld worldadm ){
JSONObject jo = Json.get(this);
System.out.printf("ASSETGETTER: %s\n",jo.toString(5));
if (jo.getString(World.JKEYS.ASSET_SYMBOL).length()==0){
if (jo.getString(GodWorld.JKEYS.ASSET_SYMBOL).length()==0){
javax.swing.JOptionPane.showMessageDialog(this, "Symbol must not be empty.",
"Error",
javax.swing.JOptionPane.ERROR_MESSAGE);
@ -287,7 +288,7 @@ public class AssetEditorPanel extends javax.swing.JPanel {
}
try {
worldadm.world.createAsset(worldadm.masterKey, jo);
worldadm.createAsset(jo);
} catch (SeSimException ex) {
javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage(),
"Error",

View File

@ -29,8 +29,7 @@ import java.awt.Window;
import java.awt.event.MouseEvent;
import opensesim.gui.Globals;
import opensesim.gui.util.EscDialog;
import opensesim.world.World;
import opensesim.world.WorldAdm;
import opensesim.world.GodWorld;
import org.json.JSONObject;
@ -40,7 +39,7 @@ import org.json.JSONObject;
*/
public class AssetListDialog extends EscDialog {
WorldAdm worldadm;
GodWorld worldadm;
/**
* Creates new form EditAssetsDialog
@ -62,7 +61,7 @@ public class AssetListDialog extends EscDialog {
this.assetListPanel.assetTable.addMouseListener(l);
}
public AssetListDialog(WorldAdm worldadm, Window parent, boolean modal) {
public AssetListDialog(GodWorld worldadm, Window parent, boolean modal) {
super(parent, modal);
this.worldadm = worldadm;
@ -173,7 +172,7 @@ public class AssetListDialog extends EscDialog {
return;
}
JSONObject o = new JSONObject();
o.put(World.JKEYS.ASSET_TYPE, type);
o.put(GodWorld.JKEYS.ASSET_TYPE, type);
AssetEditorDialog.runDialog(this, worldadm, o, null);
assetListPanel.reload();
@ -185,7 +184,7 @@ public class AssetListDialog extends EscDialog {
private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
dispose();
JSONObject o = worldadm.world.getJson();
JSONObject o = worldadm.getJson();
Globals.prefs.put("world", o.toString());
}//GEN-LAST:event_doneButtonActionPerformed
@ -205,7 +204,7 @@ public class AssetListDialog extends EscDialog {
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
JSONObject o = assetListPanel.getSelectedObject();
JSONObject ass = Globals.getAssets();
ass.remove(o.getString(World.JKEYS.ASSET_SYMBOL));
ass.remove(o.getString(GodWorld.JKEYS.ASSET_SYMBOL));
Globals.putAssets(ass);
assetListPanel.reload();
}//GEN-LAST:event_deleteButtonActionPerformed

View File

@ -32,9 +32,9 @@ import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.gui.Globals;
import opensesim.world.WorldAdm;
import opensesim.world.GodWorld;
import org.json.JSONObject;
/**
@ -43,7 +43,7 @@ import org.json.JSONObject;
*/
public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionList {
WorldAdm worldadm;
GodWorld worldadm;
/**
* Creates new form AssetList
@ -63,7 +63,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
assetTable.getColumnModel().getColumn(2).setPreferredWidth(80);
}
public AssetListPanel(WorldAdm worldadm) {
public AssetListPanel(GodWorld worldadm) {
this();
this.worldadm = worldadm;
@ -74,13 +74,13 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
public JSONObject getSelectedObject() {
int row = assetTable.getSelectedRow();
String symbol = (String) assetTable.getValueAt(row, 0);
return worldadm.world.getAssetBySymbol(symbol).getJson();
return worldadm.getAssetBySymbol(symbol).getJson();
}
final void reload() {
Collection<AbstractAsset> assets;
assets = worldadm.world.getAssetCollection();
assets = worldadm.getAssetCollection();
DefaultTableModel m = (DefaultTableModel) assetTable.getModel();
m.setRowCount(0);
for (AbstractAsset asset : assets) {
@ -94,7 +94,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
String type_name;
try {
type_name = a.getConstructor(World.class, JSONObject.class).newInstance(null, null).getTypeName();
type_name = a.getConstructor(GodWorld.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);

View File

@ -36,7 +36,8 @@ import javax.swing.DefaultComboBoxModel;
import opensesim.world.AbstractAsset;
import opensesim.gui.Globals;
import opensesim.gui.util.EscDialog;
import opensesim.world.World;
import opensesim.world.GodWorld;
import opensesim.world.RealWorld;
import org.json.JSONObject;
/**
@ -45,7 +46,8 @@ import org.json.JSONObject;
*/
public class SelectAssetTypeDialog extends EscDialog {
ArrayList<Class<AbstractAsset>>asset_types;
ArrayList<Class<AbstractAsset>> asset_types;
/**
* Creates new form SelectAssetTypeDialog
*/
@ -64,22 +66,16 @@ public class SelectAssetTypeDialog extends EscDialog {
Class<AbstractAsset> asset_type = asset_types.get(i);
System.out.printf("ACL: %s\n", asset_type.getName());
try {
Constructor<AbstractAsset> c;
c = asset_type.getConstructor(World.class,JSONObject.class);
ait = c.newInstance(null,null);
//ait = asset_type.getConstructor<AbstractAsset>(World.class,JSONObject.class).
vector.add(i, ait.getTypeName());
} catch (InstantiationException | IllegalAccessException | ClassCastException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(SelectAssetTypeDialog.class.getName()).log(Level.SEVERE, null, ex);
String tn = GodWorld.getTypeName(asset_type);
if (tn == null) {
continue;
}
vector.add(i, tn);
}
return new DefaultComboBoxModel(vector.toArray());
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
@ -151,7 +147,7 @@ public class SelectAssetTypeDialog extends EscDialog {
}// </editor-fold>//GEN-END:initComponents
String selected = null;
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
int i = this.assetTypeComboBox.getSelectedIndex();
selected = asset_types.get(i).getName();
@ -162,15 +158,14 @@ public class SelectAssetTypeDialog extends EscDialog {
dispose();
}//GEN-LAST:event_cancelButtonActionPerformed
public static String runDialog(Window parent){
public static String runDialog(Window parent) {
SelectAssetTypeDialog dialog;
dialog = new SelectAssetTypeDialog(parent,true);
dialog = new SelectAssetTypeDialog(parent, true);
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
return dialog.selected;
}
/**
* @param args the command line arguments
*/

View File

@ -2,7 +2,7 @@ package opensesim.gui.AssetPairEditor;
import java.util.Collection;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.gui.Globals;
/*
@ -37,7 +37,7 @@ import opensesim.gui.Globals;
*/
public class EditAssetPairPanel extends javax.swing.JPanel {
World world;
RealWorld world;
/**
* Creates new form EditAssetPairPanel
*/
@ -50,8 +50,8 @@ public class EditAssetPairPanel extends javax.swing.JPanel {
assetBox.removeAllItems();
currencyBox.removeAllItems();
Collection<AbstractAsset> ac = world.getAssetCollection();
for (AbstractAsset asset: ac ){
// Collection<AbstractAsset> ac = world.getAssetCollection();
/* for (AbstractAsset asset: ac ){
if (asset.isCurrency()){
this.currencyBox.addItem(asset.getSymbol());
}
@ -60,7 +60,7 @@ public class EditAssetPairPanel extends javax.swing.JPanel {
}
}
}
*/ }
/**
* This method is called from within the constructor to initialize the form.

View File

@ -25,7 +25,7 @@
*/
package opensesim.gui.AssetPairEditor;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.gui.util.EscDialog;
import opensesim.gui.Globals;
@ -34,7 +34,7 @@ import opensesim.gui.Globals;
* @author tohe
*/
public class NewJDialog extends EscDialog /*javax.swing.JDialog*/ {
World world;
RealWorld world;
/**
* Creates new form NewJDialog

View File

@ -48,13 +48,14 @@ import javax.swing.JFrame;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.gui.AssetEditor.AssetEditorPanel;
import org.json.JSONArray;
import org.json.JSONObject;
import opensesim.old_sesim.AutoTraderLoader;
import opensesim.old_sesim.IndicatorLoader;
import opensesim.util.XClassLoader.ClassCache;
import opensesim.world.GodWorld;
/**
*
@ -62,7 +63,7 @@ import opensesim.util.XClassLoader.ClassCache;
*/
public class Globals {
public static World world;
public static RealWorld world;
public static final String SESIM_FILEEXTENSION = "sesim";
public static final Double SESIM_FILEVERSION = 0.1;
@ -235,8 +236,8 @@ public class Globals {
try {
// a1 = o1.newInstance();
a1 = o1.getConstructor(World.class,JSONObject.class).newInstance(null,null);
a2 = o2.getConstructor(World.class,JSONObject.class).newInstance(null,null);
a1 = o1.getConstructor(RealWorld.class,JSONObject.class).newInstance(null,null);
a2 = o2.getConstructor(RealWorld.class,JSONObject.class).newInstance(null,null);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
return 0;
@ -420,7 +421,7 @@ public class Globals {
public static void saveFile(File f) throws FileNotFoundException {
JSONObject sobj = new JSONObject();
/* JSONObject sobj = new JSONObject();
JSONArray traders = getTraders();
JSONObject strategies = getStrategies();
@ -428,7 +429,9 @@ public class Globals {
sobj.put(PrefKeys.SESIMVERSION, SESIM_FILEVERSION);
sobj.put(PrefKeys.STRATEGIES, strategies);
sobj.put(PrefKeys.TRADERS, traders);
*/
JSONObject sobj = Globals.getWorld();
PrintWriter out;
out = new PrintWriter(f.getAbsolutePath());
out.print(sobj.toString(4));
@ -469,8 +472,8 @@ public class Globals {
public static JSONObject getWorld() {
JSONObject cfg = new JSONObject();
cfg.put(World.JKEYS.ASSETS, getAssets());
cfg.put(World.JKEYS.EXCHANGES, getExchanges());
cfg.put(GodWorld.JKEYS.ASSETS, getAssets());
cfg.put(GodWorld.JKEYS.EXCHANGES, getExchanges());
return cfg;
}

View File

@ -26,8 +26,6 @@
package opensesim.gui;
import com.google.gson.Gson;
import opensesim.chart.Help;
import opensesim.chart.SuperDlg;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsDevice;
@ -39,27 +37,14 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import opensesim.gui.AssetPairEditor.NewJDialog;
import opensesim.gui.exchangeeditor.ExchangeEditorDialog;
import opensesim.gui.exchangeeditor.ExchangeListDialog;
import org.json.JSONArray;
import org.json.JSONObject;
@ -68,9 +53,7 @@ import opensesim.old_sesim.AutoTraderInterface;
import opensesim.old_sesim.Exchange;
import opensesim.old_sesim.Scheduler;
import opensesim.util.XClassLoader;
import opensesim.world.World;
import opensesim.world.WorldAdm;
import opensesim.world.GodWorld;
/**
*
@ -79,7 +62,7 @@ import opensesim.world.WorldAdm;
public class SeSimApplication extends javax.swing.JFrame {
WorldAdm worldadm;
GodWorld worldadm;
/**
* Creates new form NewMDIApplication
@ -93,7 +76,7 @@ public class SeSimApplication extends javax.swing.JFrame {
JSONObject cfg;
cfg = new JSONObject(Globals.prefs.get("world","{}"));
worldadm = new WorldAdm(cfg);
worldadm = new GodWorld(cfg);
@ -609,7 +592,7 @@ public class SeSimApplication extends javax.swing.JFrame {
void startSim() {
World world = new World(Globals.getWorld());
GodWorld world = new GodWorld(Globals.getWorld());
@ -985,7 +968,6 @@ public class SeSimApplication extends javax.swing.JFrame {
//opensesim.world.World.MasterApi mapi = new opensesim.world.World.MasterApi();

View File

@ -26,11 +26,12 @@
package opensesim.gui.exchangeeditor;
import opensesim.world.Exchange;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.gui.Globals;
import opensesim.gui.util.Json.Export;
import opensesim.gui.util.Json.Import;
import opensesim.util.SeSimException;
import opensesim.world.GodWorld;
/**
*
@ -38,12 +39,12 @@ import opensesim.util.SeSimException;
*/
public class ExchangeEditorPanel extends javax.swing.JPanel {
World world;
GodWorld world=null;
/**
* Creates new form ExchangeEditorPanel
*/
public ExchangeEditorPanel() {
world = Globals.world;
//world = Globals.world;
initComponents();
}

View File

@ -27,7 +27,7 @@ package opensesim.gui.exchangeeditor;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.gui.Globals;
import opensesim.world.Exchange;
import org.json.JSONObject;

View File

@ -26,7 +26,8 @@
package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.GodWorld;
import opensesim.world.RealWorld;
import org.json.JSONObject;
/**
@ -35,7 +36,7 @@ import org.json.JSONObject;
*/
public class CryptoCurrency extends AbstractAsset{
public CryptoCurrency(World world, JSONObject cfg) {
public CryptoCurrency(GodWorld world, JSONObject cfg) {
super(world, cfg);
}

View File

@ -26,7 +26,8 @@
package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.GodWorld;
import opensesim.world.RealWorld;
import org.json.JSONObject;
/**
@ -35,7 +36,7 @@ import org.json.JSONObject;
*/
public class CurrencyAsset extends AbstractAsset {
public CurrencyAsset(World world, JSONObject cfg) {
public CurrencyAsset(GodWorld world, JSONObject cfg) {
super(world, cfg);
}

View File

@ -27,7 +27,8 @@ package opensesim.sesim.Assets;
import javax.swing.JPanel;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.GodWorld;
import opensesim.world.RealWorld;
import org.json.JSONObject;
/**
@ -36,7 +37,7 @@ import org.json.JSONObject;
*/
public class FurtureAsset extends AbstractAsset{
public FurtureAsset(World world, JSONObject cfg) {
public FurtureAsset(GodWorld world, JSONObject cfg) {
super(world, cfg);
}

View File

@ -26,7 +26,8 @@
package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.GodWorld;
import opensesim.world.RealWorld;
import org.json.JSONObject;
/**
@ -35,7 +36,7 @@ import org.json.JSONObject;
*/
public class StockAssett extends AbstractAsset{
public StockAssett(World world, JSONObject cfg) {
public StockAssett(GodWorld world, JSONObject cfg) {
super(world, cfg);
}

View File

@ -26,7 +26,8 @@
package opensesim.sesim.Assets;
import opensesim.world.AbstractAsset;
import opensesim.world.World;
import opensesim.world.GodWorld;
import opensesim.world.RealWorld;
import org.json.JSONObject;
/**
@ -35,7 +36,7 @@ import org.json.JSONObject;
*/
public class WarrentAsset extends AbstractAsset{
public WarrentAsset(World world, JSONObject cfg) {
public WarrentAsset(GodWorld world, JSONObject cfg) {
super(world, cfg);
}

View File

@ -27,7 +27,7 @@ package opensesim.sesim.interfaces;
import java.util.HashMap;
import javax.swing.JPanel;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.idgenerator.Id;

View File

@ -43,7 +43,7 @@ import org.json.JSONObject;
*/
public abstract class AbstractAsset implements GetJson {
World world;
GodWorld world;
private String symbol;
private String name;
@ -56,7 +56,7 @@ public abstract class AbstractAsset implements GetJson {
* @param world
* @param cfg
*/
public AbstractAsset(World world, JSONObject cfg) {
public AbstractAsset(GodWorld world, JSONObject cfg) {
if (world == null) {
return;
}
@ -105,7 +105,7 @@ public abstract class AbstractAsset implements GetJson {
public static final String JSON_DECIMALS = "decimals";
public static final int DECIMALS_DEFAULT = 2;
public static void rename(World world, AbstractAsset a, String symbol) throws Exception {
public static void rename(GodWorld world, AbstractAsset a, String symbol) throws Exception {
if (world.assetsBySymbol.get(symbol) != null) {
throw new java.lang.Exception("Can't rename asset symbol. Symbol '" + symbol + "' is already in use.");
}
@ -163,7 +163,7 @@ public abstract class AbstractAsset implements GetJson {
@Override
public JSONObject getJson() {
JSONObject cfg = new JSONObject();
cfg.put(World.JKEYS.ASSET_TYPE, this.getClass().getName());
cfg.put(GodWorld.JKEYS.ASSET_TYPE, this.getClass().getName());
cfg.put(AbstractAsset.JSON_SYMBOL, this.getSymbol());
cfg.put(AbstractAsset.JSON_DECIMALS, this.getDecimals());

View File

@ -57,8 +57,14 @@ public class AssetPair {
public AssetPair(AbstractAsset asset, AbstractAsset currency) {
this.asset = asset;
this.currency = currency;
}
public AssetPair(GodWorld world, String asset, String currency){
this.asset = world.getAssetBySymbol(asset);
this.currency = world.getAssetBySymbol(asset);
}
protected HashMap<Order.Type, SortedSet<Order>> order_books;
@ -82,9 +88,9 @@ public class AssetPair {
AssetPair ap = (AssetPair)o;
if (ap.asset==asset && ap.currency==currency)
return true;
if (ap.asset==currency && ap.currency==asset)
return false;
return true;
// if (ap.asset==currency && ap.currency==asset)
// return false;
return false;
}

View File

@ -30,7 +30,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.sesim.interfaces.Configurable;
import org.json.JSONObject;
@ -40,7 +40,7 @@ import org.json.JSONObject;
*/
public class Exchange implements Configurable{
private World world;
private RealWorld world;
private String name;
private String symbol;
@ -52,13 +52,13 @@ public class Exchange implements Configurable{
private final HashMap<AssetPair,TradingEnv> asset_pairs = new HashMap<>();
Exchange(World world, String symbol) {
Exchange(RealWorld world, String symbol) {
this.world = world;
this.symbol=symbol;
}
Exchange(World world, JSONObject cfg){
Exchange(RealWorld world, JSONObject cfg){
final int x;
}
@ -77,7 +77,7 @@ public class Exchange implements Configurable{
}
public Order createOrder(Account account, AssetPair pair, Order.Type type, double volume, double limit) {
Order o = new Order(world,account,pair,type,volume,limit);
// Order o = new Order(world,account,pair,type,volume,limit);
return null;
}

View File

@ -0,0 +1,272 @@
/*
* 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.lang.reflect.Constructor;
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.interfaces.GetJson;
import opensesim.util.SeSimException;
import opensesim.util.idgenerator.IDGenerator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class GodWorld implements GetJson, World {
public static final class JKEYS {
public static final String ASSETS = "assets";
public static final String EXCHANGES = "exchanges";
public static final String ASSET_SYMBOL = "symbol";
public static final String ASSET_TYPE = "type";
}
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<>();
/**
* Create a World object.
*
* @param cfg
*/
public GodWorld(JSONObject cfg) {
putJson(cfg);
}
public GodWorld() {
this(new JSONObject("{}"));
}
private void putJson(JSONObject cfg) {
// Read assets
JSONArray jassets = cfg.optJSONArray(GodWorld.JKEYS.ASSETS);
if (jassets == null) {
jassets = new JSONArray();
}
for (int i = 0; i < jassets.length(); i++) {
JSONObject o = jassets.optJSONObject(i);
AbstractAsset a;
try {
a = createAsset(o);
} catch (SeSimException ex) {
Logger.getLogger(RealWorld.class.getName()).log(Level.SEVERE, null, ex);
continue;
}
if (a == null) {
continue;
}
assetsById.add(a);
assetsBySymbol.put(a.getSymbol(), a);
}
}
@Override
public JSONObject getJson() {
JSONObject cfg = new JSONObject();
// Write assets
JSONArray jassets = new JSONArray();
for (AbstractAsset asset : this.getAssetCollection()) {
jassets.put(asset.getJson());
}
cfg.put(GodWorld.JKEYS.ASSETS, jassets);
return cfg;
}
private long masterkey;
public GodWorld(JSONObject cfg, long masterkey) {
this.masterkey = masterkey;
putJson(cfg);
}
public boolean checkMasterKey(long masterkey) {
return masterkey == this.masterkey;
}
public AbstractAsset createAsset(JSONObject cfg) throws SeSimException {
AbstractAsset a;
String class_name;
Class<AbstractAsset> cls;
try {
class_name = cfg.getString(JKEYS.ASSET_TYPE);
} catch (JSONException jex) {
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, jex);
return null;
}
try {
cls = (Class<AbstractAsset>) Class.forName(class_name);
a = cls.getConstructor(GodWorld.class, JSONObject.class).newInstance(this, cfg);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
if (this.assetsBySymbol.get(a.getSymbol()) != null) {
throw new SeSimException("Already defined");
}
this.assetsById.add(a);
this.assetsBySymbol.put(a.getSymbol(), a);
return a;
}
public Collection<AbstractAsset> getAssetCollection() {
return Collections.unmodifiableCollection(assetsById);
}
public AbstractAsset getAssetBySymbol(String symbol) {
return this.assetsBySymbol.get(symbol);
}
public Collection<AssetPair> getAssetPairsCollection() {
return Collections.unmodifiableCollection(assetPairs);
}
public Collection<Exchange> getExchangeCollection() {
return Collections.unmodifiableCollection(exchanges);
}
public void add(AssetPair pair) {
assetPairs.add(pair);
}
public Exchange addExchange(String symbol) throws SeSimException {
for (Exchange ex : getExchangeCollection()) {
if (ex.getSymbol().equals(symbol)) {
throw (new SeSimException("Exchange already defined."));
}
}
// Exchange ex = new Exchange(this, symbol);
// exchanges.add(ex);
// return ex;
return null;
}
/* public AbstractAsset createAsset(long key, JSONObject cfg) throws SeSimException{
if (key!=masterkey)
throw new SeSimException("Access denied.");
return this.createAsset_p(cfg);
}
*/
// static final String JSON_ASSET = "asset";
// static final String JSON_EXCHANGES = "exchanges";
/*
public JSONObject getConfig() {
JSONObject cfg = new JSONObject();
// save assets
JSONArray arr;
arr = new JSONArray();
for (AbstractAsset a : getAssetCollection()) {
arr.put(a.getConfig());
}
cfg.put(JSON_ASSET, arr);
// save exchanges
arr = new JSONArray();
for (Exchange ex : getExchangeCollection()) {
arr.put(ex.getConfig());
}
cfg.put(JSON_EXCHANGES, arr);
return cfg;
}
@Override
public void putConfig(JSONObject cfg) {
JSONArray arr = cfg.optJSONArray(JSON_ASSET);
if (arr == null) {
arr = new JSONArray();
}
for (Object o : arr) {
JSONObject acfg = (JSONObject) o;
AbstractAsset.create(this, acfg);
}
}
*/
/* public AbstractAsset createAsset(Class cls, String symbol) throws Exception {
return AbstractAsset.create(this, cls, symbol);
}
*/
/*
static public JSONArray toJson() {
JSONArray all = new JSONArray();
for (Map.Entry<Id, Asset> entry : assetsById.entrySet()) {
Id key = entry.getKey();
Asset value = entry.getValue();
all.put(value.getConfig());
}
return all;
}
*/
/**
* Get the typename of an AbstractAsset class
* @param asset_type AbstractAsset
* @return the type name
*/
public static String getTypeName(Class<AbstractAsset> asset_type) {
Constructor<AbstractAsset> c;
try {
c = asset_type.getConstructor(GodWorld.class, JSONObject.class);
AbstractAsset ait = c.newInstance(null, null);
return ait.getTypeName();
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(GodWorld.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
}

View File

@ -26,7 +26,7 @@
package opensesim.world;
import opensesim.world.Account;
import opensesim.world.World;
import opensesim.world.RealWorld;
import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.idgenerator.Id;
@ -92,9 +92,9 @@ public class Order implements Comparable<Order> {
protected final Account account;
double cost;
World world;
GodWorld world;
Order(World world, Account account, AssetPair pair, Type type,
Order(GodWorld world, Account account, AssetPair pair, Type type,
double volume, double limit) {
this.account = account;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 7u73
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -25,28 +25,26 @@
*/
package opensesim.world;
import java.util.Random;
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.interfaces.GetJson;
import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.SeSimException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author 7u83
* @author 7u83 <7u83@mail.ru>
*/
public class WorldAdm {
public World world;
public long masterKey;
public WorldAdm(JSONObject cfg){
masterKey = new Random().nextLong();
world = new World(cfg,masterKey);
}
public class RealWorld implements World{
public World getWorld() {
return world;
}
public long getMasterKey() {
return masterKey;
}
}

View File

@ -25,235 +25,10 @@
*/
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.interfaces.GetJson;
import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.SeSimException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class World implements GetJson {
public static final class JKEYS {
public static final String ASSETS = "assets";
public static final String EXCHANGES = "exchanges";
public static final String ASSET_SYMBOL = "symbol";
public static final String ASSET_TYPE = "type";
}
HashSet<AbstractAsset> assetsById = new HashSet<>();
HashMap<String, AbstractAsset> assetsBySymbol = new HashMap<>();
public interface World {
IDGenerator assetIdGenerator = new IDGenerator();
IDGenerator orderIdGenerator = new IDGenerator();
HashSet<AssetPair> assetPairs = new HashSet<>();
ArrayList<Exchange> exchanges = new ArrayList<>();
/**
* Create a World object.
*
* @param cfg
*/
public World(JSONObject cfg) {
putJson(cfg);
}
private void putJson(JSONObject cfg){
// Read assets
JSONArray jassets = cfg.optJSONArray(World.JKEYS.ASSETS);
if (jassets==null){
jassets=new JSONArray();
}
for (int i=0; i<jassets.length();i++) {
JSONObject o = jassets.optJSONObject(i);
AbstractAsset a;
try {
a = createAsset_p(o);
} catch (SeSimException ex) {
Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, ex);
return;
}
assetsById.add(a);
assetsBySymbol.put(a.getSymbol(), a);
}
}
@Override
public JSONObject getJson() {
JSONObject cfg=new JSONObject();
// Write assets
JSONArray jassets = new JSONArray();
for (AbstractAsset asset: this.getAssetCollection()){
jassets.put(asset.getJson());
}
cfg.put(World.JKEYS.ASSETS, jassets);
return cfg;
}
private long masterkey;
public World(JSONObject cfg, long masterkey){
this.masterkey=masterkey;
putJson(cfg);
}
public boolean checkMasterKey(long masterkey){
return masterkey == this.masterkey;
}
private AbstractAsset createAsset_p(JSONObject cfg) throws SeSimException {
AbstractAsset a;
String class_name;
Class<AbstractAsset> cls;
try {
class_name = cfg.getString(JKEYS.ASSET_TYPE);
}catch (JSONException jex){
Logger.getLogger(World.class.getName()).log(Level.SEVERE, null, jex);
return null;
}
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;
}
if (this.assetsBySymbol.get(a.getSymbol())!=null){
throw new SeSimException("Already defined");
}
this.assetsById.add(a);
this.assetsBySymbol.put(a.getSymbol(),a);
return a;
}
public Collection<AbstractAsset> getAssetCollection() {
return Collections.unmodifiableCollection(assetsById);
}
public AbstractAsset getAssetBySymbol(String symbol){
return this.assetsBySymbol.get(symbol);
}
public Collection<AssetPair> getAssetPairsCollection() {
return Collections.unmodifiableCollection(assetPairs);
}
public Collection<Exchange> getExchangeCollection() {
return Collections.unmodifiableCollection(exchanges);
}
public void add(AssetPair pair) {
assetPairs.add(pair);
}
public Exchange addExchange(String symbol) throws SeSimException {
for (Exchange ex : getExchangeCollection()) {
if (ex.getSymbol().equals(symbol)) {
throw (new SeSimException("Exchange already defined."));
}
}
Exchange ex = new Exchange(this, symbol);
exchanges.add(ex);
return ex;
}
public AbstractAsset createAsset(long key, JSONObject cfg) throws SeSimException{
if (key!=masterkey)
throw new SeSimException("Access denied.");
return this.createAsset_p(cfg);
}
// static final String JSON_ASSET = "asset";
// static final String JSON_EXCHANGES = "exchanges";
/*
public JSONObject getConfig() {
JSONObject cfg = new JSONObject();
// save assets
JSONArray arr;
arr = new JSONArray();
for (AbstractAsset a : getAssetCollection()) {
arr.put(a.getConfig());
}
cfg.put(JSON_ASSET, arr);
// save exchanges
arr = new JSONArray();
for (Exchange ex : getExchangeCollection()) {
arr.put(ex.getConfig());
}
cfg.put(JSON_EXCHANGES, arr);
return cfg;
}
@Override
public void putConfig(JSONObject cfg) {
JSONArray arr = cfg.optJSONArray(JSON_ASSET);
if (arr == null) {
arr = new JSONArray();
}
for (Object o : arr) {
JSONObject acfg = (JSONObject) o;
AbstractAsset.create(this, acfg);
}
}
*/
/* public AbstractAsset createAsset(Class cls, String symbol) throws Exception {
return AbstractAsset.create(this, cls, symbol);
}
*/
/*
static public JSONArray toJson() {
JSONArray all = new JSONArray();
for (Map.Entry<Id, Asset> entry : assetsById.entrySet()) {
Id key = entry.getKey();
Asset value = entry.getValue();
all.put(value.getConfig());
}
return all;
}
*/
}

View File

@ -39,36 +39,53 @@ 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() {
}
/**
* Test of createAccount method, of class Exchange.
*/
@Test
public void testCreateAccount() {
System.out.println("createAccount");
Exchange instance = new Exchange(null,(JSONObject)null);
WorldAdm worldadm = new opensesim.world.WorldAdm(new JSONObject(
"{ assets: [ "
+ "{"
+ "symbol: EUR,"
+ "name: Euro,"
+ "decimals: 2,"
+ "type: opensesim.sesim.Assets.CryptoCurrency"
+ "},"
+ "{"
+ "symbol: AAPL,"
+ "name: Apple,"
+ "decimals: 0,"
+ "type: opensesim.sesim.Assets.CryptoCurrency"
+ "}"
+ "]"
+ "}"
));
Exchange instance = new Exchange(null, (JSONObject) null);
Account expResult = null;
Account result = instance.createAccount();
assertEquals(expResult, result);
@ -76,7 +93,4 @@ public class ExchangeTest {
fail("The test case is a prototype.");
}
}

View File

@ -51,11 +51,11 @@ public class OrderTest {
public static void tearDownClass() {
}
World world;
RealWorld world;
@Before
public void setUp() {
world = new World(new JSONObject("{}"));
world = new RealWorld(new JSONObject("{}"));
}
@After