Skip to content

Commit

Permalink
Merge pull request #136 from InformationIntegrationGroup/pytransform_…
Browse files Browse the repository at this point in the history
…glue

Pytransform glue
  • Loading branch information
jasonslepicka committed Sep 19, 2014
2 parents 38eaf4f + 8b6c3af commit ec3b8fb
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 3 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,27 @@ Yes. Due to Oracles binary license issues, we can't distribute the JAR file that
<artifactId>ojdbc</artifactId>
<version>14</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/ojdbc14.jar</systemPath>
<systemPath>/Users/karma/Web-Karma/lib/ojdbc14.jar</systemPath>
</dependency>
```
Make sure that the filename mentioned in the `systemPath` element matches with your downloaded JAR file.
Make sure that the filename mentioned in the `systemPath` element matches with your downloaded JAR file; it is likely that your installation folder is different from `/Users/karma` so make sure you use the correct one.

### Are there additional steps required to import data from MySQL database? ###
Yes. Due to MySQL binary license issues, we can't distribute the JAR file that is required for importing data from an MySQL database. Following are the steps to resolve the runtime error that you will get if you try to do it with the current source code:

1. Download the appropriate MySQL driver JAR file (for JDK 1.5 and above) that matches your MySQL version. Link: http://dev.mysql.com/downloads/connector/j/
2. Put the downloaded JAR file inside `lib` folder of the Karma source code.
3. Add the following snippet in the `pom.xml` file of the `karma-jdbc` project inside the dependencies XML element:

```
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
<scope>system</scope>
<systemPath>/Users/karma/Web-Karma/lib/mysql-connector-java-5.1.32-bin.jar</systemPath>
</dependency>
```
Make sure that the filename mentioned in the `systemPath` element matches with your downloaded JAR file; it is likely that your installation folder is different from `/Users/karma` so make sure you use the correct one. The `version` will be the version of the JAR that you downloaded.


Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ private void initialize()
scripts = new ConcurrentHashMap<String, PyCode>();
PythonInterpreter interpreter = new PythonInterpreter();
compileAndAddToRepository(interpreter, PythonTransformationHelper.getImportStatements());
compileAndAddToRepository(interpreter, PythonTransformationHelper.getRowIndexDefStatement());
compileAndAddToRepository(interpreter, PythonTransformationHelper.getGetValueDefStatement());
compileAndAddToRepository(interpreter, PythonTransformationHelper.getGetValueFromNestedColumnByIndexDefStatement());
compileAndAddToRepository(interpreter, PythonTransformationHelper.getIsEmptyDefStatement());
compileAndAddToRepository(interpreter, PythonTransformationHelper.getVDefStatement());
compileAndAddToRepository(interpreter, PythonTransformationHelper.getTransformStatement());
Expand Down Expand Up @@ -67,7 +69,9 @@ private PyCode compile(PythonInterpreter interpreter, String statement) {
public void initializeInterperter(PythonInterpreter interpreter)
{
interpreter.exec(scripts.get(PythonTransformationHelper.getImportStatements()));
interpreter.exec(scripts.get(PythonTransformationHelper.getRowIndexDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getGetValueDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getGetValueFromNestedColumnByIndexDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getIsEmptyDefStatement()));
interpreter.exec(scripts.get(PythonTransformationHelper.getVDefStatement()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class PythonTransformationHelper {
private static String valueDefStatement = null;
private static String isEmptyDefStatement = null;
private static String importStatement = null;
private static String valueFromNestedColumnByIndexDefStatement = null;
private static String getRowIndexDefStatement = null;
public static String getPyObjectValueAsString(PyObject obj) {
if (obj == null)
return "";
Expand Down Expand Up @@ -96,6 +98,19 @@ public static String getImportStatements() {
return importStatement;
}

public static String getRowIndexDefStatement()
{
if(getRowIndexDefStatement == null)
{
StringBuilder methodStmt = new StringBuilder();
methodStmt.append("def getRowIndex():\n");
methodStmt.append(" factory = edu.isi.karma.rep.WorkspaceManager.getInstance().getWorkspace(workspaceid).getFactory()\n");
methodStmt.append(" node = factory.getNode(nodeid)\n");
methodStmt.append(" return node.getRowIndex()\n");
getRowIndexDefStatement = methodStmt.toString();
}
return getRowIndexDefStatement;
}
public static String getGetValueDefStatement() {

if(valueDefStatement == null)
Expand All @@ -118,6 +133,30 @@ public static String getGetValueDefStatement() {
return valueDefStatement;
}

public static String getGetValueFromNestedColumnByIndexDefStatement() {

if(valueFromNestedColumnByIndexDefStatement == null)
{
StringBuilder methodStmt = new StringBuilder();
methodStmt.append("def getValueFromNestedColumnByIndex(columnName, nestedColumnName, index):\n");
methodStmt.append(" factory = edu.isi.karma.rep.WorkspaceManager.getInstance().getWorkspace(workspaceid).getFactory()\n");
methodStmt.append(" node = factory.getNode(nodeid)\n");
methodStmt.append(" targetNode = node.getNeighborByColumnNameWithNestedColumnByRowIndex(columnName, factory, nestedColumnName, index)\n");
methodStmt.append(" if targetNode is not None:\n");
methodStmt.append(" command.addInputColumns(targetNode.getHNodeId())\n");
methodStmt.append(" value = targetNode.getValue()\n");
methodStmt.append(" if value is not None:\n");
methodStmt.append(" valueAsString = value.asString()\n");
methodStmt.append(" if valueAsString is not None:\n");
methodStmt.append(" return valueAsString\n");
methodStmt.append(" return ''\n");
valueFromNestedColumnByIndexDefStatement = methodStmt.toString();
}
return valueFromNestedColumnByIndexDefStatement;
}



public static String getIsEmptyDefStatement() {

if(isEmptyDefStatement == null)
Expand Down
9 changes: 9 additions & 0 deletions karma-common/src/main/java/edu/isi/karma/rep/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,13 @@ public Node getNeighbor(String hNodeId) {
public Node getNeighborByColumnName(String columnName, RepFactory factory) {
return belongsToRow.getNeighborByColumnName(columnName, factory);
}

public Node getNeighborByColumnNameWithNestedColumnByRowIndex(String columnName, RepFactory factory, String nestedColumnName, int index) {
return belongsToRow.getNeighborByColumnNameWithNestedColumnByRowIndex(columnName, factory, nestedColumnName, index);
}

public int getRowIndex()
{
return belongsToRow.getBelongsToTable().getRowIndex(belongsToRow);
}
}
38 changes: 38 additions & 0 deletions karma-common/src/main/java/edu/isi/karma/rep/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,44 @@ public Node getNeighborByColumnName(String columnName, RepFactory factory) {
return null;
}

public Node getNeighborWithNestedColumnByIndex(String hNodeId, RepFactory factory, String nestedColumnName, int index) {
if (nodes.containsKey(hNodeId)) {
Node nodeWithNestedColumn = nodes.get(hNodeId);
Table nestedTable = nodeWithNestedColumn.getNestedTable();
if(nestedTable != null)
{
String [] nestedColumnPath = nestedColumnName.split("/");
String nestedHNodeId = factory.getHTable(nestedTable.getHTableId()).getHNodeIdFromColumnName(nestedColumnPath[0]);
if (null != nestedHNodeId && nestedColumnPath.length ==1) {
Row r = nestedTable.getRow(index);
if(r != null)
{
return r.getNeighbor(nestedHNodeId);
}
}
else
{
return nestedTable.getRow(0).getNeighborWithNestedColumnByIndex(nestedHNodeId, factory, nestedColumnName.substring(nestedColumnName.indexOf("/")+1), index);
}
}
}
return null;
}

public Node getNeighborByColumnNameWithNestedColumnByRowIndex(String columnName, RepFactory factory, String nestedColumnName, int index) {
String hTableId = belongsToTable.getHTableId();
HTable hTable = factory.getHTable(hTableId);
String hNodeId = hTable.getHNodeIdFromColumnName(columnName);
if (null != hNodeId) {
return getNeighborWithNestedColumnByIndex(hNodeId, factory, nestedColumnName, index);
} else if (belongsToTable.getNestedTableInNode() != null) {
return belongsToTable.getNestedTableInNode()
.getNeighborByColumnNameWithNestedColumnByRowIndex(columnName, factory, nestedColumnName, index);
}
return null;
}


public boolean collectNodes(HNodePath path, Collection<Node> nodes, SuperSelection sel) {
Node n = getNode(path.getFirst().getId());
if (n == null) {
Expand Down
15 changes: 14 additions & 1 deletion karma-common/src/main/java/edu/isi/karma/rep/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ public void addNestedTableToDataTable(HNode hNode, RepFactory factory) {
r.addNestedTableToDataTable(hNode, this, factory);
}
}

public int getRowIndex(Row r)
{
return rows.indexOf(r);
}
public Row getRow(int index)
{
if(0 <= index && index < rows.size())
{
return rows.get(index);
}
return null;
}

/**
* @param startIndex
Expand All @@ -145,7 +158,7 @@ public void addNestedTableToDataTable(HNode hNode, RepFactory factory) {
*/
public ArrayList<Row> getRows(int startIndex, int count, SuperSelection sel) {
ArrayList<Row> result = new ArrayList<Row>();
Iterator<Row> itr = rows.iterator();
Iterator<Row> itr = rows.listIterator(startIndex);
int sum = 0;
while(itr.hasNext()) {
Row r = itr.next();
Expand Down

0 comments on commit ec3b8fb

Please sign in to comment.