5 "([^\\(]+?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)"
6 let edgePattern = "(.+?)[\\s]*-([->])[\\s]*(.+)"
7 let blockerPattern = "\\[Blocker\\][\\s]*(.+)"
8 let opportunityPattern = "\\[Opportunity\\][\\s]*(.+)[\\s]+([-+])[\\s]*([0-9]+.?[0-9]*)"
9 let stagePattern = "\\[(I{1,3})\\][\\s]*([0-9]+.?[0-9]*)"
12 let vertices: [Vertex]
14 let blockers: [Blocker]
15 let opportunities: [Opportunity]
28 let destination: CGPoint
40 let destination: CGPoint
43 let defaultDimensions: [CGFloat] = [
49 // Extracts the vertices from the text
51 func parseVertices(_ text: String) -> [String: CGPoint] {
53 var result: [String: CGPoint] = [:]
54 let regex = try! NSRegularExpression(pattern: vertexPattern, options: .caseInsensitive)
56 let lines = text.split(whereSeparator: \.isNewline)
59 let range = NSRange(location: 0, length: line.utf16.count)
60 let matches = regex.matches(in: String(line), options: [], range: range)
62 if matches.count > 0 && matches[0].numberOfRanges == 4 {
64 let match = matches[0]
65 let key = String(line[Range(match.range(at: 1), in: line)!])
66 let xString = String(line[Range(match.range(at: 2), in: line)!])
67 let yString = String(line[Range(match.range(at: 3), in: line)!])
68 let x = CGFloat(truncating: NumberFormatter().number(from: xString) ?? 0.0)
69 let y = CGFloat(truncating: NumberFormatter().number(from: yString) ?? 0.0)
70 let point = CGPoint(x: x, y: y)
79 func parseEdges(_ text: String, vertices: [String: CGPoint]) -> [MapEdge] {
81 var result: [MapEdge] = []
82 let regex = try! NSRegularExpression(pattern: edgePattern, options: .caseInsensitive)
84 let lines = text.split(whereSeparator: \.isNewline)
86 for (index, line) in lines.enumerated() {
87 let range = NSRange(location: 0, length: line.utf16.count)
88 let matches = regex.matches(in: String(line), options: [], range: range)
90 if matches.count > 0 && matches[0].numberOfRanges == 4 {
92 let match = matches[0]
93 let arrowhead = String(line[Range(match.range(at: 2), in: line)!]) == ">"
94 let vertexA = String(line[Range(match.range(at: 1), in: line)!])
95 let vertexB = String(line[Range(match.range(at: 3), in: line)!])
97 if let origin = vertices[vertexA] {
98 if let destination = vertices[vertexB] {
100 MapEdge(id: index, origin: origin, destination: destination, arrowhead: arrowhead))
109 func parseOpportunities(_ text: String, vertices: [String: CGPoint]) -> [Opportunity] {
111 var result: [Opportunity] = []
112 let regex = try! NSRegularExpression(pattern: opportunityPattern, options: .caseInsensitive)
114 let lines = text.split(whereSeparator: \.isNewline)
116 for (index, line) in lines.enumerated() {
117 let range = NSRange(location: 0, length: line.utf16.count)
118 let matches = regex.matches(in: String(line), options: [], range: range)
120 if matches.count > 0 && matches[0].numberOfRanges == 4 {
122 let match = matches[0]
123 let multiplier = CGFloat(
124 String(line[Range(match.range(at: 2), in: line)!]) == "-" ? -1.0 : 1.0)
125 let vertex = String(line[Range(match.range(at: 1), in: line)!])
126 let opportunityString = String(line[Range(match.range(at: 3), in: line)!])
127 let opportunity = CGFloat(
128 truncating: NumberFormatter().number(from: opportunityString) ?? 0.0)
130 if let origin = vertices[vertex] {
131 let destination = CGPoint(x: origin.x + opportunity * multiplier, y: origin.y)
132 result.append(Opportunity(id: index, origin: origin, destination: destination))
140 func parseBlockers(_ text: String, vertices: [String: CGPoint]) -> [Blocker] {
142 var result: [Blocker] = []
143 let regex = try! NSRegularExpression(pattern: blockerPattern, options: .caseInsensitive)
145 let lines = text.split(whereSeparator: \.isNewline)
147 for (index, line) in lines.enumerated() {
148 let range = NSRange(location: 0, length: line.utf16.count)
149 let matches = regex.matches(in: String(line), options: [], range: range)
151 if matches.count > 0 && matches[0].numberOfRanges == 2 {
153 let match = matches[0]
154 let vertexA = String(line[Range(match.range(at: 1), in: line)!])
156 if let position = vertices[vertexA] {
157 result.append(Blocker(id: index, position: position))
165 func parseStages(_ text: String) -> [CGFloat] {
167 var result = defaultDimensions
168 let regex = try! NSRegularExpression(pattern: stagePattern, options: .caseInsensitive)
170 let lines = text.split(whereSeparator: \.isNewline)
173 let range = NSRange(location: 0, length: line.utf16.count)
174 let matches = regex.matches(in: String(line), options: [], range: range)
176 if matches.count > 0 && matches[0].numberOfRanges == 3 {
178 let match = matches[0]
179 let stage = String(line[Range(match.range(at: 1), in: line)!])
180 let dimensionsString = String(line[Range(match.range(at: 2), in: line)!])
181 let dimensions = CGFloat(truncating: NumberFormatter().number(from: dimensionsString) ?? 0.0)
183 result[stage.count - 1] = dimensions
190 // Converts vetex dictionary to array
192 func mapVertices(_ vertices: [String: CGPoint]) -> [Vertex] {
194 return vertices.map { label, position in
196 return Vertex(id: i, label: label, position: position)
201 func parse() -> ParsedMap {
203 let text = self.content ?? ""
204 let vertices = parseVertices(text)
205 let mappedVertices = mapVertices(vertices)
206 let edges = parseEdges(text, vertices: vertices)
207 let blockers = parseBlockers(text, vertices: vertices)
208 let opportunities = parseOpportunities(text, vertices: vertices)
209 let stages = parseStages(text)
212 vertices: mappedVertices, edges: edges, blockers: blockers, opportunities: opportunities,