Exemple #1
0
func detectType(
	handlers map[string]CXAspectHandler,
	tag string, value interface{},
	cyjsNetwork *cyjs.CyJS,
	nodeAttrs *map[string]interface{},
	edgeAttrs *map[string]interface{},
	layout *map[string]interface{},
	vps *map[string]interface{}) {

	switch tag {

	case cx.NetworkAttributesTag:
		netHandler := handlers[cx.NetworkAttributesTag]
		cyjsNetwork.Data = netHandler.HandleAspect(value.([]interface{}))
	case cx.NodesTag:
		createNodes(value.([]interface{}), cyjsNetwork)
	case cx.EdgesTag:
		decodeEdges(value.([]interface{}), cyjsNetwork)
	case cx.NodeAttributesTag:
		nodeAttributeHandler := handlers[cx.NodeAttributesTag]
		na := nodeAttributeHandler.HandleAspect(value.([]interface{}))
		*nodeAttrs = mergeAttr(na, *nodeAttrs)
	case cx.EdgeAttributesTag:
		edgeAttributeHandler := handlers[cx.EdgeAttributesTag]
		ea := edgeAttributeHandler.HandleAspect(value.([]interface{}))
		*edgeAttrs = mergeAttr(ea, *edgeAttrs)
	case cx.CartesianLayoutTag:
		layoutHandler := handlers[cx.CartesianLayoutTag]
		*layout = layoutHandler.HandleAspect(value.([]interface{}))
	case cx.VisualPropertiesTag:
		vpHandler := handlers[cx.VisualPropertiesTag]
		*vps = vpHandler.HandleAspect(value.([]interface{}))
	default:
		// All others
		cyjsNetwork.CxData[tag] = value
	}
}
Exemple #2
0
func run(cxDecoder *json.Decoder, w io.Writer) {

	// Initialize handlers
	handlers := initHandlers()

	// Network Object
	networkAttr := make(map[string]interface{})

	// Elements
	var nodes []cyjs.CyJSNode
	var edges []cyjs.CyJSEdge
	layout := make(map[string]interface{})
	vps := make(map[string]interface{})

	elements := cyjs.Elements{Nodes: nodes, Edges: edges}

	// Temp storage for attributes
	nodeAttrs := make(map[string]interface{})
	edgeAttrs := make(map[string]interface{})

	cxData := make(map[string]interface{})
	// Basic Cytoscape.js object
	cyjsNetwork := cyjs.CyJS{Data: networkAttr, Elements: elements,
		CxData: cxData}

	for {
		_, err := cxDecoder.Token()

		if err == io.EOF {
			break
		} else if err != nil {
			log.Println(err)
			return
		}

		// Decode entry one-by-one.
		for cxDecoder.More() {
			var entry map[string]interface{}
			err := cxDecoder.Decode(&entry)
			if err != nil {
				log.Fatal(err)
			}

			parseCxEntry(handlers, entry, &cyjsNetwork, &nodeAttrs,
				&edgeAttrs, &layout, &vps)
		}
	}

	assignNodeAttr(cyjsNetwork.Elements.Nodes, nodeAttrs, layout)
	assignEdgeAttr(cyjsNetwork.Elements.Edges, edgeAttrs)

	// Add style to net (if style data is available...)
	if vps != nil && reflect.ValueOf(vps).IsNil() == false {
		if vps["style"] != nil {
			cyjsNetwork.Style = vps["style"].([]cyjs.SelectorEntry)
		}
	}

	jsonString, err := json.Marshal(cyjsNetwork)

	if err != nil {
		fmt.Println("ERR: ", err)
	} else {
		//		fmt.Println(string(jsonString))
		w.Write(jsonString)
	}
	//debug()
}