Skip to content

Commit

Permalink
GitHub issue #75. Process special cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
armenak committed Dec 27, 2016
1 parent 0b76d55 commit 68f3926
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.datadefender</groupId>
<artifactId>DataDefender</artifactId>
<version>0.6</version>
<version>0.7</version>
<packaging>jar</packaging>

<name>DataDefender</name>
Expand Down
Binary file modified sample_projects/database_discovery/DataDefender.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ names=names.dict
limit=1000
#models=name,location,date,time,money
models=name
extentions=com.strider.datadefender.specialcase.SinDetector.detectSin
67 changes: 62 additions & 5 deletions src/main/java/com/strider/datadefender/DatabaseDiscoverer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,24 @@
import com.strider.datadefender.database.metadata.MatchMetaData;
import com.strider.datadefender.database.sqlbuilder.ISQLBuilder;
import com.strider.datadefender.extensions.BiographicFunctions;
import com.strider.datadefender.functions.CoreFunctions;
import com.strider.datadefender.functions.Utils;
import com.strider.datadefender.requirement.Column;
import com.strider.datadefender.requirement.Parameter;
import com.strider.datadefender.specialcase.SinDetector;
import com.strider.datadefender.specialcase.SpecialCase;
import com.strider.datadefender.utils.CommonUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang3.ClassUtils;


/**
Expand Down Expand Up @@ -104,7 +115,13 @@ private List<MatchMetaData> discoverAgainstSingleModel(final IDBFactory factory,
// Start running NLP algorithms for each column and collect percentage
matches = new ArrayList<>();
MatchMetaData specialCaseData = null;
boolean specialCase = true;
boolean specialCase = false;


final String[] specialCaseFunctions = dataDiscoveryProperties.getProperty("extentions").split(",");
if (specialCaseFunctions != null && specialCaseFunctions.length > 0) {
specialCase = true;
}

final ISQLBuilder sqlBuilder = factory.createSQLBuilder();
List<Double> probabilityList;
Expand Down Expand Up @@ -153,7 +170,14 @@ private List<MatchMetaData> discoverAgainstSingleModel(final IDBFactory factory,
final String sentence = resultSet.getString(1);

if (specialCase) {
specialCaseData = SinDetector.detectSin(data, sentence);
try {
for (int i=0; i<specialCaseFunctions.length; i++) {
specialCaseData = (MatchMetaData)callExtention(specialCaseFunctions[i], data, sentence);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
log.error(e.toString());
}

}

if (sentence != null && !sentence.isEmpty()) {
Expand Down Expand Up @@ -207,14 +231,47 @@ private List<MatchMetaData> discoverAgainstSingleModel(final IDBFactory factory,
}

// Special processing
log.info("specialCaseData is null " + (specialCaseData == null));
log.info("specialCase is true" + specialCase);
if (specialCase && specialCaseData != null) {
log.info(specialCaseData.getModel());
matches.add(specialCaseData);
}
}

return matches;
}

private Object callExtention(final String function, MatchMetaData data, String text)
throws SQLException,
NoSuchMethodException,
SecurityException,
IllegalAccessException,
IllegalArgumentException,
InvocationTargetException {

if (function == null || function.equals("")) {
log.warn("Function " + function + " is not defined");
return null;
}

Object value = null;

try {
final String className = Utils.getClassName(function);
final String methodName = Utils.getMethodName(function);
final Class<?> clazz = Class.forName(className);
final Method method = Class.forName(className).getMethod(methodName, new Class[]{MatchMetaData.class, String.class});

final SpecialCase instance = (SpecialCase) Class.forName(className).newInstance();

final Map<String, Object> paramValues = new HashMap<>(2);
paramValues.put("metadata", data);
paramValues.put("text", text);

value = method.invoke(instance, data, text);

} catch (AnonymizerException | InstantiationException | ClassNotFoundException ex) {
log.error(ex.toString());
}

return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
*
* @author strider
*/
public class SinDetector {
public class SinDetector implements SpecialCase {
private static final Logger log = getLogger(SinDetector.class);

public static MatchMetaData detectSin(MatchMetaData data, String text) {
if (data.getColumnType().equals("INT") || data.getColumnType().equals("VARCHAR")) {
BiographicFunctions bf = new BiographicFunctions();
if ( ( text.matches("[0-9]+") && text.length() == 9) && bf.isValidSIN(text)) {
log.info("Valid SIN " + text);
data.setModel("sin");
data.setAverageProbability(1);
return data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.strider.datadefender.specialcase;

import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;

/**
*
* @author strider
*/
public interface SpecialCase {
}

0 comments on commit 68f3926

Please sign in to comment.