Skip to content

Commit 117f38f

Browse files
committed
Rename "priority" to "weight" in the resolver objects.
1 parent 5844ee9 commit 117f38f

5 files changed

Lines changed: 57 additions & 56 deletions

File tree

src/main/java/io/zentity/model/Resolver.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Resolver {
1919

2020
private final String name;
2121
private Set<String> attributes = new TreeSet<>();
22-
private int priority = 0;
22+
private int weight = 0;
2323

2424
public Resolver(String name, JsonNode json) throws ValidationException {
2525
validateName(name);
@@ -41,7 +41,7 @@ public Set<String> attributes() {
4141
return this.attributes;
4242
}
4343

44-
public int priority () { return this.priority; }
44+
public int weight () { return this.weight; }
4545

4646
public void attributes(JsonNode value) throws ValidationException {
4747
validateAttributes(value);
@@ -51,9 +51,9 @@ public void attributes(JsonNode value) throws ValidationException {
5151
this.attributes = attributes;
5252
}
5353

54-
public void priority(JsonNode value) throws ValidationException {
55-
validatePriority(value);
56-
this.priority = value.asInt();
54+
public void weight(JsonNode value) throws ValidationException {
55+
validateWeight(value);
56+
this.weight = value.asInt();
5757
}
5858

5959
private void validateName(String value) throws ValidationException {
@@ -75,9 +75,9 @@ private void validateAttributes(JsonNode value) throws ValidationException {
7575
}
7676
}
7777

78-
private void validatePriority(JsonNode value) throws ValidationException {
78+
private void validateWeight(JsonNode value) throws ValidationException {
7979
if (!value.isInt())
80-
throw new ValidationException("'resolvers." + this.name + ".priority' must be an integer.");
80+
throw new ValidationException("'resolvers." + this.name + ".weight' must be an integer.");
8181
}
8282

8383
private void validateObject(JsonNode object) throws ValidationException {
@@ -96,7 +96,7 @@ private void validateObject(JsonNode object) throws ValidationException {
9696
* ATTRIBUTE_NAME,
9797
* ...
9898
* ],
99-
* "priority": INTEGER
99+
* "weight": INTEGER
100100
* }
101101
* </pre>
102102
*
@@ -121,8 +121,8 @@ public void deserialize(JsonNode json) throws ValidationException {
121121
case "attributes":
122122
this.attributes(value);
123123
break;
124-
case "priority":
125-
this.priority(value);
124+
case "weight":
125+
this.weight(value);
126126
break;
127127
default:
128128
throw new ValidationException("'resolvers." + this.name + "." + name + "' is not a recognized field.");

src/main/java/io/zentity/resolution/Job.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -438,19 +438,19 @@ public static Map<String, Integer> countAttributesAcrossResolvers(Model model, L
438438
}
439439

440440
/**
441-
* Group resolvers by their level of priority.
441+
* Group resolvers by their level of weight.
442442
*
443443
* @param model The entity model.
444444
* @param resolvers The names of the resolvers to reference in the entity model.
445-
* @return For each priority level, the names of the resolvers in that priority level.
445+
* @return For each weight level, the names of the resolvers in that weight level.
446446
*/
447-
public static TreeMap<Integer, List<String>> groupResolversByPriority(Model model, List<String> resolvers) {
447+
public static TreeMap<Integer, List<String>> groupResolversByWeight(Model model, List<String> resolvers) {
448448
TreeMap<Integer, List<String>> resolverGroups = new TreeMap<>();
449449
for (String resolverName : resolvers) {
450-
Integer priority = model.resolvers().get(resolverName).priority();
451-
if (!resolverGroups.containsKey(priority))
452-
resolverGroups.put(priority, new ArrayList<>());
453-
resolverGroups.get(priority).add(resolverName);
450+
Integer weight = model.resolvers().get(resolverName).weight();
451+
if (!resolverGroups.containsKey(weight))
452+
resolverGroups.put(weight, new ArrayList<>());
453+
resolverGroups.get(weight).add(resolverName);
454454
}
455455
return resolverGroups;
456456
}
@@ -650,31 +650,32 @@ else if (size == 1)
650650
TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped= new TreeMap<>(Collections.reverseOrder());
651651
if (!this.attributes.isEmpty()) {
652652

653-
// Group the resolvers by their priority level.
654-
TreeMap<Integer, List<String>> resolverGroups = groupResolversByPriority(this.input.model(), resolvers);
653+
// Group the resolvers by their weight level.
654+
TreeMap<Integer, List<String>> resolverGroups = groupResolversByWeight(this.input.model(), resolvers);
655655

656-
// Construct a clause for each priority level in ascending order of priority.
657-
List<Integer> priorities = new ArrayList<>(resolverGroups.keySet());
658-
int numPriorityLevels= priorities.size();
659-
for (int level = 0; level < numPriorityLevels; level++) {
660-
Integer priority = priorities.get(level);
661-
List<String> resolversGroup = resolverGroups.get(priority);
656+
// Construct a clause for each weight level in descending order of weight.
657+
List<Integer> weights = new ArrayList<>(resolverGroups.keySet());
658+
Collections.reverse(weights);
659+
int numWeightLevels = weights.size();
660+
for (int level = 0; level < numWeightLevels; level++) {
661+
Integer weight = weights.get(level);
662+
List<String> resolversGroup = resolverGroups.get(weight);
662663
Map<String, Integer> counts = countAttributesAcrossResolvers(this.input.model(), resolversGroup);
663664
List<List<String>> resolversSorted = sortResolverAttributes(this.input.model(), resolversGroup, counts);
664665
resolversFilterTree = makeResolversFilterTree(resolversSorted);
665-
resolversFilterTreeGrouped.put(numPriorityLevels - level - 1, resolversFilterTree);
666+
resolversFilterTreeGrouped.put(numWeightLevels - level - 1, resolversFilterTree);
666667
resolversClause = populateResolversFilterTree(this.input.model(), indexName, resolversFilterTree, this.attributes, this.includeExplanation, _nameIdCounter);
667668

668-
// If there are multiple levels of priority, then each higher priority group of resolvers must ensure
669-
// that every lower priority resolver either matches or does not exist.
669+
// If there are multiple levels of weight, then each lower weight group of resolvers must ensure
670+
// that every higher weight resolver either matches or does not exist.
670671
List<String> parentResolversClauses = new ArrayList<>();
671672
if (level > 0) {
672673

673-
// This is a higher priority group of resolvers.
674-
// Every lower priority resolver either must match or must not exist.
674+
// This is a lower weight group of resolvers.
675+
// Every higher weight resolver either must match or must not exist.
675676
for (int parentLevel = 0; parentLevel < level; parentLevel++) {
676-
Integer parentPriority = priorities.get(parentLevel);
677-
List<String> parentResolversGroup = resolverGroups.get(parentPriority);
677+
Integer parentWeight = weights.get(parentLevel);
678+
List<String> parentResolversGroup = resolverGroups.get(parentWeight);
678679
List<String> parentResolverClauses = new ArrayList<>();
679680
for (String parentResolverName : parentResolversGroup) {
680681

@@ -695,7 +696,7 @@ else if (size == 1)
695696
parentResolverClauses.add("{\"bool\":{\"should\":[" + attributesExistsClause + "," + parentResolverClause + "]}}");
696697
}
697698

698-
// Construct a "filter" clause for every lower priority resolver clause.
699+
// Construct a "filter" clause for every higher weight resolver clause.
699700
parentResolversClauses.add("{\"bool\":{\"filter\":[" + String.join(",", parentResolverClauses) + "]}}");
700701
}
701702
}

src/test/java/io/zentity/model/ResolverTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class ResolverTest {
1212
public void testValid() throws Exception {
1313
new Resolver("resolver_name", VALID_OBJECT);
1414
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\",\"attribute_b\"]}");
15-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":1}");
16-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":0}");
17-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":-1}");
15+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":1}");
16+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":0}");
17+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":-1}");
1818
}
1919

2020
@Test(expected = ValidationException.class)
@@ -108,36 +108,36 @@ public void testInvalidAttributesNameTypeObject() throws Exception {
108108
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\",{}]}");
109109
}
110110

111-
//// "resolvers".RESOLVER_NAME."priority" //////////////////////////////////////////////////////////////////////
111+
//// "resolvers".RESOLVER_NAME."weight" //////////////////////////////////////////////////////////////////////
112112

113113
@Test(expected = ValidationException.class)
114-
public void testInvalidPriorityTypeArray() throws Exception {
115-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":[]}");
114+
public void testInvalidWeightTypeArray() throws Exception {
115+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":[]}");
116116
}
117117

118118
@Test(expected = ValidationException.class)
119-
public void testInvalidPriorityTypeBoolean() throws Exception {
120-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":true}");
119+
public void testInvalidWeightTypeBoolean() throws Exception {
120+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":true}");
121121
}
122122

123123
@Test(expected = ValidationException.class)
124-
public void testInvalidPriorityTypeFloat() throws Exception {
125-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":1.0}");
124+
public void testInvalidWeightTypeFloat() throws Exception {
125+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":1.0}");
126126
}
127127

