diff --git a/build.gradle b/build.gradle index dbd7087..c42c7ad 100755 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'com.github.CST-Group' -version '0.1.7' +version '0.1.8' sourceCompatibility = 1.8 targetCompatibility = 1.8 @@ -59,7 +59,7 @@ publishing { gpr(MavenPublication) { groupId = 'com.github.CST-Group' artifactId = 'ctm' - version = '0.1.7' + version = '0.1.8' from(components.java) } diff --git a/src/main/java/br/unicamp/ctm/representation/converter/ValueConverter.java b/src/main/java/br/unicamp/ctm/representation/converter/ValueConverter.java index fa4f71c..6dde71c 100644 --- a/src/main/java/br/unicamp/ctm/representation/converter/ValueConverter.java +++ b/src/main/java/br/unicamp/ctm/representation/converter/ValueConverter.java @@ -120,4 +120,45 @@ public List convertToList(Object object) { return list; } + public List convertNumberToBaseTen(double value) { + + List numberBaseTen = new ArrayList<>(); + + value = Math.abs(value); + + int base = 0; + double valueDivided = value; + + while(true) { + + if(value == 0 || value == 1) { + numberBaseTen.add(value); + numberBaseTen.add(0d); + + return numberBaseTen; + } + if(value > 1) { + if(valueDivided >= 1) { + valueDivided/=10; + base++; + } else { + numberBaseTen.add(valueDivided*10); + numberBaseTen.add((double) (base-1)); + + return numberBaseTen; + } + } else if(value < 1) { + if(valueDivided <= 1) { + valueDivided*=10; + base--; + } else { + numberBaseTen.add(valueDivided/10); + numberBaseTen.add((double) (base+1)); + + return numberBaseTen; + } + } + } + } + } diff --git a/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaMetadataValues.java b/src/main/java/br/unicamp/ctm/representation/idea/IdeaMetadataValues.java similarity index 96% rename from src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaMetadataValues.java rename to src/main/java/br/unicamp/ctm/representation/idea/IdeaMetadataValues.java index 7949fb4..c680520 100644 --- a/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaMetadataValues.java +++ b/src/main/java/br/unicamp/ctm/representation/idea/IdeaMetadataValues.java @@ -4,7 +4,7 @@ import java.util.Map; import scala.Char; -public class MatrixIdeaMetadataValues { +public class IdeaMetadataValues { public static Map getMetadataMap() { diff --git a/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaDeserializer.java b/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaDeserializer.java index 5b14d3d..41e4560 100644 --- a/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaDeserializer.java +++ b/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaDeserializer.java @@ -82,7 +82,7 @@ private void setType(Idea idea, T[][] matrix, int i) { } private void setValue(Idea idea, T[][] matrix, Map dictionary, int i) { - Optional> entryOptional = MatrixIdeaMetadataValues.getMetadataMap() + Optional> entryOptional = IdeaMetadataValues.getMetadataMap() .entrySet().stream().filter(entry -> entry.getValue() == matrix[i][matrix.length + 2]) .findFirst(); diff --git a/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaSerializer.java b/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaSerializer.java index fffe98f..2c1bdf9 100644 --- a/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaSerializer.java +++ b/src/main/java/br/unicamp/ctm/representation/idea/MatrixIdeaSerializer.java @@ -6,7 +6,6 @@ import br.unicamp.ctm.representation.validation.ValueValidation; import java.lang.reflect.Array; import java.util.HashMap; -import java.util.List; import java.util.Map; public class MatrixIdeaSerializer { @@ -19,7 +18,7 @@ public class MatrixIdeaSerializer { private T defaultValue; private T activeValue; - public MatrixIdeaSerializer(Class clazz, int rows, int columns, int size) { + public MatrixIdeaSerializer(Class clazz, int rows, int columns, int size) { this.size = size; this.rows = rows; this.columns = columns; @@ -109,7 +108,7 @@ private void setNameValue(Idea idea, MatrixIdea matrixIdea, T[][] matrix, int private void setMetadataValue(Idea idea, T[][] matrix, T defaultValue, int i) { if (idea.getValue() != null) { - Integer metadataValue = MatrixIdeaMetadataValues.getMetadataMap() + Integer metadataValue = IdeaMetadataValues.getMetadataMap() .get(idea.getValue().getClass()); setValue(matrix, i, columns + 2, castToGeneric(metadataValue != null ? metadataValue : defaultValue), false); diff --git a/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaBuilder.java b/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaBuilder.java new file mode 100644 index 0000000..20caa50 --- /dev/null +++ b/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaBuilder.java @@ -0,0 +1,30 @@ +package br.unicamp.ctm.representation.idea; + +import br.unicamp.ctm.representation.model.SDRIdea; + +public class SDRIdeaBuilder { + public SDRIdea build(int channels, int rows, int columns, int defaultValue, int activeValue) { + SDRIdea sdrIdea = new SDRIdea(channels, rows, columns); + + initializeMatrix(sdrIdea, channels, rows, columns, defaultValue); + + sdrIdea.setDefaultValue(defaultValue); + sdrIdea.setActiveValue(activeValue); + + return sdrIdea; + } + + private int[][][] initializeMatrix(SDRIdea sdrIdea, int channels, int rows, int columns, int defaultValue) { + + for (int k = 0; k < channels; k++) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + sdrIdea.getSdr()[k][i][j] = defaultValue; + } + } + } + + return sdrIdea.getSdr(); + } + +} diff --git a/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaDeserializer.java b/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaDeserializer.java new file mode 100644 index 0000000..380c1b3 --- /dev/null +++ b/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaDeserializer.java @@ -0,0 +1,234 @@ +package br.unicamp.ctm.representation.idea; + +import br.unicamp.ctm.representation.converter.ValueConverter; +import br.unicamp.ctm.representation.model.SDRIdea; +import br.unicamp.ctm.representation.validation.ValueValidation; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.stream.Collectors; + +public class SDRIdeaDeserializer { + + private ValueValidation valueValidation; + private Map dictionary; + private Map values; + + public SDRIdeaDeserializer(Map dictionary, Map values) { + this.valueValidation = new ValueValidation(); + this.setDictionary(dictionary); + this.setValues(values); + } + + public Idea deserialize(SDRIdea sdrIdea) + throws ClassNotFoundException, InstantiationException, IllegalAccessException { + + List ideaList = new ArrayList<>(); + + generateIdeaGraph(sdrIdea, ideaList); + + return ideaList.size() > 0 ? ideaList.stream().findFirst().get() : null; + } + + private void generateIdeaGraph(SDRIdea sdrIdea, List ideaList) { + + int[][][] sdr = sdrIdea.getSdr(); + + Map ideaRelationship = new HashMap<>(); + + for (int i = 0; i < sdr.length; i++) { + + int[][] sdrChannel = sdr[i]; + + Integer parentId = null; + if (i != 0) + parentId = (int) extractValue(sdrChannel, 0, Integer.class); + + int id = (int) extractValue(sdrChannel, 2, Integer.class); + String name = extractString(sdrChannel, 4); + int type = (int) extractValue(sdrChannel, 5, Integer.class); + int metadata = (int) extractValue(sdrChannel, 7, Integer.class); + int length = (int) extractValue(sdrChannel, 9, Integer.class); + + Idea idea = new Idea(name, null, type); + idea.setId(id); + setValue(idea, sdrChannel, metadata, length); + idea.setType(type); + + if(parentId != null) + ideaRelationship.put(id, parentId); + + ideaList.add(idea); + } + + for (int i = 0; i < ideaList.size(); i++) { + Idea idea = ideaList.get(i); + + List> relations = ideaRelationship.entrySet().stream() + .filter(entry -> entry.getValue() == idea.getId()).collect(Collectors.toList()); + + for (Entry relation : relations) { + ideaList.stream().filter(ideaFilter -> ideaFilter.getId() == relation.getKey()).findFirst() + .ifPresent(childIdea -> idea.getL().add(childIdea)); + } + } + + } + + private void setValue(Idea idea, int[][] sdrChannel, int metadata, int length) { + + Optional> entryOptional = IdeaMetadataValues.getMetadataMap() + .entrySet().stream().filter(entry -> entry.getValue() == metadata) + .findFirst(); + + if (entryOptional.isPresent()) { + Class clazz = entryOptional.get().getKey(); + + if (ValueValidation.isArray(clazz)) { + setArrayValue(idea, sdrChannel, length, clazz); + } else if (ValueValidation.isPrimitive(clazz)) { + setPrimitiveValue(idea, sdrChannel, 11, clazz); + } else if (ValueValidation.isString(clazz)) { + idea.setValue(extractString(sdrChannel, 11)); + } + } + } + + private void setArrayValue(Idea idea, int[][] sdrChannel, int length, Class clazz) { + + if (clazz.getCanonicalName().equals(double[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Double[].class.getCanonicalName())) { + idea.setValue(new double[length]); + } else if (clazz.getCanonicalName().equals(int[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Integer[].class.getCanonicalName())) { + idea.setValue(new int[length]); + } else if (clazz.getCanonicalName().equals(float[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Float[].class.getCanonicalName())) { + idea.setValue(new float[length]); + } else if (clazz.getCanonicalName().equals(short[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Short[].class.getCanonicalName())) { + idea.setValue(new short[length]); + } else if (clazz.getCanonicalName().equals(boolean[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Boolean[].class.getCanonicalName())) { + idea.setValue(new boolean[length]); + } else if (clazz.getCanonicalName().equals(long[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Long[].class.getCanonicalName())) { + idea.setValue(new long[length]); + } + + for (int i = 0; i < length; i++) { + if (clazz.getCanonicalName().equals(double[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Double[].class.getCanonicalName())) { + ((double[])idea.getValue())[i] = (double) extractValue(sdrChannel, 11+i*2, Double.class); + } else if (clazz.getCanonicalName().equals(int[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Integer[].class.getCanonicalName())) { + ((int[])idea.getValue())[i] = (int) extractValue(sdrChannel, 11+i*2, Integer.class); + } else if (clazz.getCanonicalName().equals(float[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Float[].class.getCanonicalName())) { + ((float[])idea.getValue())[i] = (float) extractValue(sdrChannel, 11+i*2, Float.class); + } else if (clazz.getCanonicalName().equals(short[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Short[].class.getCanonicalName())) { + ((short[])idea.getValue())[i] = (short) extractValue(sdrChannel, 11+i*2, Short.class); + } else if (clazz.getCanonicalName().equals(boolean[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Boolean[].class.getCanonicalName())) { + ((boolean[])idea.getValue())[i] = Boolean.parseBoolean(extractString(sdrChannel, 11+i*2)); + } else if (clazz.getCanonicalName().equals(long[].class.getCanonicalName()) || clazz.getCanonicalName().equals(Long[].class.getCanonicalName())) { + ((long[])idea.getValue())[i] = (long) extractValue(sdrChannel, 11+i*2, Long.class); + } + } + + } + + private void setPrimitiveValue(Idea idea, int[][] sdrChannel, int row, Class clazz) { + if (clazz.getCanonicalName().equals(int.class.getCanonicalName())) { + idea.setValue(extractValue(sdrChannel, row, Integer.class)); + } else if (clazz.getCanonicalName().equals(long.class.getCanonicalName())) { + idea.setValue(extractValue(sdrChannel, row, Long.class)); + } else if (clazz.getCanonicalName().equals(double.class.getCanonicalName())) { + idea.setValue(extractValue(sdrChannel, row, Double.class)); + } else if (clazz.getCanonicalName().equals(float.class.getCanonicalName())) { + idea.setValue(extractValue(sdrChannel, row, Float.class)); + } else if (clazz.getCanonicalName().equals(boolean.class.getCanonicalName())) { + idea.setValue(Boolean.valueOf(extractString(sdrChannel, 11))); + } else if (clazz.getCanonicalName().equals(short.class.getCanonicalName())) { + idea.setValue(extractValue(sdrChannel, row, Short.class)); + } else if (clazz.getCanonicalName().equals(byte.class.getCanonicalName())) { + idea.setValue(extractValue(sdrChannel, row, Byte.class)); + } + } + + private String extractString(int[][] sdrChannel, int row) { + Optional> wordOptional = getDictionary().entrySet().stream() + .filter(entry -> valueValidation.compareValue(entry.getValue(), sdrChannel[row])).findFirst(); + + return wordOptional.isPresent()? wordOptional.get().getKey() : ""; + } + + private Object extractValue(int[][] sdrChannel, int row, Class clazz) { + + int length = sdrChannel[row].length; + int range = length/4; + + String valueString = ""; + for (int i = 0; i < 4; i++) { + + int[] valueSDR = buildSDR(range, sdrChannel[row], i); + + Optional> valueOptional = getValues().entrySet().stream() + .filter(entry -> valueValidation.compareValue(entry.getValue(), valueSDR)).findFirst(); + + if(valueOptional.isPresent()) { + valueString+=valueOptional.get().getKey(); + } + + if(i==0) + valueString+="."; + } + + int[] baseSDR = buildSDR(range, sdrChannel[row+1], 0); + + Optional> baseOptional = getValues().entrySet().stream() + .filter(entry -> valueValidation.compareValue(entry.getValue(), baseSDR)).findFirst(); + + int base = 0; + + if(baseOptional.isPresent()) { + base = baseOptional.get().getKey(); + } + + int signal = sdrChannel[row+1][range] * -1 == 0? 1:-1; + + Number number = Double.parseDouble(valueString) * Math.pow(10, base) * signal; + + if(clazz == Integer.class) + return number.intValue(); + else if(clazz == Float.class) + return number.floatValue(); + else if(clazz == Short.class) + return number.shortValue(); + else if(clazz == Byte.class) + return number.byteValue(); + else if(clazz == Double.class) + return number.doubleValue(); + else if(clazz == Long.class); + return number.longValue(); + } + + private int[] buildSDR(int range, int[] sdrRow, int interval) { + int[] sdr = new int[range]; + + for (int i = 0; i < range; i++) { + sdr[i] = sdrRow[interval*range+i]; + } + + return sdr; + } + + public Map getDictionary() { + return dictionary; + } + + public void setDictionary(Map dictionary) { + this.dictionary = dictionary; + } + + public Map getValues() { + return values; + } + + public void setValues(Map values) { + this.values = values; + } +} diff --git a/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaSerializer.java b/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaSerializer.java new file mode 100644 index 0000000..897085f --- /dev/null +++ b/src/main/java/br/unicamp/ctm/representation/idea/SDRIdeaSerializer.java @@ -0,0 +1,317 @@ +package br.unicamp.ctm.representation.idea; + +import br.unicamp.ctm.representation.converter.ValueConverter; +import br.unicamp.ctm.representation.model.SDRIdea; +import br.unicamp.ctm.representation.validation.ValueValidation; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.stream.Collectors; + +public class SDRIdeaSerializer { + + private int channels; + private int rows; + private int columns; + private int defaultValue; + private int activeValue; + private ValueConverter valueConverter; + private Map dictionary; + private Map values; + private int channelCounter = 1; + + public SDRIdeaSerializer(int channels, int rows, int columns) { + this.rows = rows; + this.columns = columns; + this.channels = channels; + this.setDefaultValue(0); + this.setActiveValue(1); + this.valueConverter = new ValueConverter<>(); + this.setDictionary(new HashMap<>()); + this.setValues(new HashMap<>()); + } + + public SDRIdeaSerializer(int channels, int rows, int columns, int defaultValue, int activeValue) { + this.rows = rows; + this.columns = columns; + this.channels = channels; + this.setDefaultValue(defaultValue); + this.setActiveValue(activeValue); + this.valueConverter = new ValueConverter<>(); + this.setDictionary(new HashMap<>()); + this.setValues(new HashMap<>()); + } + + public SDRIdeaSerializer(int channels, int rows, int columns, int defaultValue, int activeValue, Map dictionary, Map values) { + this.rows = rows; + this.columns = columns; + this.channels = channels; + this.setDefaultValue(defaultValue); + this.setActiveValue(activeValue); + this.valueConverter = new ValueConverter<>(); + this.setDictionary(dictionary); + this.setValues(values); + } + + + + public SDRIdea serialize(Idea idea) throws Exception { + + if(idea != null) { + SDRIdea sdrIdea = new SDRIdeaBuilder().build(channels, rows, columns, getDefaultValue(), + getActiveValue()); + + setIdValue(idea, sdrIdea, sdrIdea.getSdr(), 0); + setNameValue(idea, sdrIdea, sdrIdea.getSdr(), 0); + setTypeValue(idea, sdrIdea, sdrIdea.getSdr(), 0); + setMetadataValue(idea, sdrIdea, sdrIdea.getSdr(), 0); + valueAnalyse(idea, sdrIdea, sdrIdea.getSdr(), 0); + + generateSDR(sdrIdea, idea); + + return sdrIdea; + + } else { + throw new Exception("Idea Graph is null."); + } + } + + private void setParentValue(Idea idea, SDRIdea sdrIdea, int[][][] sdr, int channel) { + setNumericValue(sdrIdea, sdr, channel, 0, columns, (int)idea.getId()); + } + + private void setIdValue(Idea idea, SDRIdea sdrIdea, int[][][] sdr, int channel) { + setNumericValue(sdrIdea, sdr, channel, 2, columns, (int)idea.getId()); + } + + private void valueAnalyse(Idea idea, SDRIdea sdrIdea, int[][][] sdr, int channel) { + if (ValueValidation.isArray(idea.getValue())) { + List values = valueConverter.convertToList(idea.getValue()); + for (int i = 0; i < values.size(); i++) { + setNumericValue(sdrIdea, sdr, channel, 11+i*2, columns, (Number)values.get(i)); + } + } else { + if (ValueValidation.isPrimitive(idea.getValue())) { + if(idea.getValue().getClass().equals(Boolean.class)) { + setValue(sdr, channel, 11, getArrayFromDictionary((String) idea.getValue())); + } else { + setNumericValue(sdrIdea, sdr, channel, 11, columns, (Number)idea.getValue()); + } + } else if (ValueValidation.isString(idea.getValue())) { + if (idea.getValue() != null) { + setValue(sdr, channel, 11, getArrayFromDictionary((String) idea.getValue())); + } + } + } + } + + public void generateSDR(SDRIdea sdrIdea, Idea idea) { + + for (Idea childIdea : idea.getL()) { + + setParentValue(idea, sdrIdea, sdrIdea.getSdr(), channelCounter); + setIdValue(childIdea, sdrIdea, sdrIdea.getSdr(), channelCounter); + setNameValue(childIdea, sdrIdea, sdrIdea.getSdr(), channelCounter); + setTypeValue(childIdea, sdrIdea, sdrIdea.getSdr(), channelCounter); + setMetadataValue(childIdea, sdrIdea, sdrIdea.getSdr(), channelCounter); + valueAnalyse(childIdea, sdrIdea, sdrIdea.getSdr(), channelCounter); + + channelCounter++; + generateSDR(sdrIdea, childIdea); + } + } + + private void setMetadataValue(Idea idea, SDRIdea sdrIdea, int[][][] sdr, int channel) { + if (idea.getValue() != null) { + Integer metadataValue = IdeaMetadataValues.getMetadataMap() + .get(idea.getValue().getClass()); + + setNumericValue(sdrIdea, sdr, channel, 7, columns, metadataValue); + + int length = 0; + + if (ValueValidation.isArray(idea.getValue())) { + List values = valueConverter.convertToList(idea.getValue()); + length = values.size(); + } + + setNumericValue(sdrIdea, sdr, channel, 9, columns, length); + } + } + + private void setNameValue(Idea idea, SDRIdea sdrIdea, int[][][] sdr, int channel) { + if (idea.getName() != null && getArrayFromDictionary(idea.getName()) != null) { + setValue(sdr, channel, 4, getArrayFromDictionary(idea.getName())); + } + } + + private void setTypeValue(Idea idea, SDRIdea sdrIdea, int[][][] sdr, int channel) { + setNumericValue(sdrIdea, sdr, channel, 5, columns, (int)idea.getType()); + } + + private void setValue(int[][][] sdr, int channel, int row, int[] value) { + sdr[channel][row] = value; + } + + private void setNumericValue(SDRIdea sdrIdea, int[][][] sdr, int channel, int row, int length, Number value) { + int range = length/4; + + List baseTenValue = valueConverter.convertNumberToBaseTen(value.doubleValue()); + + String valueString = String.valueOf(baseTenValue.get(0)); + valueString = valueString.replace(".", ""); + valueString = valueString.replace("-", ""); + + for (int i = 0; i < Math.min(valueString.length(), 4); i++) { + int valueInt = Integer.parseInt(String.valueOf(valueString.charAt(i))); + int[] valueSDR = getArrayFromValues(valueInt, range); + + for (int j = 0; j < valueSDR.length; j++) { + sdr[channel][row][i*range+j] = valueSDR[j]; + } + } + + int base = baseTenValue.get(1).intValue(); + int[] baseSDR = getArrayFromValues(base, range); + + for (int i = 0; i < baseSDR.length; i++) { + sdr[channel][row+1][i] = baseSDR[i]; + } + + if(value.doubleValue() < 0) { + sdr[channel][row+1][baseSDR.length] = 1; + } else { + sdr[channel][row+1][baseSDR.length] = 0; + } + + } + + public int[] getArrayFromDictionary(String word) { + if (getDictionary().containsKey(word)) { + return getDictionary().get(word); + } else { + int[] value = generateContent(columns, false, getDictionary(), new HashMap<>()); + getDictionary().put(word, value); + return value; + } + } + + + + public int[] getArrayFromValues(Integer value, Integer length) { + if (getValues().containsKey(value)) { + return getValues().get(value); + } else { + int[] arrayValue = generateContent(length, true, new HashMap<>(), getValues()); + getValues().put(value, arrayValue); + return arrayValue; + } + } + + private int[] generateContent(int length, boolean isValue, Map dictionary, Map values) { + + boolean retry = true; + + while(retry) { + int[] value = generateValue(length); + + retry = isValue? values.entrySet().stream() + .filter(entry -> entry.getValue().length == length) + .filter(entry -> compareValue(entry.getValue(), value)) + .collect(Collectors.toList()).size() > 0 : + + dictionary.entrySet().stream() + .filter(entry -> entry.getValue().length == length) + .filter(entry -> compareValue(entry.getValue(), value)) + .collect(Collectors.toList()).size() > 0; + + if(!retry) { + return value; + } + } + + return initializeValue(new int[length], getDefaultValue()); + } + + + private int[] generateValue(int length) { + + int w = length/2; + Random random = new Random(); + + int[] value = new int[length]; + + + for (int i = 0; i < w; i++) { + boolean retry = true; + + while (retry) { + int index = random.nextInt(length); + if(value[index] != 1) { + value[index] = getActiveValue(); + retry = false; + } + } + } + + return value; + } + + + private int[] initializeValue(int[] value, int defaultValue) { + for (int i = 0; i < value.length; i++) { + value[i] = defaultValue; + } + return value; + } + + private boolean compareValue(int[] newValue, int[] value) { + + if(newValue.length == value.length) { + + for (int i = 0; i < newValue.length; i++) { + if(newValue[i] != value[i]) { + return false; + } + } + + return true; + } + + return false; + } + + + public int getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(int defaultValue) { + this.defaultValue = defaultValue; + } + + public int getActiveValue() { + return activeValue; + } + + public void setActiveValue(int activeValue) { + this.activeValue = activeValue; + } + + public Map getDictionary() { + return dictionary; + } + + public void setDictionary(Map dictionary) { + this.dictionary = dictionary; + } + + public Map getValues() { + return values; + } + + public void setValues(Map values) { + this.values = values; + } +} diff --git a/src/main/java/br/unicamp/ctm/representation/model/SDRIdea.java b/src/main/java/br/unicamp/ctm/representation/model/SDRIdea.java new file mode 100644 index 0000000..a0fdcd4 --- /dev/null +++ b/src/main/java/br/unicamp/ctm/representation/model/SDRIdea.java @@ -0,0 +1,51 @@ +package br.unicamp.ctm.representation.model; + +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.stream.Collectors; +import scala.Int; + +public class SDRIdea { + + private int[][][] sdr; + private int defaultValue; + private int activeValue; + + public SDRIdea(int channels, int rows, int columns) { + this.setSdr(new int[channels][rows][columns]); + this.setDefaultValue(0); + this.setActiveValue(1); + } + + public int[][][] getSdr() { + return sdr; + } + + public void setSdr(int[][][] sdr) { + this.sdr = sdr; + } + + public int getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(int defaultValue) { + this.defaultValue = defaultValue; + } + + + + + + + + public int getActiveValue() { + return activeValue; + } + + public void setActiveValue(int activeValue) { + this.activeValue = activeValue; + } + +} diff --git a/src/main/java/br/unicamp/ctm/representation/validation/ValueValidation.java b/src/main/java/br/unicamp/ctm/representation/validation/ValueValidation.java index f6d475a..7cf00c3 100644 --- a/src/main/java/br/unicamp/ctm/representation/validation/ValueValidation.java +++ b/src/main/java/br/unicamp/ctm/representation/validation/ValueValidation.java @@ -18,7 +18,13 @@ public static boolean isArray(Class clazz) { || clazz.getCanonicalName().equals(long[].class.getCanonicalName()) || clazz.getCanonicalName().equals(float[].class.getCanonicalName()) || clazz.getCanonicalName().equals(short[].class.getCanonicalName()) - || clazz.getCanonicalName().equals(boolean[].class.getCanonicalName()); + || clazz.getCanonicalName().equals(boolean[].class.getCanonicalName()) + || clazz.getCanonicalName().equals(Integer[].class.getCanonicalName()) + || clazz.getCanonicalName().equals(Double[].class.getCanonicalName()) + || clazz.getCanonicalName().equals(Long[].class.getCanonicalName()) + || clazz.getCanonicalName().equals(Float[].class.getCanonicalName()) + || clazz.getCanonicalName().equals(Short[].class.getCanonicalName()) + || clazz.getCanonicalName().equals(Boolean[].class.getCanonicalName()); } public static boolean isPrimitive(Object object) { @@ -68,4 +74,20 @@ public static boolean isString(Class clazz) { return clazz.getCanonicalName().equals(String.class.getCanonicalName()); } + public boolean compareValue(int[] newValue, int[] value) { + + if(newValue.length == value.length) { + + for (int i = 0; i < newValue.length; i++) { + if(newValue[i] != value[i]) { + return false; + } + } + + return true; + } + + return false; + } + } diff --git a/src/test/java/br/unicamp/ctm/representation/idea/SDRIdeaSerializerTest.java b/src/test/java/br/unicamp/ctm/representation/idea/SDRIdeaSerializerTest.java new file mode 100644 index 0000000..3c3848c --- /dev/null +++ b/src/test/java/br/unicamp/ctm/representation/idea/SDRIdeaSerializerTest.java @@ -0,0 +1,42 @@ +package br.unicamp.ctm.representation.idea; + +import br.unicamp.ctm.representation.model.SDRIdea; +import org.junit.Before; +import org.junit.Test; + +public class SDRIdeaSerializerTest { + + private SDRIdeaSerializer sdrIdeaSerializer; + private SDRIdeaDeserializer sdrIdeaDeserializer; + + @Before + public void setup() { + + sdrIdeaSerializer = new SDRIdeaSerializer(10, 32, 32); + sdrIdeaDeserializer = new SDRIdeaDeserializer(sdrIdeaSerializer.getDictionary(), sdrIdeaSerializer.getValues()); + } + + private Idea initialize() { + Idea idea = new Idea("Rock Music", "Hey ho let's go!", 0); + + idea.add(new Idea("Metallica", "Black Album", 0)).add(new Idea("Unforgiven", 3.14, 1)) + .add(new Idea("Enter Sadman", "Seek and destroy")); + idea.add(new Idea("Foo Fighters", "The sky's the neighborhood", 0)) + .add(new Idea("Pretender", new long[]{256})); + idea.add(new Idea("Black Sabbath", new double[]{3.41, 2.22, 0.23}, 1)) + .add(new Idea("Paranoid", new short[]{34, 18, 10})); + idea.add(new Idea("Gun's in Roses", "Sweet child o' mine", 2)) + .add(new Idea("November Rain", new float[]{1.2f, 2f, -18f, 5.2f, -1f, 0f, 1000f})); + + return idea; + } + + @Test + public void testDeserializer() throws Exception { + Idea idea = initialize(); + + SDRIdea sdrIdea = sdrIdeaSerializer.serialize(idea); + Idea convertedIdea = sdrIdeaDeserializer.deserialize(sdrIdea); + } + +}