Skip to content
Snippets Groups Projects
Commit cb911230 authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

Bugfix : Filter should not be used

parent b7f59019
No related branches found
No related tags found
No related merge requests found
package org.hbp.mip.utils;
import org.hbp.mip.model.Dataset;
import org.hbp.mip.model.Filter;
import org.hbp.mip.model.Query;
import org.hbp.mip.model.Variable;
import org.hibernate.Session;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -21,7 +23,7 @@ public class CSVUtil {
public static Dataset parseValues(String filename, Query query)
{
List<String[]> rows = getFilteredRows(filename, query.getFilters());
List<String[]> rows = getRows(filename);
Dataset result = new Dataset();
String code = GenerateDSCode(query);
......@@ -138,8 +140,8 @@ public class CSVUtil {
return result;
}
private static List<String[]> getFilteredRows(String filename, List<Filter> filters) {
List<String[]> filteredRows = new LinkedList<>();
private static List<String[]> getRows(String filename) {
List<String[]> rows = new LinkedList<>();
try {
InputStream is = Dataset.class.getClassLoader().getResourceAsStream(filename);
InputStreamReader isr = new InputStreamReader(is);
......@@ -147,9 +149,7 @@ public class CSVUtil {
String[] firstRow = br.readLine().split(SEPARATOR, -1); // 1st row -> headers
for (String line = br.readLine(); line != null; line = br.readLine()) {
String[] row = line.split(SEPARATOR, -1);
if (matchFilter(row, filters, firstRow)) {
filteredRows.add(row);
}
rows.add(row);
}
br.close();
isr.close();
......@@ -158,7 +158,7 @@ public class CSVUtil {
e.printStackTrace();
}
return filteredRows;
return rows;
}
private static int find(String code, String[] firstRow) {
......@@ -169,121 +169,6 @@ public class CSVUtil {
return -1;
}
private static boolean matchFilter(String[] row, List<Filter> filters,
String[] firstRow) {
int nbPassed = 0;
boolean temp;
for (Filter f : filters) {
int idx = find(f.getVariable().getCode(), firstRow);
String op = f.getOperator();
List<String> testValues = f.getValues();
temp = false;
switch (op) {
case "eq":
for (String t : testValues) {
if (t.equals(row[idx])) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
case "lt": {
double r = Double.parseDouble(row[idx]);
for (String t : testValues) {
double d = Double.parseDouble(t);
if (r < d) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
}
case "gt": {
double r = Double.parseDouble(row[idx]);
for (String t : testValues) {
double d = Double.parseDouble(t);
if (r > d) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
}
case "gte": {
double r = Double.parseDouble(row[idx]);
for (String t : testValues) {
double d = Double.parseDouble(t);
if (r >= d) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
}
case "lte": {
double r = Double.parseDouble(row[idx]);
for (String t : testValues) {
double d = Double.parseDouble(t);
if (r <= d) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
}
case "neq":
for (String t : testValues) {
if (!t.equals(row[idx])) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
case "in":
for (String t : testValues) {
if (t.equals(row[idx])) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
case "notin":
for (String t : testValues) {
if (!t.equals(row[idx])) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
case "between": {
double r = Double.parseDouble(row[idx]);
if (testValues.size() == 2) {
double min = Double.parseDouble(testValues.get(0));
double max = Double.parseDouble(testValues.get(1));
if (r >= min && r <= max) {
temp = true;
}
}
if (temp)
nbPassed++;
break;
}
}
}
return nbPassed == filters.size();
}
private static String GenerateDSCode(Query query) {
String prefix = "DS";
String queryStr = Integer.toString(query.hashCode());
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment