Exemplo n.º 1
0
func TestParseSexpWithMemstore(t *testing.T) {
	Convey("With a Memstore", t, func() {
		ts := graph_memstore.NewMemTripleStore()

		Convey("It should parse an empty query", func() {
			it := BuildIteratorTreeForQuery(ts, "()")
			So(it.Type(), ShouldEqual, "null")
		})

		Convey("It should get a single triple linkage", func() {
			ts.AddTriple(graph.MakeTriple("i", "can", "win", ""))
			query := "($a (:can \"win\"))"
			So(len(query), ShouldEqual, 17)
			it := BuildIteratorTreeForQuery(ts, query)
			So(it.Type(), ShouldEqual, "and")
			out, ok := it.Next()
			So(ok, ShouldBeTrue)
			So(out, ShouldEqual, ts.GetIdFor("i"))
		})

		Convey("It can get an internal linkage", func() {
			ts.AddTriple(graph.MakeTriple("i", "can", "win", ""))
			query := "(\"i\" (:can $a))"
			it := BuildIteratorTreeForQuery(ts, query)
			So(it.Type(), ShouldEqual, "and")
			out, ok := it.Next()
			So(ok, ShouldBeTrue)
			So(out, ShouldEqual, ts.GetIdFor("i"))
		})

	})
}
Exemplo n.º 2
0
func ParseLineToTriple(str string) *graph.Triple {
	// Skip leading whitespace.
	str = skipWhitespace(str)
	// Check for a comment
	if str != "" && str[0] == '#' {
		return nil
	}
	sub, remainder := getTripleComponent(str)
	if sub == nil {
		return nil
	}
	str = skipWhitespace(remainder)
	pred, remainder := getTripleComponent(str)
	if pred == nil {
		return nil
	}
	str = skipWhitespace(remainder)
	obj, remainder := getTripleComponent(str)
	if obj == nil {
		return nil
	}
	str = skipWhitespace(remainder)
	prov_ptr, remainder := getTripleComponent(str)
	var prov string
	if prov_ptr == nil {
		prov = ""
	} else {
		prov = *prov_ptr
	}
	str = skipWhitespace(remainder)
	if str != "" && str[0] == '.' {
		return graph.MakeTriple(*sub, *pred, *obj, prov)
	}
	return nil
}
Exemplo n.º 3
0
func TestTreeConstraintTagParse(t *testing.T) {
	ts := graph_memstore.NewMemTripleStore()
	ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
	ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
	query := "(\"i\"\n" +
		"(:like\n" +
		"($a (:is :good))))"
	it := BuildIteratorTreeForQuery(ts, query)
	_, ok := it.Next()
	if !ok {
		t.Error("Got no results")
	}
	tags := make(map[string]graph.TSVal)
	it.TagResults(&tags)
	if ts.GetNameFor(tags["$a"]) != "food" {
		t.Errorf("Got %s, expected food", ts.GetNameFor(tags["$a"]))
	}

}
Exemplo n.º 4
0
func TestTreeConstraintParse(t *testing.T) {
	ts := graph_memstore.NewMemTripleStore()
	ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
	ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
	query := "(\"i\"\n" +
		"(:like\n" +
		"($a (:is :good))))"
	it := BuildIteratorTreeForQuery(ts, query)
	if it.Type() != "and" {
		t.Error("Odd iterator tree. Got: %s", it.DebugString(0))
	}
	out, ok := it.Next()
	if !ok {
		t.Error("Got no results")
	}
	if out != ts.GetIdFor("i") {
		t.Errorf("Got %d, expected %d", out, ts.GetIdFor("i"))
	}
}
Exemplo n.º 5
0
func (ts *MongoTripleStore) GetTriple(val graph.TSVal) *graph.Triple {
	var bsonDoc bson.M
	err := ts.db.C("triples").FindId(val.(string)).One(&bsonDoc)
	if err != nil {
		log.Println("Error: Couldn't retrieve triple", val.(string), err)
	}
	return graph.MakeTriple(
		bsonDoc["Sub"].(string),
		bsonDoc["Pred"].(string),
		bsonDoc["Obj"].(string),
		bsonDoc["Provenance"].(string))
}
Exemplo n.º 6
0
func TestLoadDatabase(t *testing.T) {
	var ts *LevelDBTripleStore

	Convey("Given a created database path", t, func() {
		tmpDir, _ := ioutil.TempDir(os.TempDir(), "cayley_test")
		t.Log(tmpDir)
		ok := CreateNewLevelDB(tmpDir)
		So(ok, ShouldBeTrue)
		ts = NewDefaultLevelDBTripleStore(tmpDir, nil)

		Convey("Can load a single triple", func() {
			ts.AddTriple(graph.MakeTriple("Something", "points_to", "Something Else", "context"))
			So(ts.GetNameFor(ts.GetIdFor("Something")), ShouldEqual, "Something")
			So(ts.Size(), ShouldEqual, 1)
		})

		Convey("Can load many triples", func() {

			ts.AddTripleSet(makeTripleSet())
			So(ts.Size(), ShouldEqual, 11)
			So(ts.GetSizeFor(ts.GetIdFor("B")), ShouldEqual, 5)

			Convey("Can delete triples", func() {
				ts.RemoveTriple(graph.MakeTriple("A", "follows", "B", ""))
				So(ts.Size(), ShouldEqual, 10)
				So(ts.GetSizeFor(ts.GetIdFor("B")), ShouldEqual, 4)
			})
		})

		Reset(func() {
			ts.Close()
			os.RemoveAll(tmpDir)
		})

	})

}
Exemplo n.º 7
0
func TestRemoveTriple(t *testing.T) {
	ts := MakeTestingMemstore()
	ts.RemoveTriple(graph.MakeTriple("E", "follows", "F", ""))
	fixed := ts.MakeFixed()
	fixed.AddValue(ts.GetIdFor("E"))
	lto := graph.NewLinksToIterator(ts, fixed, "s")
	fixed2 := ts.MakeFixed()
	fixed2.AddValue(ts.GetIdFor("follows"))
	lto2 := graph.NewLinksToIterator(ts, fixed2, "p")
	innerAnd := graph.NewAndIterator()
	innerAnd.AddSubIterator(lto2)
	innerAnd.AddSubIterator(lto)
	hasa := graph.NewHasaIterator(ts, innerAnd, "o")
	newIt, _ := hasa.Optimize()
	_, ok := newIt.Next()
	if ok {
		t.Error("E should not have any followers.")
	}
}
Exemplo n.º 8
0
func makeTripleSet() []*graph.Triple {
	tripleSet := []*graph.Triple{
		graph.MakeTriple("A", "follows", "B", ""),
		graph.MakeTriple("C", "follows", "B", ""),
		graph.MakeTriple("C", "follows", "D", ""),
		graph.MakeTriple("D", "follows", "B", ""),
		graph.MakeTriple("B", "follows", "F", ""),
		graph.MakeTriple("F", "follows", "G", ""),
		graph.MakeTriple("D", "follows", "G", ""),
		graph.MakeTriple("E", "follows", "F", ""),
		graph.MakeTriple("B", "status", "cool", "status_graph"),
		graph.MakeTriple("D", "status", "cool", "status_graph"),
		graph.MakeTriple("G", "status", "cool", "status_graph"),
	}
	return tripleSet
}
Exemplo n.º 9
0
func TestSetIterator(t *testing.T) {
	var ts *LevelDBTripleStore
	var tmpDir string

	Convey("Given a prepared database", t, func() {
		tmpDir, _ = ioutil.TempDir(os.TempDir(), "cayley_test")
		t.Log(tmpDir)
		defer os.RemoveAll(tmpDir)
		ok := CreateNewLevelDB(tmpDir)
		So(ok, ShouldBeTrue)
		ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
		ts.AddTripleSet(makeTripleSet())
		var it graph.Iterator

		Convey("Can create a subject iterator", func() {
			it = ts.GetTripleIterator("s", ts.GetIdFor("C"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("C", "follows", "B", "").ToString(),
					graph.MakeTriple("C", "follows", "D", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

			Convey("And checkable", func() {
				and := graph.NewAndIterator()
				and.AddSubIterator(ts.GetTriplesAllIterator())
				and.AddSubIterator(it)

				expected := []string{
					graph.MakeTriple("C", "follows", "B", "").ToString(),
					graph.MakeTriple("C", "follows", "D", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})
			Reset(func() {
				it.Reset()
			})

		})

		Convey("Can create an object iterator", func() {
			it = ts.GetTripleIterator("o", ts.GetIdFor("F"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("B", "follows", "F", "").ToString(),
					graph.MakeTriple("E", "follows", "F", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

			Convey("Mutually and-checkable", func() {
				and := graph.NewAndIterator()
				and.AddSubIterator(ts.GetTripleIterator("s", ts.GetIdFor("B")))
				and.AddSubIterator(it)

				expected := []string{
					graph.MakeTriple("B", "follows", "F", "").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

		})

		Convey("Can create a predicate iterator", func() {
			it = ts.GetTripleIterator("p", ts.GetIdFor("status"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("D", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("G", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

		})

		Convey("Can create a provenance iterator", func() {
			it = ts.GetTripleIterator("c", ts.GetIdFor("status_graph"))

			Convey("Containing the right things", func() {
				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("D", "status", "cool", "status_graph").ToString(),
					graph.MakeTriple("G", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, it)
				sort.Strings(actual)
				sort.Strings(expected)
				So(actual, ShouldResemble, expected)
			})

			Convey("Can be cross-checked", func() {
				and := graph.NewAndIterator()
				// Order is important
				and.AddSubIterator(ts.GetTripleIterator("s", ts.GetIdFor("B")))
				and.AddSubIterator(it)

				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				So(actual, ShouldResemble, expected)
			})

			Convey("Can check against other iterators", func() {
				and := graph.NewAndIterator()
				// Order is important
				and.AddSubIterator(it)
				and.AddSubIterator(ts.GetTripleIterator("s", ts.GetIdFor("B")))

				expected := []string{
					graph.MakeTriple("B", "status", "cool", "status_graph").ToString(),
				}
				actual := extractTripleFromIterator(ts, and)
				So(actual, ShouldResemble, expected)
			})
			Reset(func() {
				it.Reset()
			})

		})

		Reset(func() {
			ts.Close()
		})

	})

}
Exemplo n.º 10
0
func MakeTestingMemstore() *MemTripleStore {
	ts := NewMemTripleStore()
	ts.AddTriple(graph.MakeTriple("A", "follows", "B", ""))
	ts.AddTriple(graph.MakeTriple("C", "follows", "B", ""))
	ts.AddTriple(graph.MakeTriple("C", "follows", "D", ""))
	ts.AddTriple(graph.MakeTriple("D", "follows", "B", ""))
	ts.AddTriple(graph.MakeTriple("B", "follows", "F", ""))
	ts.AddTriple(graph.MakeTriple("F", "follows", "G", ""))
	ts.AddTriple(graph.MakeTriple("D", "follows", "G", ""))
	ts.AddTriple(graph.MakeTriple("E", "follows", "F", ""))
	ts.AddTriple(graph.MakeTriple("B", "status", "cool", "status_graph"))
	ts.AddTriple(graph.MakeTriple("D", "status", "cool", "status_graph"))
	ts.AddTriple(graph.MakeTriple("G", "status", "cool", "status_graph"))
	return ts
}