Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Swift Function Builders to init WhereClause #9

Open
wants to merge 4 commits into
base: m@ster
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -76,7 +76,7 @@ let varColor = Var("color")
let varAge = Var("age")

let query = SelectQuery(
where: WhereClause(patterns:
where: WhereClause {
subject(varS)
.rdfTypeIsImasIdol()
.imasTitle(is: .literal("CinderellaGirls", lang: "en"))
@@ -86,7 +86,7 @@ let query = SelectQuery(
.imasColor(is: varColor)
.foafAge(is: varAge)
}
.triples),
},
having: [.logical(varHeight <= 149)],
order: [.by(varHeight)],
limit: 100)
@@ -11,20 +11,17 @@ struct EpisodeSong: Codable {

let query = SelectQuery(
capture: .vars([Var("n"), Var("st"), Var("title")]),
where: WhereClause(patterns:
where: WhereClause {
subject(Var("live")).rdfTypeIsPrismLive()
.prismPerformer(is: "solami_smile")
.prismSongPerformed(is: Var("song"))
.prismLiveOfEpisode(is: Var("ep"))
.triples
+ subject(Var("song")).rdfTypeIsPrismSong()
.prismName(is: Var("title"))
.triples
+ subject(Var("ep")).rdfTypeIsPrismEpisode()
.prism話数(is: Var("n"))
.prismサブタイトル(is: Var("st"))
.triples
),
subject(Var("song")).rdfTypeIsPrismSong()
.prismName(is: Var("title"))
subject(Var("ep")).rdfTypeIsPrismEpisode()
.prism話数(is: Var("n"))
.prismサブタイトル(is: Var("st"))
},
order: [.by(Var("n"))],
limit: 100)

@@ -23,12 +23,12 @@ class ViewController: NSViewController {

let heightQuery = Query(select: SelectQuery(
capture: .vars([Var("o"), Var("h")]),
where: WhereClause(patterns:
where: WhereClause {
subject(Var("s"))
.rdfTypeIsImasIdol()
.rdfsLabel(is: Var("o"))
.schemaHeight(is: Var("h"))
.triples),
},
order: [.asc(v: Var("h")), .by(Var("s"))],
limit: 10))
NSLog("%@", "heightQuery = \n\n\(Serializer.serialize(heightQuery))")
@@ -38,16 +38,15 @@ class ViewController: NSViewController {
capture: SelectClause.Capture.expressions([
(Var("ユニット名"), nil),
(Var("メンバー"), Expression(.groupConcat(distinct: false, expression: Expression(Var("名前")), separator: ", ")))]),
where: WhereClause(patterns:
where: WhereClause {
subject(Var("s"))
.rdfTypeIsImasUnit()
.schemaName(is: Var("ユニット名"))
.schemaMember(is: Var("m"))
.triples
+ subject(Var("m"))
.rdfTypeIsImasIdol()
.rdfsLabel(is: Var("名前"))
.triples),
subject(Var("m"))
.rdfTypeIsImasIdol()
.rdfsLabel(is: Var("名前"))
},
group: [.var(Var("ユニット名"))],
order: [.by(Var("ユニット名"))],
limit: 10))
@@ -56,30 +55,29 @@ class ViewController: NSViewController {

let mayukiki = Query(select: SelectQuery(
capture: .vars([Var("利き手"), Var("名前")]),
where: WhereClause(patterns:
where: WhereClause {
subject(Var("s"))
.rdfTypeIsImasIdol()
.rdfsLabel(is: Var("name"))
.imasHandedness(is: Var("利き手"))
.filter(.CONTAINS(v: Var("name"), sub: "まゆ"))
.triples
+ subject(Var("idol"))
.rdfTypeIsImasIdol()
.imasHandedness(is: Var("利き手"))
.rdfsLabel(is: Var("名前"))
.triples),
subject(Var("idol"))
.rdfTypeIsImasIdol()
.imasHandedness(is: Var("利き手"))
.rdfsLabel(is: Var("名前"))
},
limit: 10))
NSLog("%@", "mayukiki = \n\n\(Serializer.serialize(mayukiki))")
print("\n\n\n")

let liveSongs = SelectQuery(
capture: .expressions([(Var("回数"), .init(.count(distinct: false, expression: .init(Var("name"))))),
(Var("楽曲名"), .init(.sample(distinct: false, expression: .init(Var("name")))))]),
where: WhereClause(patterns:
where: WhereClause {
subject(Var("s"))
.rdfTypeIsImasSetlistNumber()
.schemaName(is: Var("name"))
.triples),
},
group: [.var(Var("name"))],
having: [.logical(Var("回数") > 4)],
order: [.desc(v: Var("回数"))],
@@ -91,12 +89,12 @@ class ViewController: NSViewController {
let varName = Var("name")
let varHeight = Var("身長")
let idolNames = SelectQuery(
where: WhereClause(patterns:
where: WhereClause {
subject(varS).rdfTypeIsImasIdol()
.imasNameKana(is: varName)
.schemaHeight(is: varHeight)
.optional {$0.imasColor(is: Var("color"))}
.triples),
},
having: [.logical(varHeight <= 149)],
order: [.by(.RAND)],
limit: 10)
@@ -114,13 +112,13 @@ class ViewController: NSViewController {
(varName, Expression(.sample(distinct: false, expression: .init(Var("なまえ"))))),
(Var("date"), Expression(.sample(distinct: false, expression: .init(Var("誕生日"))))),
]),
where: WhereClause(patterns:
where: WhereClause {
subject(varS)
.rdfTypeIsImasIdol()
.schemaBirthDate(is: Var("誕生日"))
.rdfsLabel(is: Var("なまえ"))
.filter(.regex(v: Var("誕生日"), pattern: today))
.triples),
},
group: [.var(Var("なまえ"))],
order: [.by(varName)]
)
@@ -148,22 +146,20 @@ class ViewController: NSViewController {
(Var("name"), Expression(Var("ユニット名"))),
(Var("memberNames_concat"), Expression(.groupConcat(distinct: false, expression: Expression(Var("ユニットメンバー名")), separator: ","))),
(Var("types_concat"), Expression(.groupConcat(distinct: false, expression: Expression(Var("ユニットメンバー属性")), separator: ",")))]),
where: WhereClause(patterns:
where: WhereClause {
subject(Var("橘ありす"))
.rdfTypeIsImasIdol()
.rdfsLabel(is: "橘ありす")
.schemaMemberOf(is: Var("ありす参加ユニット"))
.triples
+ subject(Var("ありす参加ユニット"))
.rdfTypeIsImasUnit()
.schemaName(is: Var("ユニット名"))
.schemaMember(is: Var("ユニットメンバー"))
.triples
+ subject(Var("ユニットメンバー"))
.rdfTypeIsImasIdol()
.rdfsLabel(is: Var("ユニットメンバー名"))
.imasType(is: Var("ユニットメンバー属性"))
.triples),
subject(Var("ありす参加ユニット"))
.rdfTypeIsImasUnit()
.schemaName(is: Var("ユニット名"))
.schemaMember(is: Var("ユニットメンバー"))
subject(Var("ユニットメンバー"))
.rdfTypeIsImasIdol()
.rdfsLabel(is: Var("ユニットメンバー名"))
.imasType(is: Var("ユニットメンバー属性"))
},
group: [.var(Var("ユニット名"))],
order: [.by(.count(distinct: false, expression: Expression(Var("ユニットメンバー"))))],
limit: 100)
@@ -105,3 +105,27 @@ public func | (lhs: PropertyListPathNotEmpty.Verb, rhs: PropertyListPathNotEmpty
default: fatalError("not yet implemented")
}
}

// MARK: - Function Builders
public protocol TriplesBuildable {
var triples: [GroupGraphPatternSubType] { get }
}
extension TripleBuilder: TriplesBuildable {}
struct TriplesList: TriplesBuildable {
var builders: [TriplesBuildable]
var triples: [GroupGraphPatternSubType] {builders.flatMap {$0.triples}}
}

@_functionBuilder
public struct WhereClauseBuilder {
public static func buildBlock(_ builders: TriplesBuildable...) -> TriplesBuildable {
TriplesList(builders: builders)
}
}

extension WhereClause {
public init(@WhereClauseBuilder builder: () -> TriplesBuildable) {
self.init(patterns: builder().triples)
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.