-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrolThread.go
183 lines (155 loc) · 4.36 KB
/
controlThread.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"log"
"io"
"strconv"
"encoding/csv"
)
func pythonCall(progName string, inChannel chan <- string, workflowNumber string) {
cmd := exec.Command("python3", progName, workflowNumber)
out, err := cmd.CombinedOutput()
log.Println(cmd.Run())
if err != nil {
fmt.Println(err)
os.Exit(3)
}
fmt.Println(string(out))
msg := string(out)[:len(out)-1]
inChannel <- msg
}
func simplePythonCall(progName string){
cmd := exec.Command("python3", progName)
cmd.Stdout = os.Stdout
cmd.Stderr = os. Stderr
log.Println(cmd.Run())
}
func messagePassing(inChannel <- chan string, outChannel chan <- string ){
msg := <- inChannel
outChannel <- msg
}
//reads a file and returns an array of comments beginning with ##
func readLines( progName string) [20]string{
var commandsArray [20]string
file, err := os.Open(progName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
i := 0
for scanner.Scan() {
command := scanner.Text()
if len(command) >1{
if command[0:2] == "##" {
commandsArray[i] = command[2:len(command)]
i++
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return commandsArray
}
func studentTotalMarks() int{
//CALCULATE STUDENT TOTAL MARKS
csvfile1, err1 := os.Open("/home/mpiuser/Desktop/multiplyByTwo.csv")
if err1 != nil {
log.Fatalln("Couldn't open the csv file", err1)
}
r1 := csv.NewReader(csvfile1)
var studentMarks [10]string
var count = 0
//read student marks csv file (record wise) and put in studentMarks array
for {
// Read each record from csv
record1, err1 := r1.Read()
if err1 == io.EOF {
break
}
if err1 != nil {
log.Fatal(err1)
}
studentMarks[count]=record1[0]
count = count + 1
}
var studentMarksInt = []int{}
//convert student marks array to int
for _, i := range studentMarks {
j, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
studentMarksInt = append(studentMarksInt, j)
}
var studentMarksTotal int
//calculate student's total marks
for _, num := range studentMarksInt {
studentMarksTotal += num
}
return studentMarksTotal
}
func classAverage() float64{
//RETREIVE CLASS AVERAGE
csvfile2, err2 := os.Open("/home/mpiuser/Desktop/calculateAverage.csv")
if err2 != nil {
log.Fatalln("Couldn't open the csv file", err2)
}
r2 := csv.NewReader(csvfile2)
var classAverage string
//read class average string value from csv file
for {
record2, err2 := r2.Read()
if err2 == io.EOF {
break
}
if err2 != nil {
log.Fatal(err2)
}
classAverage=record2[0]
}
//convert class average to float64
classAverageFloat, _ := strconv.ParseFloat(classAverage, 8)
return classAverageFloat
}
func main(){
simplePythonCall("logo.py")
simplePythonCall("workflow/parslConfig.py")
commandsArray := readLines("workflow/userScript.py")
fmt.Println(commandsArray)
//start module execution from here onwards
inChannelModule1 := make(chan string, 1)
outChannelModule1 := make(chan string, 1)
go pythonCall("workflow/"+commandsArray[0], inChannelModule1,"1")
go messagePassing(inChannelModule1, outChannelModule1)
fmt.Println(<-outChannelModule1)
outChannelModule2 := make(chan string, 1)
go pythonCall("workflow/"+commandsArray[1], outChannelModule2, "1")
go messagePassing(outChannelModule1, outChannelModule2)
fmt.Println(<- outChannelModule2)
outChannelModule3 := make(chan string, 1)
go pythonCall("workflow/"+commandsArray[2], outChannelModule3, "1")
go messagePassing(outChannelModule2, outChannelModule3)
fmt.Println(<- outChannelModule3)
studentTot := studentTotalMarks()
fmt.Println("Your total is", studentTot)
classAvg := classAverage()
fmt.Println("The class average is", classAvg)
//Dynamic decision making based on student total and class average
outChannelModule4 := make(chan string, 1)
if studentTot >= int(classAvg) {
//send to 'amazing' module
go pythonCall("workflow/"+commandsArray[3], outChannelModule4, "1")
go messagePassing(outChannelModule3, outChannelModule4)
fmt.Println(<- outChannelModule4)
} else {
//send to 'terrible' module
go pythonCall("workflow/"+commandsArray[4], outChannelModule4, "1")
go messagePassing(outChannelModule3, outChannelModule4)
fmt.Println(<- outChannelModule4)
}
}