EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
PowerPlantDispatchPlanRepository.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 org.springframework.data.neo4j.annotation.Query;
19 import org.springframework.data.neo4j.annotation.QueryType;
20 import org.springframework.data.neo4j.repository.GraphRepository;
21 import org.springframework.data.repository.query.Param;
22 import org.springframework.stereotype.Repository;
23 
30 
39 @Repository
40 public interface PowerPlantDispatchPlanRepository extends GraphRepository<PowerPlantDispatchPlan> {
41 
42  // @Query(value = "g.V.filter{it.getProperty('__type__')=='emlab.gen.domain.market.electricity.PowerPlantDispatchPlan' && it.getProperty('time')==time}", type = QueryType.Gremlin)
43  // public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForTime(@Param("time") long time);
44 
45  @Query("START ppdp=node:__types__(\"className:emlab.gen.domain.market.electricity.PowerPlantDispatchPlan\") WHERE (ppdp.time={time} AND ppdp.forecast = {forecast}) RETURN ppdp")
46  public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForTime(@Param("time") long time,
47  @Param("forecast") boolean forecast);
48 
49  @Query(value = "result = g.v(plant).in('POWERPLANT_DISPATCHPLAN').as('x').propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).out('SEGMENT_DISPATCHPLAN').idFilter(segment, FilterPipe.Filter.EQUAL).back('x')", type = QueryType.Gremlin)
50  public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlanForPowerPlantForSegmentForTime(@Param("plant") PowerPlant plant,
51  @Param("segment") Segment segment, @Param("time") long time,
52  @Param("forecast") boolean forecast);
53 
54  @Query(value = "result = g.v(plant).in('POWERPLANT_DISPATCHPLAN').as('x').propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).out('SEGMENT_DISPATCHPLAN').idFilter(segment, FilterPipe.Filter.EQUAL).back('x').propertyFilter('time', FilterPipe.Filter.EQUAL, time); if(!result.hasNext()){return null;} else{return result.next();}", type = QueryType.Gremlin)
55  public PowerPlantDispatchPlan findOnePowerPlantDispatchPlanForPowerPlantForSegmentForTime(@Param("plant") PowerPlant plant,
56  @Param("segment") Segment segment, @Param("time") long time,
57  @Param("forecast") boolean forecast);
58 
59  // @Query(value = "g.v(segment).in('SEGMENT_DISPATCHPLAN').propertyFilter('time', FilterPipe.Filter.EQUAL, time)", type = QueryType.Gremlin)
60  // public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForSegmentForTime(@Param("segment") Segment segment,
61  // @Param("time") long time);
62 
63  @Query("START segment = node({segment}) MATCH (segment)<-[:SEGMENT_DISPATCHPLAN]-(ppdp) WHERE (ppdp.time = {time} AND ppdp.forecast={forecast}) RETURN ppdp")
64  public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForSegmentForTime(@Param("segment") Segment segment,
65  @Param("time") long time, @Param("forecast") boolean forecast);
66 
67  // @Query(value = "g.v(segment).in('SEGMENT_DISPATCHPLAN').propertyFilter('time', FilterPipe.Filter.EQUAL, time).sort{it.price}._()", type = QueryType.Gremlin)
68  // public Iterable<PowerPlantDispatchPlan> findSortedPowerPlantDispatchPlansForSegmentForTime(@Param("segment") Segment segment,
69  // @Param("time") long time);
70 
71  @Query("START segment = node({segment}) MATCH (segment)<-[:SEGMENT_DISPATCHPLAN]-(ppdp) WHERE (ppdp.time = {time} AND ppdp.forecast={forecast}) RETURN ppdp ORDER BY ppdp.price")
72  public Iterable<PowerPlantDispatchPlan> findSortedPowerPlantDispatchPlansForSegmentForTime(@Param("segment") Segment segment,
73  @Param("time") long time, @Param("forecast") boolean forecast);
74 
75  // descending order
76  @Query("START segment = node({segment}) MATCH (segment)<-[:SEGMENT_DISPATCHPLAN]-(ppdp) WHERE (ppdp.time = {time} AND ppdp.forecast={forecast}) RETURN ppdp ORDER BY ppdp.price desc")
77  public Iterable<PowerPlantDispatchPlan> findDescendingSortedPowerPlantDispatchPlansForSegmentForTime(
78  @Param("segment") Segment segment, @Param("time") long time, @Param("forecast") boolean forecast);
79 
80  @Query(value = "g.v(plant).in('POWERPLANT_DISPATCHPLAN').propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).propertyFilter('time', FilterPipe.Filter.EQUAL, time)", type = QueryType.Gremlin)
81  public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForPowerPlantForTime(@Param("plant") PowerPlant plant,
82  @Param("time") long time, @Param("forecast") boolean forecast);
83 
84  @Query(value = "g.v(producer).out('BIDDER').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast)", type = QueryType.Gremlin)
85  public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForEnergyProducerForTime(
86  @Param("producer") EnergyProducer producer, @Param("time") long time, @Param("forecast") boolean forecast);
87 
88  @Query(value = "g.v(producer).out('BIDDER').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).as('x').out('POWERPLANT_DISPATCHPLAN').filter{it==g.v(tech)}.back('x')", type = QueryType.Gremlin)
89  public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForEnergyProducerForTimeForTechnology(
90  @Param("producer") EnergyProducer producer, @Param("time") long time,
91  @Param("tech") PowerGeneratingTechnology pgt, @Param("forecast") boolean forecast);
92 
93 
94  @Query(value = "g.v(producer).out('BIDDER').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('status', FilterPipe.Filter.GREATER_THAN_EQUAL , 2).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast)", type = QueryType.Gremlin)
95  public Iterable<PowerPlantDispatchPlan> findAllAcceptedPowerPlantDispatchPlansForEnergyProducerForTime(
96  @Param("producer") EnergyProducer producer, @Param("time") long time, @Param("forecast") boolean forecast);
97 
98  @Query(value = "sum=0;ppdps=g.v(producer).out('BIDDER').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('status', FilterPipe.Filter.GREATER_THAN_EQUAL , 2).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast);"
99  +
100  "for(ppdp in ppdps){"+
101  "totalAmount = ppdp.getProperty('acceptedAmount') + ppdp.getProperty('capacityLongTermContract');"+
102  "hoursInSegment = ppdp.out('SEGMENT_DISPATCHPLAN').next().getProperty('lengthInHours');"+
103  "production = totalAmount * hoursInSegment;"+
104  "sum = sum + production};"
105  + " return sum;", type = QueryType.Gremlin)
106  public double calculateTotalProductionForEnergyProducerForTime(@Param("producer") EnergyProducer producer,
107  @Param("time") long time, @Param("forecast") boolean forecast);
108 
109  @Query(value = "sum=0;ppdps=g.v(producer).out('BIDDER').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('status', FilterPipe.Filter.GREATER_THAN_EQUAL , 2).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).as('x').out('POWERPLANT_DISPATCHPLAN').out('TECHNOLOGY').filter{it==g.v(tech)}.back('x');"
110  + "for(ppdp in ppdps){"
111  + "totalAmount = ppdp.getProperty('acceptedAmount') + ppdp.getProperty('capacityLongTermContract');"
112  + "hoursInSegment = ppdp.out('SEGMENT_DISPATCHPLAN').next().getProperty('lengthInHours');"
113  + "production = totalAmount * hoursInSegment;" + "sum = sum + production};" + " return sum;", type = QueryType.Gremlin)
114  public double calculateTotalProductionForEnergyProducerForTimeForTechnology(
115  @Param("producer") EnergyProducer producer, @Param("time") long time,
116  @Param("tech") PowerGeneratingTechnology pgt, @Param("forecast") boolean forecast);
117 
118  @Query(value = "g.v(producer).out('BIDDER').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).as('x').out('SEGMENT_DISPATCHPLAN').idFilter(segment, FilterPipe.Filter.EQUAL).back('x')", type = QueryType.Gremlin)
119  public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForEnergyProducerForTimeAndSegment(
120  @Param("segment") Segment segment, @Param("producer") EnergyProducer producer, @Param("time") long time, @Param("forecast") boolean forecast);
121 
122  @Query(value = "g.v(producer).out('BIDDER').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).propertyFilter('status', FilterPipe.Filter.GREATER_THAN_EQUAL, 2).as('x').out('SEGMENT_DISPATCHPLAN').idFilter(segment, FilterPipe.Filter.EQUAL).back('x')", type = QueryType.Gremlin)
123  public Iterable<PowerPlantDispatchPlan> findAllAcceptedPowerPlantDispatchPlansForEnergyProducerForTimeAndSegment(
124  @Param("segment") Segment segment, @Param("producer") EnergyProducer producer, @Param("time") long time, @Param("forecast") boolean forecast);
125 
126  // @Query("START segment = node({segment}), market=node({market}) MATCH (segment)<-[:SEGMENT_DISPATCHPLAN]-(ppdp)-[:BIDDINGMARKET]->(market) WHERE (ppdp.time = {time}) and (ppdp.status >= 2) RETURN ppdp")
127  // public Iterable<PowerPlantDispatchPlan> findAllAcceptedPowerPlantDispatchPlansForMarketSegmentAndTime(
128  // @Param("market") ElectricitySpotMarket esm, @Param("segment") Segment segment, @Param("time") long time);
129 
130  @Query(value = "g.v(market).in('BIDDINGMARKET').propertyFilter('time', FilterPipe.Filter.EQUAL, time).propertyFilter('forecast', FilterPipe.Filter.EQUAL, forecast).propertyFilter('status', FilterPipe.Filter.GREATER_THAN_EQUAL, 2).as('x').out('SEGMENT_DISPATCHPLAN').idFilter(segment, FilterPipe.Filter.EQUAL).back('x')", type = QueryType.Gremlin)
131  public Iterable<PowerPlantDispatchPlan> findAllAcceptedPowerPlantDispatchPlansForMarketSegmentAndTime(
132  @Param("market") ElectricitySpotMarket esm, @Param("segment") Segment segment, @Param("time") long time, @Param("forecast") boolean forecast);
133 
134  // @Query("START segment = node({segment} MATCH (segment)<-[:SEGMENT_DISPATCHPLAN]-(ppdp)<-[:BIDDER]-(node({producer})) WHERE (ppdp.time = {time}) AND (ppdp.status >=1) RETURN ppdp")
135  // public Iterable<PowerPlantDispatchPlan> findAllAcceptedPowerPlantDispatchPlansForEnergyProducerForTimeAndSegment(
136  // @Param("segment") Segment segment, @Param("producer") EnergyProducer producer, @Param("time") long time);
137 }
138 
139 // package emlab.gen.repository;
140 //
141 // import java.util.ArrayList;
142 // import java.util.List;
143 //
144 // import org.springframework.stereotype.Repository;
145 // import org.springframework.transaction.annotation.Transactional;
146 //
147 // import com.tinkerpop.blueprints.pgm.Vertex;
148 // import com.tinkerpop.gremlin.pipes.filter.PropertyFilterPipe;
149 // import com.tinkerpop.pipes.Pipe;
150 // import com.tinkerpop.pipes.filter.FilterPipe;
151 // import com.tinkerpop.pipes.util.Pipeline;
152 //
153 // import emlab.gen.domain.agent.EnergyProducer;
154 // import emlab.gen.domain.market.electricity.ElectricitySpotMarket;
155 // import emlab.gen.domain.market.electricity.PowerPlantDispatchPlan;
156 // import emlab.gen.domain.market.electricity.Segment;
157 // import emlab.gen.domain.technology.PowerPlant;
158 //
159 // @Repository
160 // public class PowerPlantDispatchPlanRepository extends AbstractRepository<PowerPlantDispatchPlan> {
161 //
162 // public PowerPlantDispatchPlan findOnePowerPlantDispatchPlanForPowerPlantForSegmentForTime(PowerPlant plant, Segment segment, long time) {
163 // for (PowerPlantDispatchPlan plan : findAllPowerPlantDispatchPlansForPowerPlantForTime(plant, time)) {
164 // if (plan.getSegment().equals(segment)) {
165 // return plan;
166 // }
167 // }
168 // return null;
169 // }
170 //
171 // public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForSegmentForTime(Segment segment, long time) {
172 //
173 // // get incoming bids
174 // Pipe<Vertex, Vertex> bids = new LabeledEdgePipe("SEGMENT_DISPATCHPLAN", LabeledEdgePipe.Step.BOTH_BOTH);
175 // // filter by time
176 // Pipe<Vertex, Vertex> timeFilter = new PropertyFilterPipe<Vertex, Long>("time", time, FilterPipe.Filter.EQUAL);
177 // // create pipeline
178 // Pipe<Vertex, Vertex> pipeline = new Pipeline<Vertex, Vertex>(bids, timeFilter);
179 // return this.findAllByPipe(segment, pipeline);
180 // }
181 //
182 // public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForTime(long time) {
183 // List<PowerPlantDispatchPlan> list = new ArrayList<PowerPlantDispatchPlan>();
184 // for (PowerPlantDispatchPlan plan : findAll()) {
185 // if (plan.getTime() == time) {
186 // list.add(plan);
187 // }
188 // }
189 // return list;
190 // }
191 //
192 // public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForPowerPlantForTime(PowerPlant powerPlant, long time) {
193 // Pipe<Vertex, Vertex> bids = new LabeledEdgePipe("POWERPLANT_DISPATCHPLAN", LabeledEdgePipe.Step.BOTH_BOTH);
194 // // filter by time
195 // Pipe<Vertex, Vertex> timeFilter = new PropertyFilterPipe<Vertex, Long>("time", time, FilterPipe.Filter.EQUAL);
196 // // create pipeline
197 // Pipe<Vertex, Vertex> pipeline = new Pipeline<Vertex, Vertex>(bids, timeFilter);
198 //
199 // return this.findAllByPipe(powerPlant, pipeline);
200 // }
201 //
202 // public Iterable<PowerPlantDispatchPlan> findAllPowerPlantDispatchPlansForEnergyProducerForTime(EnergyProducer energyProducer, long time) {
203 // Pipe<Vertex, Vertex> bids = new LabeledEdgePipe("BIDDER", LabeledEdgePipe.Step.BOTH_BOTH);
204 // // filter by time
205 // Pipe<Vertex, Vertex> timeFilter = new PropertyFilterPipe<Vertex, Long>("time", time, FilterPipe.Filter.EQUAL);
206 // // create pipeline
207 // Pipe<Vertex, Vertex> pipeline = new Pipeline<Vertex, Vertex>(bids, timeFilter);
208 //
209 // return this.findAllByPipe(energyProducer, pipeline);
210 // }
211 //
212 // @Transactional
213 // public PowerPlantDispatchPlan submitOrUpdatePowerPlantDispatchPlanForSpotMarket(PowerPlant plant, EnergyProducer producer,
214 // ElectricitySpotMarket market, Segment segment, long time, double price, double capacity) {
215 //
216 // // make a new one if it
217 // PowerPlantDispatchPlan plan = findOnePowerPlantDispatchPlanForPowerPlantForSegmentForTime(plant, segment, time);
218 // if (plan == null) {
219 // plan = new PowerPlantDispatchPlan().persist();
220 // plan.setPowerPlant(plant);
221 // plan.setSegment(segment);
222 // plan.setTime(time);
223 //
224 // }
225 // plan.setBidder(producer);
226 // plan.setBiddingMarket(market);
227 // plan.setPrice(price);
228 // plan.setCapacitySpotMarket(capacity);
229 // plan.setCapacityLongTermContract(0d);
230 // return null;
231 // }
232 //
233 // @Transactional
234 // public void updateCapacityLongTermContract(PowerPlantDispatchPlan plan, double capacity) {
235 // plan.setCapacityLongTermContract(capacity);
236 // // if(plan.getCapacitySpotMarket() + capacity >
237 // // plan.getPowerPlant().getTechnology().getCapacity()){
238 // // logger.warn("PROBLEM: Adding to much ltc capacity to dispatch plan: "
239 // // + plan);
240 // // }
241 // }
242 //
243 // @Transactional
244 // public void updateCapacitySpotMarket(PowerPlantDispatchPlan plan, double capacity) {
245 // plan.setCapacitySpotMarket(capacity);
246 // // if(plan.getCapacityLongTermContract() + capacity >
247 // // plan.getPowerPlant().getTechnology().getCapacity()){
248 // // logger.warn("PROBLEM: Adding to much spot capacity to dispatch plan: "
249 // // + plan);
250 // // }
251 // }
252 // }