Skip to content

Commit

Permalink
Added SDR serializer and deserializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardofroes committed Aug 13, 2022
1 parent 26c9bb4 commit de2cff6
Show file tree
Hide file tree
Showing 11 changed files with 744 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.github.CST-Group'
version '0.1.7'
version '0.1.8'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,45 @@ public List convertToList(Object object) {
return list;
}

public List<Double> convertNumberToBaseTen(double value) {

List<Double> 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;
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Map;
import scala.Char;

public class MatrixIdeaMetadataValues {
public class IdeaMetadataValues {

public static Map<Class, Integer> getMetadataMap() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void setType(Idea idea, T[][] matrix, int i) {
}

private void setValue(Idea idea, T[][] matrix, Map<String, Integer> dictionary, int i) {
Optional<Entry<Class, Integer>> entryOptional = MatrixIdeaMetadataValues.getMetadataMap()
Optional<Entry<Class, Integer>> entryOptional = IdeaMetadataValues.getMetadataMap()
.entrySet().stream().filter(entry -> entry.getValue() == matrix[i][matrix.length + 2])
.findFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand All @@ -19,7 +18,7 @@ public class MatrixIdeaSerializer<T> {
private T defaultValue;
private T activeValue;

public MatrixIdeaSerializer(Class<T> clazz, int rows, int columns, int size) {
public MatrixIdeaSerializer(Class<T> clazz, int rows, int columns, int size) {
this.size = size;
this.rows = rows;
this.columns = columns;
Expand Down Expand Up @@ -109,7 +108,7 @@ private void setNameValue(Idea idea, MatrixIdea<T> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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<String, int[]> dictionary;
private Map<Integer, int[]> values;

public SDRIdeaDeserializer(Map<String, int[]> dictionary, Map<Integer, int[]> values) {
this.valueValidation = new ValueValidation();
this.setDictionary(dictionary);
this.setValues(values);
}

public Idea deserialize(SDRIdea sdrIdea)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {

List<Idea> ideaList = new ArrayList<>();

generateIdeaGraph(sdrIdea, ideaList);

return ideaList.size() > 0 ? ideaList.stream().findFirst().get() : null;
}

private void generateIdeaGraph(SDRIdea sdrIdea, List<Idea> ideaList) {

int[][][] sdr = sdrIdea.getSdr();

Map<Integer, Integer> 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<Entry<Integer, Integer>> relations = ideaRelationship.entrySet().stream()
.filter(entry -> entry.getValue() == idea.getId()).collect(Collectors.toList());

for (Entry<Integer, Integer> 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<Entry<Class, Integer>> 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<Entry<String, int[]>> 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<Entry<Integer, int[]>> 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<Entry<Integer, int[]>> 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<String, int[]> getDictionary() {
return dictionary;
}

public void setDictionary(Map<String, int[]> dictionary) {
this.dictionary = dictionary;
}

public Map<Integer, int[]> getValues() {
return values;
}

public void setValues(Map<Integer, int[]> values) {
this.values = values;
}
}
Loading

0 comments on commit de2cff6

Please sign in to comment.