diff --git a/Test.hs b/Test.hs
index fffd831..9e3f7a6 100644
--- a/Test.hs
+++ b/Test.hs
@@ -1,21 +1,33 @@
+{-# LANGUAGE OverloadedStrings #-}
+
module Main where
-import qualified Pangraph.XMLTemplate as PT
+import Data.ByteString.Char8 (pack)
+import qualified Data.ByteString as BS
import qualified Pangraph as P
-import qualified Text.XML.Hexml as H
+import qualified Pangraph.GraphML.Parser as GraphML_P
-import Data.Either
+type Parser = GraphML_P.Template -> BS.ByteString -> Either BS.ByteString P.Pangraph
main :: IO ()
main = do
- x <- readFile "examples/graphs/small.graphml"
- let bs = head $ rights [H.parse $ PT.strToByteString x]
- let (nodes, e) = PT.resolvePath bs n
- let nodes' =map (\h -> PT.getAttPairs h e) nodes
- putStr "#-\t"
- putStrLn $ show nodes
- putStr "#-\t"
- putStrLn $ show nodes'
+ xs <- readFile paths
+ case runParser parser (pack xs) of
+ Left x -> error (show x)
+ Right y -> if y == graphs
+ then putStr "Test Passed"
+ else error $ "Test failed----\n" ++ show y
where
- n::(PT.Path, PT.Element)
- n =(PT.strToByteString "graphml graph node", PT.strToByteString "id")
+ paths = "examples/graphs/small.graphml"
+ parser = GraphML_P.parseGraph
+ graphs =
+ P.makePangraph
+ [P.makeNode [P.makeAtt ("id","n0")],
+ P.makeNode [P.makeAtt ("id","n1")],
+ P.makeNode [P.makeAtt ("id","n2")]]
+ [P.makeEdge
+ [P.makeAtt ("source","n0"),
+ P.makeAtt ("target","n2")]]
+
+runParser :: Parser -> BS.ByteString ->Either BS.ByteString P.Pangraph
+runParser p file = p (head GraphML_P.template) $ file
diff --git a/examples/code/Reading.hs b/examples/code/Reading.hs
index 2cdaba0..36228dd 100644
--- a/examples/code/Reading.hs
+++ b/examples/code/Reading.hs
@@ -1,12 +1,13 @@
module Reading where
-import System.IO
-import Pangraph
-import qualified Pangraph.GraphML.Parser as G
+import qualified Pangraph.GraphML.Parser as GraphML_P
+import Data.ByteString.Char8 (pack)
-main::IO()
-main=do
+main:: IO ()
+main = do
fileName <- getLine
file <- readFile fileName
- let graph = G.parseAsString file
- putStr $ show graph
+ let graph = GraphML_P.parseGraph (head GraphML_P.template) $ pack file
+ case graph of
+ Left x -> putStrLn (show x)
+ Right y -> putStrLn (show y)
diff --git a/examples/code/VHDL.hs b/examples/code/VHDL.hs
deleted file mode 100644
index 2443467..0000000
--- a/examples/code/VHDL.hs
+++ /dev/null
@@ -1,11 +0,0 @@
-module VHDL where
-
-import Pangraph
-import qualified Pangraph.VHDL.Writer as V
-
-main::IO()
-main=do
- let vhdlString = V.writeVHDL graph
- putStrLn vhdlString
- where
- graph = ShortFile [ShortGraph [Node [Att ("id","n0")],Node [Att ("id","n1")],Node [Att ("id","n2")]] [Edge [Att ("source","n0"),Att ("target","n2")]]]
diff --git a/examples/code/Writing.hs b/examples/code/Writing.hs
deleted file mode 100644
index abd1778..0000000
--- a/examples/code/Writing.hs
+++ /dev/null
@@ -1,12 +0,0 @@
-module Writing where
-
-import System.IO
-import Pangraph
-import qualified Pangraph.GraphML.Writer as G
-
-main::IO()
-main=do
- filePath <- getLine
- G.writeGraph filePath graph
- where
- graph = ShortFile [ShortGraph [Node [Att ("id","n0")],Node [Att ("id","n1")],Node [Att ("id","n2")]] [Edge [Att ("source","n0"),Att ("target","n2")]]]
diff --git a/examples/graphs/small.graphml b/examples/graphs/small.graphml
index c22fd15..9908e03 100644
--- a/examples/graphs/small.graphml
+++ b/examples/graphs/small.graphml
@@ -7,6 +7,6 @@
-
+
diff --git a/pangraph.cabal b/pangraph.cabal
index 9785db8..fa52c2a 100644
--- a/pangraph.cabal
+++ b/pangraph.cabal
@@ -11,23 +11,26 @@ copyright: 2016 Joe Scott
category: Web
build-type: Simple
cabal-version: >=1.10
-tested-with:
- GHC==8.0.2
+tested-with: GHC==8.0.2
library
- hs-source-dirs: src
- exposed-modules: Pangraph
- other-modules: Pangraph.XMLTemplate
- , Pangraph.Graph
- build-depends: base >= 4.7 && < 5
- , hexml
- , bytestring
- default-language: Haskell2010
+ hs-source-dirs: src
+ exposed-modules: Pangraph
+ , Pangraph.GraphML.Parser
+ other-modules: Pangraph.XMLTemplate
+ , Pangraph.Graph
+ build-depends: base >= 4.8 && < 5
+ , hexml
+ , bytestring
+ default-language: Haskell2010
+ GHC-options: -Wall -fwarn-tabs -O2
--- test-suite pangraph-test
--- type: exitcode-stdio-1.0
--- main-is: Test.hs
--- build-depends: base >= 4.8 && < 5
--- , pangraph
--- default-language: Haskell2010
--- GHC-options: -Wall -fwarn-tabs
+test-suite pangraph-test
+ type: exitcode-stdio-1.0
+ main-is: Test.hs
+ build-depends: base >= 4.8 && < 5
+ , pangraph
+ , hexml
+ , bytestring
+ default-language: Haskell2010
+ GHC-options: -Wall -fwarn-tabs
diff --git a/src/Pangraph.hs b/src/Pangraph.hs
index a1ad60d..b0c2651 100644
--- a/src/Pangraph.hs
+++ b/src/Pangraph.hs
@@ -3,7 +3,7 @@ Pangraph,
Node,
Edge,
Att,
-attributes,
+att,
nodes,
edges,
key,
@@ -16,21 +16,21 @@ makePangraph
import qualified Data.ByteString as BS
-data Pangraph = Pangraph [Node] [Edge] deriving (Show)
-data Node = Node [Att] deriving (Show)
-data Edge = Edge [Att] deriving (Show)
-data Att = Att (BS.ByteString, BS.ByteString) deriving (Show)
+data Pangraph = Pangraph [Node] [Edge] deriving (Show, Eq)
+data Node = Node [Att] deriving (Show, Eq)
+data Edge = Edge [Att] deriving (Show, Eq)
+data Att = Att (BS.ByteString, BS.ByteString) deriving (Show, Eq)
type Identifier = BS.ByteString
type Field = BS.ByteString
class HasAtt a where
- attributes:: a -> [Att]
+ att:: a -> [Att]
instance HasAtt Node where
- attributes (Node a) = a
+ att (Node a) = a
instance HasAtt Edge where
- attributes (Edge a) = a
+ att (Edge a) = a
-- Pangraph type getters
@@ -48,8 +48,8 @@ value (Att a) = snd a
-- Pangraph type contructors
-makeAtt:: Identifier -> Field -> Att
-makeAtt i f = Att (i, f)
+makeAtt:: (Identifier , Field) -> Att
+makeAtt a = Att a
makeNode:: [Att] -> Node
makeNode as = Node as
diff --git a/src/Pangraph/GraphML/Parser.hs b/src/Pangraph/GraphML/Parser.hs
new file mode 100644
index 0000000..77208de
--- /dev/null
+++ b/src/Pangraph/GraphML/Parser.hs
@@ -0,0 +1,19 @@
+module Pangraph.GraphML.Parser
+( parseGraph
+, template
+, PT.Template
+) where
+
+import qualified Pangraph as P
+import qualified Text.XML.Hexml as H
+import qualified Data.ByteString as BS
+import qualified Pangraph.XMLTemplate as PT
+
+parseGraph:: PT.Template -> BS.ByteString -> Either BS.ByteString P.Pangraph
+parseGraph t file =
+ case H.parse file of
+ Left x -> Left x
+ Right x -> Right $ PT.parseFromTemplate t x
+
+template:: [PT.Template]
+template = PT.graphMLTemplate
diff --git a/src/Pangraph/XMLTemplate.hs b/src/Pangraph/XMLTemplate.hs
index f7800a8..eebd0ae 100644
--- a/src/Pangraph/XMLTemplate.hs
+++ b/src/Pangraph/XMLTemplate.hs
@@ -1,14 +1,10 @@
+{-# LANGUAGE OverloadedStrings #-}
+
module Pangraph.XMLTemplate
--- ( Template
--- -- Name,
--- -- Keys,
--- -- Text,
--- -- root,
--- -- name,
--- -- keys,
--- -- text
--- )
-where
+( Template,
+ graphMLTemplate,
+ parseFromTemplate
+) where
import Data.Maybe
@@ -16,10 +12,6 @@ import qualified Pangraph as P
import qualified Text.XML.Hexml as H
import qualified Data.ByteString as BS
-import qualified Data.ByteString.Char8 as BC
-
--- import qualified Data.ByteString.Lazy as BS
--- import qualified Data.ByteString.Lazy.Char8 as BC
data Template = XML [Node] [Edge]
data Node = Node [(Path, Element)]
@@ -29,16 +21,13 @@ type Path = BS.ByteString
type Element = BS.ByteString
type HexmlNode = H.Node
-strToByteString:: String -> BS.ByteString
-strToByteString ws = BC.pack ws
-graphMLTemplate:: Template
+graphMLTemplate:: [Template]
graphMLTemplate=
- XML
- [Node [(strToByteString"graphml graph node",
- strToByteString"id")]]
- [Edge [(strToByteString"graphml graph edge",
- strToByteString"id source target")]]
+ [XML
+ [Node [("graphml graph node", "id")]]
+ [Edge [("graphml graph edge", "source target")]]
+ ]
parseFromTemplate:: Template -> HexmlNode -> P.Pangraph
parseFromTemplate (XML nt et) root=P.makePangraph (concat $ map (nodes root) nt) (concat $ map (edges root) et)
@@ -50,26 +39,26 @@ edges:: HexmlNode -> Edge -> [P.Edge]
edges n (Edge pe) = concat $ map (makeEntity n (P.makeEdge)) pe
makeEntity:: HexmlNode -> ([P.Att] -> a) -> (Path, Element) -> [a]
-makeEntity root f a = do
- let (nodes, e) = resolvePath root a
- let attList = fmap (\h -> getAttPairs h e) nodes
- return $ fmap f attList
+makeEntity root f a = fmap f attList
+ where
+ (hexmlNodes, e) = resolvePath root a
+ attList = fmap (\h -> getAttPairs h e) hexmlNodes
resolvePath:: HexmlNode -> (Path, Element) -> ([HexmlNode], Element)
resolvePath h (p, e) =
- let delim = BS.head $ strToByteString " "
+ let delim = BS.head " "
in (resolvePathRecursive h $ BS.split delim p, e)
resolvePathRecursive:: HexmlNode -> [Path] -> [HexmlNode]
resolvePathRecursive h [] = [h]
resolvePathRecursive h bs = concat $
- fmap (\h -> resolvePathRecursive h (tail bs)) children
+ fmap (\c -> resolvePathRecursive c (tail bs)) children
where
children = H.childrenBy h $ head bs
getAttPairs:: HexmlNode -> Element -> [P.Att]
getAttPairs h e =fmap toAtt $ catMaybes $ fmap (\a ->H.attributeBy h a) $ BS.split delim e
where
- delim = BS.head $ strToByteString " "
+ delim = BS.head " "
toAtt:: H.Attribute -> P.Att
- toAtt a = P.makeAtt (H.attributeName a) (H.attributeValue a)
+ toAtt a = P.makeAtt (H.attributeName a, H.attributeValue a)