module TypedGraph.Graph
( TypedGraph
, untypedGraph
, extractNodeType
, extractEdgeType
, typeGraph
, TypedGraph.Graph.null
, newTypedNodes
, newTypedEdges
, typedNodes
, typedEdges
, untypedNodes
, untypedEdges
) where
import Abstract.Cardinality
import Abstract.Morphism
import Data.Maybe (fromMaybe)
import Graph.Graph as G
import Graph.GraphMorphism
type TypedGraph a b = GraphMorphism (Maybe a) (Maybe b)
instance Cardinality (GraphMorphism a b) where
cardinality = cardinality . domain
untypedGraph :: TypedGraph a b -> Graph (Maybe a) (Maybe b)
untypedGraph = domain
typeGraph :: TypedGraph a b -> Graph (Maybe a) (Maybe b)
typeGraph = codomain
null :: TypedGraph a b -> Bool
null = G.null . untypedGraph
extractNodeType :: TypedGraph a b -> NodeId -> NodeId
extractNodeType gm n = fromMaybe (error "Node not typed") $ applyNode gm n
extractEdgeType :: TypedGraph a b -> EdgeId -> EdgeId
extractEdgeType gm e = fromMaybe (error "edge not typed") $ applyEdge gm e
newTypedNodes :: TypedGraph a b -> [NodeId]
newTypedNodes tg = newNodes $ untypedGraph tg
newTypedEdges :: TypedGraph a b -> [EdgeId]
newTypedEdges tg = newEdges $ untypedGraph tg
typedNodes :: TypedGraph a b -> [(NodeId, NodeId)]
typedNodes tg = map withType $ nodeIds (untypedGraph tg)
where withType node = (node, extractNodeType tg node)
typedEdges :: TypedGraph a b -> [(EdgeId, NodeId, NodeId, EdgeId)]
typedEdges tg = map withType $ edges graph
where graph = untypedGraph tg
withType edge = (edgeId edge, sourceId edge, targetId edge, extractEdgeType tg (edgeId edge))
untypedNodes :: TypedGraph a b -> [NodeId]
untypedNodes tg = nodeIds $ untypedGraph tg
untypedEdges :: TypedGraph a b -> [EdgeId]
untypedEdges tg = edgeIds $ untypedGraph tg