本文整理汇总了Golang中github.com/google/badwolf/triple/literal.DefaultBuilder函数的典型用法代码示例。如果您正苦于以下问题:Golang DefaultBuilder函数的具体用法?Golang DefaultBuilder怎么用?Golang DefaultBuilder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DefaultBuilder函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSumAccumulators
func TestSumAccumulators(t *testing.T) {
// int64 sum accumulator.
var (
iv interface{}
ia = NewSumInt64LiteralAccumulator(0)
)
for i := int64(0); i < 5; i++ {
l, _ := literal.DefaultBuilder().Build(literal.Int64, i)
iv, _ = ia.Accumulate(l)
}
if got, want := iv.(int64), int64(10); got != want {
t.Errorf("Int64 sum accumulator failed; got %d, want %d", got, want)
}
// float64 sum accumulator.
var (
fv interface{}
fa = NewSumFloat64LiteralAccumulator(0)
)
for i := float64(0); i < 5; i += 1.0 {
l, _ := literal.DefaultBuilder().Build(literal.Float64, i)
fv, _ = fa.Accumulate(l)
}
if got, want := fv.(float64), float64(10); got != want {
t.Errorf("Int64 sum accumulator failed; got %f, want %f", got, want)
}
}
示例2: groupRangeReduce
// groupRangeReduce takes a sorted range and generates a new row containing
// the aggregated columns and the non aggregated ones.
func (t *Table) groupRangeReduce(i, j int, alias map[string]string, acc map[string]Accumulator) (Row, error) {
if i > j {
return nil, fmt.Errorf("cannot aggregate empty ranges [%d, %d)", i, j)
}
// Initialize the range and accumulator results.
rng := t.data[i:j]
vaccs := make(map[string]interface{})
// Reset the accumulators.
for _, a := range acc {
a.Reset()
}
// Aggregate the range using the provided aggregators.
for _, r := range rng {
for b, a := range acc {
av, err := a.Accumulate(r[b])
if err != nil {
return nil, err
}
vaccs[b] = av
}
}
// Create a new row based on the resulting aggregations with the proper
// binding aliasing and the non aggregated values.
newRow := Row{}
for b, v := range rng[0] {
acc, ok := vaccs[b]
if !ok {
if a, ok := alias[b]; ok {
newRow[a] = v
} else {
newRow[b] = v
}
} else {
a, ok := alias[b]
if !ok {
return nil, fmt.Errorf("aggregated bindings require and alias; binding %s missing alias", b)
}
// Accumulators currently only can return numeric literals.
switch acc.(type) {
case int64:
l, err := literal.DefaultBuilder().Build(literal.Int64, acc)
if err != nil {
return nil, err
}
newRow[a] = &Cell{L: l}
case float64:
l, err := literal.DefaultBuilder().Build(literal.Float64, acc)
if err != nil {
return nil, err
}
newRow[a] = &Cell{L: l}
default:
return nil, fmt.Errorf("aggregation of binding %s returned unknown value %v or type", b, acc)
}
}
}
return newRow, nil
}
示例3: TestCellString
func TestCellString(t *testing.T) {
now := time.Now()
n := node.NewBlankNode()
p, err := predicate.NewImmutable("foo")
if err != nil {
t.Fatalf("failed to create predicate with error %v", err)
}
l, err := literal.DefaultBuilder().Parse(`"true"^^type:bool`)
if err != nil {
t.Fatalf("failed to create literal with error %v", err)
}
testTable := []struct {
c *Cell
want string
}{
{c: &Cell{S: "foo"}, want: `foo`},
{c: &Cell{N: n}, want: n.String()},
{c: &Cell{P: p}, want: p.String()},
{c: &Cell{L: l}, want: l.String()},
{c: &Cell{T: &now}, want: now.Format(time.RFC3339Nano)},
}
for _, entry := range testTable {
if got := entry.c.String(); got != entry.want {
t.Errorf("Cell.String failed to return the right string; got %q, want %q", got, entry.want)
}
}
}
示例4: TestCountAccumulators
func TestCountAccumulators(t *testing.T) {
// Count accumulator.
var (
cv interface{}
ca = NewCountAccumulator()
)
for i := int64(0); i < 5; i++ {
cv, _ = ca.Accumulate(i)
}
if got, want := cv.(int64), int64(5); got != want {
t.Errorf("Count accumulator failed; got %d, want %d", got, want)
}
// Count distint accumulator
var (
dv interface{}
da = NewCountDistinctAccumulator()
)
for i := int64(0); i < 10; i++ {
l, _ := literal.DefaultBuilder().Build(literal.Int64, i%2)
dv, _ = da.Accumulate(l)
}
if got, want := dv.(int64), int64(2); got != want {
t.Errorf("Count distinct accumulator failed; got %d, want %d", got, want)
}
}
示例5: limitCollection
// limitCollection collects the limit of rows to return as indicated by the
// LIMIT clause.
func limitCollection() ElementHook {
var f func(st *Statement, ce ConsumedElement) (ElementHook, error)
f = func(st *Statement, ce ConsumedElement) (ElementHook, error) {
if ce.IsSymbol() || ce.token.Type == lexer.ItemLimit {
return f, nil
}
if ce.token.Type != lexer.ItemLiteral {
return nil, fmt.Errorf("limit clause required an int64 literal; found %v instead", ce.token)
}
l, err := literal.DefaultBuilder().Parse(ce.token.Text)
if err != nil {
return nil, fmt.Errorf("failed to parse limit literal %q with error %v", ce.token.Text, err)
}
if l.Type() != literal.Int64 {
return nil, fmt.Errorf("limit required an int64 value; found %s instead", l)
}
lv, err := l.Int64()
if err != nil {
return nil, fmt.Errorf("failed to retrieve the int64 value for literal %v with error %v", l, err)
}
st.limitSet, st.limit = true, lv
return f, nil
}
return f
}
示例6: populateBenchmarkStore
func populateBenchmarkStore(b *testing.B) storage.Store {
s, ctx := memory.NewStore(), context.Background()
g, err := s.NewGraph(ctx, "?test")
if err != nil {
b.Fatalf("memory.NewGraph failed to create \"?test\" with error %v", err)
}
buf := bytes.NewBufferString(testTriples)
if _, err := io.ReadIntoGraph(ctx, g, buf, literal.DefaultBuilder()); err != nil {
b.Fatalf("io.ReadIntoGraph failed to read test graph with error %v", err)
}
trpls := make(chan *triple.Triple)
go func() {
if err := g.Triples(ctx, trpls); err != nil {
b.Fatal(err)
}
}()
cnt := 0
for _ = range trpls {
cnt++
}
if got, want := cnt, len(strings.Split(testTriples, "\n"))-1; got != want {
b.Fatalf("Failed to import all test triples; got %v, want %v", got, want)
}
return s
}
示例7: TestDataAccessTripleToRowObjectBindingsDroping
func TestDataAccessTripleToRowObjectBindingsDroping(t *testing.T) {
n, p, _ := testNodeTemporalPredicateLiteral(t)
ts, err := p.TimeAnchor()
if err != nil {
t.Fatal(err)
}
testTable := []struct {
t string
cls *semantic.GraphClause
bc *table.Cell
ac *table.Cell
tc *table.Cell
ic *table.Cell
tsc *table.Cell
atc *table.Cell
}{
{
t: n.String() + "\t" + p.String() + "\t" + n.String(),
cls: &semantic.GraphClause{
OBinding: "?o",
OAlias: "?alias",
OTypeAlias: "?o",
OIDAlias: "?id",
},
bc: &table.Cell{N: n},
ac: &table.Cell{N: n},
tc: &table.Cell{S: table.CellString(n.Type().String())},
ic: &table.Cell{S: table.CellString(n.ID().String())},
},
{
t: n.String() + "\t" + p.String() + "\t" + p.String(),
cls: &semantic.GraphClause{
OBinding: "?o",
OAlias: "?alias",
OIDAlias: "?id",
OAnchorBinding: "?ts",
OAnchorAlias: "?o",
},
bc: &table.Cell{P: p},
ac: &table.Cell{P: p},
ic: &table.Cell{S: table.CellString(string(p.ID()))},
tsc: &table.Cell{T: ts},
atc: &table.Cell{T: ts},
},
}
for _, entry := range testTable {
tpl, err := triple.Parse(entry.t, literal.DefaultBuilder())
if err != nil {
t.Errorf("triple.Parse failed to parse valid triple %q with error %v", entry.t, err)
}
r, err := tripleToRow(tpl, entry.cls)
if err != nil {
t.Errorf("tripleToRow for triple %q and clasuse %v, failed with error %v", tpl, entry.cls, err)
}
if r != nil {
t.Errorf("tripleToRow for triple %q and clasuse %v, failed to drop triple and returned %v", tpl, entry.cls, r)
}
}
}
示例8: TestDataAccessTripleToRowPredicateBindings
func TestDataAccessTripleToRowPredicateBindings(t *testing.T) {
n, p, _ := testNodeTemporalPredicateLiteral(t)
ts, err := p.TimeAnchor()
if err != nil {
t.Fatal(err)
}
testTable := []struct {
t string
cls *semantic.GraphClause
bc *table.Cell
ac *table.Cell
ic *table.Cell
tc *table.Cell
atc *table.Cell
}{
{
t: n.String() + "\t" + p.String() + "\t" + n.String(),
cls: &semantic.GraphClause{
PBinding: "?p",
PAlias: "?alias",
PIDAlias: "?id",
PAnchorBinding: "?ts",
PAnchorAlias: "?tsa",
},
bc: &table.Cell{P: p},
ac: &table.Cell{P: p},
ic: &table.Cell{S: table.CellString(string(p.ID()))},
tc: &table.Cell{T: ts},
atc: &table.Cell{T: ts},
},
}
for _, entry := range testTable {
tpl, err := triple.Parse(entry.t, literal.DefaultBuilder())
if err != nil {
t.Errorf("triple.Parse failed to parse valid triple %q with error %v", entry.t, err)
}
r, err := tripleToRow(tpl, entry.cls)
if err != nil {
t.Errorf("tripleToRow for triple %q and clasuse %v, failed with error %v", tpl, entry.cls, err)
}
if got, want := r["?p"], entry.bc; !reflect.DeepEqual(got, want) {
t.Errorf("tripleToRow failed to return right value for binding \"?p\"; got %q, want %q", got, want)
}
if got, want := r["?alias"], entry.ac; !reflect.DeepEqual(got, want) {
t.Errorf("tripleToRow failed to return right value for binding alias \"?alias\"; got %q, want %q", got, want)
}
if got, want := r["?id"], entry.ic; !reflect.DeepEqual(got, want) {
t.Errorf("tripleToRow failed to return right value for binding \"?id\"; got %q, want %q", got, want)
}
if got, want := r["?ts"], entry.tc; !reflect.DeepEqual(got, want) {
t.Errorf("tripleToRow failed to return right value for binding \"?ts\"; got %q, want %q", got, want)
}
if got, want := r["?tsa"], entry.tc; !reflect.DeepEqual(got, want) {
t.Errorf("tripleToRow failed to return right value for binding \"?tsa\"; got %q, want %q", got, want)
}
}
}
示例9: TestSerializationContents
func TestSerializationContents(t *testing.T) {
var buffer bytes.Buffer
ts, ctx := getTestTriples(t), context.Background()
g, err := memory.NewStore().NewGraph(ctx, "test")
if err != nil {
t.Fatalf("memory.NewStore().NewGraph should have never failed to create a graph")
}
if err := g.AddTriples(ctx, ts); err != nil {
t.Errorf("storage.AddTriples should have not fail to add triples %v with error %v", ts, err)
}
// Serialize to a buffer.
cnt, err := WriteGraph(ctx, &buffer, g)
if err != nil {
t.Errorf("io.WriteGraph failed to read %s with error %v", buffer.String(), err)
}
if cnt != 6 {
t.Errorf("io.WriteGraph should have been able to write 6 triples not %d", cnt)
}
// Deserialize from a buffer.
g2, err := memory.DefaultStore.NewGraph(ctx, "test2")
if err != nil {
t.Fatalf("memory.DefaultStore.NewGraph should have never failed to create a graph")
}
cnt2, err := ReadIntoGraph(ctx, g2, &buffer, literal.DefaultBuilder())
if err != nil {
t.Errorf("io.readIntoGraph failed to read %s with error %v", buffer.String(), err)
}
if cnt2 != 6 {
t.Errorf("io.readIntoGraph should have been able to read 6 triples not %d", cnt2)
}
// Check the graphs are equal
m := make(map[string]bool)
gs := 0
gtpls, err := g.Triples(ctx)
if err != nil {
t.Errorf("g.Triples failed to retrieve triples with error %v", err)
}
for trpl := range gtpls {
m[trpl.GUID()] = true
gs++
}
gos := 0
g2tpls, err := g2.Triples(ctx)
if err != nil {
t.Errorf("g2.Triples failed to retrieve triples with error %v", err)
}
for trpl := range g2tpls {
if _, ok := m[trpl.GUID()]; !ok {
t.Errorf("Failed to unmarshal marshaled triple; could not find triple %s", trpl.String())
}
gos++
}
if gs != gos || gs != 6 || gos != 6 {
t.Errorf("Failed to unmarshal marshaled the right number of triples, %d != %d != 6", gs, gos)
}
}
示例10: TestReify
func TestReify(t *testing.T) {
tr, err := ParseTriple("/some/type<some id>\t\"foo\"@[]\t\"bar\"@[]", literal.DefaultBuilder())
if err != nil {
t.Fatalf("triple.ParseTriple failed to parse valid triple with error %v", err)
}
rts, bn := tr.Reify()
if len(rts) != 4 || bn == nil {
t.Errorf("triple.Reify failed to create 4 valid triples and a valid blank node; returned %v, %s instead", rts, bn)
}
}
示例11: ToLiteral
// ToLiteral converts the node found by the lexer and converts it into a
// BadWolf literal.
func ToLiteral(ce ConsumedElement) (*literal.Literal, error) {
if ce.IsSymbol() {
return nil, fmt.Errorf("semantic.ToLiteral cannot convert symbol %v to a literal", ce)
}
tkn := ce.Token()
if tkn.Type != lexer.ItemLiteral {
return nil, fmt.Errorf("semantic.ToLiteral cannot convert token type %s to a literal", tkn.Type)
}
return literal.DefaultBuilder().Parse(tkn.Text)
}
示例12: TestParsetriple
func TestParsetriple(t *testing.T) {
ss := []string{
"/some/type<some id>\t\"foo\"@[]\t/some/type<some id>",
"/some/type<some id>\t\"foo\"@[]\t\"bar\"@[]",
}
for _, s := range ss {
if _, err := ParseTriple(s, literal.DefaultBuilder()); err != nil {
t.Errorf("triple.Parse failed to parse valid triple %s with error %v", s, err)
}
}
}
示例13: InitializeCommands
// InitializeCommands initializes the available commands with the given storage
// instance.
func InitializeCommands(driver storage.Store, chanSize, bulkTripleOpSize, builderSize int, rl repl.ReadLiner) []*command.Command {
return []*command.Command{
assert.New(driver, literal.DefaultBuilder(), chanSize),
benchmark.New(driver, chanSize),
export.New(driver, bulkTripleOpSize),
load.New(driver, bulkTripleOpSize, builderSize),
run.New(driver, chanSize),
repl.New(driver, chanSize, bulkTripleOpSize, builderSize, rl),
version.New(),
}
}
示例14: TestStatementAddData
func TestStatementAddData(t *testing.T) {
tr, err := triple.ParseTriple(`/_<foo> "foo"@[] /_<bar>`, literal.DefaultBuilder())
if err != nil {
t.Fatalf("triple.ParseTriple failed to parse valid triple with error %v", err)
}
st := &Statement{}
st.BindType(Query)
st.AddData(tr)
if got, want := st.Data(), []*triple.Triple{tr}; !reflect.DeepEqual(got, want) {
t.Errorf("semantic.AddData returned the wrong data avaiable; got %v, want %v", got, want)
}
}
示例15: init
func init() {
dach = dataAccumulator(literal.DefaultBuilder())
gach = graphAccumulator()
wnch = whereNextWorkingClause()
wich = whereInitWorkingClause()
wsch = whereSubjectClause()
wpch = wherePredicateClause()
woch = whereObjectClause()
predicateRegexp = regexp.MustCompile(`^"(.+)"@\["?([^\]"]*)"?\]$`)
boundRegexp = regexp.MustCompile(`^"(.+)"@\["?([^\]"]*)"?,"?([^\]"]*)"?\]$`)
}