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]
34 var shape: VertexShape = .circle
37 enum VertexShape: String {
47 let destination: CGPoint
59 let destination: CGPoint
62 struct StageDimensions {
64 let dimensions: CGFloat
67 private let defaultDimensions: [CGFloat] = [
73 // MARK: - MapParserStrategy protocol
75 protocol MapParserStrategy {
76 func canHandle(line: String) -> Bool
77 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any)
80 struct AnyMapParserStrategy: MapParserStrategy {
82 private let base: MapParserStrategy
84 init<T: MapParserStrategy>(_ base: T) {
88 func canHandle(line: String) -> Bool {
89 return base.canHandle(line: line)
91 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
92 return base.handle(index: index, line: line, vertices: vertices)
96 // MARK: - Map Builder
99 var vertices: [String: Vertex] = [:]
100 private var edges: [MapEdge] = []
101 private var blockers: [Blocker] = []
102 private var opportunities: [Opportunity] = []
103 private var stages: [CGFloat] = defaultDimensions
105 func addObjectToMap(type: Any.Type, object: Any) {
106 if type == Vertex.self {
107 let vertex = object as! Vertex
108 vertices[vertex.label] = vertex
111 if type == MapEdge.self {
112 let edge = object as! MapEdge
116 if type == Blocker.self {
117 let blocker = object as! Blocker
118 blockers.append(blocker)
121 if type == Opportunity.self {
122 let opportunity = object as! Opportunity
123 opportunities.append(opportunity)
126 if type == StageDimensions.self {
127 let stageDimensions = object as! StageDimensions
128 stages[stageDimensions.index] = stageDimensions.dimensions
133 func build() -> ParsedMap {
134 let mappedVertices = vertices.map { label, vertex in return vertex }
136 vertices: mappedVertices, edges: edges, blockers: blockers, opportunities: opportunities,