on master help sign in
switch from easyplot to gnuplot package to support more configurable gnuplot output
59387a1AgentM
ProjectM36/Relation/Show/Gnuplot.hs+31−37
@@ -4,8 +4,11 @@ import ProjectM36.Base
4import ProjectM36.Relation
4import ProjectM36.Relation
5import ProjectM36.Tuple
5import ProjectM36.Tuple
6import qualified ProjectM36.Attribute as A
6import qualified ProjectM36.Attribute as A
7−import Graphics.EasyPlot
8import qualified Data.Vector as V
7import qualified Data.Vector as V
8+import qualified Graphics.Gnuplot.Simple as GP
9+import qualified Graphics.Gnuplot.Plot.ThreeDimensional as Plot3D
10+import qualified Graphics.Gnuplot.Graph.ThreeDimensional as Graph3D
11+import qualified Graphics.Gnuplot.Advanced as GPA
9
12
10--this module support plotting relations containing integer attributes with arity 1,2, or 3 only
13--this module support plotting relations containing integer attributes with arity 1,2, or 3 only
11--nested relations?
14--nested relations?
@@ -19,80 +22,71 @@ data PlotError = InvalidAttributeCountError |
19intFromAtomIndex :: Int -> RelationTuple -> Int
22intFromAtomIndex :: Int -> RelationTuple -> Int
20intFromAtomIndex index tup = (\(IntAtom i) -> i) $ (tupleAtoms tup) V.! index
23intFromAtomIndex index tup = (\(IntAtom i) -> i) $ (tupleAtoms tup) V.! index
21
24
25+pointStyle :: GP.PlotStyle
26+pointStyle = GP.PlotStyle { GP.plotType = GP.Points,
27+ GP.lineSpec = GP.CustomStyle [GP.PointSize 2.0]}
28+
22−graph1DRelation :: Relation -> Graph2D Double Double
29+graph1DRelation :: Relation -> IO ()
23−graph1DRelation rel = Data2D [Title "rel"] [] $ map (\i -> (fromIntegral i, 0.0)) (points1DRelation rel)
30+graph1DRelation rel = GP.plotPathStyle [] pointStyle $ map (\i -> (i, 0)) (points1DRelation rel)
24
31
25points1DRelation :: Relation -> [Int]
32points1DRelation :: Relation -> [Int]
26points1DRelation rel = relFold folder [] rel
33points1DRelation rel = relFold folder [] rel
27 where
34 where
28 folder tup acc = intFromAtomIndex 0 tup : acc
35 folder tup acc = intFromAtomIndex 0 tup : acc
29
36
30−graph2DRelation :: Relation -> Graph2D Double Double
37+graph2DRelation :: Relation -> IO ()
31−graph2DRelation rel = Data2D [Title "rel"] [Step 1.0] $ map (\(x,y) -> (fromIntegral x, fromIntegral y)) $ points2DRelation rel
38+graph2DRelation rel = GP.plotPathStyle [] pointStyle $ points2DRelation rel
32
39
33points2DRelation :: Relation -> [(Int, Int)]
40points2DRelation :: Relation -> [(Int, Int)]
34points2DRelation rel = relFold folder [] rel
41points2DRelation rel = relFold folder [] rel
35 where
42 where
36 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup) : acc
43 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup) : acc
37
44
38−graph3DRelation :: Relation -> [Graph3D Double Double Double]
45+graph3DRelation :: Relation -> IO ()
39−graph3DRelation rel = map lineGraph $ points3DRelation rel
46+graph3DRelation rel = do
40− where
47+ _ <- GPA.plotDefault $ Plot3D.cloud Graph3D.points $ points3DRelation rel
41− lineGraph (x,y,z) = Data3D [Title "rel", Style Lines] [StepX 1.0, StepY 1.0] $ [(fromIntegral x, fromIntegral y, fromIntegral z),
48+ return ()
42− (fromIntegral x, 0, 0)]
43
49
44points3DRelation :: Relation -> [(Int, Int, Int)]
50points3DRelation :: Relation -> [(Int, Int, Int)]
45points3DRelation rel = relFold folder [] rel
51points3DRelation rel = relFold folder [] rel
46 where
52 where
47 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup, intFromAtomIndex 2 tup) : acc
53 folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup, intFromAtomIndex 2 tup) : acc
48
54
55+{-
49sample1DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType]) [[IntAtom 2], [IntAtom 3]] of
56sample1DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType]) [[IntAtom 2], [IntAtom 3]] of
50 Right rel -> rel
57 Right rel -> rel
51 Left _ -> undefined
58 Left _ -> undefined
52
59
53−sample2dRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType]) [[IntAtom 2, IntAtom 3],
60+sample2DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType]) [[IntAtom 2, IntAtom 3],
54− [IntAtom 2, IntAtom 4]] of
61+ [IntAtom 2, IntAtom 4]] of
55 Right rel -> rel
62 Right rel -> rel
56 Left _ -> undefined
63 Left _ -> undefined
57
64
58−sample3dRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType, Attribute "z" IntAtomType]) [[IntAtom 2, IntAtom 3, IntAtom 3],
65+sample3DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType, Attribute "z" IntAtomType]) [[IntAtom 2, IntAtom 3, IntAtom 3],
59 [IntAtom 2, IntAtom 4, IntAtom 4]] of
66 [IntAtom 2, IntAtom 4, IntAtom 4]] of
60 Right rel -> rel
67 Right rel -> rel
61 Left _ -> undefined
68 Left _ -> undefined
62−
63−type G2D = Graph2D Double Double
64−type G3D = Graph3D Double Double Double
65
69
70+-}
71+
66−plotRelation :: Relation -> Either PlotError (Either G2D [G3D])
72+plotRelation :: Relation -> IO (Maybe PlotError)
67plotRelation rel = let attrTypes = V.replicate (arity rel) IntAtomType in
73plotRelation rel = let attrTypes = V.replicate (arity rel) IntAtomType in
68 if attrTypes /= A.atomTypes (attributes rel) then
74 if attrTypes /= A.atomTypes (attributes rel) then
69− Left InvalidAttributeTypeError
75+ return $ Just InvalidAttributeTypeError
70 else
76 else
71 case arity rel of
77 case arity rel of
72− 1 -> Right $ Left $ graph1DRelation rel
78+ 1 -> graph1DRelation rel >> return Nothing
73− 2 -> Right $ Left $ graph2DRelation rel
79+ 2 -> graph2DRelation rel >> return Nothing
74− 3 -> Right $ Right $ graph3DRelation rel
80+ 3 -> graph3DRelation rel >> return Nothing
75− _ -> Left InvalidAttributeCountError
81+ _ -> return $ Just InvalidAttributeCountError
82+
76
83
84+{-
77--PNG
85--PNG
78savePlottedRelation :: String -> Relation -> IO ()
86savePlottedRelation :: String -> Relation -> IO ()
79savePlottedRelation path rel = case plotRelation rel of
87savePlottedRelation path rel = case plotRelation rel of
80 Left err -> putStrLn (show err)
88 Left err -> putStrLn (show err)
81 Right (Left g2d) -> plot' [] (PNG path) g2d >> return ()
89 Right (Left g2d) -> plot' [] (PNG path) g2d >> return ()
82 Right (Right g3d) -> plot' [] (PNG path) g3d >> return ()
90 Right (Right g3d) -> plot' [] (PNG path) g3d >> return ()
83−
91+-}
84−--X11
85−showPlottedRelation :: Relation -> IO ()
86−showPlottedRelation rel = case plotRelation rel of
87− Left err -> putStrLn (show err)
88− Right (Left g2d) -> plot' [] X11 g2d >> return ()
89− Right (Right g3d) -> plot' [] X11 g3d >> return ()
90−
91−
92−ezplot :: Relation -> IO ()
93−ezplot rel = do
94− case plotRelation rel of
95− Left err -> putStrLn $ show err
96− Right (Left g2d) -> plot' [Interactive] X11 g2d >> return ()
97− Right (Right g3d) -> plot' [Interactive] X11 g3d >> return ()
98
92
TutorialD/Interpreter/RODatabaseContextOperator.hs+4−1
@@ -14,6 +14,7 @@ import qualified Data.Text as T
14import ProjectM36.Relation.Show.Term
14import ProjectM36.Relation.Show.Term
15import ProjectM36.Relation.Show.Gnuplot
15import ProjectM36.Relation.Show.Gnuplot
16import qualified Data.Map as M
16import qualified Data.Map as M
17+import Data.Maybe
17
18
18--operators which only rely on database context reading
19--operators which only rely on database context reading
19data RODatabaseContextOperator where
20data RODatabaseContextOperator where
@@ -81,7 +82,9 @@ evalRODatabaseContextOp context (ShowRelation expr) = do
81evalRODatabaseContextOp context (PlotRelation expr) = case runState (evalRelationalExpr expr) context of
82evalRODatabaseContextOp context (PlotRelation expr) = case runState (evalRelationalExpr expr) context of
82 (Left err, _) -> DisplayErrorResult $ T.pack (show err)
83 (Left err, _) -> DisplayErrorResult $ T.pack (show err)
83--(Right rel, _) -> DisplayIOResult $ showPlottedRelation rel
84--(Right rel, _) -> DisplayIOResult $ showPlottedRelation rel
84− (Right rel, _) -> DisplayIOResult $ savePlottedRelation "/tmp/graph.png" rel
85+ (Right rel, _) -> DisplayIOResult $ do
86+ err <- plotRelation rel
87+ when (isJust err) $ putStrLn (show err)
85
88
86evalRODatabaseContextOp context (ShowConstraint name) =
89evalRODatabaseContextOp context (ShowConstraint name) =
87 case name of
90 case name of
project-m36.cabal+1−1
@@ -7,7 +7,7 @@ Category: Relational Algebra
7Cabal-Version: >= 1.2
7Cabal-Version: >= 1.2
8
8
9Library
9Library
10− Build-Depends: base,mtl,containers,unordered-containers,parsec,hashable,haskeline,directory,uuid,cassava,text,bytestring,deepseq,deepseq-generics,vector,parallel,monad-parallel,transformers,easyplot
10+ Build-Depends: base,mtl,containers,unordered-containers,parsec,hashable,haskeline,directory,uuid,cassava,text,bytestring,deepseq,deepseq-generics,vector,parallel,monad-parallel,transformers,gnuplot
11 Exposed-Modules: ProjectM36.Error,
11 Exposed-Modules: ProjectM36.Error,
12 ProjectM36.Transaction,
12 ProjectM36.Transaction,
13 ProjectM36.Transaction.Show,
13 ProjectM36.Transaction.Show,