-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduling.go
124 lines (112 loc) · 2.5 KB
/
scheduling.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"math/rand"
"os"
"schedule-planning/schedule"
"time"
)
func PrintTaskList(s *schedule.Schedule) string {
var bufer bytes.Buffer
tasks := s.Tasks()
bufer.WriteString("# task\tstart\tfinish\tearly\tlate\n")
for _, t := range tasks {
bufer.WriteString(fmt.Sprintf("%s\t%5d\t%5d\t%5d\t%5d\n", t.Remark, t.Start, t.Start+t.Duration, t.Earliest, t.Latest))
}
return bufer.String()
}
func PrintCP(s *schedule.Schedule, cp []int) string {
width := 80
var bufer bytes.Buffer
for i := 0; i < width; i++ {
bufer.WriteString("_")
}
bufer.WriteString("\n")
for elem, t := range s.Tasks() {
flag := false
for _, j := range cp {
if j == elem {
flag = true
break
}
}
for i := 0; i < t.Earliest; i++ {
bufer.WriteString(" ")
}
for i := t.Earliest; i < t.Start; i++ {
if i == t.Earliest {
bufer.WriteString("|")
} else {
bufer.WriteString("-")
}
}
bufer.WriteString(fmt.Sprintf("[%s", t.Remark))
for i := 0; i < t.Duration-7; i++ {
if flag {
bufer.WriteString("#")
} else {
bufer.WriteString(":")
}
}
bufer.WriteString("]")
for i := t.Start; i < t.Latest; i++ {
if i == t.Latest-1 {
bufer.WriteString("|")
} else {
bufer.WriteString("-")
}
}
bufer.WriteString("\n")
}
for i := 0; i < width; i++ {
bufer.WriteString("_")
}
bufer.WriteString("\n")
return bufer.String()
}
func main() {
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
outFile := os.Stdout
defer outFile.Close()
outputFlag := flag.String("o", "", "write task list to file")
flag.Parse()
scheduling := schedule.New()
for i := 0; i < 10; i++ {
tp := 0
if rand.Float32() > 0.6 {
tp = 1
}
scheduling.CreateTask(tp, fmt.Sprintf("Task_%d", i), rand.Int()%8+10)
}
scheduling.CreateLink(0, 1)
scheduling.CreateLink(0, 4)
scheduling.CreateLink(1, 3)
scheduling.CreateLink(1, 7)
scheduling.CreateLink(2, 4)
scheduling.CreateLink(2, 7)
scheduling.CreateLink(3, 5)
scheduling.CreateLink(4, 9)
scheduling.CreateLink(5, 8)
scheduling.CreateLink(6, 7)
scheduling.CreateLink(7, 8)
// find and print critical path
cp := scheduling.CalculateCP()
fmt.Println(PrintCP(scheduling, cp))
if *outputFlag != "" {
if file, err := os.Create(*outputFlag); err != nil {
fmt.Println(err)
os.Exit(1)
} else {
outFile = file
writer := bufio.NewWriter(outFile)
writer.WriteString(PrintTaskList(scheduling))
if err := writer.Flush(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
}