EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
SubmitBidsToCommodityMarketRole.java
1 /*******************************************************************************
2  * Copyright 2012 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package emlab.gen.role.market;
17 
18 import java.util.HashMap;
19 
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.transaction.annotation.Transactional;
22 
23 import agentspring.role.Role;
24 import agentspring.role.RoleComponent;
32 import emlab.gen.role.AbstractEnergyProducerRole;
33 
46 @RoleComponent
47 public class SubmitBidsToCommodityMarketRole extends AbstractEnergyProducerRole implements Role<EnergyProducer> {
48 
49  @Autowired
50  Reps reps;
51 
52  @Override
53  @Transactional
54  public void act(EnergyProducer producer) {
55 
56  logger.info("Purchasing commodities");
57 
58  HashMap<Substance, Double> fuelAmounts = new HashMap<Substance, Double>();
59 
60  for (PowerPlant plant : reps.powerPlantRepository.findOperationalPowerPlantsByOwner(producer, getCurrentTick())) {
61 
62  double totalSupply = plant.calculateElectricityOutputAtTime(getCurrentTick(), false);
63 
64  for (SubstanceShareInFuelMix share : plant.getFuelMix()) {
65 
66  double amount = share.getShare() * totalSupply;
67  Substance substance = share.getSubstance();
68 
69  // already in? Than add to total
70  if (fuelAmounts.containsKey(substance)) {
71  amount += fuelAmounts.get(substance);
72  }
73  fuelAmounts.put(substance, amount);
74  }
75  }
76 
77  for (Substance substance : fuelAmounts.keySet()) {
78  // find the totals and the right market. Place one bid for each
79  // substance (fuel)
80  if (!fuelAmounts.get(substance).isNaN() && fuelAmounts.get(substance) > 0) {
81  Bid bid = reps.nonTransactionalCreateRepository.submitBidToMarket(
82  reps.marketRepository.findFirstMarketBySubstance(substance), producer, getCurrentTick(), false, Double.MAX_VALUE,
83  fuelAmounts.get(substance));
84  logger.info("Submited bid " + bid);
85  }
86  }
87  }
88 
89 }
Iterable< PowerPlant > findOperationalPowerPlantsByOwner(@Param("owner") EnergyProducer owner,@Param("tick") long tick)