EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
ContractRepository.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.repository;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.data.neo4j.support.Neo4jTemplate;
25 import org.springframework.stereotype.Repository;
26 import org.springframework.transaction.annotation.Transactional;
27 
28 import com.tinkerpop.blueprints.pgm.Vertex;
29 import com.tinkerpop.pipes.Pipe;
30 import com.tinkerpop.pipes.util.Pipeline;
31 
44 
45 @Repository
46 public class ContractRepository extends AbstractRepository<Contract> {
47 
48  static Logger logger = LoggerFactory.getLogger(ContractRepository.class);
49 
50  @Autowired
51  Neo4jTemplate template;
52 
53  public Iterable<LongTermContract> findLongTermContractsForEnergyProducerActiveAtTime(EnergyProducer energyProducer, long time) {
54  Pipe<Vertex, Vertex> contractPipe = new LabeledEdgePipe("CONTRACT_FROM", LabeledEdgePipe.Step.IN_OUT);
55  // filter by time
56  Pipe<Vertex, Vertex> pipeline = new Pipeline<Vertex, Vertex>(contractPipe);
57 
58  List<LongTermContract> list = new ArrayList<LongTermContract>();
59  // Only if current time is between start and finish time
60  for (Contract contract : findAllByPipe(energyProducer, pipeline)) {
61  if (contract.getStart() <= time && contract.getFinish() >= time) {
62  list.add((LongTermContract) contract);
63  }
64  }
65  return list;
66  }
67 
68  public Iterable<LongTermContract> findLongTermContractsForEnergyProducerForSegmentActiveAtTime(EnergyProducer energyProducer,
69  Segment segment, long time) {
70 
71  Pipe<Vertex, Vertex> contractPipe = new LabeledEdgePipe("CONTRACT_FROM", LabeledEdgePipe.Step.IN_OUT);
72 
73  Pipe<Vertex, Vertex> pipeline = new Pipeline<Vertex, Vertex>(contractPipe);
74 
75  List<LongTermContract> list = new ArrayList<LongTermContract>();
76  for (Contract contract : findAllByPipe(energyProducer, pipeline)) {
77 
78  // filter by time
79  LongTermContract ltc = (LongTermContract) contract;
80  if (ltc.getStart() <= time && ltc.getFinish() >= time) {
81  if (ltc.getLongTermContractType().getSegments().contains(segment)) {
82  list.add((LongTermContract) contract);
83  }
84  }
85  }
86  return list;
87  }
88 
89  public Iterable<Contract> findLongTermContractsForEnergyConsumerActiveAtTime(EnergyConsumer energyConsumer, long time) {
90  Pipe<Vertex, Vertex> contractPipe = new LabeledEdgePipe("CONTRACT_TO", LabeledEdgePipe.Step.IN_OUT);
91  // filter by time
92  Pipe<Vertex, Vertex> pipeline = new Pipeline<Vertex, Vertex>(contractPipe);
93 
94  List<Contract> list = new ArrayList<Contract>();
95  // Only if current time is between start and finish time
96  for (Contract contract : findAllByPipe(energyConsumer, pipeline)) {
97  if (contract.getStart() <= time && contract.getFinish() >= time) {
98  list.add(contract);
99  }
100  }
101  return list;
102  }
103 
104  public Iterable<LongTermContract> findLongTermContractsForEnergyConsumerForSegmentActiveAtTime(EnergyConsumer consumer,
105  Segment segment, long time) {
106 
107  List<LongTermContract> list = new ArrayList<LongTermContract>();
108  for (Contract contract : findLongTermContractsForEnergyConsumerActiveAtTime(consumer, time)) {
109 
110  // filter by time
111  LongTermContract ltc = (LongTermContract) contract;
112  if (ltc.getStart() <= time && ltc.getFinish() >= time) {
113  if (ltc.getLongTermContractType().getSegments().contains(segment)) {
114  list.add((LongTermContract) contract);
115  }
116  }
117  }
118  return list;
119  }
120 
121  public LongTermContract findLongTermContractForPowerPlantActiveAtTime(PowerPlant plant, long time) {
122 
123  for (Contract c : findAll()) {
125  // It active
126  if (ltc.getStart() <= time && ltc.getFinish() >= time) {
127  if (ltc.getUnderlyingPowerPlant().equals(plant)) {
128  return ltc;
129  }
130  }
131  }
132  return null;
133  }
134 
135  public Iterable<LongTermContract> findLongTermContractsForEnergyConsumerForSegmentForZoneActiveAtTime(EnergyConsumer consumer,
136  Segment segment, Zone zone, long currentTick) {
137  List<LongTermContract> list = new ArrayList<LongTermContract>();
138  for (LongTermContract ltc : findLongTermContractsForEnergyConsumerForSegmentActiveAtTime(consumer, segment, currentTick)) {
139  if (ltc.getZone().equals(zone)) {
140  list.add(ltc);
141  }
142  }
143  return list;
144  }
145 
151  // TODO not transactional, so make it transactional when used.
153  Zone zone, double price, double capacity, LongTermContractType longTermContractType, long time,
154  LongTermContractDuration duration, boolean signed, Substance mainFuel, double fuelPassThroughFactor,
155  double co2PassThroughFactor, double fuelPriceStart, double co2PriceStart) {
156 
157  LongTermContract contract = new LongTermContract().persist();
158  contract.setUnderlyingPowerPlant(plant);
159  contract.setFrom(seller);
160  contract.setTo(buyer);
161  contract.setZone(zone);
162  contract.setPricePerUnit(price);
163  contract.setCapacity(capacity);
164  contract.setLongTermContractType(longTermContractType);
165  contract.setStart(time);
166  contract.setFinish(time + duration.getDuration() - 1);
167  contract.setDuration(duration);
168  contract.setSigned(signed);
169  contract.setMainFuel(mainFuel);
170  contract.setFuelPassThroughFactor(fuelPassThroughFactor);
171  contract.setCo2PassThroughFactor(co2PassThroughFactor);
172  contract.setFuelPriceStart(fuelPriceStart);
173  contract.setCo2PriceStart(co2PriceStart);
174  return contract;
175  }
176 
177  // TODO not transactional, so make it transactional when used.
178  public LongTermContractOffer submitLongTermContractOfferForElectricity(EnergyProducer seller, PowerPlant plant, Zone zone,
179  double price, double capacity, LongTermContractType longTermContractType, long time, LongTermContractDuration duration,
180  Substance mainFuel, double fuelPassThroughFactor, double co2PassThroughFactor, double fuelPriceStart, double co2PriceStart) {
181 
182  LongTermContractOffer offer = new LongTermContractOffer().persist();
183  offer.setSeller(seller);
184  offer.setUnderlyingPowerPlant(plant);
185  offer.setZone(zone);
186  offer.setPrice(price);
187  offer.setCapacity(capacity);
188  offer.setLongTermContractType(longTermContractType);
189  offer.setStart(time);
190  offer.setDuration(duration);
191  offer.setMainFuel(mainFuel);
192  offer.setFuelPassThroughFactor(fuelPassThroughFactor);
193  offer.setCo2PassThroughFactor(co2PassThroughFactor);
194  offer.setFuelPriceStart(fuelPriceStart);
195  offer.setCo2PriceStart(co2PriceStart);
196  return offer;
197  }
198 
199  @Transactional
200  public void removeOffer(LongTermContractOffer offer) {
201  offer.remove();
202  }
203 
204  @Transactional
205  public void removeAllOffers() {
206  for (LongTermContractOffer offer : template.repositoryFor(LongTermContractOffer.class).findAll()) {
207  offer.remove();
208  }
209  }
210 
211  @Transactional
212  public void reassignLongTermContractToNewPowerPlant(LongTermContract longTermContract, PowerPlant plant) {
213  longTermContract.setUnderlyingPowerPlant(plant);
214  }
215 
216 }
LongTermContract submitLongTermContractForElectricity(PowerPlant plant, DecarbonizationAgent seller, DecarbonizationAgent buyer, Zone zone, double price, double capacity, LongTermContractType longTermContractType, long time, LongTermContractDuration duration, boolean signed, Substance mainFuel, double fuelPassThroughFactor, double co2PassThroughFactor, double fuelPriceStart, double co2PriceStart)