Skip to content

Commit

Permalink
Handle collisions between spec values and dictionary 225
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jul 7, 2024
1 parent d7b2e9e commit 9532cfc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package giis.tdrules.store.loader.gen;

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

Expand Down Expand Up @@ -30,6 +32,9 @@ public class DictionaryAttrGen extends DeterministicAttrGen {
//Stores all configurations for a given coordinate
public class DictionaryContainer {
private String[] values;
// forbidden because some has been user specified
private Set<String> blacklist;

private int lastIndex = -1;

private String mask = "";
Expand All @@ -42,6 +47,7 @@ public class DictionaryContainer {

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

public boolean hasValues() {
Expand Down Expand Up @@ -72,15 +78,6 @@ private int indexOf(String value) {
return -1;
}

private String[] remove(int index) {
if (index < 0)
return this.values;
String[] copy = new String[this.values.length - 1];
System.arraycopy(this.values, 0, copy, 0, index);
System.arraycopy(this.values, index + 1, copy, index, copy.length - index);
return copy;
}

@Override
public String toString() {
return Arrays.asList(values).toString();
Expand Down Expand Up @@ -113,6 +110,7 @@ public DictionaryAttrGen with(String entityName, String attrName) {
*/
public DictionaryAttrGen dictionary(String... values) {
currentConfiguringContainer.values = values;
currentConfiguringContainer.blacklist = new HashSet<String>();
return this;
}

Expand Down Expand Up @@ -157,8 +155,10 @@ private String getNewStringFromDictionary(DictionaryContainer container) {
if (!container.hasValues())
return null;
container.lastIndex++;
skipBlacklist(container);
return getStringFromDictionary(container, container.lastIndex);
}

private String getStringFromDictionary(DictionaryContainer container, int index) {
// if all strings have been generated, recycles the dictionary
int actualIndex = index % container.values.length;
Expand All @@ -170,6 +170,14 @@ private String getStringFromDictionary(DictionaryContainer container, int index)
return value;
}

private void skipBlacklist(DictionaryContainer container) {
for (int i=container.lastIndex; i<container.values.length; i++)
if (container.blacklist.contains(container.values[i]))
container.lastIndex++;
else
return;
}

private String getKey(String entityName, String attrName) {
return entityName.toLowerCase() + "." + attrName.toLowerCase();
}
Expand Down Expand Up @@ -218,8 +226,8 @@ 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 removed
container.values = container.remove(index);
if (index >= 0) { // collision detected, the dictionary items is blackliste
container.blacklist.add(container.values[index]);
log.warn("Collision between specified value '{}' and an item in the dictionary, removing this item", value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ protected DataLoader getGenerator(TdSchema model) {

@Test
@Parameters({ "_;xx;_;_;_;_ , aa;xx;bb;cc;aa-1;bb-1, no collision",
"bb;_;_;_;_;_ , bb;aa;cc;aa-1;cc-1;aa-2, spec before collision index",
"_;bb;_;_;_;_ , aa;bb;cc;aa-1;cc-1;aa-2, spec at collision index",
"_;aa;_;_;_;_ , aa;aa;cc;bb-1;cc-1;bb-2, spec after collision index (can't avoid)",
"_;_;_;aa;_;_ , aa;bb;cc;aa;cc-1;bb-2, spec after collision index at recycle position",
"bb;_;_;_;_;_ , bb;aa;cc;aa-1;bb-1;cc-1, spec before collision index",
"_;bb;_;_;_;_ , aa;bb;cc;aa-1;bb-1;cc-1, spec at collision index",
"_;aa;_;_;_;_ , aa;aa;bb;cc;aa-1;bb-1, spec after collision index (can't avoid)",
"_;_;_;aa;_;_ , aa;bb;cc;aa;aa-1;bb-1, spec after collision index at recycle position",

"bb;_;bb;_;_;_ , bb;aa;bb;cc;aa-1;cc-1, multiple spec before collision index",
"_;bb;_;bb;_;_ , aa;bb;cc;bb;aa-1;cc-1, multiple spec at collision index",
"bb;_;bb;_;_;_ , bb;aa;bb;cc;aa-1;bb-1, multiple spec before collision index",
"_;bb;_;bb;_;_ , aa;bb;cc;bb;aa-1;bb-1, multiple spec at collision index",

"aa;_;_;_;_;_ , aa;bb;cc;bb-1;cc-1;bb-2, collision at first index",
"_;_;cc;_;_;_ , aa;bb;cc;aa-1;bb-1;aa-2, collision at last index",
"aa;_;_;_;_;_ , aa;bb;cc;aa-1;bb-1;cc-1, collision at first index",
"_;_;cc;_;_;_ , aa;bb;cc;aa-1;bb-1;cc-1, collision at last index",

"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",
Expand Down

0 comments on commit 9532cfc

Please sign in to comment.