Skip to content

Commit 4d272e9

Browse files
committed
reorganized java files into a project structure
1 parent 6b307b1 commit 4d272e9

10 files changed

+389
-0
lines changed

PipelineScript/.classpath

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="lib" path="lib/junit-4.12.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

PipelineScript/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

PipelineScript/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>PipelineScript</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

PipelineScript/lib/junit-4.12.jar

308 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.pipelinescript;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class FileManager {
13+
Map<String, String> fileMap;
14+
public FileManager(){
15+
this.fileMap = new HashMap<>();
16+
}
17+
public void create(String variable, String filepath){
18+
fileMap.put(variable,filepath);
19+
}
20+
public void delete(String variable){
21+
fileMap.remove(variable);
22+
}
23+
public void move(String variable, String filepath){
24+
fileMap.put(variable, filepath);
25+
}
26+
public void set(String variable, Object value){
27+
28+
}
29+
30+
public Object get(String variable){
31+
32+
return new Object();
33+
}
34+
// make sure it points proper CSV file
35+
public Graph getGraphFromCSV(String variable){
36+
String path = fileMap.get(variable);
37+
BufferedReader br = null;
38+
String line = "";
39+
String cvsSplitBy = "\t";
40+
List<String[]> rtn = null;
41+
try {
42+
br = new BufferedReader(new FileReader(path));
43+
} catch (FileNotFoundException e) {
44+
// TODO Auto-generated catch block
45+
e.printStackTrace();
46+
}
47+
try {
48+
rtn = new ArrayList<>();
49+
while ((line = br.readLine()) != null) {
50+
String[] l = line.split(cvsSplitBy);
51+
rtn.add(l);
52+
}
53+
} catch (IOException e) {
54+
// TODO Auto-generated catch block
55+
e.printStackTrace();
56+
}
57+
58+
return new Graph(rtn);
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.pipelinescript;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class FileManagerTest {
7+
protected FileManager filemanager = new FileManager();
8+
9+
//filemanager = new FileManager();
10+
String [][]qtable = {{"0","1","1"},{"1","2","2"},{"0","2","4"}};
11+
Graph gr = new Graph(qtable);
12+
13+
@Test
14+
public void testCase0() {
15+
filemanager.create("test0", "./TestFile.csv");
16+
Graph g2 = filemanager.getGraphFromCSV("test0");
17+
Assert.assertEquals(g2 , gr);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.pipelinescript;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
// node1, node2, weight
10+
public class Graph extends Table {
11+
Table graph;
12+
private List<Node> nodes;
13+
public Graph(int numofEdge) {
14+
super(numofEdge,3);
15+
graph = getTable();
16+
//nodes = new ArrayList<>();
17+
}
18+
public Graph(String[][] g) {
19+
super(g);
20+
graph = getTable();
21+
//nodes = new ArrayList<>();
22+
}
23+
public int toCSV(String fileName,
24+
String separator){
25+
//Delimiter used in CSV file
26+
27+
final String COMMADELIMITER = ",";
28+
final String NEW_LINE_SEPARATOR = "\n";
29+
final String TAB_SEPARATOR = "\t";
30+
// IF YOU LIKE TO ADD FILE HEADER AS OPTION
31+
final String FILE_HEADER = "id,firstName,lastName,gender,age";
32+
33+
FileWriter fileWriter = null;
34+
35+
try{
36+
fileWriter = new FileWriter(fileName);
37+
// Write the CSV file header
38+
//fileWriter.append(FILE_HEADER.toString());
39+
//Add a new line separator after the header
40+
// fileWriter.append(NEW_LINE_SEPARATOR);
41+
String[][] table = graph.getTableString();
42+
for (String[] line : table) {
43+
for (int j = 0 ; j < line.length ; j++) {
44+
fileWriter.append(line[j]);
45+
fileWriter.append(separator);
46+
}
47+
fileWriter.append(NEW_LINE_SEPARATOR);
48+
}
49+
System.out.println("CSV file was created successfully !!!");
50+
return 1;
51+
}catch(Exception e){
52+
return -1;
53+
/*
54+
System.out.println("Error in CsvFileWriter !!!");
55+
e.printStackTrace();
56+
*/
57+
58+
} finally {
59+
try {
60+
fileWriter.flush();
61+
fileWriter.close();
62+
} catch (IOException e) {
63+
return -1;
64+
/*
65+
System.out.println("Error while flushing/closing fileWriter !!!");
66+
e.printStackTrace();
67+
*/
68+
}
69+
}
70+
71+
72+
73+
}
74+
75+
public List<Node> getGraphNode(){
76+
List<Node> nodes = new ArrayList<>();
77+
String [][]edge = graph.getTableString();
78+
int numEdge = edge.length;
79+
80+
for (String[] ed : edge) {
81+
int n1 = Integer.parseInt(ed[0]);
82+
int n2 = Integer.parseInt(ed[1]);
83+
int weig = Integer.parseInt(ed[2]);
84+
85+
if (!nodes.contains(new Node(n1))) {
86+
nodes.add(new Node(n1));
87+
}
88+
if (!nodes.contains(new Node(n2))){
89+
nodes.add(new Node(n2));
90+
}
91+
Node node1 = nodes.get(nodes.indexOf(new Node(n1)));
92+
Node node2 = nodes.get(nodes.indexOf(new Node(n2)));
93+
node1.adj.put(node2, weig);
94+
// INDIRECTED GRAPH, REMOVE THIS LINE TO MAKE IT DIRECTED GRAPH
95+
node2.adj.put(node1, weig);
96+
}
97+
return nodes;
98+
}
99+
100+
}
101+
class Node {
102+
String stringData;
103+
int nodeNumber;
104+
Map<Node, Integer> adj;
105+
public Node() {
106+
stringData = null;
107+
adj = new HashMap<>();
108+
}
109+
110+
public Node(int nodeNum) {
111+
adj = new HashMap<>();
112+
this.nodeNumber= nodeNum;
113+
}
114+
public Node(int nodeNum , String sData) {
115+
adj = new HashMap<>();
116+
this.nodeNumber= nodeNum;
117+
stringData = sData;
118+
119+
}
120+
public Map getNeighbor() {
121+
return adj;
122+
}
123+
124+
@Override
125+
public boolean equals(Object o){
126+
Node n = (Node)o;
127+
return n.nodeNumber == this.nodeNumber ? true:false;
128+
}
129+
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.pipelinescript;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
7+
8+
public class GraphTest {
9+
10+
protected Graph graph;
11+
12+
String [][]qtable = {{"0","1","1"},{"1","2","2"},{"0","2","4"}};
13+
Graph gr = new Graph(qtable);
14+
15+
@Test
16+
public void testCase0() {
17+
Assert.assertEquals(gr.toCSV("TestFile", "\t"), 1);
18+
}
19+
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.pipelinescript;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class Table {
9+
private String[][] table;
10+
11+
public Table(int col, int row){
12+
this.table = new String[col][row];
13+
}
14+
public Table(String[][] t ){
15+
this.table = t;
16+
}
17+
public Table(List<String[]> tab){
18+
this.table = new String[tab.size()][tab.get(0).length];
19+
for (int i = 0 ; i < table.length ; i++) {
20+
for (int j = 0 ; j < table[0].length ; j++) {
21+
this.table[i][j] = ((String[])tab.get(i))[j];
22+
}
23+
}
24+
}
25+
26+
@Override
27+
public boolean equals(Object other) {
28+
29+
Table otherTable = (Table)other;
30+
String [][]comp = otherTable.table;
31+
for (int i = 0 ; i < table.length ; i++) {
32+
for (int j = 0 ; j < table[0].length ; j++) {
33+
if (!this.table[i][j].equals(comp[i][j])) {
34+
return false;
35+
}
36+
}
37+
}
38+
39+
return true;
40+
}
41+
private String getRow(String[] row){
42+
String rtn = "";
43+
for (String s : row) {
44+
rtn += s;
45+
}
46+
return rtn;
47+
}
48+
public Table getTable(){
49+
return this;
50+
}
51+
public String[][] getTableString(){
52+
return table;
53+
}
54+
public String get(int i,int j) {
55+
return table[i][j];
56+
}
57+
public void set(int i,int j, String val) {
58+
table[i][j] = val;
59+
}
60+
61+
public Table union(Table otherTable) {
62+
63+
String [][]comp = otherTable.table;
64+
List<String[]> tbl = new ArrayList<>();
65+
for (int i = 0 ; i < comp.length ; i++){
66+
String [] arr1 = comp[i];
67+
String [] arr2 = this.table[i];
68+
if (!isEqual(arr1, arr2)){
69+
tbl.add(arr2);
70+
}
71+
tbl.add(arr1);
72+
}
73+
return new Table(tbl);
74+
}
75+
private boolean isEqual(String [] arr1, String [] arr2){
76+
77+
if (arr1.length != arr2.length) {
78+
return false;
79+
}
80+
81+
for (int i = 0 ; i < arr1.length ; i++ ) {
82+
if (!arr1[i].equals(arr1[i])){
83+
return false;
84+
}
85+
}
86+
return true;
87+
}
88+
89+
public String[][] intersection(Table otherTable) {
90+
String[][] returnTable = null;
91+
List<String[]> rtnList = new ArrayList<>();
92+
Map<String, String[]> rowMap = new HashMap<>();
93+
94+
for (String[] entre : getTable().table){
95+
rowMap.put(getRow(entre), entre);
96+
}
97+
98+
for (String[] otherRow : otherTable.table ) {
99+
if (rowMap.containsKey(getRow(otherRow))){
100+
rtnList.add(otherRow);
101+
}
102+
}
103+
returnTable = new String[rtnList.size()][rtnList.get(0).length];
104+
for (int i = 0 ; i < rtnList.size() ; i++ ) {
105+
returnTable[i] = rtnList.get(i);
106+
}
107+
return returnTable;
108+
109+
}
110+
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.pipelinescript;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
7+
public class TableTest {
8+
protected Table table;
9+
String[][] st = {{"sel", "test"}, {"tr","ed"}};
10+
Table t1 = new Table(st);
11+
12+
String[][] st2 = {{"sel1", "test1"}, {"tr","ed"}};
13+
Table t2 = new Table(st2);
14+
15+
String[][] res = {{"tr","ed"}};
16+
17+
@Test(timeout = 2000)
18+
public void testCase0() {
19+
Assert.assertEquals(res, t1.intersection(t2));
20+
}
21+
}

0 commit comments

Comments
 (0)