func main() { // To see how most of this works, see hello_world -- this just add in a transaction store, err := cayley.NewMemoryGraph() if err != nil { log.Fatalln(err) } // Create a transaction of work to do // NOTE: the transaction is independent of the storage type, so comes from cayley rather than store t := cayley.NewTransaction() t.AddQuad(quad.Make("food", "is", "good", nil)) t.AddQuad(quad.Make("phrase of the day", "is of course", "Hello World!", nil)) t.AddQuad(quad.Make("cats", "are", "awesome", nil)) t.AddQuad(quad.Make("cats", "are", "scary", nil)) t.AddQuad(quad.Make("cats", "want to", "kill you", nil)) // Apply the transaction err = store.ApplyTransaction(t) if err != nil { log.Fatalln(err) } p := cayley.StartPath(store, quad.String("cats")).Out(quad.String("are")) err = p.Iterate(nil).EachValue(nil, func(v quad.Value) { fmt.Println("cats are", v.Native()) }) if err != nil { log.Fatalln(err) } }
// loadTestData adds all the test data into the database. func loadTestData(context interface{}, db *db.DB) error { // Make sure the old data is clear. if err := unloadTestData(context, db); err != nil { if !graph.IsQuadNotExist(err) { return err } } // ----------------------------------------------------------- // Load example items, relationships, views, and patterns. items, rels, vs, pats, err := wirefix.Get() if err != nil { return err } if err := wirefix.Add(context, db, items, rels, vs, pats); err != nil { return err } // ----------------------------------------------------------- // Build the example graph. mongoURI := cfg.MustURL("MONGO_URI") if err := cayleyshelf.InitQuadStore(mongoURI.String()); err != nil { return err } var quads []quad.Quad quads = append(quads, quad.Make(wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"authored", wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"flagged", wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", "")) tx := cayley.NewTransaction() for _, quad := range quads { tx.AddQuad(quad) } if err := db.NewCayley(tests.Context, tests.TestSession); err != nil { return err } store, err := db.GraphHandle(tests.Context) if err != nil { return err } defer store.Close() if err := store.ApplyTransaction(tx); err != nil { return err } return nil }
func main() { // File for your new BoltDB. Use path to regular file and not temporary in the real world tmpfile, err := ioutil.TempFile("", "example") if err != nil { log.Fatal(err) } defer os.Remove(tmpfile.Name()) // clean up // Initialize the database graph.InitQuadStore("bolt", tmpfile.Name(), nil) // Open and use the database store, err := cayley.NewGraph("bolt", tmpfile.Name(), nil) if err != nil { log.Fatalln(err) } store.AddQuad(quad.Make("phrase of the day", "is of course", "Hello BoltDB!", "demo graph")) // Now we create the path, to get to our data p := cayley.StartPath(store, quad.String("phrase of the day")).Out(quad.String("is of course")) // This is more advanced example of the query. // Simpler equivalent can be found in hello_world example. // Now we get an iterator for the path and optimize it. // The second return is if it was optimized, but we don't care for now. it, _ := p.BuildIterator().Optimize() // Optimize iterator on quad store level. // After this step iterators will be replaced with backend-specific ones. it, _ = store.OptimizeIterator(it) // remember to cleanup after yourself defer it.Close() // While we have items for it.Next() { token := it.Result() // get a ref to a node (backend-specific) value := store.NameOf(token) // get the value in the node (RDF) nativeValue := quad.NativeOf(value) // convert value to normal Go type fmt.Println(nativeValue) // print it! } if err := it.Err(); err != nil { log.Fatalln(err) } }
// unloadTestData removes all the test data from the database. func unloadTestData(context interface{}, db *db.DB) error { // ------------------------------------------------------------ // Clear items, relationships, and views. wirefix.Remove("context", db, wirePrefix) // ------------------------------------------------------------ // Clear cayley graph. var quads []quad.Quad quads = append(quads, quad.Make(wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"authored", wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"flagged", wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", "")) tx := cayley.NewTransaction() for _, quad := range quads { tx.RemoveQuad(quad) } if err := db.NewCayley(tests.Context, tests.TestSession); err != nil { return err } store, err := db.GraphHandle(tests.Context) if err != nil { return err } defer store.Close() if err := store.ApplyTransaction(tx); err != nil { return err } return nil }
func main() { // Create a brand new graph store, err := cayley.NewMemoryGraph() if err != nil { log.Fatalln(err) } store.AddQuad(quad.Make("phrase of the day", "is of course", "Hello World!", nil)) // Now we create the path, to get to our data p := cayley.StartPath(store, quad.String("phrase of the day")).Out(quad.String("is of course")) // Now we iterate over results. Arguments: // 1. Optional context used for cancellation. // 2. Quad store, but we can omit it because we have already built path with it. err = p.Iterate(nil).EachValue(nil, func(value quad.Value) { nativeValue := quad.NativeOf(value) // this converts RDF values to normal Go types fmt.Println(nativeValue) }) if err != nil { log.Fatalln(err) } }
) var parseTests = []struct { message string input string expect []quad.Quad err error }{ { message: "parse correct JSON", input: `[ {"subject": "foo", "predicate": "bar", "object": "baz"}, {"subject": "foo", "predicate": "bar", "object": "baz", "label": "graph"} ]`, expect: []quad.Quad{ quad.Make("foo", "bar", "baz", nil), quad.Make("foo", "bar", "baz", "graph"), }, err: nil, }, { message: "parse correct JSON with extra field", input: `[ {"subject": "foo", "predicate": "bar", "object": "foo", "something_else": "extra data"} ]`, expect: []quad.Quad{ quad.Make("foo", "bar", "foo", nil), }, err: nil, }, {
func Quad(subject, predicate, object, label interface{}) quad.Quad { return quad.Make(subject, predicate, object, label) }