EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
ClearingPointRepositoryOld.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.Iterator;
19 import java.util.List;
20 
21 import org.springframework.stereotype.Repository;
22 import org.springframework.transaction.annotation.Transactional;
23 
24 import com.tinkerpop.blueprints.pgm.Vertex;
25 import com.tinkerpop.gremlin.pipes.filter.PropertyFilterPipe;
26 import com.tinkerpop.pipes.Pipe;
27 import com.tinkerpop.pipes.filter.FilterPipe;
28 import com.tinkerpop.pipes.filter.FilterPipe.Filter;
29 import com.tinkerpop.pipes.util.Pipeline;
30 
37 
44 @Repository
45 public class ClearingPointRepositoryOld extends AbstractRepository<ClearingPoint> {
46 
47  public ClearingPoint findClearingPointForSegmentAndTime(Segment segment, long time, boolean forecast) {
48  Iterator<ClearingPoint> i = findClearingPointsForSegmentAndTime(segment, time, forecast).iterator();
49  if (i.hasNext()) {
50  return i.next();
51  }
52  return null;
53  }
54 
55  public Iterable<ClearingPoint> findClearingPointsForSegmentAndTime(Segment segment, long time, boolean forecast) {
56  Pipe<Vertex, Vertex> clearingPointsPipe2 = new LabeledEdgePipe("SEGMENT_POINT", LabeledEdgePipe.Step.IN_OUT);
57  // filter by time
58  Pipe<Vertex, Vertex> timeFilter = new PropertyFilterPipe<Vertex, Long>("time", time, FilterPipe.Filter.EQUAL);
59  Pipe<Vertex, Vertex> forecastFilter = new PropertyFilterPipe<Vertex, Boolean>("forecast", forecast,
60  Filter.EQUAL);
61  Pipeline<Vertex, Vertex> clearingPoint = new Pipeline<Vertex, Vertex>(clearingPointsPipe2, timeFilter,
62  forecastFilter);
63  return findAllByPipe(segment, clearingPoint);
64  }
65 
66 
67  public ClearingPoint findClearingPointForMarketAndTime(DecarbonizationMarket market, long time, boolean forecast) {
68 
69  Iterator<ClearingPoint> i = findClearingPointsForMarketAndTime(market, time, forecast).iterator();
70  if (i.hasNext()) {
71  return i.next();
72  }
73  return null;
74  }
75 
76 
77  public Iterable<ClearingPoint> findClearingPointsForMarketAndTime(DecarbonizationMarket market, long time,
78  boolean forecast) {
79  // TODO: test this
80  Pipe<Vertex, Vertex> clearingPoints = new LabeledEdgePipe("MARKET_POINT", LabeledEdgePipe.Step.IN_OUT);
81  // filter by time
82  Pipe<Vertex, Vertex> timeFilter = new PropertyFilterPipe<Vertex, Long>("time", time, FilterPipe.Filter.EQUAL);
83  Pipe<Vertex, Vertex> forecastFilter = new PropertyFilterPipe<Vertex, Boolean>("forecast", forecast,
84  Filter.EQUAL);
85  Pipeline<Vertex, Vertex> clearingPoint = new Pipeline<Vertex, Vertex>(clearingPoints, timeFilter,
86  forecastFilter);
87 
88  return findAllByPipe(market, clearingPoint);
89  }
90 
91  @Transactional
92  public ClearingPoint createOrUpdateClearingPoint(DecarbonizationMarket abstractMarket, double price, double volume,
93  long time, boolean forecast) {
94  ClearingPoint point = null;
95  if (findClearingPointsForMarketAndTime(abstractMarket, time, forecast).iterator().hasNext()) {
96  point = findClearingPointsForMarketAndTime(abstractMarket, time, forecast).iterator().next();
97  } else {
98  point = new ClearingPoint().persist();
99  }
100  point.setAbstractMarket(abstractMarket);
101  point.setPrice(price);
102  point.setTime(time);
103  point.setVolume(volume);
104  point.setForecast(forecast);
105  return point;
106  }
107 
108  @Transactional
109  public SegmentClearingPoint createOrUpdateSegmentClearingPoint(Segment segment,
110  DecarbonizationMarket abstractMarket, double price, double volume, double interconnectorFlow, long time,
111  boolean forecast) {
112  SegmentClearingPoint point = null;
113  // TODO make this a pipe
114  List<SegmentClearingPoint> points = Utils.asCastedList(findClearingPointsForMarketAndTime(abstractMarket, time,
115  forecast));
116  for (SegmentClearingPoint onepoint : points) {
117  if (onepoint.getSegment().equals(segment)) {
118  point = onepoint;
119  }
120  }
121  if (point == null) {
122  point = new SegmentClearingPoint().persist();
123  }
124  point.setAbstractMarket(abstractMarket);
125  point.setPrice(price);
126  point.setTime(time);
127  point.setVolume(volume);
128  point.setSegment(segment);
129  point.setForecast(forecast);
130  point.setInterconnectorFlow(interconnectorFlow);
131  return point;
132  }
133 
134  @Transactional
135  public CO2MarketClearingPoint createOrUpdateCO2MarketClearingPoint(DecarbonizationMarket abstractMarket,
136  double price, double volume, boolean emergencyTriggerActivated, double emergencyTriggerOutflow, long time,
137  boolean forecast) {
138  CO2MarketClearingPoint point = null;
139  // TODO make this a pipe
140  if (findClearingPointsForMarketAndTime(abstractMarket, time, forecast).iterator().hasNext()) {
141  point = (CO2MarketClearingPoint) findClearingPointsForMarketAndTime(abstractMarket, time, forecast)
142  .iterator().next();
143  } else {
144  point = new CO2MarketClearingPoint().persist();
145  }
146  point.setAbstractMarket(abstractMarket);
147  point.setPrice(price);
148  point.setTime(time);
149  point.setVolume(volume);
150  point.setEmergencyTriggerActivated(emergencyTriggerActivated);
151  point.setEmergencyTriggerOutflow(emergencyTriggerOutflow);
152  point.setForecast(forecast);
153  return point;
154  }
155 
156 }