Skip to content

Commit

Permalink
improves Excel and SMWSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Mar 9, 2018
1 parent ffaf4bb commit f628e9f
Show file tree
Hide file tree
Showing 13 changed files with 1,175 additions and 69 deletions.
Expand Up @@ -25,12 +25,23 @@

/**
* filter for property keys
*
* @author wf
*
*/
public interface Keys {
public static final String [] EMPTY_KEYS= {};
public static final String[] EMPTY_KEYS = {};

public boolean hasKey(String key);

public default boolean isEmpty() {
if (this.getKeysList().isPresent())
return this.getKeysList().get().size() == 0;
else
return true;
}

Optional<List<String>> getKeysList();

String[] getKeys();
}
Expand Up @@ -38,6 +38,9 @@
*
*/
public interface SimpleNode extends SimpleGraph {

static String SELF_LABEL="mysimplenode";

static Consumer<Vertex> printDebug = vertex -> vertex.properties()
.forEachRemaining(prop -> System.out.println(String.format("%s.%s=%s",
vertex.label(), prop.label(), prop.value())));
Expand Down Expand Up @@ -157,5 +160,31 @@ public default void printNameValues(PrintStream out) {
public default void forAll(Consumer<Vertex> consumer) {
g().V().forEachRemaining(consumer);
};

/**
* get the selfLabel for this Node
* @return - the self label
*/
public default String getSelfLabel() {
return SELF_LABEL;
}

/**
* get the Keys
* @return the keys
*/
public Keys getKeys();

/**
* get the SimpleNode of a Vertex
* @param vertex
* @return - the simpleNode if available or null if not
*/
public static SimpleNode of(Vertex vertex) {
if (vertex.property(SimpleNode.SELF_LABEL).isPresent()) {
return (SimpleNode) vertex.property(SimpleNode.SELF_LABEL).value();
}
return null;
}

}
Expand Up @@ -82,10 +82,10 @@ public void reinit() throws Exception {
// simple nodes references are not o.k.
// <data
// key="mysimplenode">com.bitplan.wikidata.WikiDataNode@7905a0b8</data>
system.graph().traversal().V().has("mysimplenode")
system.graph().traversal().V().has(SimpleNode.SELF_LABEL)
.forEachRemaining(node -> {
try {
Object simpleNodeObject = node.property("mysimplenode").value();
Object simpleNodeObject = node.property(SimpleNode.SELF_LABEL).value();
if (simpleNodeObject instanceof String) {
Map<String, Object> map = new HashMap<String, Object>();
node.properties().forEachRemaining(nodeprop -> {
Expand All @@ -97,8 +97,8 @@ public void reinit() throws Exception {
SimpleNode newNode = constructor.newInstance(system,varargs);
newNode.setVertex(node);
newNode.setMap(map);
map.put("mysimplenode", newNode);
node.property("mysimplenode", newNode);
map.put(SimpleNode.SELF_LABEL, newNode);
node.property(SimpleNode.SELF_LABEL, newNode);
}
} catch (Exception e) {
exceptionHolder.setValue(e);
Expand Down
Expand Up @@ -26,7 +26,6 @@

import com.bitplan.simplegraph.core.Keys;


/**
* default implementation for property keys
* @author wf
Expand Down
Expand Up @@ -94,6 +94,10 @@ public void setMap(Map<String, Object> map) {
this.map = map;
}

public Keys getKeys() {
return keys;
}

@Override
public Vertex setVertexFromMap() {
return setVertexFromMap(initMap());
Expand All @@ -111,7 +115,7 @@ public Vertex setVertexFromMap(Map<String, Object> map) {
addKeyValue(keyValueList, T.label, this.kind);
// now add the pointer to me
// TODO - filter / ignore me when saving
addKeyValue(keyValueList, "mysimplenode", this);
addKeyValue(keyValueList, getSelfLabel(), this);
// add all key values from my map
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() != null)
Expand All @@ -135,4 +139,21 @@ private void addKeyValue(List<Object> keyValueList, Object name,
keyValueList.add(value);
}

/**
* convert vertex property contents to a map
* @param vertex
* @param keys
* @return a map
*/
public static Map<String, Object> vertexToMap(Vertex vertex, String ... keys) {
Map<String,Object> map=new HashMap<String,Object>();
if (keys.length==0)
keys=vertex.keys().toArray(new String[vertex.keys().size()]);
for (String key:keys) {
if (vertex.property(key).isPresent())
map.put(key, vertex.property(key).value());
}
return map;
}

}
Expand Up @@ -36,7 +36,6 @@
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
Expand All @@ -50,6 +49,8 @@
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import com.bitplan.simplegraph.core.Keys;
import com.bitplan.simplegraph.core.SimpleNode;
import com.bitplan.simplegraph.impl.Holder;

/**
Expand All @@ -62,10 +63,11 @@ public class Excel {
public static boolean debug = false;
protected static Logger LOGGER = Logger
.getLogger("com.bitplan.simplegraph.excel");
private static CellStyle boldStyle;

public XSSFWorkbook workbook = null;
public Throwable error;
private FormulaEvaluator evaluator;
// private FormulaEvaluator evaluator;

/**
* get the contents of a sheet
Expand Down Expand Up @@ -124,12 +126,17 @@ private Object getCellValue(XSSFCell cell) {
return cellValue;
}

/**
* create an Excel sheet from the given url
*
* @param url
*/
public Excel(String url) {
// http://stackoverflow.com/questions/5836965/how-to-open-xlsx-files-with-poi-ss
try {
InputStream is = new URL(url).openStream();
workbook = new XSSFWorkbook(is);
evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// evaluator = workbook.getCreationHelper().createFormulaEvaluator();
} catch (Throwable th) {
error = th;
}
Expand Down Expand Up @@ -187,13 +194,13 @@ public static Workbook createWorkBook(GraphTraversalSource g) {
*/
private static <T> void addSheet(Workbook wb, String itemLabel,
GraphTraversal<T, T> items) {
CellStyle boldStyle = wb.createCellStyle();
boldStyle = wb.createCellStyle();
Font font = wb.createFont();
font.setBold(true);
boldStyle.setFont(font);

// create one sheet per Label
Sheet sheet = wb.createSheet(itemLabel);
Sheet sheet = wb.createSheet(fixSheetName(itemLabel));
// rowIndex to be used in Lambda starting from 0
Holder<Integer> rowIndex = new Holder<Integer>(0);
// look for nodes/vertices with the given vertexLabel
Expand All @@ -209,24 +216,16 @@ private static <T> void addSheet(Workbook wb, String itemLabel,
// add cells for the given row
Holder<Integer> colIndex = new Holder<Integer>(0);
if (item instanceof Vertex) {
addCell(headerRow, "id", colIndex.getFirstValue(), boldStyle);
colIndex.setValue(colIndex.getFirstValue() + 1);
addCell(headerRow, "id", colIndex, boldStyle);
Vertex vertex = (Vertex) item;
vertex.properties().forEachRemaining(prop -> {
addCell(headerRow, prop.label(), colIndex.getFirstValue(),
boldStyle);
colIndex.setValue(colIndex.getFirstValue() + 1);
});
addCells(headerRow, vertex, colIndex, true);
} else if (item instanceof Edge) {
Edge edge = (Edge) item;
edge.properties().forEachRemaining(prop -> {
addCell(headerRow, edge.label(), colIndex.getFirstValue(),
boldStyle);
colIndex.setValue(colIndex.getFirstValue() + 1);
addCell(headerRow, edge.label(), colIndex, boldStyle);
});
int col = colIndex.getFirstValue();
addCell(headerRow, "in", col, boldStyle);
addCell(headerRow, "out", col + 1, boldStyle);
addCell(headerRow, "in", colIndex, boldStyle);
addCell(headerRow, "out", colIndex, boldStyle);
}
// create a new Row
rowHolder.setValue(sheet.createRow(++rowNumber));
Expand All @@ -236,26 +235,71 @@ private static <T> void addSheet(Workbook wb, String itemLabel,
Holder<Integer> colIndex = new Holder<Integer>(0);
if (item instanceof Vertex) {
Vertex vertex = (Vertex) item;
addCell(row, vertex.id(), colIndex.getFirstValue(), null);
colIndex.setValue(colIndex.getFirstValue() + 1);
vertex.properties().forEachRemaining(prop -> {
addCell(row, prop.value(), colIndex.getFirstValue(), null);
colIndex.setValue(colIndex.getFirstValue() + 1);
});
addCell(row, vertex.id(), colIndex, null);
addCells(row, vertex, colIndex, false);
} else if (item instanceof Edge) {
Edge edge = (Edge) item;
edge.properties().forEachRemaining(prop -> {
addCell(row, prop.value(), colIndex.getFirstValue(), null);
colIndex.setValue(colIndex.getFirstValue() + 1);
addCell(row, prop.value(), colIndex, null);
});
int col = colIndex.getFirstValue();
addCell(row, edge.inVertex().id(), col, null);
addCell(row, edge.outVertex().id(), col + 1, null);
addCell(row, edge.inVertex().id(), colIndex, null);
addCell(row, edge.outVertex().id(), colIndex, null);
}
rowIndex.setValue(rowIndex.getFirstValue() + 1);
});
}

/**
* add the cells for the given vertex to the given row
*
* @param row
* @param vertex
* @param colIndex
* @param header
*/
private static void addCells(Row row, Vertex vertex, Holder<Integer> colIndex,
boolean header) {
SimpleNode simpleNode = SimpleNode.of(vertex);
boolean done=false;
// do we have SimpleNode with a predefined order of keys?
if (simpleNode != null) {
Keys keys = simpleNode.getKeys();
if (!keys.isEmpty()) {
for (String key:keys.getKeysList().get()) {
Object value=null;
if (vertex.property(key).isPresent())
value=vertex.property(key).value();
addCell(row,header?key:value,colIndex,header?boldStyle:null);
}
// ordered mode done
done=true;
}
}
// their is no order - try our luck with the set of properties
if (!done) {
vertex.properties().forEachRemaining(prop -> {
if (prop.label() != SimpleNode.SELF_LABEL)
addCell(row, header ? prop.label() : prop.value(), colIndex,
header ? boldStyle : null);
});
}
}

/**
* https://stackoverflow.com/questions/451452/valid-characters-for-excel-sheet-names
*
* @param sheetName
* @return a valid sheetName
*/
private static String fixSheetName(String sheetName) {
String invalid = "[]*/\\?";
for (int i = 0; i < invalid.length(); i++) {
char invalidChar = invalid.charAt(i);
sheetName = sheetName.replace(invalidChar, '_');
}
return sheetName;
}

/**
* get sheets
*
Expand All @@ -278,16 +322,18 @@ public List<XSSFSheet> getSheets() {
* @param colIndex
* @param cellStyle
*/
private static void addCell(Row row, Object value, int colIndex,
private static void addCell(Row row, Object value, Holder<Integer> colIndex,
CellStyle cellStyle) {
Cell cell = row.createCell(colIndex);
Cell cell = row.createCell(colIndex.getFirstValue());
colIndex.setValue(colIndex.getFirstValue() + 1);
if (value instanceof Integer) {
Integer intValue = (Integer) value;
cell.setCellValue(intValue.doubleValue());
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
} else {
cell.setCellValue(value.toString()); // type handling!
if (value!=null)
cell.setCellValue(value.toString()); // type handling!
}
if (cellStyle != null)
cell.setCellStyle(cellStyle);
Expand All @@ -308,6 +354,13 @@ public void save(String path) throws Exception {
save(workbook, path);
}

/**
* save the given work book to the given path
*
* @param wb
* @param path
* @throws Exception
*/
public static void save(Workbook wb, String path) throws Exception {
FileOutputStream fileOut = new FileOutputStream(path);
wb.write(fileOut);
Expand Down
Expand Up @@ -61,11 +61,22 @@ public Class<? extends SimpleNode> getNodeClass() {
return WorkBookNode.class;
}

/**
* create a workbook based on the given GraphTraversalSource
* @param g
* @return the Workbook
*/
public Workbook createWorkBook(GraphTraversalSource g) {
// delegate the call to static function of Excel
return Excel.createWorkBook(g);
}

/**
* save the workbook with the given filename
* @param wb
* @param fileName
* @throws Exception
*/
public void save(Workbook wb, String fileName) throws Exception {
Excel.save(wb,fileName);
}
Expand Down
Expand Up @@ -41,9 +41,10 @@ public class MapNode extends SimpleNodeImpl {

/**
* initialize me from a map
* @param keys
*/
public MapNode(SimpleGraph graph,String kind,Map<String, Object> map) {
super(graph,kind);
public MapNode(SimpleGraph graph,String kind,Map<String, Object> map, String ... keys) {
super(graph,kind,keys);
super.setVertexFromMap(map);
}

Expand Down

0 comments on commit f628e9f

Please sign in to comment.