128128
@Test(expected = ValidationException.class)
129-
public void testInvalidPriorityTypeNull() throws Exception {
130-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":null}");
129+
public void testInvalidWeightTypeNull() throws Exception {
130+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":null}");
131131
}
132132

133133
@Test(expected = ValidationException.class)
134-
public void testInvalidPriorityTypeObject() throws Exception {
135-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":{}}");
134+
public void testInvalidWeightTypeObject() throws Exception {
135+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":{}}");
136136
}
137137

138138
@Test(expected = ValidationException.class)
139-
public void testInvalidPriorityTypeString() throws Exception {
140-
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":\"1\"}");
139+
public void testInvalidWeightTypeString() throws Exception {
140+
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":\"1\"}");
141141
}
142142

143143
}

src/test/java/io/zentity/resolution/JobIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public class JobIT extends AbstractITCase {
268268
" }\n" +
269269
"}", ContentType.APPLICATION_JSON);
270270

271-
private final StringEntity TEST_PAYLOAD_JOB_PRIORITY = new StringEntity("{\n" +
271+
private final StringEntity TEST_PAYLOAD_JOB_RESOLVER_WEIGHT= new StringEntity("{\n" +
272272
" \"attributes\": {\n" +
273273
" \"attribute_a\": [ \"a_10\" ],\n" +
274274
" \"attribute_b\": [ \"b_10\" ]\n" +
@@ -851,12 +851,12 @@ public void testJobScopeExcludeAndIncludeAttributes() throws Exception {
851851
}
852852
}
853853

854-
public void testJobPriority() throws Exception {
854+
public void testJobResolverWeight() throws Exception {
855855
prepareTestResources();
856856
try {
857857
String endpoint = "_zentity/resolution/zentity_test_entity_b";
858858
Request postResolution = new Request("POST", endpoint);
859-
postResolution.setEntity(TEST_PAYLOAD_JOB_PRIORITY);
859+
postResolution.setEntity(TEST_PAYLOAD_JOB_RESOLVER_WEIGHT);
860860
Response response = client.performRequest(postResolution);
861861
JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
862862
assertEquals(json.get("hits").get("total").asInt(), 4);

src/test/resources/TestEntityModelB.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@
2121
"attributes": [
2222
"attribute_a", "attribute_b"
2323
],
24-
"priority": 1
24+
"weight": -1
2525
},
2626
"resolver_ac": {
2727
"attributes": [
2828
"attribute_a", "attribute_c"
2929
],
30-
"priority": 1
30+
"weight": -1
3131
},
3232
"resolver_bc": {
3333
"attributes": [
3434
"attribute_b", "attribute_c"
3535
],
36-
"priority": -1
36+
"weight": 1
3737
},
3838
"resolver_cd": {
3939
"attributes": [
4040
"attribute_c", "attribute_d"
4141
],
42-
"priority": 1
42+
"weight": -1
4343
},
4444
"resolver_x": {
4545
"attributes": [

0 commit comments

Comments
 (0)