Skip to content

Commit

Permalink
Symbolic spec values of attributes with dictionary, close #225
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jul 8, 2024
1 parent 67d03c9 commit 29d3af9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package giis.tdrules.store.loader.gen;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -34,6 +36,8 @@ public class DictionaryContainer {
private String[] values;
// forbidden because some has been user specified
private Set<String> blacklist;
// symbolic spec: each value will get a value from dictionary
private Map<String, String> symspec;

private int lastIndex = -1;

Expand All @@ -47,7 +51,8 @@ public class DictionaryContainer {

public void reset() {
lastIndex = -1;
blacklist= new HashSet<>();
blacklist = new HashSet<>();
symspec = new HashMap<>();
}

public boolean hasValues() {
Expand Down Expand Up @@ -111,6 +116,7 @@ public DictionaryAttrGen with(String entityName, String attrName) {
public DictionaryAttrGen dictionary(String... values) {
currentConfiguringContainer.values = values;
currentConfiguringContainer.blacklist = new HashSet<>();
currentConfiguringContainer.symspec = new HashMap<>();
return this;
}

Expand Down Expand Up @@ -226,9 +232,22 @@ public String transformSpecValue(String entityName, String attrName, String valu

// Manage collisions between spec value and items in dictionary
int index = container.indexOf(value);
if (index >= 0) { // collision detected, the dictionary items is blackliste
if (index >= 0) { // collision detected, the dictionary items is blacklisted
container.blacklist.add(container.values[index]);
log.warn("Collision between specified value '{}' and an item in the dictionary, removing this item", value);
} else if (StringUtils.isNumeric(value)) {
// A symbolic spec, gets a new value from the dictionary and remembers if it is the first time that appears
// subsequent occurrences remember the first value
String newValue;
if (!container.symspec.containsKey(value)) {
newValue = getNewStringFromDictionary(container);
container.symspec.put(value, newValue);
log.warn("Symbolic specified value '{}' displayed as '{}' from dictionary", value, newValue);
} else {
newValue=container.symspec.get(value);
log.warn("Symbolic specified value '{}' displayed as '{}' from previous display value", value, newValue);
}
return newValue;
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* Test data generation both with and without dictionaries is in
* TestOaLocalGeneration; here generation when there are collisions between
* specified values for a string attribute and values in the dictionary is
* tested (this causes these items removed from the dictionary)
* tested (this causes these items removed from the dictionary).
* Also test symbolic specified values for attributes that have a dictionary.
*/
@RunWith(JUnitParamsRunner.class)
public class TestOaDictionaryCollisions extends Base {
Expand Down Expand Up @@ -49,6 +50,14 @@ protected DataLoader getGenerator(TdSchema model) {

"aa-1;_;_;_;_;_ , aa-1;aa;bb;cc;aa-1;bb-1, exclude collision with recycled items",
"_;_;_;aa-1;_;_ , aa;bb;cc;aa-1;aa-1;bb-1, exclude collision with recycled items",

"1;_;_;_;_;_ , aa;bb;cc;aa-1;bb-1;cc-1, simbolic spec at first index",
"_;_;1;_;_;_ , aa;bb;cc;aa-1;bb-1;cc-1, simbolic spec at last index",
"_;_;_;1;_;_ , aa;bb;cc;aa-1;bb-1;cc-1, simbolic spec at recycle position",
"1;_;2;_;_;_ , aa;bb;cc;aa-1;bb-1;cc-1, multiple simbolic spec",
"1;_;_;1;_;_ , aa;bb;cc;aa;aa-1;bb-1, repeated symbolic spec",

"bb;1;2;1;_;_ , bb;aa;cc;aa;aa-1;bb-1, Combination of everything",
})
public void testDictionaryCollisionWithSpecValue(String specs, String outs, String message) {
String[] spec = specs.split(";");
Expand Down

0 comments on commit 29d3af9

Please sign in to comment.