use of dna.dataStructures.StatementType in project dna by leifeld.
the class Exporter method populateExcludeVariableList.
/**
* Sets a new {@link DefaultListModel} in the excludeVariableList and adds variables conditional on the statement type selected
*/
public void populateExcludeVariableList() {
excludeValues.clear();
StatementType selected = (StatementType) statementTypeBox.getSelectedItem();
String[] excludeVariableItems = getVariablesList(selected, true, true, true, true);
DefaultListModel<String> excludeVariableModel = new DefaultListModel<String>();
for (int i = 0; i < excludeVariableItems.length - 4; i++) {
excludeVariableModel.addElement(excludeVariableItems[i]);
excludeValues.put(excludeVariableItems[i], new ArrayList<String>());
}
excludeVariableModel.addElement("author");
excludeVariableModel.addElement("source");
excludeVariableModel.addElement("section");
excludeVariableModel.addElement("type");
excludeVariableList.setModel(excludeVariableModel);
}
use of dna.dataStructures.StatementType in project dna by leifeld.
the class SqlConnection method getAllData.
/**
* @return Data object.
*/
public Data getAllData() {
Data data = new Data();
data.setSettings(getAllSettings());
data.setDocuments(getAllDocuments());
data.setCoders(getAllCoders());
data.setCoderRelations(getAllCoderRelations());
data.setRegexes(getAllRegexes());
data.setStatementLinks(getAllStatementLinks());
data.setStatementTypes(getAllStatementTypes());
ArrayList<Statement> statements = new ArrayList<Statement>();
try {
String myQuery = "SELECT * FROM STATEMENTS";
PreparedStatement preStatement = (PreparedStatement) connection.prepareStatement(myQuery);
ResultSet result = preStatement.executeQuery();
if (result.next()) {
do {
int id = result.getInt("ID");
int documentId = result.getInt("DocumentId");
int start = result.getInt("Start");
int stop = result.getInt("Stop");
int statementTypeId = result.getInt("StatementTypeId");
int coder = result.getInt("Coder");
Date date = data.getDocument(documentId).getDate();
StatementType st = data.getStatementTypeById(statementTypeId);
LinkedHashMap<String, Object> values = new LinkedHashMap<String, Object>();
Iterator<String> keyIterator = st.getVariables().keySet().iterator();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
String value = st.getVariables().get(key);
String tableExtension = "";
if (value.equals("boolean")) {
tableExtension = "BOOLEAN";
} else if (value.equals("integer")) {
tableExtension = "INTEGER";
} else if (value.equals("short text")) {
tableExtension = "SHORTTEXT";
} else if (value.equals("long text")) {
tableExtension = "LONGTEXT";
}
String myQuery2 = "SELECT * FROM DATA" + tableExtension + " WHERE StatementId = " + id + " AND VariableId = (SELECT ID FROM VARIABLES WHERE StatementTypeId = " + statementTypeId + " AND Variable = '" + key + "')";
PreparedStatement preStatement2 = (PreparedStatement) connection.prepareStatement(myQuery2);
ResultSet result2 = preStatement2.executeQuery();
if (result2.next()) {
do {
values.put(key, result2.getObject("Value"));
} while (result2.next());
}
result2.close();
preStatement2.close();
if (values.size() == 0 || values.get(key) == null) {
// Fix errors here if no statement contents availabe
System.err.print("Statement " + id + ": variable \"" + key + "\" was not saved... ");
String query = "SELECT ID FROM VARIABLES WHERE (StatementTypeId = " + statementTypeId + " AND Variable = '" + key + "')";
int varId = (int) executeQueryForObject(query);
String replacementValue = "0";
if (value.equals("short text") || value.equals("long text")) {
replacementValue = "''";
}
String statement = "INSERT INTO DATA" + tableExtension + " (StatementId, VariableId, StatementTypeId, Value) " + "Values (" + id + ", " + varId + ", " + statementTypeId + ", " + replacementValue + ")";
executeStatement(statement);
if (value.equals("short text") || value.equals("long text")) {
values.put(key, "");
} else {
values.put(key, 0);
}
System.err.println("The problem has been fixed. Please review this statement.");
}
}
Statement statement = new Statement(id, documentId, start, stop, date, statementTypeId, coder, values);
statements.add(statement);
} while (result.next());
}
result.close();
preStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
data.setStatements(statements);
data.setAttributes(getAllAttributes());
return data;
}
use of dna.dataStructures.StatementType in project dna by leifeld.
the class SqlConnection method getAllStatementTypes.
/**
* @return Array list of all statement types in the SQL database.
*/
private ArrayList<StatementType> getAllStatementTypes() {
ArrayList<StatementType> al = new ArrayList<StatementType>();
try {
String myQuery = "SELECT * FROM STATEMENTTYPES";
PreparedStatement preStatement = (PreparedStatement) connection.prepareStatement(myQuery);
ResultSet result = preStatement.executeQuery();
if (result.next()) {
do {
int id = result.getInt("id");
String label = result.getString("Label");
Color color = new Color(result.getInt("red"), result.getInt("green"), result.getInt("blue"));
LinkedHashMap<String, String> variables = new LinkedHashMap<String, String>();
String myQuery2 = "SELECT * FROM VARIABLES WHERE StatementTypeId = " + id;
PreparedStatement preStatement2 = (PreparedStatement) connection.prepareStatement(myQuery2);
ResultSet result2 = preStatement2.executeQuery();
if (result2.next()) {
do {
variables.put(result2.getString("Variable"), result2.getString("DataType"));
} while (result2.next());
}
result2.close();
preStatement2.close();
StatementType statementType = new StatementType(id, label, color, variables);
al.add(statementType);
} while (result.next());
}
result.close();
preStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return al;
}
use of dna.dataStructures.StatementType in project dna by leifeld.
the class DocStatsPanel method computeStats.
public void computeStats() {
clear();
int numDocuments = Dna.data.getDocuments().size();
int numStatements = Dna.dna.gui.rightPanel.statementPanel.ssc.getRowCount();
// int statementLinks = Dna.dna.gui.rightPanel.linkedTableModel.getRowCount();
String statText = "Documents: " + numDocuments + "\n" + "Statements: " + numStatements + "\n";
for (StatementType st : Dna.data.getStatementTypes()) {
statText = statText + "\n\"" + st.getLabel() + "\" Variables:\n";
String[] vars = st.getVariables().keySet().toArray(new String[st.getVariables().keySet().size()]);
ArrayList<Statement> s = Dna.data.getStatementsByStatementTypeId(st.getId());
for (int j = 0; j < vars.length; j++) {
ArrayList<Object> varEntries = new ArrayList<Object>();
for (int i = 0; i < s.size(); i++) {
if (!varEntries.contains(s.get(i).getValues().get(vars[j]))) {
varEntries.add(s.get(i).getValues().get(vars[j]));
}
}
int count = varEntries.size();
statText = statText + " " + vars[j] + ": " + count + "\n";
}
}
tf.setEditable(true);
tf.setText(statText);
tf.setEditable(false);
}
use of dna.dataStructures.StatementType in project dna by leifeld.
the class Exporter method rNetwork.
/**
* Compute one-mode or two-mode network matrix based on R arguments.
*
* @param networkType The network type as provided by rDNA (can be 'eventlist', 'twomode', or 'onemode')
* @param statementType Statement type as a String
* @param variable1 First variable for export, provided as a String
* @param variable1Document boolean indicating if the first variable is at the document level
* @param variable2 Second variable for export, provided as a String
* @param variable2Document boolean indicating if the second variable is at the document level
* @param qualifier Qualifier variable as a String
* @param qualifierAggregation Aggregation rule for the qualifier variable (can be 'ignore', 'combine', 'subtract', 'congruence', or 'conflict')
* @param normalization Normalization setting as a String, as provided by rDNA (can be 'no', 'activity', 'prominence', 'average', 'Jaccard', or 'cosine')
* @param includeIsolates boolean indicating whether nodes not currently present should still be inserted into the network matrix
* @param duplicates An input String from rDNA that can be 'include', 'document', 'week', 'month', 'year', or 'acrossrange'
* @param startDate Start date for the export, provided as a String with format "dd.MM.yyyy"
* @param stopDate Stop date for the export, provided as a String with format "dd.MM.yyyy"
* @param startTime Start time for the export, provided as a String with format "HH:mm:ss"
* @param stopTime Stop time for the export, provided as a String with format "HH:mm:ss"
* @param timewindow A string indicating the time window setting. Valid options are 'no', 'events', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', and 'years'.
* @param windowsize An int indicating the duration of the time window in the units specified in the timeWindow argument.
* @param excludeVariables A String array with n elements, indicating the variable of the n'th value
* @param excludeValues A String array with n elements, indicating the value pertaining to the n'th variable String
* @param excludeAuthors A String array of values to exclude in the 'author' variable at the document level
* @param excludeSources A String array of values to exclude in the 'source' variable at the document level
* @param excludeSections A String array of values to exclude in the 'section' variable at the document level
* @param excludeTypes A String array of values to exclude in the 'type' variable at the document level
* @param invertValues boolean indicating whether the statement-level exclude values should be included (= true) rather than excluded
* @param invertAuthors boolean indicating whether the document-level author values should be included (= true) rather than excluded
* @param invertSources boolean indicating whether the document-level source values should be included (= true) rather than excluded
* @param invertSections boolean indicating whether the document-level section values should be included (= true) rather than excluded
* @param invertTypes boolean indicating whether the document-level type values should be included (= true) rather than excluded
* @param verbose Report progress to the console?
* @return A Matrix object containing the resulting one-mode or two-mode network
*/
public void rNetwork(String networkType, String statementType, String variable1, boolean variable1Document, String variable2, boolean variable2Document, String qualifier, String qualifierAggregation, String normalization, boolean includeIsolates, String duplicates, String startDate, String stopDate, String startTime, String stopTime, String timewindow, int windowsize, String[] excludeVariables, String[] excludeValues, String[] excludeAuthors, String[] excludeSources, String[] excludeSections, String[] excludeTypes, boolean invertValues, boolean invertAuthors, boolean invertSources, boolean invertSections, boolean invertTypes, boolean verbose) {
// step 1: preprocess arguments
int max = 5;
if (networkType.equals("eventlist")) {
max = 4;
}
if (verbose == true) {
System.out.print("(1/" + max + "): Processing network options... ");
}
networkType = formatNetworkType(networkType);
StatementType st = processStatementType(networkType, statementType, variable1, variable2, qualifier, qualifierAggregation);
boolean ignoreQualifier = qualifier.equals("ignore");
int statementTypeId = st.getId();
normalization = formatNormalization(networkType, normalization);
duplicates = formatDuplicates(duplicates);
Date start = formatDate(startDate, startTime);
Date stop = formatDate(stopDate, stopTime);
if (timewindow == null || timewindow.startsWith("no")) {
timewindow = "no time window";
}
if (timewindow.equals("seconds")) {
timewindow = "using seconds";
}
if (timewindow.equals("minutes")) {
timewindow = "using minutes";
}
if (timewindow.equals("hours")) {
timewindow = "using hours";
}
if (timewindow.equals("days")) {
timewindow = "using days";
}
if (timewindow.equals("weeks")) {
timewindow = "using weeks";
}
if (timewindow.equals("months")) {
timewindow = "using months";
}
if (timewindow.equals("years")) {
timewindow = "using years";
}
if (timewindow.equals("events")) {
timewindow = "using events";
}
HashMap<String, ArrayList<String>> map = processExcludeVariables(excludeVariables, excludeValues, invertValues, data.getStatements(), data.getStatements(), data.getDocuments(), statementTypeId, includeIsolates);
ArrayList<String> authorExclude = processExcludeDocument("author", excludeAuthors, invertAuthors, data.getStatements(), data.getStatements(), data.getDocuments(), statementTypeId, includeIsolates);
ArrayList<String> sourceExclude = processExcludeDocument("source", excludeSources, invertSources, data.getStatements(), data.getStatements(), data.getDocuments(), statementTypeId, includeIsolates);
ArrayList<String> sectionExclude = processExcludeDocument("section", excludeSections, invertSections, data.getStatements(), data.getStatements(), data.getDocuments(), statementTypeId, includeIsolates);
ArrayList<String> typeExclude = processExcludeDocument("type", excludeTypes, invertTypes, data.getStatements(), data.getStatements(), data.getDocuments(), statementTypeId, includeIsolates);
if (verbose == true) {
System.out.print("Done.\n");
}
// step 2: filter
boolean filterEmptyFields = true;
if (networkType.equals("Event list")) {
filterEmptyFields = false;
}
if (verbose == true) {
System.out.print("(2/" + max + "): Filtering statements...\n");
}
this.filteredStatements = filter(data.getStatements(), data.getDocuments(), start, stop, st, variable1, variable2, variable1Document, variable2Document, qualifier, ignoreQualifier, duplicates, authorExclude, sourceExclude, sectionExclude, typeExclude, map, filterEmptyFields, verbose);
if (verbose == true) {
System.out.print(this.filteredStatements.size() + " out of " + data.getStatements().size() + " statements retained.\n");
}
if (!timewindow.equals("no time window") && startDate.equals("01.01.1900") && startTime.equals("00:00:00")) {
start = filteredStatements.get(0).getDate();
}
if (!timewindow.equals("no time window") && stopDate.equals("31.12.2099") && stopTime.equals("23:59:59")) {
if (timewindow.equals("using events")) {
stop = filteredStatements.get(filteredStatements.size() - 1).getDate();
} else {
GregorianCalendar stopTemp = new GregorianCalendar();
stopTemp.setTime(start);
GregorianCalendar lastDateTemp = new GregorianCalendar();
lastDateTemp.setTime(filteredStatements.get(filteredStatements.size() - 1).getDate());
while (stopTemp.before(lastDateTemp)) {
if (timewindow.equals("using seconds")) {
stopTemp.add(Calendar.SECOND, 1);
}
if (timewindow.equals("using minutes")) {
stopTemp.add(Calendar.MINUTE, 1);
}
if (timewindow.equals("using hours")) {
stopTemp.add(Calendar.HOUR, 1);
}
if (timewindow.equals("using days")) {
stopTemp.add(Calendar.DAY_OF_MONTH, 1);
}
if (timewindow.equals("using weeks")) {
stopTemp.add(Calendar.WEEK_OF_YEAR, 1);
}
if (timewindow.equals("using months")) {
stopTemp.add(Calendar.MONTH, 1);
}
if (timewindow.equals("using years")) {
stopTemp.add(Calendar.YEAR, 1);
}
}
stop = stopTemp.getTime();
}
}
// step 3: compile node labels
String[] names1 = null;
String[] names2 = null;
if (!networkType.equals("Event list")) {
if (verbose == true) {
System.out.print("(3/" + max + "): Compiling node labels... ");
}
names1 = extractLabels(this.filteredStatements, data.getStatements(), data.getDocuments(), variable1, variable1Document, statementTypeId, includeIsolates);
names2 = extractLabels(this.filteredStatements, data.getStatements(), data.getDocuments(), variable2, variable2Document, statementTypeId, includeIsolates);
if (verbose == true) {
System.out.print(names1.length + " entries for the first and " + names2.length + " entries for the second variable.\n");
}
}
// step 4: create matrix
if (verbose == true) {
int step = 4;
if (networkType.equals("Event list")) {
step = 3;
}
System.out.print("(" + step + "/" + max + "): Computing network matrix... ");
}
Matrix m = null;
timeWindowMatrices = null;
if (networkType.equals("Two-mode network")) {
if (timewindow.equals("no time window")) {
m = computeTwoModeMatrix(filteredStatements, data.getDocuments(), st, variable1, variable2, variable1Document, variable2Document, names1, names2, qualifier, qualifierAggregation, normalization, verbose);
} else {
this.timeWindowMatrices = computeTimeWindowMatrices(filteredStatements, data.getDocuments(), st, variable1, variable2, variable1Document, variable2Document, names1, names2, qualifier, qualifierAggregation, normalization, true, start, stop, timewindow, windowsize, includeIsolates);
}
this.matrix = m;
} else if (networkType.equals("One-mode network")) {
if (timewindow.equals("no time window")) {
m = computeOneModeMatrix(filteredStatements, data.getDocuments(), st, variable1, variable2, variable1Document, variable2Document, names1, names2, qualifier, qualifierAggregation, normalization);
} else {
this.timeWindowMatrices = computeTimeWindowMatrices(filteredStatements, data.getDocuments(), st, variable1, variable2, variable1Document, variable2Document, names1, names2, qualifier, qualifierAggregation, normalization, false, start, stop, timewindow, windowsize, includeIsolates);
}
this.matrix = m;
} else if (networkType.equals("Event list")) {
this.matrix = null;
this.eventListColumnsR = eventListR(filteredStatements, data.getDocuments(), st);
}
if (verbose == true) {
System.out.print("Done.\n");
int step = 5;
if (networkType.equals("Event list")) {
step = 4;
}
System.out.println("(" + step + "/" + max + "): Retrieving results.");
}
}
Aggregations