Share
Embed
iOSDC2018.pdf
Yuki Yasoshima
August 31, 2018
Transcript
110 slides
-
VJqPX IUUQTHJUIVCDPNIJSPLJEBJDIJVJqPX [࠷ॳʹ] Ϣʔβʔ͕ݟΔͷΛॻ͖·͢ɻ -- Ϣʔβʔ͕͢ΔߦಈΛॻ͖·͢ɻ [࣍ʹ] Ϣʔβʔ͕ݟΔͷ -- Ϣʔβʔ͕͢Δ͜ͱ̍
==> ͦͷ݁Ռ̍ Ϣʔβʔ͕͢Δ͜ͱ̎ ==> ͦͷ݁Ռ̎ [ͦͷ݁Ռ̍] ݁Ռ [ͦͷ݁Ռ̎] ݁Ռ -
digraph { graph [label = "Door", labelloc = "t", labeljust
= "l", fontcolor = "#333333", rankdir = "TB", fontname = "Osaka-Mono", fontsize = "12", charset = "UTF-8"] node [fontcolor = "#333333", fillcolor = "#F9F9F9", fontname = "Osaka-Mono", style = "solid,filled", fontsize = "10", color = "#CCCCCC"] edge [fontcolor = "#333333", fontname = "Osaka-Mono", fontsize = "9", color = "#AAAAAA"] opened [shape = "record", label = "{opened|։͍͍ͯΔ}", color = "#BBBBBB"] closing [shape = "record", label = "{closing|ด͡Δ}", color = "#40C5EE"] closed [shape = "record", label = "{closed|ด͍ͯ͡Δ}", color = "#00D54B"] opening [shape = "record", label = "{opening|։͘}", color = "#40C5EE"] "opened" -> "closing" [style = "solid", color = "#99DEF3"] "closing" -> "closed" [style = "dashed", color = "#AAAAAA"] "closed" -> "opening" [style = "solid", color = "#99DEF3"] // // Door.swift // import FlowGraph // υΞͷঢ়ଶ class Door: FlowGraphType { private(set) var isOpen: Bool = false { didSet { print("isOpen \(self.isOpen)") if let handler = self.isOpenHandler { handler(self.isOpen) } } } var isOpenHandler: ((Bool) -> Void)? enum WaitingState { case closed case opened } enum RunningState { case opening case closing } enum EventKind { case open case close } typealias Event = (kind: EventKind, object: Door) private let graph: FlowGraph<Door> init() { let builder = FlowGraphBuilder<Door>() // ด͍ͯ͡Δ builder.add(waiting: .closed) { event in if case .open = event.kind { return .run(.opening, event) } else { return .stay } } // ։͍͍ͯΔ builder.add(waiting: .opened) { event in if case .close = event.kind { return .run(.closing, event) } else { return .stay } } %PPSTXJGU %PPSEPU %PPSTWH -
$ swift build -c release $ swift run -c release
FlowGraphDotConverter ೖྗ͢ΔϑΝΠϧʢෳʣ--output ग़ྗ͢ΔϑΥϧμ $ dot -T svg ೖྗ͢Δύε -o ग़ྗ͢Δύε Ϗϧυ͢Δʢ4XJGU1BDLBHF.BOBHFSʣ 4XJGUϑΝΠϧΛղੳͯ͠(SBQIWJ[ͷܗࣜʹग़ྗ (SBQIWJ[Ͱਤʹม͢Δ $POWFSUFSͷ࣮ߦ -
'MPX(SBQI5ZQFʹద߹ͨ͠ܕΛఆٛ public protocol FlowGraphType { associatedtype WaitingState: Hashable associatedtype RunningState:
Hashable associatedtype Event ... } -
class Door: FlowGraphType { enum WaitingState { case closed case
opened } enum RunningState { case opening case closing } typealias Event = (kind: EventKind, object: Door) ... } 'MPX(SBQI5ZQFʹద߹ͨ͠ܕΛఆٛ -
class Door: FlowGraphType { enum WaitingState { case closed case
opened } enum RunningState { case opening case closing } typealias Event = (kind: EventKind, object: Door) ... } ػ͢ΔεςʔτͷܕʢFOVNਪʣ 'MPX(SBQI5ZQFʹ४ڌͨ͠ܕΛఆٛ -
class Door: FlowGraphType { enum WaitingState { case closed case
opened } enum RunningState { case opening case closing } typealias Event = (kind: EventKind, object: Door) ... } ࣮ߦ͢ΔεςʔτͷܕʢFOVNਪʣ 'MPX(SBQI5ZQFʹ४ڌͨ͠ܕΛఆٛ -
class Door: FlowGraphType { enum WaitingState { case closed case
opened } enum RunningState { case opening case closing } typealias Event = (kind: EventKind, object: Door) ... } ड͚औΔΠϕϯτͷܕʢͳΜͰʣ 'MPX(SBQI5ZQFʹ४ڌͨ͠ܕΛఆٛ enum EventKind { case open case close } -
builder.add(waiting: .closed) { event in if case .open = event.kind
{ return .run(.opening, event) } else { return .stay } } 8BJUJOH4UBUFͷొ -
builder.add(waiting: .closed) { event in if case .open = event.kind
{ return .run(.opening, event) } else { return .stay } } 8BJUJOH4UBUFͷొ ొ͢Δεςʔτ໊ -
builder.add(waiting: .closed) { event in if case .open = event.kind
{ return .run(.opening, event) } else { return .stay } } 8BJUJOH4UBUFͷొ ొ͢ΔΫϩʔδϟ -
builder.add(waiting: .closed) { event in if case .open = event.kind
{ return .run(.opening, event) } else { return .stay } } ΠϕϯτΛड͚औΔ 8BJUJOH4UBUFͷొ -
builder.add(waiting: .closed) { event in if case .open = event.kind
{ return .run(.opening, event) } else { return .stay } } 8BJUJOH4UBUFͷొ ผͷεςʔτʹભҠ͢Δ -
builder.add(waiting: .closed) { event in if case .open = event.kind
{ return .run(.opening, event) } else { return .stay } } 8BJUJOH4UBUFͷొ return .wait(.opened) ผͷεςʔτʹભҠ͢Δ -
builder.add(waiting: .closed) { event in if case .open = event.kind
{ return .run(.opening, event) } else { return .stay } } Կ͠ͳ͍ 8BJUJOH4UBUFͷొ -
builder.add(running: .opening) { event in event.object.isOpen = true return .wait(.opened)
} Կ͔ॲཧΛ͢Δ 3VOOJOH4UBUFͷొ -
builder.add(running: .opening) { event in event.object.isOpen = true return .wait(.opened)
} ผͷεςʔτʹભҠ͢Δ 3VOOJOH4UBUFͷొ ❇3VOOJOH4UBUFʹཹ·Δ͜ͱͰ͖ͳ͍ -
\ LFZEJBHOPTUJD@TUBHFTPVSDFEJBHOPTUJDTUBHFTXJGUQBSTF LFZMFOHUI LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZBDDFTTJCJMJUZTPVSDFMBOHTXJGUBDDFTTJCJMJUZJOUFSOBM
LFZCPEZMFOHUI LFZCPEZP⒎TFU LFZFMFNFOUT< \ LFZLJOETPVSDFMBOHTXJGUTUSVDUVSFFMFNUZQFSFG LFZMFOHUI LFZP⒎TFU ^ > LFZJOIFSJUFEUZQFT< \ LFZOBNF'MPX(SBQI5ZQF ^ > LFZLJOETPVSDFMBOHTXJGUEFDMDMBTT LFZMFOHUI LFZOBNF%PPS LFZOBNFMFOHUI 4USVDUVSF -
\ LFZEJBHOPTUJD@TUBHFTPVSDFEJBHOPTUJDTUBHFT LFZMFOHUI LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZBDDFTTJCJMJUZTPVSDFMBOHTXJGUBDDFTTJCJ LFZCPEZMFOHUI
LFZCPEZP⒎TFU LFZFMFNFOUT< \ LFZLJOETPVSDFMBOHTXJGUTUSVDUVSFFMFN LFZMFOHUI LFZP⒎TFU P⒎TFUͱMFOHUIͰ ཁૉͷจࣈྻͷҐஔ͕ औಘͰ͖Δ -
\ LFZEJBHOPTUJD@TUBHFTPVSDFEJBHOPTUJDTUBHFT LFZMFOHUI LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZBDDFTTJCJMJUZTPVSDFMBOHTXJGUBDDFTTJCJ LFZCPEZMFOHUI
LFZCPEZP⒎TFU LFZFMFNFOUT< \ LFZLJOETPVSDFMBOHTXJGUTUSVDUVSFFMFN LFZMFOHUI LFZP⒎TFU TVCTUSVDUVSFʹࢠͷཁૉͷྻ -
LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZBDDFTTJCJMJUZTPVSDFMBOHTXJGUBDDFTTJCJ LFZCPEZMFOHUI LFZCPEZP⒎TFU LFZFMFNFOUT<
\ LFZLJOETPVSDFMBOHTXJGUTUSVDUVSFFMFN LFZMFOHUI LFZP⒎TFU ^ > LFZJOIFSJUFEUZQFT< LJOEཁૉͷछྨ -
// Door.swift // import FlowGraph // υΞͷঢ়ଶ class Door: FlowGraphType
{ private(set) var isOpen: Bool = false didSet { print("isOpen \(self.isOpen)") if let handler = self.isOpenHa handler(self.isOpen) } } } var isOpenHandler: ((Bool) -> Void)? -
LFZJOIFSJUFEUZQFT< \ LFZOBNF'MPX(SBQI5ZQF ^ > LFZLJOETPVSDFMBOHTXJGUEFDMDMBTT LFZMFOHUI
LFZOBNF%PPS LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZSVOUJNF@OBNF@5U$@@NBJO@@%PPS LFZTVCTUSVDUVSF< \ LFZBDDFTTJCJMJUZTPVSDFMBOHTXJGUBDDFTTJCJM LJOE͕DMBTT -
LFZJOIFSJUFEUZQFT< \ LFZOBNF'MPX(SBQI5ZQF ^ > LFZLJOETPVSDFMBOHTXJGUEFDMDMBTT LFZMFOHUI
LFZOBNF%PPS LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZSVOUJNF@OBNF@5U$@@NBJO@@%PPS LFZTVCTUSVDUVSF< \ LFZBDDFTTJCJMJUZTPVSDFMBOHTXJGUBDDFTTJCJM P⒎TFUͱMFOHIͰ ΫϥεͷҐஔΛऔಘ -
LFZJOIFSJUFEUZQFT< \ LFZOBNF'MPX(SBQI5ZQF ^ > LFZLJOETPVSDFMBOHTXJGUEFDMDMBTT LFZMFOHUI
LFZOBNF%PPS LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZSVOUJNF@OBNF@5U$@@NBJO@@%PPS LFZTVCTUSVDUVSF< \ LFZBDDFTTJCJMJUZTPVSDFMBOHTXJGUBDDFTTJCJM OBNFʹDMBTTͷ໊લ -
init() { let builder = FlowGraphBuilder<Door>() // ด͍ͯ͡Δ builder.add(waiting: .closed)
{ event in if case .open = event.kind { return .run(.opening, event) } else { return .stay } } // ։͍͍ͯΔ builder.add(waiting: .opened) { event in if case .close = event.kind { return .run(.closing, event) } else { return .stay -
\ LFZCPEZMFOHUI LFZCPEZP⒎TFU LFZLJOETPVSDFMBOHTXJGUFYQSDBMM LFZMFOHUI LFZOBNFCVJMEFSBEE
LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZCPEZMFOHUI LFZCPEZP⒎TFU LFZLJOETPVSDFMBOHTXJGUFYQSBSHVNFOU LFZMFOHUI LFZOBNFXBJUJOH LFZOBNFMFOHUI FYQSDBMM͕ؔݺͼग़͠ -
\ LFZCPEZMFOHUI LFZCPEZP⒎TFU LFZLJOETPVSDFMBOHTXJGUFYQSDBMM LFZMFOHUI LFZOBNFCVJMEFSBEE
LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZCPEZMFOHUI LFZCPEZP⒎TFU LFZLJOETPVSDFMBOHTXJGUFYQSBSHVNFOU LFZMFOHUI LFZOBNFXBJUJOH LFZOBNFMFOHUI FYQSDBMMͷOBNFʹ ໊ؔ -
LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZCPEZMFOHUI
LFZCPEZP⒎TFU LFZLJOETPVSDFMBOHTXJGUFYQSBSHVNFOU LFZMFOHUI LFZOBNFXBJUJOH LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU ^ \ LFZCPEZMFOHUI LFZCPEZP⒎TFU BSHVNFOU͕ ؔͷҾ -
LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZCPEZMFOHUI
LFZCPEZP⒎TFU LFZLJOETPVSDFMBOHTXJGUFYQSBSHVNFOU LFZMFOHUI LFZOBNFXBJUJOH LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU ^ \ LFZCPEZMFOHUI LFZCPEZP⒎TFU BSHVNFOUͷOBNFʹ Ҿͷϥϕϧ -
LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU LFZTVCTUSVDUVSF< \ LFZCPEZMFOHUI
LFZCPEZP⒎TFU LFZLJOETPVSDFMBOHTXJGUFYQSBSHVNFOU LFZMFOHUI LFZOBNFXBJUJOH LFZOBNFMFOHUI LFZOBNFP⒎TFU LFZP⒎TFU ^ \ LFZCPEZMFOHUI LFZCPEZP⒎TFU Ҿͷͷ෦Ґஔ͚ͩ -
init() { let builder = FlowGraphBuilder<Door>() // ด͍ͯ͡Δ builder.add(waiting: .closed)
{ event in if case .open = event.kind { return .run(.opening, event) } else { return .stay } } // ։͍͍ͯΔ builder.add(waiting: .opened) { event in if case .close = event.kind { return .run(.closing, event) } else { return .stay SFUVSOͷҐஔΘ͔Βͳ͍ -
{type:comment offset:0, length:3, content:'// '}, {type:comment offset:3, length:15, content:'// Door.swift
'}, {type:comment offset:18, length:3, content:'// '}, {type:keyword offset:22, length:6, content:'import'}, {type:identifier offset:29, length:9, content:'FlowGraph'}, {type:comment offset:40, length:19, content:'// υΞͷঢ়ଶ '}, {type:keyword offset:59, length:5, content:'class'}, {type:identifier offset:65, length:4, content:'Door'}, {type:typeidentifier offset:71, length:13, content:'FlowGraphType'}, {type:attributeBuiltin offset:91, length:12, content:'private(set)'}, {type:keyword offset:104, length:3, content:'var'}, {type:identifier offset:108, length:6, content:'isOpen'}, {type:typeidentifier offset:116, length:4, content:'Bool'}, {type:keyword offset:123, length:5, content:'false'}, {type:keyword offset:139, length:6, content:'didSet'}, {type:identifier offset:160, length:5, content:'print'}, {type:string offset:166, length:8, content:'"isOpen '}, {type:stringInterpolationAnchor offset:175, length:1, content:'('}, {type:keyword offset:176, length:4, content:'self'}, {type:identifier offset:181, length:6, content:'isOpen'}, {type:stringInterpolationAnchor offset:187, length:1, content:')'}, -
init() { let builder = FlowGraphBuilder<Door>() // ด͍ͯ͡Δ builder.add(waiting: .closed)
{ event in if case .open = event.kind { return .run(.opening, event) } else { return .stay } } // ։͍͍ͯΔ builder.add(waiting: .opened) { event in if case .close = event.kind { return .run(.closing, event) } else { return .stay ؔͷCPEZ෦ -
'}, {type:identifier offset:800, length:7, content:'builder'}, {type:identifier offset:808, length:3, content:'add'}, {type:identifier
offset:812, length:7, content:'waiting'}, {type:identifier offset:822, length:6, content:'closed'}, {type:identifier offset:832, length:5, content:'event'}, {type:keyword offset:838, length:2, content:'in'}, {type:keyword offset:853, length:2, content:'if'}, {type:keyword offset:856, length:4, content:'case'}, {type:identifier offset:862, length:4, content:'open'}, {type:identifier offset:869, length:5, content:'event'}, {type:identifier offset:875, length:4, content:'kind'}, {type:keyword offset:898, length:6, content:'return'}, {type:identifier offset:906, length:3, content:'run'}, {type:identifier offset:911, length:7, content:'opening'}, {type:identifier offset:920, length:5, content:'event'}, {type:keyword offset:941, length:4, content:'else'}, {type:keyword offset:964, length:6, content:'return'}, {type:identifier offset:972, length:4, content:'stay'}, {type:comment offset:1018, length:19, content:'// ։͍͍ͯΔ '}, {type:identifier offset:1045, length:7, content:'builder'}, {type:identifier offset:1053, length:3, content:'add'}, {type:identifier offset:1057, length:7, content:'waiting'}, {type:identifier offset:1067, length:6, content:'opened'}, -
'}, {type:identifier offset:800, length:7, content:'builder'}, {type:identifier offset:808, length:3, content:'add'}, {type:identifier
offset:812, length:7, content:'waiting'}, {type:identifier offset:822, length:6, content:'closed'}, {type:identifier offset:832, length:5, content:'event'}, {type:keyword offset:838, length:2, content:'in'}, {type:keyword offset:853, length:2, content:'if'}, {type:keyword offset:856, length:4, content:'case'}, {type:identifier offset:862, length:4, content:'open'}, {type:identifier offset:869, length:5, content:'event'}, {type:identifier offset:875, length:4, content:'kind'}, {type:keyword offset:898, length:6, content:'return'}, {type:identifier offset:906, length:3, content:'run'}, {type:identifier offset:911, length:7, content:'opening'}, {type:identifier offset:920, length:5, content:'event'}, {type:keyword offset:941, length:4, content:'else'}, {type:keyword offset:964, length:6, content:'return'}, {type:identifier offset:972, length:4, content:'stay'}, {type:comment offset:1018, length:19, content:'// ։͍͍ͯΔ '}, {type:identifier offset:1045, length:7, content:'builder'}, {type:identifier offset:1053, length:3, content:'add'}, {type:identifier offset:1057, length:7, content:'waiting'}, {type:identifier offset:1067, length:6, content:'opened'}, -
waiting closed event in if case open event kind return
run opening event else return stay ಄͔Βॱ൪ʹݟ͍ͯ͘ -
waiting closed event in if case open event kind return
run opening event else return stay XBJUJOH͔ SVOOJOH ͳΒʜ XBJUJOH -
waiting closed event in if case open event kind return
run opening event else return stay ࣍ εςʔτ໊ XBJUJOH DMPTFE -
waiting closed event in if case open event kind return
run opening event else return stay JOͷޙ͔Β Ϋϩʔδϟ XBJUJOH DMPTFE -
waiting closed event in if case open event kind return
run opening event else return stay SFUVSOΛ୳͢ XBJUJOH DMPTFE -
waiting closed event in if case open event kind return
run opening event else return stay SVO͔ XBJUͳΒʜ XBJUJOH DMPTFE -
waiting closed event in if case open event kind return
run opening event else return stay ਐΉઌͷ εςʔτ໊ XBJUJOH DMPTFE PQFOJOH -
waiting closed event in if case open event kind return
run opening event else return stay SFUVSOΛ୳͢ XBJUJOH DMPTFE PQFOJOH -
waiting closed event in if case open event kind return
run opening event else return stay TUBZͳΒແࢹ XBJUJOH DMPTFE PQFOJOH -
digraph { graph [label = "Door", labelloc = "t", labeljust
= "l", fontcolor = "#333333", rankdir = "TB", fontname = "Osaka-Mono", fontsize = "12", charset = "UTF-8"] node [fontcolor = "#333333", fillcolor = "#F9F9F9", fontname = "Osaka-Mono", style = "solid,filled", fontsize = "10", color = "#CCCCCC"] edge [fontcolor = "#333333", fontname = "Osaka-Mono", fontsize = "9", color = "#AAAAAA"] opened [shape = "record", label = "{opened|։͍͍ͯΔ}", color = "#BBBBBB"] closing [shape = "record", label = "{closing|ด͡Δ}", color = "#40C5EE"] closed [shape = "record", label = "{closed|ด͍ͯ͡Δ}", color = "#00D54B"] opening [shape = "record", label = "{opening|։͘}", color = "#40C5EE"] "opened" -> "closing" [style = "solid", color = "#99DEF3"] "closing" -> "closed" [style = "dashed", color = "#AAAAAA"] "closed" -> "opening" [style = "solid", color = "#99DEF3"] "opening" -> "opened" [style = "dashed", color = "#AAAAAA"] } %PPSEPU -
digraph { graph [label = "Door", labelloc = "t fontcolor
= "#333333", rankdi fontname = "Osaka-Mono", font charset = "UTF-8"] node [fontcolor = "#333333", fillcol fontname = "Osaka-Mono", style fontsize = "10", color = "#CCC edge [fontcolor = "#333333", fontnam fontsize = "9", color = "#AAAA opened [shape = "record", label = "{ EJHSBQI༗άϥϑ %PPSEPU -
charset = "UTF-8"] node [fontcolor = "#333333", fillcolor = "
fontname = "Osaka-Mono", style = "so fontsize = "10", color = "#CCCCCC"] edge [fontcolor = "#333333", fontname = "O fontsize = "9", color = "#AAAAAA"] opened [shape = "record", label = "{opened|։͍͍ͯΔ}", color = "#BBBBBB"] closing [shape = "record", label = "{closi color = "#40C5EE"] closed [shape = "record", label = "{closed color = "#00D54B"] opening [shape = "record", label = "{openi color = "#40C5EE"] ϊʔυͷ໊લ ϊʔυ -
charset = "UTF-8"] node [fontcolor = "#333333", fillcolor = "
fontname = "Osaka-Mono", style = "so fontsize = "10", color = "#CCCCCC"] edge [fontcolor = "#333333", fontname = "O fontsize = "9", color = "#AAAAAA"] opened [shape = "record", label = "{opened|։͍͍ͯΔ}", color = "#BBBBBB"] closing [shape = "record", label = "{closi color = "#40C5EE"] closed [shape = "record", label = "{closed color = "#00D54B"] opening [shape = "record", label = "{openi color = "#40C5EE"] <>ͷதʹදࣔ͞ΕΔத ϊʔυ -
closing [shape = "record", label = "{closing color = "#40C5EE"]
closed [shape = "record", label = "{closed|ด color = "#00D54B"] opening [shape = "record", label = "{opening color = "#40C5EE"] "opened" -> "closing" [style = "solid", color = "#99DEF3"] "closing" -> "closed" [style = "dashed", col "closed" -> "opening" [style = "solid", colo "opening" -> "opened" [style = "dashed", col } Τοδ ͰϊʔυΛͭͳ͛Δ