-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
141 lines (120 loc) · 3.08 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Knowledge Graph: SPOCK
Copyright (C) 2016 - 2023 Dmitry Kolesnikov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package spock
//
// The file define streaming protocol for hexastore
//
import (
"github.com/kshard/xsd"
)
// Stream of knowledge statements ⟨s,p,o,c,k⟩
type Stream interface {
Head() SPOCK
Next() bool
FMap(func(SPOCK) error) error
}
type filter struct {
pred func(SPOCK) bool
stream Stream
}
func (filter *filter) Head() SPOCK {
return filter.stream.Head()
}
func (filter *filter) Next() bool {
for {
if !filter.stream.Next() {
return false
}
if filter.pred(filter.stream.Head()) {
return true
}
}
}
func (filter *filter) FMap(f func(SPOCK) error) error {
for filter.Next() {
if err := f(filter.Head()); err != nil {
return err
}
}
return nil
}
func NewFilter(pred func(SPOCK) bool, stream Stream) Stream {
return &filter{pred: pred, stream: stream}
}
func NewFilterO(hint Hint, q *Predicate[xsd.Value], stream Stream) Stream {
switch hint {
case HINT_MATCH:
return NewFilter(
func(spock SPOCK) bool { return xsd.Compare(spock.O, q.Value) == 0 },
stream,
)
case HINT_FILTER_PREFIX:
return NewFilter(
func(spock SPOCK) bool { return xsd.HasPrefix(spock.O, q.Value) },
stream,
)
case HINT_FILTER:
switch q.Clause {
case LT:
return NewFilter(
func(spock SPOCK) bool { return xsd.Compare(spock.O, q.Value) == -1 },
stream,
)
case GT:
return NewFilter(
func(spock SPOCK) bool { return xsd.Compare(spock.O, q.Value) == 1 },
stream,
)
case IN:
return NewFilter(
func(spock SPOCK) bool {
return xsd.Compare(spock.O, q.Value) >= 0 && xsd.Compare(spock.O, q.Other) <= 0
},
stream,
)
}
}
return stream
}
func NewFilterP(hint Hint, q *Predicate[xsd.AnyURI], stream Stream) Stream {
switch hint {
case HINT_MATCH:
return NewFilter(
func(spock SPOCK) bool { return spock.P == q.Value },
stream,
)
// case HINT_FILTER_PREFIX:
// return NewFilter(
// func(spock SPOCK) bool { return strings.HasPrefix(string(spock.P), string(q.Value)) },
// stream,
// )
}
return stream
}
func NewFilterS(hint Hint, q *Predicate[xsd.AnyURI], stream Stream) Stream {
switch hint {
case HINT_MATCH:
return NewFilter(
func(spock SPOCK) bool { return spock.S == q.Value },
stream,
)
// case HINT_FILTER_PREFIX:
// return NewFilter(
// func(spock SPOCK) bool { return strings.HasPrefix(string(spock.S), string(q.Value)) },
// stream,
// )
}
return stream
}