Exemple #1
0
// Eval loads the triples in the file against as indicated by the command.
func Eval(ctx context.Context, usage string, args []string, store storage.Store, bulkSize, builderSize int) int {
	if len(args) <= 3 {
		fmt.Fprintf(os.Stderr, "[ERROR] Missing required file path and/or graph names.\n\n%s", usage)
		return 2
	}
	graphs, lb := strings.Split(args[len(args)-1], ","), literal.NewBoundedBuilder(builderSize)
	trplsChan, errChan, doneChan := make(chan *triple.Triple), make(chan error), make(chan bool)
	path := args[len(args)-2]
	go storeTriple(ctx, store, graphs, bulkSize, trplsChan, errChan, doneChan)
	cnt, err := io.ProcessLines(path, func(line string) error {
		t, err := triple.Parse(line, lb)
		if err != nil {
			return err
		}
		trplsChan <- t
		return <-errChan
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "[ERROR] Failed to process file %q. Ivalid triple on line %d. %v\n", path, cnt, err)
		return 2
	}
	doneChan <- true
	if err := <-errChan; err != nil {
		fmt.Fprintf(os.Stderr, "[ERROR] Failed to process file %q. Ivalid triple on line %d. %v\n", path, cnt, err)
		return 2
	}
	fmt.Printf("Successfully processed %d lines from file %q.\nTriples loaded into graphs:\n\t- %s\n", cnt, path, strings.Join(graphs, "\n\t- "))
	return 0
}
Exemple #2
0
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)
		}
	}
}
Exemple #3
0
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)
		}
	}
}
Exemple #4
0
func TestStatementAddData(t *testing.T) {
	tr, err := triple.Parse(`/_<foo> "foo"@[] /_<bar>`, literal.DefaultBuilder())
	if err != nil {
		t.Fatalf("triple.Parse 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)
	}
}
Exemple #5
0
func getTestTriples(t *testing.T, trpls []string) []*triple.Triple {
	var ts []*triple.Triple
	for _, s := range trpls {
		trpl, err := triple.Parse(s, literal.DefaultBuilder())
		if err != nil {
			t.Fatalf("triple.Parse failed to parse valid triple %s with error %v", s, err)
			continue
		}
		ts = append(ts, trpl)
	}
	return ts
}
Exemple #6
0
func createTriples(t *testing.T, ss []string) []*triple.Triple {
	ts := []*triple.Triple{}
	for _, s := range ss {
		trpl, err := triple.Parse(s, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %s with error %v", s, err)
			continue
		}
		ts = append(ts, trpl)
	}
	return ts
}
Exemple #7
0
func TestDataAccessBasicBindings(t *testing.T) {
	n, p, l := testNodePredicateLiteral(t)
	cls := &semantic.GraphClause{
		SBinding: "?s",
		PBinding: "?p",
		OBinding: "?o",
	}
	testTable := []struct {
		t  string
		sc *table.Cell
		pc *table.Cell
		oc *table.Cell
	}{
		{
			t:  n.String() + "\t" + p.String() + "\t" + n.String(),
			sc: &table.Cell{N: n},
			pc: &table.Cell{P: p},
			oc: &table.Cell{N: n},
		},
		{
			t:  n.String() + "\t" + p.String() + "\t" + p.String(),
			sc: &table.Cell{N: n},
			pc: &table.Cell{P: p},
			oc: &table.Cell{P: p},
		},
		{
			t:  n.String() + "\t" + p.String() + "\t" + l.String(),
			sc: &table.Cell{N: n},
			pc: &table.Cell{P: p},
			oc: &table.Cell{L: l},
		},
	}
	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, cls)
		if err != nil {
			t.Errorf("tripleToRow for triple %q and clasuse %v, failed with error %v", tpl, cls, err)
		}
		if got, want := r["?s"], entry.sc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?s\"; got %q, want %q", got, want)
		}
		if got, want := r["?p"], entry.pc; !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["?o"], entry.oc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?o\"; got %q, want %q", got, want)
		}
	}
}
Exemple #8
0
func TestDataAccessTripleToRowSubjectBindings(t *testing.T) {
	n, p, _ := testNodePredicateLiteral(t)
	testTable := []struct {
		t   string
		cls *semantic.GraphClause
		sc  *table.Cell
		ac  *table.Cell
		tc  *table.Cell
		ic  *table.Cell
	}{
		{
			t: n.String() + "\t" + p.String() + "\t" + n.String(),
			cls: &semantic.GraphClause{
				SBinding:   "?s",
				SAlias:     "?alias",
				STypeAlias: "?type",
				SIDAlias:   "?id",
			},
			sc: &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())},
		},
	}
	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["?s"], entry.sc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?s\"; 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["?type"], entry.tc; !reflect.DeepEqual(got, want) {
			t.Errorf("tripleToRow failed to return right value for binding \"?type\"; 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 \"?ud\"; got %q, want %q", got, want)
		}
	}
}
Exemple #9
0
// ReadIntoGraph reads a graph out of the provided reader. The data on the
// reader is interpret as text. Each line represents one triple using the
// standard serialized format. ReadIntoGraph will stop if fails to Parse
// a triple on the stream. The triples read till then would have also been
// added to the graph. The int value returns the number of triples added.
func ReadIntoGraph(ctx context.Context, g storage.Graph, r io.Reader, b literal.Builder) (int, error) {
	cnt, scanner := 0, bufio.NewScanner(r)
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		text := strings.TrimSpace(scanner.Text())
		if text == "" {
			continue
		}
		t, err := triple.Parse(text, b)
		if err != nil {
			return cnt, err
		}
		cnt++
		g.AddTriples(ctx, []*triple.Triple{t})
	}
	return cnt, nil
}
Exemple #10
0
func getTestTriples(t *testing.T) []*triple.Triple {
	ts := []*triple.Triple{}
	ss := []string{
		"/u<john>\t\"knows\"@[]\t/u<mary>",
		"/u<john>\t\"knows\"@[]\t/u<peter>",
		"/u<john>\t\"knows\"@[]\t/u<alice>",
		"/u<mary>\t\"knows\"@[]\t/u<andrew>",
		"/u<mary>\t\"knows\"@[]\t/u<kim>",
		"/u<mary>\t\"knows\"@[]\t/u<alice>",
	}
	for _, s := range ss {
		trpl, err := triple.Parse(s, literal.DefaultBuilder())
		if err != nil {
			t.Errorf("triple.Parse failed to parse valid triple %s with error %v", s, err)
			continue
		}
		ts = append(ts, trpl)
	}
	return ts
}
Exemple #11
0
// populateSources create all the graph required for the story and
// populates it with the provided data.
func (s *Story) populateSources(ctx context.Context, st storage.Store, b literal.Builder) error {
	for _, src := range s.Sources {
		g, err := getGraphFromStore(ctx, st, src.ID)
		if err != nil {
			return err
		}
		var trps []*triple.Triple
		for _, trp := range src.Facts {
			t, err := triple.Parse(trp, b)
			if err != nil {
				return err
			}
			trps = append(trps, t)
		}
		if err := g.AddTriples(ctx, trps); err != nil {
			return err
		}
	}
	return nil
}