EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
ReassignPowerPlantsToLongTermElectricityContractsRole.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.ArrayList;
19 import java.util.List;
20 
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.transaction.annotation.Transactional;
23 
24 import agentspring.role.Role;
25 import agentspring.role.RoleComponent;
32 import emlab.gen.role.AbstractEnergyProducerRole;
33 
43 @RoleComponent
45  AbstractEnergyProducerRole implements Role<EnergyProducer> {
46 
47  @Autowired
48  Reps reps;
49 
50  @Transactional
51  public void act(EnergyProducer producer) {
52 
53  // When old power plant is dismantled, we take over existing contract by
54  // new power plant
55  List<LongTermContract> ltcsToBeReplaced = new ArrayList<LongTermContract>();
56 
57  for (LongTermContract ltc : reps.contractRepository
58  .findLongTermContractsForEnergyProducerActiveAtTime(producer,
59  getCurrentTick())) {
60 
61  // Only if the contract is still active the next tick
62  if (ltc.getFinish() > getCurrentTick()) {
63 
64  // if the underlying power plant is dismantled
65  if (!ltc.getUnderlyingPowerPlant().isOperational(
66  getCurrentTick())) {
67  logger.info(
68  "Powerplant {} underlying ltc {} is dismantled, contract should be reassigned",
69  ltc.getUnderlyingPowerPlant(), ltc);
70  ltcsToBeReplaced.add(ltc);
71  }
72  }
73  }
74 
75  // TODO random reassigning now. Should check volumes better and check
76  // for similar technologies to make sure the type of contract fits.
77  int nrOfLtcsToBeReplaced = ltcsToBeReplaced.size();
78  int index = 0;
79 
80  // Go over the power plants to find one without a contract for each
81  // contract that needs replacement
82  if (nrOfLtcsToBeReplaced > 0) {
83  for (PowerPlant plant : reps.powerPlantRepository
85  getCurrentTick())) {
86 
87  // if there are still contracts to be replaced
88  if (index < nrOfLtcsToBeReplaced) {
89  if (reps.contractRepository
90  .findLongTermContractForPowerPlantActiveAtTime(
91  plant, getCurrentTick()) == null) {
92  if (plant.isWithinTechnicalLifetime(getCurrentTick())) {
93  logger.warn(
94  "Powerplant {} underlying ltc has been replaced by plant {}",
95  ltcsToBeReplaced.get(index)
96  .getUnderlyingPowerPlant(), plant);
97  reps.contractRepository
98  .reassignLongTermContractToNewPowerPlant(
99  ltcsToBeReplaced.get(index), plant);
100  index++;
101  }
102  }
103  }
104  }
105  logger.warn(
106  "Have replaced {} long-term contracts out of {} that needed to be replaced",
107  index, nrOfLtcsToBeReplaced);
108  }
109  }
110 
111 }
Iterable< PowerPlant > findOperationalPowerPlantsByOwner(@Param("owner") EnergyProducer owner,@Param("tick") long tick)