Pro Yearly is on sale from $80 to $50! »

Kotlinでクエリ言語のクライアントを作った話

 Kotlinでクエリ言語のクライアントを作った話

集まれKotlin好き!Kotlin愛好会 vol.10のLT資料です

https://love-kotlin.connpass.com/event/124099/

Otonashi: https://github.com/subroh0508/otonashi

7c3b3366947123ba6772698b09edf4e2?s=128

subroh_0508

April 25, 2019
Tweet

Transcript

  1. ,PUMJOͰΫΤϦݴޠͷ ΫϥΠΞϯτΛ࡞ͬͨ࿩ ʹ͜͠Γ͞ͿΖʙ!TVCSPI@

  2. ࣗݾ঺հ w ʹ͜͠Γ͞ͿΖʙ w גࣜձࣾ#FBS5BJM w "OESPJEΤϯδχΞ ,PUMJO+BWB ˑ w

    8FCΤϯδχΞ 3BJMT3FBDU  w ,PUMJOྺ໿೥ ೥݄ʙ  w झຯͰαʔόαΠυɺ,PUMJO+4 5XJUUFS*%!TVCSPI@
  3. ໨࣍ ,PUMJO੡41"32-ΫϥΠΞϯτʮ0UPOBTIJʯͷ঺հ w 41"32-ͷղઆ w ੡࡞ͷϞνϕʔγϣϯ ࣮૷ղઆ ίʔσΟϯάσϞ

  4. ࡞ͬͨϞϊ w ,PUMJO੡41"32-ΫϥΠΞϯτʮ0UPOBTIJʯ w (JUIVCTVCSPIPUPOBTIJ w 3%'༻ΫΤϦݴޠͷ41"32-Λ,PUMJOͰॻ͖΍͘͢͢ΔϥΠϒϥϦ w JOpYؔ਺ɾϥϜμࣜɾԋࢉࢠΦʔόʔϩʔυ౳Λ׆༻

  5. 41"32-ͱ͸ w 3%'༻ΫΤϦݴޠ ن֨8$  w 3%'Ͱද͞Εͨ-JOLFE0QFO%BUBͷݕࡧʹ࢖͏ΫΤϦݴޠ w ྫ%#1FEJB 8JLJQFEJBΛ3%'ԽɺΫΤϦݕࡧ͠΍ͨ͘͢͠Ϟϊ

  6. ࣮ࡍͷΫΤϦ w ೔ຊͷ౎ಓ෎ݝ໊ͱ8JLJQFEJBͷΞϒετϥΫτΛऔಘ select ?pref_name ?abstract where { ?pref rdf:type

    schema:AdministrativeArea; dbo:country ?country; rdfs:label ?pref_name; dbo:abstract ?abstract. filter( contains(str(?country), "日本") ) } ݁Ռ ੜΫΤϦ
  7. ੡࡞ͷϞνϕʔγϣϯ w ༷ʑͳΤϯυϙΠϯτ͕ଘࡏ w ࠃཱࠃձਤॻؗͷయڌσʔλ IUUQJEOEMHPKQJOGPSNBUJPOTQBSRM  w ژ౎ࢢͷࢪઃσʔλ IUUQTTQBSRMDJUZLZPUPMHKQTQBSRM

     w JN!TQBSRM IUUQTTQBSRMDSTTOLZYZ[JNBT ৭ʑͳΤϯυϙΠϯτ͕͋ͬͯ໘നͦ͏ "DUJWF3FDPSEͬΆ͘ΧδϡΞϧʹΫΤϦୟ͚ͨΒָͦ͠͏ ͱ͍͏͔JN!TQBSRMͰແݶʹ༡ͼ͍ͨ ຊԻ ˞ΞΠυϧϚελʔͷΩϟϥΫλʔɾָۂɾ୆ࢺ౳ͷσʔλ
  8. 0WFSWJFX w ೔ຊͷ౎ಓ෎ݝ໊ͱ8JLJQFEJBͷΞϒετϥΫτΛऔಘ select ?pref_name ?abstract where { ?pref rdf:type

    schema:AdministrativeArea; dbo:country ?country; rdfs:label ?pref_name; dbo:abstract ?abstract. filter( contains(str(?country), "日本") ) } ੜΫΤϦ client.where { v("pref") be { rdfP.type to schemaC.administrativeArea and iri("dbo:country") to v("country") and rdfsP.label to v("pref_name") and iri("dbo:abstract") to v("abstract") } filter { contains(str(v("country")), "日本") } }.select { + v("pref_name") + v("abstract") } 0UPOBTIJ
  9. ࣮૷ղઆ w 5SJQMFͷදݱ w ओޠɾड़ޠɾ໨తޠͰߏ੒͞ΕΔʮτϦϓϧɾύλʔϯʯ ?pref rdf:type schema:AdministrativeArea. v("pref") be

    { rdfP.type to schemaC.administrativeArea }
  10. ࣮૷ղઆ w 5SJQMFͷදݱ w ओޠɾड़ޠɾ໨తޠͰߏ੒͞ΕΔʮτϦϓϧɾύλʔϯʯ ?pref rdf:type schema:AdministrativeArea. v("pref") be

    { rdfP.type to schemaC.administrativeArea } ओޠ ड़ޠ ໨తޠ ओޠ ड़ޠ ໨తޠ
  11. ࣮૷ղઆ w 5SJQMFͷදݱ w ओޠɾड़ޠɾ໨తޠͰߏ੒͞ΕΔʮτϦϓϧɾύλʔϯʯ ?pref rdf:type schema:AdministrativeArea. v("pref") be

    { rdfP.type to schemaC.administrativeArea } ओޠ ड़ޠ ໨తޠ ओޠ ड़ޠ ໨తޠ JOpYؔ਺
  12. JOpYؔ਺ w ʮؔ਺ͷϨγʔόʔʯʮؔ਺໊ʯʮҾ਺ʯΛฒ΂ͯॻ͚Δؔ਺ w ϝϯόؔ਺PS֦ுؔ਺ w ୯ҰͷҾ਺ w ʮJOpYʯΩʔϫʔυ͕͍͍ͭͯΔ

  13. ࣮૷ͨؔ͠਺ infix fun TripleItem.be(pattern: TriplePattern.() -> Unit): GraphPattern { patterns.add(TriplePattern(this,

    vocabulary).apply(pattern)) return this@GraphPattern } infix fun TripleItem.to(`object`: TripleItem): Triple<TripleItem, TripleItem, TripleItem> { triples.addAll(listOf(Triples(subject, this, `object`), Period)) return Triple(subject, this, `object`) } CFؔ਺ (SBQI1BUUFSOΫϥε಺ UPؔ਺ 5SJQMF1BUUFSOΫϥε಺
  14. ࣮૷ͨؔ͠਺ infix fun TripleItem.be(pattern: TriplePattern.() -> Unit): GraphPattern { patterns.add(TriplePattern(this,

    vocabulary).apply(pattern)) return this@GraphPattern } infix fun TripleItem.to(`object`: TripleItem): Triple<TripleItem, TripleItem, TripleItem> { triples.addAll(listOf(Triples(subject, this, `object`), Period)) return Triple(subject, this, `object`) } CFؔ਺ (SBQI1BUUFSOΫϥε಺ UPؔ਺ 5SJQMF1BUUFSOΫϥε಺
  15. ࣮૷ͨؔ͠਺ infix fun TripleItem.be(pattern: TriplePattern.() -> Unit): GraphPattern { patterns.add(TriplePattern(this,

    vocabulary).apply(pattern)) return this@GraphPattern } infix fun TripleItem.to(`object`: TripleItem): Triple<TripleItem, TripleItem, TripleItem> { triples.addAll(listOf(Triples(subject, this, `object`), Period)) return Triple(subject, this, `object`) } CFؔ਺ (SBQI1BUUFSOΫϥε಺ UPؔ਺ 5SJQMF1BUUFSOΫϥε಺
  16. ࣮૷ͨؔ͠਺ infix fun TripleItem.be(pattern: TriplePattern.() -> Unit): GraphPattern { patterns.add(TriplePattern(this,

    vocabulary).apply(pattern)) return this@GraphPattern } infix fun TripleItem.to(`object`: TripleItem): Triple<TripleItem, TripleItem, TripleItem> { triples.addAll(listOf(Triples(subject, this, `object`), Period)) return Triple(subject, this, `object`) } CFؔ਺ (SBQI1BUUFSOΫϥε಺ UPؔ਺ 5SJQMF1BUUFSOΫϥε಺
  17. ࣮૷ղઆ w 5SJQMFͷදݱ w ओޠɾड़ޠɾ໨తޠͰߏ੒͞ΕΔʮτϦϓϧɾύλʔϯʯ ?pref rdf:type schema:AdministrativeArea. v("pref") be

    { rdfP.type to schemaC.administrativeArea } ओޠ ड़ޠ ໨తޠ ओޠ ड़ޠ ໨తޠ
  18. ࣮૷ղઆ w 5SJQMFͷදݱ w ओޠɾड़ޠɾ໨తޠͰߏ੒͞ΕΔʮτϦϓϧɾύλʔϯʯ ?pref rdf:type schema:AdministrativeArea. v("pref") be

    { rdfP.type to schemaC.administrativeArea } ओޠ ड़ޠ ໨తޠ ओޠ ड़ޠ ໨తޠ ϥϜμࣜ
  19. ϥϜμࣜ w ؔ਺Λએݴͤͣɺͦͷ৔Ͱม਺ͷΑ͏ʹ౉͢ w ؔ਺ͷϨγʔόʔΛ ࢦఆ͢Δ͜ͱ΋Մೳ class HTML { fun

    body() { ... } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } html { // レシーバ付きラムダがここから始まる body() // レシーバオブジェクトのメソッドを呼んでいる }
  20. ϥϜμࣜ w ؔ਺Λએݴͤͣɺͦͷ৔Ͱม਺ͷΑ͏ʹ౉͢ w ؔ਺ͷϨγʔόʔΛ ࢦఆ͢Δ͜ͱ΋Մೳ class HTML { fun

    body() { ... } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } html { // レシーバ付きラムダがここから始まる body() // レシーバオブジェクトのメソッドを呼んでいる } Ϩγʔόʔ෇ϥϜμࣜ ˠϨγʔόʔʹ)5.-ΫϥεΛࢦఆ
  21. ϥϜμࣜ w ؔ਺Λએݴͤͣɺͦͷ৔Ͱม਺ͷΑ͏ʹ౉͢ w ؔ਺ͷϨγʔόʔΛ ࢦఆ͢Δ͜ͱ΋Մೳ class HTML { fun

    body() { ... } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } html { // レシーバ付きラムダがここから始まる body() // レシーバオブジェクトのメソッドを呼んでいる } )5.-ܕΠϯελϯεੜ੒ ˠϨγʔόʔࢦఆɺ࣮ߦ
  22. ϥϜμࣜ w ؔ਺Λએݴͤͣɺͦͷ৔Ͱม਺ͷΑ͏ʹ౉͢ w ؔ਺ͷϨγʔόʔΛ ࢦఆ͢Δ͜ͱ΋Մೳ class HTML { fun

    body() { ... } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html } html { // レシーバ付きラムダがここから始まる body() // レシーバオブジェクトのメソッドを呼んでいる } JOJU͕࣮ࡍʹ࣮ߦ͞Ε͍ͯΔͱ͜Ζ ˠதׅހJOJUͷॲཧ ˠதׅހͷதͷUIJT͸)5.-Ϋϥε ɹʹͳΔ
  23. ࣮૷ղઆ w ͳͥϥϜμࣜΛ࢖͏ͷ͔ w ϥϜμࣜΛ࢖Θͳ͍৔߹ w CF΋UP΋5SJQMF*UFNΫϥεಉ࢜Λͭͳ͙JOpYؔ਺ ˠແݶʹܨ͛ΒΕͯ͠·͏ʂɹߏจΤϥʔ͕࣮ߦ͢Δ·Ͱ෼͔Βͳ͍ʂ v("pref") be

    rdfP.type be schemaC.administrativeArea be … v("pref") to rdfP.type to schemaC.administrativeArea to … v("pref") be { rdfP.type to schemaC.administrativeArea }
  24. ࣮૷ղઆ w ϥϜμࣜΛ࢖ͬͨ৔߹ w CFؔ਺ˠ(SBQI1BUUFSOΫϥε಺ʹ࣮૷ɺҾ਺͸5SJQMF1BUUFSO 6OJU w UPؔ਺ˠ5SJQMF1BUUFSOΫϥε಺ʹ࣮૷ infix fun

    TripleItem.be(pattern: TriplePattern.() -> Unit): GraphPattern { patterns.add(TriplePattern(this, vocabulary).apply(pattern)) return this@GraphPattern } ʜCF\ʜUPʜ^ͱ͔͠ॻ͚ͳ͍Α͏ʹڧ੍ʂ ΫΤϦΛୟ͘·Ͱ෼͔Βͳ͔ͬͨߏจΤϥʔ͕࣮ߦલʹ෼͔Δʂ
  25. ίʔσΟϯάσϞ

  26. ·ͱΊ w ,PUMJOͰ41"32-ΫϥΠΞϯτΛ࣮૷ͨ͠ w ύζϧͬΆָ͍ͯ͘͠ʂ w ීஈ͋·Γ৮Βͳ͍,PUMJOͷݴޠ࢓༷Λ༗ޮʹ׆༻Ͱ͖ͯ໘ന͍ʂ w ϓϩμΫτʹ׆͔͢ʹ͸ w

    αʔόʔαΠυ,PUMJOͰෳࡶͳݕࡧΫΤϦΛؾ࣋ͪΑ͘ॻ͖͍ͨ࣌ w ۀ຿ϩδοΫʹϢʔβʔ͕ΧελϜՄೳͳܭࢉ͕ࣜݱΕΔ࣌ ྫฐࣾͷܦඅਫ਼ࢉαʔϏεͷ೔౰ܭࢉࣜ
  27. IUUQTHJUIVCDPNTVCSPIPUPOBTIJ

  28. IUUQTHJUIVCDPNTVCSPIPUPOBTIJ

  29. ,PUPSJ3BUF#BEHF IUUQTHJUIVCDPNTVCSPI,PUPSJ3BUF#BEHF

  30. ,PUPSJ3BUF#BEHF IUUQTHJUIVCDPNTVCSPI,PUPSJ3BUF#BEHF ˞͜ͷϨϙδτϦࣗମ͸+BWBTDSJQU