Skip to content

Commit 93671e5

Browse files
scalbertmSCALBERT Mathieujonenst
authored
feat: Match network to rule (#13)
Co-authored-by: SCALBERT Mathieu <[email protected]> Co-authored-by: Jon Harper <[email protected]>
1 parent 48d6b67 commit 93671e5

File tree

16 files changed

+461
-25
lines changed

16 files changed

+461
-25
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
<groupId>javax.xml.bind</groupId>
140140
<artifactId>jaxb-api</artifactId>
141141
</dependency>
142+
<dependency>
143+
<groupId>org.springframework</groupId>
144+
<artifactId>spring-context</artifactId>
145+
</dependency>
142146
<dependency>
143147
<groupId>com.powsybl</groupId>
144148
<artifactId>powsybl-config-classic</artifactId>
@@ -209,5 +213,10 @@
209213
<groupId>org.springframework.boot</groupId>
210214
<artifactId>spring-boot</artifactId>
211215
</dependency>
216+
<dependency>
217+
<groupId>io.springfox</groupId>
218+
<artifactId>springfox-core</artifactId>
219+
<version>2.9.2</version>
220+
</dependency>
212221
</dependencies>
213222
</project>

src/main/java/org/gridsuite/mapping/server/controller/NetworkController.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import io.swagger.v3.oas.annotations.responses.ApiResponses;
1212
import io.swagger.v3.oas.annotations.tags.Tag;
1313
import lombok.AllArgsConstructor;
14-
import org.gridsuite.mapping.server.dto.EquipmentValues;
14+
import org.gridsuite.mapping.server.dto.MatchedRule;
15+
import org.gridsuite.mapping.server.dto.NetworkValues;
1516
import org.gridsuite.mapping.server.dto.OutputNetwork;
17+
import org.gridsuite.mapping.server.dto.RuleToMatch;
1618
import org.gridsuite.mapping.server.service.NetworkService;
1719
import org.gridsuite.mapping.server.service.implementation.NetworkServiceImpl;
1820
import org.springframework.context.annotation.ComponentScan;
@@ -42,7 +44,7 @@ public class NetworkController {
4244
@ApiResponses(value = {
4345
@ApiResponse(responseCode = "200", description = "List of property values of the network")})
4446

45-
public ResponseEntity<List<EquipmentValues>> getNetworkValuesFromExistingCase(@PathVariable("networkUuid") UUID networkUuid) {
47+
public ResponseEntity<NetworkValues> getNetworkValuesFromExistingCase(@PathVariable("networkUuid") UUID networkUuid) {
4648
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(networkService.getNetworkValuesFromExistingNetwork(networkUuid));
4749
}
4850

@@ -59,8 +61,16 @@ public ResponseEntity<List<OutputNetwork>> getKnownNetworks() {
5961
@Operation(summary = "Post a network and retrieve its values")
6062
@ApiResponses(value = {
6163
@ApiResponse(responseCode = "200", description = "List of property values of the network")})
62-
public ResponseEntity<List<EquipmentValues>> getNetworkValues(@RequestPart("file") MultipartFile networkFile) {
64+
public ResponseEntity<NetworkValues> getNetworkValues(@RequestPart("file") MultipartFile networkFile) {
6365
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(networkService.getNetworkValues(networkFile));
6466
}
6567

68+
@PostMapping(value = "/{networkUuid}/matches/rule")
69+
@Operation(summary = "Get the equipment ids that matches the given rule.")
70+
@ApiResponses(value = {
71+
@ApiResponse(responseCode = "200", description = "Possible property values of the network")})
72+
public ResponseEntity<MatchedRule> getNetworkMatches(@PathVariable("networkUuid") UUID networkUuid, @RequestBody RuleToMatch ruleToMatch) {
73+
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(networkService.getNetworkMatches(networkUuid, ruleToMatch));
74+
}
75+
6676
}

src/main/java/org/gridsuite/mapping/server/dto/InputMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public MappingEntity convertMappingToEntity() {
4646
public InputMapping(MappingEntity mappingEntity) {
4747
name = mappingEntity.getName();
4848
controlledParameters = mappingEntity.isControlledParameters();
49-
rules = mappingEntity.getRules().stream().map(ruleEntity -> new Rule(ruleEntity)).collect(Collectors.toList());
50-
automata = mappingEntity.getAutomata().stream().map(automatonEntity -> AbstractAutomaton.instantiateFromEntity(automatonEntity)).collect(Collectors.toList());
49+
rules = mappingEntity.getRules().stream().map(Rule::new).collect(Collectors.toList());
50+
automata = mappingEntity.getAutomata().stream().map(AbstractAutomaton::instantiateFromEntity).collect(Collectors.toList());
5151
}
5252
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.dto;
8+
9+
import io.swagger.v3.oas.annotations.media.Schema;
10+
import lombok.AllArgsConstructor;
11+
import lombok.Data;
12+
13+
import java.util.List;
14+
15+
/**
16+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
17+
*/
18+
@Data
19+
@AllArgsConstructor
20+
@Schema(description = "MatchedRule")
21+
public class MatchedRule {
22+
@Schema(description = "Rule Index")
23+
private int ruleIndex;
24+
25+
@Schema(description = "Matched Ids")
26+
private List<String> matchedIds;
27+
}
28+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.dto;
8+
9+
import lombok.AllArgsConstructor;
10+
import lombok.Data;
11+
import lombok.NoArgsConstructor;
12+
13+
import java.util.List;
14+
import java.util.UUID;
15+
16+
/**
17+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
18+
*/
19+
@Data
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
public class NetworkValues {
23+
private UUID networkId;
24+
private List<EquipmentValues> propertyValues;
25+
}

src/main/java/org/gridsuite/mapping/server/dto/Rule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public Rule(RuleEntity ruleEntity) {
7171
setGroup = ruleEntity.getSetGroup();
7272
groupType = ruleEntity.getGroupType();
7373
composition = ruleEntity.getComposition();
74-
filters = ruleEntity.getFilters().stream().map(filterEmbeddable -> AbstractFilter.createFilterFromEntity(filterEmbeddable)).collect(Collectors.toList());
74+
filters = ruleEntity.getFilters().stream().map(AbstractFilter::createFilterFromEntity).collect(Collectors.toList());
7575
}
7676

7777
// Needs to put the default rule last, hence going for the most specific rule to the most generic
78-
public static Comparator<Rule> ruleComparator = Comparator.comparing(rule -> -rule.getFilters().size());
78+
public static final Comparator<Rule> RULE_COMPARATOR = Comparator.comparing(rule -> -rule.getFilters().size());
7979
}
8080

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) 2021, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.mapping.server.dto;
8+
9+
import io.swagger.v3.oas.annotations.media.Schema;
10+
import lombok.Data;
11+
import org.gridsuite.mapping.server.dto.filters.AbstractFilter;
12+
import org.gridsuite.mapping.server.utils.EquipmentType;
13+
14+
import java.util.List;
15+
16+
/**
17+
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
18+
*/
19+
@Data
20+
@Schema(description = "RuleToMatch")
21+
public class RuleToMatch {
22+
@Schema(description = "Rule Index")
23+
private int ruleIndex;
24+
25+
@Schema(description = "Equipment type")
26+
private EquipmentType equipmentType;
27+
28+
@Schema(description = "Composition")
29+
private String composition;
30+
31+
@Schema(description = "Filters")
32+
private List<AbstractFilter> filters;
33+
}
34+

src/main/java/org/gridsuite/mapping/server/dto/filters/AbstractFilter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import lombok.Data;
1212
import org.gridsuite.mapping.server.model.FilterEntity;
1313
import org.gridsuite.mapping.server.model.RuleEntity;
14-
import org.gridsuite.mapping.server.utils.Operands;
1514
import org.gridsuite.mapping.server.utils.Methods;
15+
import org.gridsuite.mapping.server.utils.Operands;
1616
import org.springframework.http.HttpStatus;
1717
import org.springframework.web.server.ResponseStatusException;
1818

@@ -53,12 +53,14 @@ public static AbstractFilter createFilterFromEntity(FilterEntity filterEntity) {
5353
stringFilter.setOperand(filterEntity.getOperand());
5454
stringFilter.setValue(Methods.convertStringToList(filterEntity.getValue()));
5555
return stringFilter;
56-
default :
56+
default:
5757
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Unknown filter type");
5858
}
5959
}
6060

6161
public abstract FilterEntity convertFilterToEntity(RuleEntity rule);
6262

6363
public abstract String convertFilterToString();
64+
65+
public abstract boolean matchValueToFilter(String valueToTest);
6466
}

src/main/java/org/gridsuite/mapping/server/dto/filters/BooleanFilter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
import lombok.NoArgsConstructor;
1212
import org.gridsuite.mapping.server.model.FilterEntity;
1313
import org.gridsuite.mapping.server.model.RuleEntity;
14-
import org.gridsuite.mapping.server.utils.PropertyType;
1514
import org.gridsuite.mapping.server.utils.Methods;
15+
import org.gridsuite.mapping.server.utils.Operands;
16+
import org.gridsuite.mapping.server.utils.PropertyType;
1617

1718
/**
1819
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
@@ -49,4 +50,10 @@ public String convertFilterToString() {
4950
}
5051
return String.format("equipment.%s %s %b", this.getProperty(), stringOperand, value);
5152
}
53+
54+
@Override
55+
public boolean matchValueToFilter(String valueToTest) {
56+
boolean isNot = this.getOperand().equals(Operands.NOT_EQUALS);
57+
return isNot != (Methods.convertStringToBoolean(valueToTest) == value);
58+
}
5259
}

src/main/java/org/gridsuite/mapping/server/dto/filters/NumberFilter.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.gridsuite.mapping.server.utils.PropertyType;
1616
import org.springframework.http.HttpStatus;
1717
import org.springframework.web.server.ResponseStatusException;
18+
import org.gridsuite.mapping.server.utils.Operands;
1819

1920
import java.util.List;
2021
import java.util.Locale;
@@ -91,4 +92,38 @@ public String convertFilterToString() {
9192

9293
}
9394
}
95+
96+
@Override
97+
public boolean matchValueToFilter(String valueToTest) {
98+
boolean isMatched = false;
99+
100+
Float floatValue = Float.parseFloat(valueToTest);
101+
102+
switch (this.getOperand()) {
103+
case EQUALS:
104+
case IN:
105+
case NOT_IN:
106+
case NOT_EQUALS:
107+
boolean isNot = this.getOperand().equals(Operands.NOT_IN) || this.getOperand().equals(Operands.NOT_EQUALS);
108+
isMatched = isNot != value.stream().reduce(false, (acc, filterUniqueValue) -> acc || (Float.compare(floatValue, filterUniqueValue) == 0
109+
), (a, b) -> a || b);
110+
break;
111+
case LOWER:
112+
isMatched = Float.compare(floatValue, value.get(0)) == -1;
113+
break;
114+
case LOWER_OR_EQUALS:
115+
isMatched = Float.compare(floatValue, value.get(0)) <= 0;
116+
break;
117+
case HIGHER:
118+
isMatched = Float.compare(floatValue, value.get(0)) == 1;
119+
break;
120+
case HIGHER_OR_EQUALS:
121+
isMatched = Float.compare(floatValue, value.get(0)) >= 0;
122+
break;
123+
default:
124+
break;
125+
}
126+
return isMatched;
127+
}
94128
}
129+

0 commit comments

Comments
 (0)