6 struct MapParsingPatterns {
7 static let vertex = try! NSRegularExpression(
9 "([^\\(]+?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(?:\\[(.*?)\\])?",
10 options: .caseInsensitive)
11 static let edge = try! NSRegularExpression(
12 pattern: "(.+?)[\\s]*-([->])[\\s]*(.+)", options: .caseInsensitive)
13 static let blocker = try! NSRegularExpression(
14 pattern: "\\[(Blocker)\\][\\s]*(.+)", options: .caseInsensitive)
15 static let opportunity = try! NSRegularExpression(
16 pattern: "\\[(Opportunity)\\][\\s]*(.+)[\\s]+([-+])[\\s]*([0-9]+.?[0-9]*)",
17 options: .caseInsensitive)
18 static let stage = try! NSRegularExpression(
19 pattern: "\\[(I{1,3})\\][\\s]*([0-9]+.?[0-9]*)", options: .caseInsensitive)
23 let vertices: [Vertex]
25 let blockers: [Blocker]
26 let opportunities: [Opportunity]
29 static let empty: ParsedMap = ParsedMap(
30 vertices: [], edges: [], blockers: [], opportunities: [], stages: defaultDimensions)
37 var shape: VertexShape = .circle
40 enum VertexShape: String {
50 let destination: CGPoint
62 let destination: CGPoint
65 struct StageDimensions {
67 let dimensions: CGFloat
70 private let defaultDimensions: [CGFloat] = [
76 // MARK: - MapParserStrategy protocol
78 protocol MapParserStrategy {
79 func canHandle(line: String) -> Bool
80 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any)
83 struct AnyMapParserStrategy: MapParserStrategy {
85 private let base: MapParserStrategy
87 init<T: MapParserStrategy>(_ base: T) {
91 func canHandle(line: String) -> Bool {
92 return base.canHandle(line: line)
94 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
95 return base.handle(index: index, line: line, vertices: vertices)
99 // MARK: - Map Builder
102 var vertices: [String: Vertex] = [:]
103 private var edges: [MapEdge] = []
104 private var blockers: [Blocker] = []
105 private var opportunities: [Opportunity] = []
106 private var stages: [CGFloat] = defaultDimensions
108 func addObjectToMap(type: Any.Type, object: Any) {
109 if type == Vertex.self {
110 let vertex = object as! Vertex
111 vertices[vertex.label] = vertex
114 if type == MapEdge.self {
115 let edge = object as! MapEdge
119 if type == Blocker.self {
120 let blocker = object as! Blocker
121 blockers.append(blocker)
124 if type == Opportunity.self {
125 let opportunity = object as! Opportunity
126 opportunities.append(opportunity)
129 if type == StageDimensions.self {
130 let stageDimensions = object as! StageDimensions
131 stages[stageDimensions.index] = stageDimensions.dimensions
136 func build() -> ParsedMap {
137 let mappedVertices = vertices.map { label, vertex in return vertex }
139 vertices: mappedVertices, edges: edges, blockers: blockers, opportunities: opportunities,