EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
PowerPlantEntryParser.java
1 /*******************************************************************************
2  * Copyright 2013 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.domain.factory;
17 
18 import java.io.IOException;
19 import java.util.List;
20 
21 import org.codehaus.groovy.syntax.ReadException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Autowired;
25 
26 import com.googlecode.jcsv.reader.CSVEntryParser;
27 
34 
59 public class PowerPlantEntryParser implements CSVEntryParser<PowerPlant> {
60 
61  private final List<EnergyProducer> producers;
62 
63  private final List<PowerGeneratingTechnology> technologies;
64 
65  private final List<PowerGridNode> powerGridNodes;
66 
70  public PowerPlantEntryParser(List<EnergyProducer> producers, List<PowerGeneratingTechnology> technologies,
71  List<PowerGridNode> powerGridNodes) {
72  this.producers = producers;
73  this.technologies = technologies;
74  this.powerGridNodes = powerGridNodes;
75 
76  }
77 
78  @Autowired
79  Reps reps;
80 
81  static final Logger logger = LoggerFactory.getLogger(PowerPlantEntryParser.class);
82 
83  @Override
84  public PowerPlant parseEntry(String... data) {
85  String name = data[0];
86  String technologyName = data[1];
87  String locationName = data[2];
88  int age = Integer.parseInt(data[3]);
89  String ownerName = "";
90  if (data.length > 3)
91  ownerName = data[4];
92 
93  double capacity = 0;
94  if (data.length > 4 && !data[5].isEmpty())
95  capacity = Double.parseDouble(data[5]);
96  double efficiency = 0;
97  if (data.length > 5 && !data[6].isEmpty()) {
98  efficiency = Double.parseDouble(data[6]);
99  }
100 
101  EnergyProducer energyProducer = null;
102  if (!ownerName.isEmpty()) {
103  for (EnergyProducer producer : producers) {
104  if (producer.getName().equals(ownerName)) {
105  energyProducer = producer;
106  break;
107  }
108  }
109  } else {
110  energyProducer = getRandomProducer(producers);
111  }
112  PowerGeneratingTechnology pgt = null;
113  if (!technologyName.isEmpty()) {
114  for (PowerGeneratingTechnology ppTechnology : technologies) {
115  if (ppTechnology.getName().equals(technologyName)) {
116  pgt = ppTechnology;
117  break;
118  }
119 
120  }
121  } else {
122  pgt = technologies.get(0);
123  }
124  PowerGridNode powerGridNode = null;
125  if (!locationName.isEmpty()) {
126  for (PowerGridNode node : powerGridNodes) {
127  if (node.getName().equals(locationName)) {
128  powerGridNode = node;
129  break;
130  }
131 
132  }
133  } else {
134  try {
135  throw new ReadException("Location fields is not allowed to be empty!", new IOException());
136  } catch (ReadException e) {
137  e.printStackTrace();
138  }
139  }
140  try {
141  return createPowerPlant(name, pgt, energyProducer, powerGridNode, age, capacity, efficiency);
142  } catch (NullPointerException e) {
143  logger.warn("ERROR: Name: \"" + name + "\",Pgt: " + pgt + ", EnergyProducer" + energyProducer + ", Node:"
144  + powerGridNode
145  + ", Age:" + age + ", Capacity: " + capacity + ", Efficiency:" + efficiency);
146  throw e;
147  }
148  }
149 
150  private PowerPlant createPowerPlant(String name, PowerGeneratingTechnology technology,
151  EnergyProducer energyProducer,
152  PowerGridNode location, int age, double capacity, double efficiency) {
153  PowerPlant plant = new PowerPlant().persist();
154  plant.setName(name);
155  plant.setTechnology(technology);
156  plant.setOwner(energyProducer);
157  plant.setLocation(location);
158  plant.setConstructionStartTime(-(technology.getExpectedLeadtime() + technology.getExpectedPermittime() + age));
159  plant.setActualLeadtime(plant.getTechnology().getExpectedLeadtime());
160  plant.setActualPermittime(plant.getTechnology().getExpectedPermittime());
161  plant.setExpectedEndOfLife(plant.getConstructionStartTime() + plant.getActualPermittime()
162  + plant.getActualLeadtime() + plant.getTechnology().getExpectedLifetime());
163  if (capacity == 0) {
164  plant.setActualNominalCapacity(technology.getCapacity() * location.getCapacityMultiplicationFactor());
165  } else {
166  plant.setActualNominalCapacity(capacity);
167  }
168 
169  plant.calculateAndSetActualInvestedCapital(plant.getConstructionStartTime());
170  if (efficiency == 0) {
171  plant.calculateAndSetActualEfficiency(plant.getConstructionStartTime());
172  } else {
173  plant.setActualEfficiency(efficiency);
174  }
175  plant.calculateAndSetActualFixedOperatingCosts(plant.getConstructionStartTime());
176  plant.setDismantleTime(1000);
177  // BigBank bigbank = reps.genericRepository.findFirst(BigBank.class);
178 
179  Loan loan = new Loan().persist();
180  loan.setFrom(energyProducer);
181  loan.setTo(null);
182  double amountPerPayment = determineLoanAnnuities(
183  plant.getActualInvestedCapital() * energyProducer.getDebtRatioOfInvestments(), plant.getTechnology()
184  .getDepreciationTime(), energyProducer.getLoanInterestRate());
185  loan.setAmountPerPayment(amountPerPayment);
186  loan.setTotalNumberOfPayments(plant.getTechnology().getDepreciationTime());
187  loan.setLoanStartTime(plant.getConstructionStartTime());
188  loan.setNumberOfPaymentsDone(-plant.getConstructionStartTime());// Some
189  // payments
190  // are
191  // already
192  // made
193  plant.setLoan(loan);
194  loan.setRegardingPowerPlant(plant);
195  return plant;
196  }
197 
198  private EnergyProducer getRandomProducer(List<EnergyProducer> producers) {
199  if (producers.size() > 0) {
200  int size = producers.size();
201  int index = getRandomIndexFromList(size);
202  return producers.get(index);
203  }
204  return null;
205  }
206 
207  private int getRandomIndexFromList(int size) {
208  return (int) Math.min(Math.floor(Math.random() * size), size - 1);
209  }
210 
211  public double determineLoanAnnuities(double totalLoan, double payBackTime, double interestRate) {
212 
213  double q = 1 + interestRate;
214  double annuity = totalLoan * (Math.pow(q, payBackTime) * (q - 1)) / (Math.pow(q, payBackTime) - 1);
215 
216  return annuity;
217  }
218 
219 }