📄 ruleview.java
字号:
package asm;import java.awt.*;import java.awt.event.*;import java.util.Vector;/** * Title: Artificial Stock Market * Description: 人工模拟股市(来源:SFI的Swarm版本)的Java版本 * Copyright: Copyright (c) 2003 * Company: http://agents.yeah.net * @author jake * @version 1.0 */public class RuleView extends Frame implements Runnable{ Thread runner1;//定义独立线程 List lstRules = new List(); AsmModel local; Label label1 = new Label(); Choice choiceAgent = new Choice(); Button btnRefresh = new Button(); TextArea txtShow = new TextArea(); int nSelRule; int index[]; String area[]; Label label2 = new Label(); CheckboxGroup checkboxGroup1 = new CheckboxGroup(); Choice choiceMethod = new Choice(); Label label3 = new Label(); TextField txtWorld = new TextField(); Label label4 = new Label(); TextField txtPrice = new TextField(); Label label5 = new Label(); TextField txtDividend = new TextField(); public RuleView(AsmModel loc) { super("查看规则"); local=loc; try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(null); lstRules.setBounds(new Rectangle(11, 27, 769, 283)); lstRules.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { lstRules_mouseClicked(e); } }); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { this_windowClosing(e); } }); label1.setText("选择Agent:"); label1.setBounds(new Rectangle(6, 446, 70, 16)); choiceAgent.setBounds(new Rectangle(76, 446, 70, 17)); for(int i=0;i<local.agentList.size();i++){ choiceAgent.addItem(Integer.toString(i)); } this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(WindowEvent e) { this_windowOpened(e); } }); btnRefresh.setLabel("刷新"); btnRefresh.setBounds(new Rectangle(177, 443, 98, 20)); txtShow.setBounds(new Rectangle(9, 335, 600, 104)); this.setBackground(SystemColor.inactiveCaptionBorder); label2.setText("查看:"); label2.setBounds(new Rectangle(11, 315, 49, 16)); choiceMethod.setBounds(new Rectangle(65, 315, 103, 16)); choiceMethod.addItem("全部规则"); choiceMethod.addItem("激活的规则"); label3.setText("当前世界的编码:"); label3.setBounds(new Rectangle(212, 313, 99, 16)); txtWorld.setBounds(new Rectangle(314, 315, 465, 18)); label4.setText("价格:"); label4.setBounds(new Rectangle(618, 356, 43, 16)); txtPrice.setBounds(new Rectangle(660, 356, 112, 20)); label5.setText("股息:"); label5.setBounds(new Rectangle(618, 399, 43, 20)); txtDividend.setBounds(new Rectangle(660, 401, 111, 19)); this.add(lstRules, null); this.add(label1, null); this.add(choiceAgent, null); this.add(btnRefresh, null); this.add(txtShow, null); this.add(label2, null); this.add(choiceMethod, null); this.add(label3, null); this.add(txtWorld, null); this.add(label4, null); this.add(label5, null); this.add(txtPrice, null); this.add(txtDividend, null); } public void updateWorld() { String world=local.world.getRealWorld(); txtWorld.setText(world); txtPrice.setText(String.valueOf(local.world.getPrice())); txtDividend.setText(String.valueOf(local.world.getDividend())); Vector agList=local.agentList; String output[]; lstRules.removeAll(); output=new String[8]; Agent ag=(Agent)agList.elementAt(choiceAgent.getSelectedIndex()); Vector activeRules; if(choiceMethod.getSelectedIndex()==0){ activeRules=ag.fcastList; }else{ activeRules=ag.activeList; } index=new int[activeRules.size()]; area=new String[activeRules.size()]; for(int i=0;i<activeRules.size();i++){ index[i]=i; } int size=activeRules.size(); for(int m=0;m<size-1;m++){ for(int n=m+1;n<size;n++){ if(ag.GetStrength(activeRules,index[m])<ag.GetStrength(activeRules,index[n])){ int tmp=index[m]; index[m]=index[n]; index[n]=tmp; } } } String space="____"; String out="编号"+space+"预测值"+space+"____强度"+space+space+"a"+space+space+"b"+space+space+space+"c"+space+space +"方差"+space+"次数"; space="\t"; lstRules.add(out); for(int i=0;i<activeRules.size();i++){ out=""; out=Integer.toString(index[i]); int s=4-out.length(); for(int k=1;k<s;k++){ out+="_"; } out+=space; ag.GetRule(activeRules,output,index[i]); int j; for(j=0;j<7;j++){ for(int k=1;k<=10;k++){ if(k<=output[j].length()){ out+=output[j].substring(k-1,k); }else{ out+="_"; } } out+=space; } area[i]=output[j]; lstRules.add(out); } //lstRules.repaint(); //super.paint(g); } public void stop() { if (runner1!=null) { // running = false; runner1.stop(); runner1=null; } } public void run() { while(true){ updateWorld(); try{Thread.sleep(200000);}catch(InterruptedException e){}; } } void this_windowClosing(WindowEvent e) { this.hide(); this.dispose(); } void this_windowOpened(WindowEvent e) { if(runner1==null){ runner1=new Thread(this); runner1.start(); } } public boolean action(Event evt, Object o) { if(evt.target==btnRefresh){ updateWorld(); return true; }else if(evt.target==choiceAgent){ updateWorld(); return true; }else if(evt.target==lstRules){ nSelRule=lstRules.getSelectedIndex(); txtShow.setText(area[nSelRule]); return true; }else if(evt.target==choiceMethod){ updateWorld(); return true; } return false; } void lstRules_mouseClicked(MouseEvent e) { nSelRule=lstRules.getSelectedIndex(); if(nSelRule==0)return; nSelRule--; String condition=area[nSelRule]; String explain=""; for(int i=3;i<condition.length();i++){ String tmp=condition.substring(i,i+1); if(tmp.equals("1")){ explain+=local.world.descriptionOfBit(i)+" and\n"; }else if(tmp.equals("0")){ explain+="not "+local.world.descriptionOfBit(i)+" and\n"; } } String result; condition=condition.replace('2','#'); result="条件编码:"+condition+"\n"; result+="解释:"+"\n"; result+="if\n"+explain; txtShow.setText(result); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -