Skip to content

Commit

Permalink
Rename "priority" to "weight" in the resolver objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
davemoore- committed Jul 13, 2019
1 parent 5844ee9 commit 117f38f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 56 deletions.
20 changes: 10 additions & 10 deletions src/main/java/io/zentity/model/Resolver.java
Expand Up @@ -19,7 +19,7 @@ public class Resolver {

private final String name;
private Set<String> attributes = new TreeSet<>();
private int priority = 0;
private int weight = 0;

public Resolver(String name, JsonNode json) throws ValidationException {
validateName(name);
Expand All @@ -41,7 +41,7 @@ public Set<String> attributes() {
return this.attributes;
}

public int priority () { return this.priority; }
public int weight () { return this.weight; }

public void attributes(JsonNode value) throws ValidationException {
validateAttributes(value);
Expand All @@ -51,9 +51,9 @@ public void attributes(JsonNode value) throws ValidationException {
this.attributes = attributes;
}

public void priority(JsonNode value) throws ValidationException {
validatePriority(value);
this.priority = value.asInt();
public void weight(JsonNode value) throws ValidationException {
validateWeight(value);
this.weight = value.asInt();
}

private void validateName(String value) throws ValidationException {
Expand All @@ -75,9 +75,9 @@ private void validateAttributes(JsonNode value) throws ValidationException {
}
}

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

private void validateObject(JsonNode object) throws ValidationException {
Expand All @@ -96,7 +96,7 @@ private void validateObject(JsonNode object) throws ValidationException {
* ATTRIBUTE_NAME,
* ...
* ],
* "priority": INTEGER
* "weight": INTEGER
* }
* </pre>
*
Expand All @@ -121,8 +121,8 @@ public void deserialize(JsonNode json) throws ValidationException {
case "attributes":
this.attributes(value);
break;
case "priority":
this.priority(value);
case "weight":
this.weight(value);
break;
default:
throw new ValidationException("'resolvers." + this.name + "." + name + "' is not a recognized field.");
Expand Down
47 changes: 24 additions & 23 deletions src/main/java/io/zentity/resolution/Job.java
Expand Up @@ -438,19 +438,19 @@ public static Map<String, Integer> countAttributesAcrossResolvers(Model model, L
}

/**
* Group resolvers by their level of priority.
* Group resolvers by their level of weight.
*
* @param model The entity model.
* @param resolvers The names of the resolvers to reference in the entity model.
* @return For each priority level, the names of the resolvers in that priority level.
* @return For each weight level, the names of the resolvers in that weight level.
*/
public static TreeMap<Integer, List<String>> groupResolversByPriority(Model model, List<String> resolvers) {
public static TreeMap<Integer, List<String>> groupResolversByWeight(Model model, List<String> resolvers) {
TreeMap<Integer, List<String>> resolverGroups = new TreeMap<>();
for (String resolverName : resolvers) {
Integer priority = model.resolvers().get(resolverName).priority();
if (!resolverGroups.containsKey(priority))
resolverGroups.put(priority, new ArrayList<>());
resolverGroups.get(priority).add(resolverName);
Integer weight = model.resolvers().get(resolverName).weight();
if (!resolverGroups.containsKey(weight))
resolverGroups.put(weight, new ArrayList<>());
resolverGroups.get(weight).add(resolverName);
}
return resolverGroups;
}
Expand Down Expand Up @@ -650,31 +650,32 @@ else if (size == 1)
TreeMap<Integer, TreeMap<String, TreeMap>> resolversFilterTreeGrouped= new TreeMap<>(Collections.reverseOrder());
if (!this.attributes.isEmpty()) {

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

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

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

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

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

// Construct a "filter" clause for every lower priority resolver clause.
// Construct a "filter" clause for every higher weight resolver clause.
parentResolversClauses.add("{\"bool\":{\"filter\":[" + String.join(",", parentResolverClauses) + "]}}");
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/test/java/io/zentity/model/ResolverTest.java
Expand Up @@ -12,9 +12,9 @@ public class ResolverTest {
public void testValid() throws Exception {
new Resolver("resolver_name", VALID_OBJECT);
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\",\"attribute_b\"]}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":1}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":0}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"priority\":-1}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":1}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":0}");
new Resolver("resolver_name", "{\"attributes\":[\"attribute_a\"],\"weight\":-1}");
}

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

//// "resolvers".RESOLVER_NAME."priority" //////////////////////////////////////////////////////////////////////
//// "resolvers".RESOLVER_NAME."weight" //////////////////////////////////////////////////////////////////////

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

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

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

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

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

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

}
6 changes: 3 additions & 3 deletions src/test/java/io/zentity/resolution/JobIT.java
Expand Up @@ -268,7 +268,7 @@ public class JobIT extends AbstractITCase {
" }\n" +
"}", ContentType.APPLICATION_JSON);

private final StringEntity TEST_PAYLOAD_JOB_PRIORITY = new StringEntity("{\n" +
private final StringEntity TEST_PAYLOAD_JOB_RESOLVER_WEIGHT= new StringEntity("{\n" +
" \"attributes\": {\n" +
" \"attribute_a\": [ \"a_10\" ],\n" +
" \"attribute_b\": [ \"b_10\" ]\n" +
Expand Down Expand Up @@ -851,12 +851,12 @@ public void testJobScopeExcludeAndIncludeAttributes() throws Exception {
}
}

public void testJobPriority() throws Exception {
public void testJobResolverWeight() throws Exception {
prepareTestResources();
try {
String endpoint = "_zentity/resolution/zentity_test_entity_b";
Request postResolution = new Request("POST", endpoint);
postResolution.setEntity(TEST_PAYLOAD_JOB_PRIORITY);
postResolution.setEntity(TEST_PAYLOAD_JOB_RESOLVER_WEIGHT);
Response response = client.performRequest(postResolution);
JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
assertEquals(json.get("hits").get("total").asInt(), 4);
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/TestEntityModelB.json
Expand Up @@ -21,25 +21,25 @@
"attributes": [
"attribute_a", "attribute_b"
],
"priority": 1
"weight": -1
},
"resolver_ac": {
"attributes": [
"attribute_a", "attribute_c"
],
"priority": 1
"weight": -1
},
"resolver_bc": {
"attributes": [
"attribute_b", "attribute_c"
],
"priority": -1
"weight": 1
},
"resolver_cd": {
"attributes": [
"attribute_c", "attribute_d"
],
"priority": 1
"weight": -1
},
"resolver_x": {
"attributes": [
Expand Down

0 comments on commit 117f38f

Please sign in to comment.