-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
…rings internally and the Hexml parser. Added examples/Reading.hs to demo the interface.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
data Node = Node [Att] deriving (Show, Eq) | ||
data Edge = Edge [Att] deriving (Show, Eq) | ||
data Att = Att (BS.ByteString, BS.ByteString) deriving (Show, Eq) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
type Identifier = BS.ByteString | ||
type Field = BS.ByteString | ||
|
||
class HasAtt a where | ||
attributes:: a -> [Att] | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
snowleopard
|
||
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 | ||
This comment has been minimized.
Sorry, something went wrong.
snowleopard
|
||
makeAtt a = Att a | ||
|
||
makeNode:: [Att] -> Node | ||
makeNode as = Node as | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Deriving
Show
breaks the abstraction: if you change the implementation, the users will be able to see this. I suggest to implementShow
manually viamakePangraph
(seeData.Set
as an example).