refractor(queryAsPath): Use a wrapper with ops
This commit is contained in:
parent
4de3f712d2
commit
5d82987cc6
4 changed files with 68 additions and 11 deletions
|
|
@ -84,18 +84,18 @@ public class DroneController {
|
|||
}
|
||||
|
||||
@PostMapping("/query")
|
||||
public int[] getIdByAttrMapPost(@RequestBody AttrComparatorDto[] attrComparators) {
|
||||
return new int[]{};
|
||||
public String[] getIdByAttrMapPost(@RequestBody AttrComparatorDto[] attrComparators) {
|
||||
return droneService.dronesSatisfyingAttributes(attrComparators);
|
||||
}
|
||||
|
||||
@PostMapping("/queryAvailableDrones")
|
||||
public int[] queryAvailableDrones(@RequestBody MedDispathRecDto[] records) {
|
||||
return new int[]{};
|
||||
return new int[] {};
|
||||
}
|
||||
|
||||
@PostMapping("/calcDeliveryPath")
|
||||
public DeliveryPathDto calculateDeliveryPath(@RequestBody MedDispathRecDto[] record) {
|
||||
return new DeliveryPathDto(0.0f, 0, new DronePathDto[]{});
|
||||
return new DeliveryPathDto(0.0f, 0, new DronePathDto[] {});
|
||||
}
|
||||
|
||||
@PostMapping("/calcDeliveryPathAsGeoJson")
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
package io.github.js0ny.ilp_coursework.data;
|
||||
|
||||
public enum AttrOperator {
|
||||
}
|
||||
|
|
@ -1,9 +1,13 @@
|
|||
package io.github.js0ny.ilp_coursework.service;
|
||||
|
||||
import io.github.js0ny.ilp_coursework.data.AttrComparatorDto;
|
||||
import io.github.js0ny.ilp_coursework.data.DroneDto;
|
||||
import io.github.js0ny.ilp_coursework.util.AttrOperator;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
|
@ -97,6 +101,20 @@ public class DroneInfoService {
|
|||
* @return array of drone ids matching the attribute name and value
|
||||
*/
|
||||
public String[] dronesWithAttribute(String attrName, String attrVal) {
|
||||
// Call the helper with EQ operator
|
||||
return dronesWithAttributeCompared(attrName, attrVal, AttrOperator.EQ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper that wraps the dynamic querying with different comparison operators
|
||||
*
|
||||
* @param attrName the attribute name to filter on
|
||||
* @param attrVal the attribute value to filter on
|
||||
* @param op the comparison operator
|
||||
* @return array of drone ids matching the attribute name and value (filtered by
|
||||
* {@code op})
|
||||
*/
|
||||
private String[] dronesWithAttributeCompared(String attrName, String attrVal, AttrOperator op) {
|
||||
URI droneUrl = URI.create(baseUrl).resolve(dronesEndpoint);
|
||||
// This is required to make sure the response is valid
|
||||
DroneDto[] drones = restTemplate.getForObject(
|
||||
|
|
@ -117,7 +135,7 @@ public class DroneInfoService {
|
|||
JsonNode attrNode = node.findValue(attrName);
|
||||
if (attrNode != null) {
|
||||
// Manually handle different types of JsonNode
|
||||
return isValueMatched(attrNode, attrVal);
|
||||
return isValueMatched(attrNode, attrVal, op);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -126,6 +144,26 @@ public class DroneInfoService {
|
|||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
// TODO: Implement this
|
||||
public String[] dronesSatisfyingAttributes(AttrComparatorDto[] attrComparators) {
|
||||
Set<String> matchingDroneIds = null;
|
||||
for (var comparator : attrComparators) {
|
||||
String attribute = comparator.attribute();
|
||||
String operator = comparator.operator();
|
||||
String value = comparator.value();
|
||||
AttrOperator op = AttrOperator.fromString(operator);
|
||||
String[] ids = new String[] {};// Arrays.stream
|
||||
// isValueMatched(attribute, value, op);
|
||||
if (matchingDroneIds == null) {
|
||||
matchingDroneIds = new HashSet<>(Arrays.asList(ids));
|
||||
}
|
||||
}
|
||||
if (matchingDroneIds == null) {
|
||||
return new String[] {};
|
||||
}
|
||||
return matchingDroneIds.toArray(String[]::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for dynamic querying, to compare the json value with given value in
|
||||
* {@code String}.
|
||||
|
|
@ -134,7 +172,7 @@ public class DroneInfoService {
|
|||
* @param attrVal The Value passed, in {@code String}
|
||||
* @return {@code true} if given values are equal, otherwise false.
|
||||
*/
|
||||
private boolean isValueMatched(JsonNode node, String attrVal) {
|
||||
private boolean isValueMatched(JsonNode node, String attrVal, AttrOperator op) {
|
||||
if (node.isTextual()) {
|
||||
return node.asText().equals(attrVal);
|
||||
} else if (node.isNumber()) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
package io.github.js0ny.ilp_coursework.util;
|
||||
|
||||
public enum AttrOperator {
|
||||
EQ("="),
|
||||
NEQ("!="),
|
||||
GT(">"),
|
||||
LT("<");
|
||||
|
||||
private final String symbol;
|
||||
|
||||
AttrOperator(String symbol) {
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
public static AttrOperator fromString(String symbol) {
|
||||
for (AttrOperator op : AttrOperator.values()) {
|
||||
if (op.symbol.equals(symbol)) {
|
||||
return op;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown operator: " + symbol);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue