EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
FiltersImpl.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.util;
17 
18 import org.neo4j.graphdb.Node;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.data.neo4j.aspects.core.NodeBacked;
21 import org.springframework.data.neo4j.support.Neo4jTemplate;
22 
23 import agentspring.facade.Filters;
24 import agentspring.trend.Trend;
25 
26 import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jVertex;
27 
36 import emlab.gen.role.AbstractEnergyProducerRole;
37 
38 public class FiltersImpl implements Filters {
39  @Autowired
40  Neo4jTemplate template;
41 
42  @Autowired
43  Reps reps;
44 
45  private Dummy dummy;
46 
47  @Override
48  public void init() {
49  dummy = new Dummy();
50  }
51 
52  public boolean plantIsOperational(Object node, long tick) {
53  NodeBacked entity = this.getEntity(node);
54  if (!(entity instanceof PowerPlant))
55  throw new RuntimeException("Vertex is not a Power plant");
56  PowerPlant plant = (PowerPlant) entity;
57  return plant.isOperational(tick);
58  }
59 
60  public boolean plantIsExpectedToBeOperational(Object node, long tick) {
61  NodeBacked entity = this.getEntity(node);
62  if (!(entity instanceof PowerPlant))
63  throw new RuntimeException("Vertex is not a Power plant");
64  PowerPlant plant = (PowerPlant) entity;
65  return plant.isExpectedToBeOperational(tick);
66  }
67 
68  public boolean plantIsInPipeline(Object node, long tick) {
69  NodeBacked entity = this.getEntity(node);
70  if (!(entity instanceof PowerPlant))
71  throw new RuntimeException("Vertex is not a Power plant");
72  PowerPlant plant = (PowerPlant) entity;
73  return plant.isInPipeline(tick);
74  }
75 
76  public double calculateCO2Emissions(Object node, long tick) {
77  NodeBacked entity = this.getEntity(node);
78  if (!(entity instanceof PowerPlant))
79  throw new RuntimeException("Vertex is not a Power plant");
80  PowerPlant plant = (PowerPlant) entity;
81  return plant.calculateCO2EmissionsAtTime(tick, false);
82  }
83 
84  public double getTrendValue(Object node, long tick) {
85  NodeBacked entity = this.getEntity(node);
86  if (!(entity instanceof Trend)) {
87  throw new RuntimeException("Vertex is not a Trend");
88  }
89  Trend trend = (Trend) entity;
90  return trend.getValue(tick);
91  }
92 
93  public double findLastKnownPriceOnMarket(Object node, long tick) {
94  NodeBacked entity = this.getEntity(node);
95  if (!(entity instanceof DecarbonizationMarket)) {
96  throw new RuntimeException("Vertex is not a Market");
97  }
98  DecarbonizationMarket market = (DecarbonizationMarket) entity;
99 
100  // Emiliano stuff:
101  Double average = calculateAverageMarketPriceBasedOnClearingPoints(reps.clearingPointRepositoryOld
102  .findClearingPointsForMarketAndTime(market, tick, false));
103  Substance substance = market.getSubstance();
104 
105  if (average != null) {
106  return average;
107  }
108 
109  average = calculateAverageMarketPriceBasedOnClearingPoints(reps.clearingPointRepositoryOld.findClearingPointsForMarketAndTime(
110  market, tick - 1, false));
111  if (average != null) {
112  return average;
113  }
114 
115  if (market.getReferencePrice() > 0) {
116  return market.getReferencePrice();
117  }
118 
119  for (CommoditySupplier supplier : reps.genericRepository.findAll(CommoditySupplier.class)) {
120  if (supplier.getSubstance().equals(substance)) {
121  return supplier.getPriceOfCommodity().getValue(tick);
122  }
123  }
124 
125  return 0d;
126  }
127 
128  public double determineProductionOfDispatchPlanInMWh(Object node, long tick) {
129  NodeBacked entity = this.getEntity(node);
130  if (!(entity instanceof PowerPlantDispatchPlan)) {
131  throw new RuntimeException("Vertex is not a Dispatch plan");
132  }
133  PowerPlantDispatchPlan plan = (PowerPlantDispatchPlan) entity;
134 
135  if (tick == plan.getTime()) {
136  return plan.getSegment().getLengthInHours()
137  * (plan.getAcceptedAmount() + plan.getCapacityLongTermContract());
138  }
139  return 0d;
140  }
141 
142  public double findLastKnownPriceOnMarketInGJ(Object node, long tick) {
143  double price = findLastKnownPriceOnMarket(node, tick);
144 
145  NodeBacked entity = this.getEntity(node);
146  if (!(entity instanceof DecarbonizationMarket)) {
147  throw new RuntimeException("Vertex is not a Market");
148  }
149  DecarbonizationMarket market = (DecarbonizationMarket) entity;
150 
151  Substance substance = market.getSubstance();
152 
153  return price / substance.getEnergyDensity();
154  }
155 
156  private Double calculateAverageMarketPriceBasedOnClearingPoints(Iterable<ClearingPoint> clearingPoints) {
157  double priceTimesVolume = 0d;
158  double volume = 0d;
159 
160  for (ClearingPoint point : clearingPoints) {
161  priceTimesVolume += point.getPrice() * point.getVolume();
162  volume += point.getVolume();
163  }
164  if (volume > 0) {
165  return priceTimesVolume / volume;
166  }
167  return null;
168  }
169 
170  private NodeBacked getEntity(Object node) {
171  if (!(node instanceof Neo4jVertex))
172  throw new RuntimeException("Object is not neo4j vertex");
173  Neo4jVertex vertex = (Neo4jVertex) node;
174  Node n = vertex.getRawVertex();
175  NodeBacked entity = template.createEntityFromStoredType(n);
176  return entity;
177  }
178 
179  public boolean ltcIsActive(Object node, long tick) {
180  NodeBacked entity = this.getEntity(node);
181  if (!(entity instanceof LongTermContract))
182  throw new RuntimeException("Vertex is not a Power plant");
183  LongTermContract contract = (LongTermContract) entity;
184  if (contract.getStart() <= tick & contract.getFinish() >= tick) {
185  return true;
186  } else {
187  return false;
188  }
189  }
190 
191  private class Dummy extends AbstractEnergyProducerRole {
192 
193  public Reps getReps() {
194  return reps;
195  }
196 
197  }
198 }