Skip to content

Commit

Permalink
Cleansing commit
Browse files Browse the repository at this point in the history
- Removes 250++ warnings (emitted by Java and the annotation processor)
- Removes an annoying false positive error emitted by the annotation
processor regarding ISkill / IAgent subclasses.
- Adds a simple COUNTER class based on AtomicInteger to the "developer
tools" and bases all computation of unique indices
(SymbolDescription.index, etc.) on it
  • Loading branch information
AlexisDrogoul committed Jan 29, 2022
1 parent d36560e commit f4eece2
Show file tree
Hide file tree
Showing 203 changed files with 32,466 additions and 20,399 deletions.
@@ -1,3 +1,13 @@
/*******************************************************************************************************
*
* GenStarGamaSurveyUtils.java, in espacedev.gaml.extensions.genstar, is part of the source code of the GAMA modeling
* and simulation platform (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package espacedev.gaml.extensions.genstar.utils;

import java.io.FileNotFoundException;
Expand All @@ -18,173 +28,217 @@
import msi.gama.util.file.csv.CsvReader.Stats;
import msi.gaml.types.IType;
import msi.gaml.types.Types;
import ummisco.gama.dev.utils.DEBUG;

/**
* The Class GenStarGamaSurveyUtils.
*/
/*
* Encapsulation (more a copy/past) of CsvReader utilities to explore csv files
* Encapsulation (more a copy/past) of CsvReader utilities to explore csv files
*/
public class GenStarGamaSurveyUtils {

public GenStarGamaSurveyUtils(IScope scope, GamaCSVFile survey, List<Attribute<? extends IValue>> atts) throws FileNotFoundException {
this.path = Paths.get(FileUtils.constructAbsoluteFilePath(scope, survey.getPath(scope), false));
this.stats = CsvReader.getStats(this.path.toString(),null);
/**
* Instantiates a new gen star gama survey utils.
*
* @param scope
* the scope
* @param survey
* the survey
* @param atts
* the atts
* @throws FileNotFoundException
* the file not found exception
*/
public GenStarGamaSurveyUtils(final IScope scope, final GamaCSVFile survey,
final List<Attribute<? extends IValue>> atts) throws FileNotFoundException {
this.path = Paths.get(FileUtils.constructAbsoluteFilePath(scope, survey.getPath(scope), false));
this.stats = CsvReader.getStats(this.path.toString(), null);
this.atts = atts;
}


/** The stats. */
private final Stats stats;

/** The path. */
private final Path path;

/** The atts. */
private final List<Attribute<? extends IValue>> atts;


/** The row header number. */
// All is here
private int[] rowHeaderNumber = new int[] {-1,-1};
private int[] columnHeaderNumber = new int[] {-1,-1};
@SuppressWarnings("rawtypes")
private IType inferedType = null;
private final int[] rowHeaderNumber = { -1, -1 };

/** The column header number. */
private final int[] columnHeaderNumber = { -1, -1 };

/** The infered type. */
@SuppressWarnings ("rawtypes") private IType inferedType = null;

/** The total. */
private Double total = null;

/**
* TODO : find the number of row headers
*
* @return
*/
public int inferRowHeaders() {
if (rowHeaderNumber[0]==-1) {
if (rowHeaderNumber[0] == -1) {

CsvReader reader = null;
int first = rowHeaderNumber[0];
int headerLength = 0;
try {
reader = new CsvReader(path.toString(),stats.delimiter);
reader = new CsvReader(path.toString(), stats.delimiter);
boolean isData = false;
do {

do {
if (reader.readRecord()) {
List<String> vals = Arrays.asList(reader.getValues()).stream()
.filter(s -> !s.isBlank()).collect(Collectors.toList());
Optional<Attribute<? extends IValue>> oa = atts.stream()
.filter(a -> a.getValueSpace().getValues().stream()
.allMatch(v -> vals.contains(v.getStringValue())))
.findFirst();
if (oa.isPresent()) { headerLength = oa.get().getValueSpace().getValues().size(); } else { isData = true; }
List<String> vals = Arrays.asList(reader.getValues()).stream().filter(s -> !s.isBlank())
.collect(Collectors.toList());
Optional<Attribute<? extends IValue>> oa = atts.stream().filter(a -> a.getValueSpace()
.getValues().stream().allMatch(v -> vals.contains(v.getStringValue()))).findFirst();
if (oa.isPresent()) {
headerLength = oa.get().getValueSpace().getValues().size();
} else {
isData = true;
}
}
first += 1;
} while (!isData);



} catch (final IOException e) {}
reader.close();
rowHeaderNumber[0] = first;
columnHeaderNumber[1] = headerLength;
}

return rowHeaderNumber[0];
}

/**
* TODO : find the number of column headers
*
* @return
*/
public int inferColumnHeaders() {
if (columnHeaderNumber[0]==-1) {
public int inferColumnHeaders() {
if (columnHeaderNumber[0] == -1) {
CsvReader reader = null;
int first = 0;
int columnLength = 0;
try {
reader = new CsvReader(path.toString(),stats.delimiter);
reader = new CsvReader(path.toString(), stats.delimiter);

int idx = inferRowHeaders();
do { reader.skipLine(); } while (--idx>0);
do { reader.skipLine(); } while (--idx > 0);

boolean isData = false;
if (reader.readRecord()) {
List<String> vals = Arrays.asList(reader.getValues()).stream()
.filter(s -> !s.isBlank()).collect(Collectors.toList());
List<String> vals = Arrays.asList(reader.getValues()).stream().filter(s -> !s.isBlank())
.collect(Collectors.toList());
int tmp = 0;
do {
do {
String current = vals.get(tmp++);
Optional<Attribute<? extends IValue>> oa = atts.stream()
.filter(a -> a.getValueSpace().contains(current))
.findFirst();
if (oa.isPresent()) {
columnLength = oa.get().getValueSpace().getValues().size() > columnLength ?
oa.get().getValueSpace().getValues().size() : columnLength;
Optional<Attribute<? extends IValue>> oa =
atts.stream().filter(a -> a.getValueSpace().contains(current)).findFirst();
if (oa.isPresent()) {
columnLength = oa.get().getValueSpace().getValues().size() > columnLength
? oa.get().getValueSpace().getValues().size() : columnLength;
} else {
isData = true;
first = vals.indexOf(current);
}
}
while (!isData);

} while (!isData);

}

} catch (final IOException e) {}
reader.close();
columnHeaderNumber[0] = first;
rowHeaderNumber[1] = columnLength;

}
return columnHeaderNumber[0];
}

/**
* Infer the type of data contains in the csv file
*
* @return
*/
@SuppressWarnings("rawtypes")
@SuppressWarnings ("rawtypes")
public IType inferDataType() {
if (inferedType!=null) { return inferedType; }
if (inferedType != null) return inferedType;
inferedType = Types.NO_TYPE;
CsvReader reader = null;
try {
reader = new CsvReader(path.toString(),stats.delimiter);

reader = new CsvReader(path.toString(), stats.delimiter);
int idx = inferRowHeaders();
do { reader.skipLine(); } while (--idx>0);
if(reader.readRecord()) {
do { reader.skipLine(); } while (--idx > 0);

if (reader.readRecord()) {
String[] firstLineData = reader.getValues();
inferedType = processRecordType(Arrays.copyOfRange(firstLineData,inferColumnHeaders(),firstLineData.length));
inferedType = processRecordType(
Arrays.copyOfRange(firstLineData, inferColumnHeaders(), firstLineData.length));
}

} catch (final IOException e) {}
reader.close();
return inferedType;
}

/**
* Compute the sum of data contained in the csv file: if there is non numerical data, then
* Compute the sum of data contained in the csv file: if there is non numerical data, then
*
* @return
*/
public Double getTotalData() {
if (total!=null) {return total;}
if (!inferedType.isNumber()) {total=-1d;}
if (total != null) return total;
if (!inferedType.isNumber()) { total = -1d; }
CsvReader reader = null;
try {
reader = new CsvReader(path.toString(),stats.delimiter);
int[] min = new int[] {inferRowHeaders(),inferColumnHeaders()};
int[] max = new int[] {columnHeaderNumber[0]+columnHeaderNumber[1],rowHeaderNumber[0]+rowHeaderNumber[1]};
System.out.println("Data matrix is"+min+max);
reader = new CsvReader(path.toString(), stats.delimiter);
int[] min = { inferRowHeaders(), inferColumnHeaders() };
int[] max = { columnHeaderNumber[0] + columnHeaderNumber[1], rowHeaderNumber[0] + rowHeaderNumber[1] };
DEBUG.OUT("Data matrix is" + DEBUG.TO_STRING(min) + DEBUG.TO_STRING(max));
int idx = min[0];
do { reader.skipLine(); } while (--idx>0);
do { reader.skipLine(); } while (--idx > 0);

int ldx = max[1];
while (ldx-- > 0) {
if(reader.readRecord()) {
String[] lineData = Arrays.copyOfRange(reader.getValues(),min[1],max[0]);
for (int i = 0; i < lineData.length; i++) {total += Double.valueOf(lineData[i]);}
if (reader.readRecord()) {
String[] lineData = Arrays.copyOfRange(reader.getValues(), min[1], max[0]);
for (String element : lineData) { total += Double.valueOf(element); }
}
}
System.out.println("Total value is "+total);

System.out.println("Total value is " + total);

} catch (final IOException e) {}
reader.close();
return total;
}

private class StringAnalysis {

/**
* The Class StringAnalysis.
*/
private static class StringAnalysis {

/** The is float. */
boolean isFloat = true;

/** The is int. */
boolean isInt = true;

/**
* Instantiates a new string analysis.
*
* @param s
* the s
*/
StringAnalysis(final String s) {

for (final char c : s.toCharArray()) {
Expand All @@ -196,46 +250,55 @@ private class StringAnalysis {
isInt = false;
isFloat = false;
break;
} else if (c == ',' || c == ';' || c == '|' || c == ':' || c == '/'
|| Character.isWhitespace(c)) {
} else if (c == ',' || c == ';' || c == '|' || c == ':' || c == '/' || Character.isWhitespace(c)) {
isInt = false;
isFloat = false;
}
}
}
if (isInt && isFloat) {
isFloat = false;
}
if (isInt && isFloat) { isFloat = false; }
}

}

@SuppressWarnings("rawtypes")

/**
* Process record type.
*
* @param values
* the values
* @return the i type
*/
@SuppressWarnings ("rawtypes")
protected IType processRecordType(final String[] values) {
IType temp = null;
for (final String s : values) {
final StringAnalysis sa = new StringAnalysis(s);
if (sa.isInt) {
if (temp == null) {
temp = Types.INT;
}
if (temp == null) { temp = Types.INT; }
} else if (sa.isFloat) {
if (temp == null || temp == Types.INT) {
temp = Types.FLOAT;
}
if (temp == null || temp == Types.INT) { temp = Types.FLOAT; }
} else {
temp = Types.NO_TYPE;
}

}
// in case nothing has been read (i.e. empty file)
if (temp == null) {
temp = Types.NO_TYPE;
}
if (temp == null) { temp = Types.NO_TYPE; }
return temp;
}


/**
* Gets the path.
*
* @return the path
*/
public Path getPath() { return path; }

/**
* Gets the delimiter.
*
* @return the delimiter
*/
public Character getDelimiter() { return stats.delimiter; }

}

0 comments on commit f4eece2

Please sign in to comment.