예제 #1
0
파일: graph.go 프로젝트: timtadh/graple
func LoadVertex(g *goiso.Graph, supportAttr string, vids types.Map, nodeAttrs *bptree.BpTree, supportAttrs map[int]string, data []byte) (err error) {
	obj, err := ParseJson(data)
	if err != nil {
		return err
	}
	_id, err := obj["id"].(json.Number).Int64()
	if err != nil {
		return err
	}
	label := strings.TrimSpace(obj["label"].(string))
	id := int(_id)
	vertex := g.AddVertex(id, label)
	err = vids.Put(types.Int(id), vertex)
	if err != nil {
		return err
	}
	if nodeAttrs != nil {
		bid := make([]byte, 4)
		binary.BigEndian.PutUint32(bid, uint32(vertex.Idx))
		err = nodeAttrs.Add(bid, data)
		if err != nil {
			return err
		}
		if supportAttr != "" {
			if _, has := obj[supportAttr]; !has {
				return fmt.Errorf("vertex did not have required supportAttr %v\n%v", supportAttr, string(data))
			}
			supportAttrs[vertex.Idx] = obj[supportAttr].(string)
		}
	}
	return nil
}
예제 #2
0
파일: maximal.go 프로젝트: timtadh/graple
func addToLabels(labels *bptree.BpTree, label []byte) {
	has, err := labels.Has(label)
	if err != nil {
		log.Println(err)
	}
	if !has {
		err = labels.Add(label, []byte{0})
		if err != nil {
			log.Println(err)
		}
	}
}
예제 #3
0
파일: main.go 프로젝트: timtadh/graple
func writePattern(count int, outDir string, embeddings, patterns io.Writer, nodeAttrs *bptree.BpTree, all store.Findable, key []byte) {
	patDir := EmptyDir(path.Join(outDir, fmt.Sprintf("%d", count)))
	patDot := path.Join(patDir, "pattern.dot")
	patVeg := path.Join(patDir, "pattern.veg")
	patName := path.Join(patDir, "pattern.name")
	patCount := path.Join(patDir, "count")
	instDir := EmptyDir(path.Join(patDir, "instances"))
	i := 0
	for _, sg, next := all.Find(key)(); next != nil; _, sg, next = next() {
		if i == 0 {
			fmt.Fprintln(patterns, "//", sg.Label())
			fmt.Fprintln(patterns)
			fmt.Fprintln(patterns, sg.String())
			fmt.Fprintln(embeddings, "//", sg.Label())
			fmt.Fprintln(embeddings)
			if pat, err := os.Create(patDot); err != nil {
				log.Fatal(err)
			} else {
				fmt.Fprintln(pat, sg.String())
				pat.Close()
			}
			if name, err := os.Create(patName); err != nil {
				log.Fatal(err)
			} else {
				fmt.Fprintln(name, sg.Label())
				name.Close()
			}
			if veg, err := os.Create(patVeg); err != nil {
				log.Fatal(err)
			} else {
				veg.Write(sg.VEG(nil))
				veg.Close()
			}
		}
		curDir := EmptyDir(path.Join(instDir, fmt.Sprintf("%d", i)))
		emDot := path.Join(curDir, "embedding.dot")
		emVeg := path.Join(curDir, "embedding.veg")
		if nodeAttrs != nil {
			attrs := make(map[int]map[string]interface{})
			for _, v := range sg.V {
				bid := make([]byte, 4)
				binary.BigEndian.PutUint32(bid, uint32(v.Id))
				err := nodeAttrs.DoFind(
					bid,
					func(key, value []byte) error {
						a, err := graph.ParseJson(value)
						if err != nil {
							log.Fatal(err)
						}
						attrs[v.Id] = a
						return nil
					})
				if err != nil {
					log.Fatal(err)
				}
			}
			fmt.Fprintln(embeddings, sg.StringWithAttrs(attrs))
			if em, err := os.Create(emDot); err != nil {
				log.Fatal(err)
			} else {
				fmt.Fprintln(em, sg.StringWithAttrs(attrs))
				em.Close()
			}
			if veg, err := os.Create(emVeg); err != nil {
				log.Fatal(err)
			} else {
				veg.Write(sg.VEG(attrs))
				veg.Close()
			}
		} else {
			fmt.Fprintln(embeddings, sg.String())
			if em, err := os.Create(emDot); err != nil {
				log.Fatal(err)
			} else {
				fmt.Fprintln(em, sg.String())
				em.Close()
			}
			if veg, err := os.Create(emVeg); err != nil {
				log.Fatal(err)
			} else {
				veg.Write(sg.VEG(nil))
				veg.Close()
			}
		}
		i++
	}
	if c, err := os.Create(patCount); err != nil {
		log.Fatal(err)
	} else {
		fmt.Fprintln(c, i)
		c.Close()
	}
	fmt.Fprintln(patterns)
	fmt.Fprintln(patterns)
	fmt.Fprintln(embeddings)
	fmt.Fprintln(embeddings)
}