Exemplo n.º 1
0
  public StatesPanel() {
    setLayout(new BorderLayout());

    for (final Currency currency : Currency.values()) {
      final JPanel panelForCurrency = new StatePanelForCurrency(currency);
      jTabbedPaneCurrency.addTab(currency.getIso4217Code(), panelForCurrency);
    }

    jTabbedPaneCurrency.addChangeListener(
        new ChangeListener() {
          @Override
          public void stateChanged(final ChangeEvent e) {
            if (e.getSource() instanceof JTabbedPane) {
              final JTabbedPane pane = (JTabbedPane) e.getSource();
              final StatePanelForCurrency selectedComponent =
                  (StatePanelForCurrency) pane.getSelectedComponent();
              selectedComponent.notifyListener();
            }
          }
        });

    add(jTabbedPaneCurrency, BorderLayout.CENTER);
  }
  @Test
  public void testGetAgentMethods() {
    for (final Currency currency : Currency.values()) {
      // state
      Assert.assertNotNull(ApplicationContext.getInstance().getAgentService().findState(currency));

      // central bank
      Assert.assertNotNull(
          ApplicationContext.getInstance().getAgentService().findCentralBank(currency));

      // credit banks
      Assert.assertEquals(
          2, ApplicationContext.getInstance().getAgentService().findCreditBanks(currency).size());

      // central bank
      Assert.assertNotNull(
          ApplicationContext.getInstance().getAgentService().findRandomCreditBank(currency));

      // factories
      Assert.assertEquals(
          2, ApplicationContext.getInstance().getAgentService().findFactories(currency).size());

      Assert.assertEquals(
          1,
          ApplicationContext.getInstance()
              .getAgentService()
              .findFactories(currency, GoodType.WHEAT)
              .size());

      Assert.assertEquals(
          1,
          ApplicationContext.getInstance()
              .getAgentService()
              .findFactories(currency, GoodType.COAL)
              .size());

      // traders
      Assert.assertEquals(
          1, ApplicationContext.getInstance().getAgentService().findTraders(currency).size());

      // households
      Assert.assertEquals(
          2, ApplicationContext.getInstance().getAgentService().findHouseholds(currency).size());
    }
  }
  @Override
  public void constructAgentsFromConfiguration() {
    for (final Currency currency : Currency.values()) {
      if (ApplicationContext.getInstance().getConfiguration().stateConfig.getNumber(currency)
          == 1) {
        // initialize states
        ApplicationContext.getInstance().getAgentService().findState(currency);
      }
    }

    for (final Currency currency : Currency.values()) {
      if (ApplicationContext.getInstance().getConfiguration().centralBankConfig.getNumber(currency)
          == 1) {
        // initialize central banks
        ApplicationContext.getInstance().getAgentService().findCentralBank(currency);
      }
    }

    for (final Currency currency : Currency.values()) {
      final Set<Currency> offeredCurrencies = new HashSet<Currency>();
      offeredCurrencies.add(currency);

      // initialize credit banks
      for (int i = 0;
          i
              < ApplicationContext.getInstance()
                  .getConfiguration()
                  .creditBankConfig
                  .getNumber(currency);
          i++) {
        ApplicationContext.getInstance()
            .getCreditBankFactory()
            .newInstanceCreditBank(offeredCurrencies, currency);
      }
    }

    for (final Currency currency : Currency.values()) {
      // initialize factories
      for (final GoodType goodType : GoodType.values()) {
        if (!GoodType.LABOURHOUR.equals(goodType)) {
          for (int i = 0;
              i
                  < ApplicationContext.getInstance()
                      .getConfiguration()
                      .factoryConfig
                      .getNumber(currency, goodType);
              i++) {
            ApplicationContext.getInstance()
                .getFactoryFactory()
                .newInstanceFactory(goodType, currency);
          }
        }
      }
    }

    for (final Currency currency : Currency.values()) {
      // initialize traders
      for (int i = 0;
          i < ApplicationContext.getInstance().getConfiguration().traderConfig.getNumber(currency);
          i++) {
        ApplicationContext.getInstance().getTraderFactory().newInstanceTrader(currency);
      }
    }

    final int householdLifeSpanInDays =
        ApplicationContext.getInstance().getConfiguration().householdConfig.getLifespanInDays();
    // division by 2, so that households have time left until
    // retirement
    final int householdAgeLimit = householdLifeSpanInDays / 2;

    for (final Currency currency : Currency.values()) {
      // initialize households
      for (int i = 0;
          i
              < ApplicationContext.getInstance()
                  .getConfiguration()
                  .householdConfig
                  .getNumber(currency);
          i++) {
        final int ageInDays =
            ApplicationContext.getInstance().getRandomNumberGenerator().nextInt(householdAgeLimit);
        ApplicationContext.getInstance()
            .getHouseholdFactory()
            .newInstanceHousehold(currency, ageInDays);
      }
    }

    HibernateUtil.flushSession();
  }