use of org.gephi.graph.api.TimeFormat in project gephi by gephi.
the class AttributeTableCSVExporter method writeCSVFile.
/**
* <p>Export a AttributeTable to the specified file.</p>
*
* @param graph Graph containing the table and rows
* @param table Table to export. Cannot be null
* @param out Ouput stream to write. Cannot be null.
* @param separator Separator to use for separating values of a row in the CSV file. If null ',' will be used.
* @param charset Charset encoding for the file. If null, UTF-8 will be used
* @param columnIndexesToExport Indicates the indexes of the columns to export. All columns will be exported if null. For special columns in edges table, use {@code FAKE_COLUMN_EDGE_} values in this class.
* @param rows Elements (table rows: nodes/edges) to include in the exported file. Cannot be null. If null, all nodes/edges will be exported.
* @throws IOException When an error happens while writing the file
*/
public static void writeCSVFile(Graph graph, Table table, OutputStream out, Character separator, Charset charset, Integer[] columnIndexesToExport, Element[] rows) throws IOException {
if (out == null) {
throw new IllegalArgumentException("out cannot be null");
}
if (separator == null) {
separator = DEFAULT_SEPARATOR;
}
if (charset == null) {
charset = Charset.forName("UTF-8");
}
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
boolean isEdgeTable = ac.isEdgeTable(table);
if (rows == null) {
if (isEdgeTable) {
rows = graph.getEdges().toArray();
} else {
rows = graph.getNodes().toArray();
}
}
TimeFormat timeFormat = graph.getModel().getTimeFormat();
DateTimeZone timeZone = graph.getModel().getTimeZone();
if (columnIndexesToExport == null) {
List<Integer> columnIndexesToExportList = new ArrayList<>();
//Add special columns for edges table:
if (isEdgeTable) {
columnIndexesToExportList.add(FAKE_COLUMN_EDGE_SOURCE);
columnIndexesToExportList.add(FAKE_COLUMN_EDGE_TARGET);
columnIndexesToExportList.add(FAKE_COLUMN_EDGE_TYPE);
}
for (Column column : table) {
columnIndexesToExportList.add(column.getIndex());
}
columnIndexesToExport = columnIndexesToExportList.toArray(new Integer[0]);
}
CsvWriter writer = new CsvWriter(out, separator, charset);
//Write column headers:
for (int column = 0; column < columnIndexesToExport.length; column++) {
int columnIndex = columnIndexesToExport[column];
if (columnIndex == FAKE_COLUMN_EDGE_SOURCE) {
writer.write("Source");
} else if (columnIndex == FAKE_COLUMN_EDGE_TARGET) {
writer.write("Target");
} else if (columnIndex == FAKE_COLUMN_EDGE_TYPE) {
writer.write("Type");
} else {
//Use the title only if it's the same as the id (case insensitive):
String columnId = table.getColumn(columnIndex).getId();
String columnTitle = table.getColumn(columnIndex).getId();
String columnHeader = columnId.equalsIgnoreCase(columnTitle) ? columnTitle : columnId;
writer.write(columnHeader, true);
}
}
writer.endRecord();
//Write rows:
Object value;
String text;
for (int row = 0; row < rows.length; row++) {
for (int i = 0; i < columnIndexesToExport.length; i++) {
int columnIndex = columnIndexesToExport[i];
if (columnIndex == FAKE_COLUMN_EDGE_SOURCE) {
value = ((Edge) rows[row]).getSource().getId();
} else if (columnIndex == FAKE_COLUMN_EDGE_TARGET) {
value = ((Edge) rows[row]).getTarget().getId();
} else if (columnIndex == FAKE_COLUMN_EDGE_TYPE) {
value = ((Edge) rows[row]).isDirected() ? "Directed" : "Undirected";
} else {
value = rows[row].getAttribute(table.getColumn(columnIndex));
}
if (value != null) {
text = AttributeUtils.print(value, timeFormat, timeZone);
} else {
text = "";
}
writer.write(text, true);
}
writer.endRecord();
}
writer.close();
}
use of org.gephi.graph.api.TimeFormat in project gephi by gephi.
the class AttributeColumnsControllerImpl method detectNodeDuplicatesByColumn.
@Override
public List<List<Node>> detectNodeDuplicatesByColumn(Column column, boolean caseSensitive) {
final HashMap<String, List<Node>> valuesMap = new HashMap<>();
Graph graph = Lookup.getDefault().lookup(GraphController.class).getGraphModel().getGraph();
Object value;
String strValue;
TimeFormat timeFormat = graph.getModel().getTimeFormat();
DateTimeZone timeZone = graph.getModel().getTimeZone();
for (Node node : graph.getNodes().toArray()) {
value = node.getAttribute(column);
if (value != null) {
strValue = AttributeUtils.print(value, timeFormat, timeZone);
if (!caseSensitive) {
strValue = strValue.toLowerCase();
}
if (valuesMap.containsKey(strValue)) {
valuesMap.get(strValue).add(node);
} else {
ArrayList<Node> newGroup = new ArrayList<>();
newGroup.add(node);
valuesMap.put(strValue, newGroup);
}
}
}
final List<List<Node>> groupsList = new ArrayList<>();
for (List<Node> group : valuesMap.values()) {
if (group.size() > 1) {
groupsList.add(group);
}
}
return groupsList;
}
use of org.gephi.graph.api.TimeFormat in project gephi by gephi.
the class AttributeColumnsMergeStrategiesControllerImpl method joinWithSeparatorMerge.
@Override
public Column joinWithSeparatorMerge(Table table, Column[] columnsToMerge, Class newColumnType, String newColumnTitle, String separator) {
if (table == null || columnsToMerge == null) {
throw new IllegalArgumentException("Table or columns can't be null");
}
AttributeColumnsController ac = Lookup.getDefault().lookup(AttributeColumnsController.class);
Column newColumn;
//Create as STRING column by default. Then it can be duplicated to other type.
newColumn = ac.addAttributeColumn(table, newColumnTitle, newColumnType != null ? newColumnType : String.class);
if (newColumn == null) {
return null;
}
if (separator == null) {
separator = "";
}
Object value;
StringBuilder sb;
final int columnsCount = columnsToMerge.length;
GraphModel graphModel = table.getGraph().getModel();
TimeFormat timeFormat = graphModel.getTimeFormat();
DateTimeZone timeZone = graphModel.getTimeZone();
for (Element row : ac.getTableAttributeRows(table)) {
sb = new StringBuilder();
for (int i = 0; i < columnsCount; i++) {
value = row.getAttribute(columnsToMerge[i]);
if (value != null) {
sb.append(AttributeUtils.print(value, timeFormat, timeZone));
if (i < columnsCount - 1) {
sb.append(separator);
}
}
}
ac.setAttributeValue(sb.toString(), row, newColumn);
}
return newColumn;
}
use of org.gephi.graph.api.TimeFormat in project gephi by gephi.
the class AttributeColumnsControllerImpl method createBooleanMatchesColumn.
@Override
public Column createBooleanMatchesColumn(Table table, Column column, String newColumnTitle, Pattern pattern) {
if (pattern != null) {
Column newColumn = addAttributeColumn(table, newColumnTitle, Boolean.class);
if (newColumn == null) {
return null;
}
Matcher matcher;
Object value;
TimeFormat timeFormat = table.getGraph().getModel().getTimeFormat();
DateTimeZone timeZone = table.getGraph().getModel().getTimeZone();
for (Element row : getTableAttributeRows(table)) {
value = row.getAttribute(column);
if (value != null) {
matcher = pattern.matcher(AttributeUtils.print(value, timeFormat, timeZone));
} else {
matcher = pattern.matcher("");
}
row.setAttribute(newColumn, matcher.matches());
}
return newColumn;
} else {
return null;
}
}
use of org.gephi.graph.api.TimeFormat in project gephi by gephi.
the class AttributeColumnsControllerImpl method createFoundGroupsListColumn.
@Override
public Column createFoundGroupsListColumn(Table table, Column column, String newColumnTitle, Pattern pattern) {
if (pattern != null) {
Column newColumn = addAttributeColumn(table, newColumnTitle, String[].class);
if (newColumn == null) {
return null;
}
Matcher matcher;
Object value;
ArrayList<String> foundGroups = new ArrayList<>();
TimeFormat timeFormat = table.getGraph().getModel().getTimeFormat();
DateTimeZone timeZone = table.getGraph().getModel().getTimeZone();
for (Element row : getTableAttributeRows(table)) {
value = row.getAttribute(column);
if (value != null) {
matcher = pattern.matcher(AttributeUtils.print(value, timeFormat, timeZone));
} else {
matcher = pattern.matcher("");
}
while (matcher.find()) {
foundGroups.add(matcher.group());
}
if (foundGroups.size() > 0) {
row.setAttribute(newColumn, foundGroups.toArray(new String[0]));
foundGroups.clear();
} else {
row.setAttribute(newColumn, null);
}
}
return newColumn;
} else {
return null;
}
}
Aggregations