on master help sign in
basic gnuplot relation rendering
commit82a67911fe0fabf8b7b514baa3023ef9b79340f1 authorAgentM <agentm@themactionfaction.com> dateMon 22 Jun 2015, 23:50 (11 years ago) parent636861e
6 files changed+122−3
ProjectM36/Attribute.hs+3 ProjectM36/Relation/Show/Gnuplot.hs+95 TutorialD/Interpreter.hs+3 TutorialD/Interpreter/Base.hs+2 TutorialD/Interpreter/RODatabaseContextOperator.hs+16−1 project-m36.cabal+3−2
ProjectM36/Attribute.hs+3
@@ -25,6 +25,9 @@ attributeName (Attribute name _) = name
2525atomType :: Attribute -> AtomType
2626atomType (Attribute _ atype) = atype
2727
28+atomTypes :: Attributes -> V.Vector AtomType
29+atomTypes attrs = V.map atomType attrs
30+
2831isRelationAtomType :: AtomType -> Bool
2932isRelationAtomType (RelationAtomType _) = True
3033isRelationAtomType _ = False
ProjectM36/Relation/Show/Gnuplot.hsnew file+95
@@ -0,0 +1,95 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module ProjectM36.Relation.Show.Gnuplot where
3+import ProjectM36.Base
4+import ProjectM36.Relation
5+import ProjectM36.Tuple
6+import qualified ProjectM36.Attribute as A
7+import Graphics.EasyPlot
8+import qualified Data.Vector as V
9+
10+--this module support plotting relations containing integer attributes with arity 1,2, or 3 only
11+--nested relations?
12+
13+data PlotError = InvalidAttributeCountError |
14+ InvalidAttributeTypeError
15+ deriving (Show)
16+
17+--plotRelation :: Relation -> Either PlotError
18+
19+intFromAtomIndex :: Int -> RelationTuple -> Int
20+intFromAtomIndex index tup = (\(IntAtom i) -> i) $ (tupleAtoms tup) V.! index
21+
22+graph1DRelation :: Relation -> Graph2D Double Double
23+graph1DRelation rel = Data2D [Title "rel"] [] $ map (\i -> (fromIntegral i, 0.0)) (points1DRelation rel)
24+
25+points1DRelation :: Relation -> [Int]
26+points1DRelation rel = relFold folder [] rel
27+ where
28+ folder tup acc = intFromAtomIndex 0 tup : acc
29+
30+graph2DRelation :: Relation -> Graph2D Double Double
31+graph2DRelation rel = Data2D [Title "rel"] [Step 1.0] $ map (\(x,y) -> (fromIntegral x, fromIntegral y)) $ points2DRelation rel
32+
33+points2DRelation :: Relation -> [(Int, Int)]
34+points2DRelation rel = relFold folder [] rel
35+ where
36+ folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup) : acc
37+
38+graph3DRelation :: Relation -> Graph3D Double Double Double
39+graph3DRelation rel = Data3D [Title "rel"] [StepX 1.0, StepY 1.0] $ map (\(x,y,z) -> (fromIntegral x, fromIntegral y, fromIntegral z)) $ points3DRelation rel
40+
41+points3DRelation :: Relation -> [(Int, Int, Int)]
42+points3DRelation rel = relFold folder [] rel
43+ where
44+ folder tup acc = (intFromAtomIndex 0 tup, intFromAtomIndex 1 tup, intFromAtomIndex 2 tup) : acc
45+
46+sample1DRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType]) [[IntAtom 2], [IntAtom 3]] of
47+ Right rel -> rel
48+ Left _ -> undefined
49+
50+sample2dRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType]) [[IntAtom 2, IntAtom 3],
51+ [IntAtom 2, IntAtom 4]] of
52+ Right rel -> rel
53+ Left _ -> undefined
54+
55+sample3dRelation = case mkRelationFromList (A.attributesFromList [Attribute "x" IntAtomType, Attribute "y" IntAtomType, Attribute "z" IntAtomType]) [[IntAtom 2, IntAtom 3, IntAtom 3],
56+ [IntAtom 2, IntAtom 4, IntAtom 4]] of
57+ Right rel -> rel
58+ Left _ -> undefined
59+
60+type G2D = Graph2D Double Double
61+type G3D = Graph3D Double Double Double
62+
63+plotRelation :: Relation -> Either PlotError (Either G2D G3D)
64+plotRelation rel = let attrTypes = V.replicate (arity rel) IntAtomType in
65+ if attrTypes /= A.atomTypes (attributes rel) then
66+ Left InvalidAttributeTypeError
67+ else
68+ case arity rel of
69+ 1 -> Right $ Left $ graph1DRelation rel
70+ 2 -> Right $ Left $ graph2DRelation rel
71+ 3 -> Right $ Right $ graph3DRelation rel
72+ _ -> Left InvalidAttributeCountError
73+
74+--PNG
75+savePlottedRelation :: String -> Relation -> IO ()
76+savePlottedRelation path rel = case plotRelation rel of
77+ Left err -> putStrLn (show err)
78+ Right (Left g2d) -> plot' [] (PNG path) g2d >> return ()
79+ Right (Right g3d) -> plot' [] (PNG path) g3d >> return ()
80+
81+--X11
82+showPlottedRelation :: Relation -> IO ()
83+showPlottedRelation rel = case plotRelation rel of
84+ Left err -> putStrLn (show err)
85+ Right (Left g2d) -> plot' [] X11 g2d >> return ()
86+ Right (Right g3d) -> plot' [] X11 g3d >> return ()
87+
88+
89+ezplot :: Relation -> IO ()
90+ezplot rel = do
91+ case plotRelation rel of
92+ Left err -> putStrLn $ show err
93+ Right (Left g2d) -> plot' [Interactive] X11 g2d >> return ()
94+ Right (Right g3d) -> plot' [Interactive] X11 g3d >> return ()
95+
TutorialD/Interpreter.hs+3
@@ -69,6 +69,9 @@ reprLoop currentTransaction graph = do
6969 case interpret newUUID currentTransaction graph line of
7070 (QuitResult, _, _) -> return ()
7171 (DisplayErrorResult err, _, _) -> TIO.hPutStrLn stderr ("ERR: " `T.append` err) >> roloop
72+ (DisplayIOResult outio, updatedTrans, updatedGraph) -> do
73+ outio
74+ reprLoop updatedTrans updatedGraph
7275 (DisplayResult out, updatedTrans, updatedGraph) -> do
7376 TIO.putStrLn out
7477 reprLoop updatedTrans updatedGraph
TutorialD/Interpreter/Base.hs+2
@@ -61,11 +61,13 @@ showRelationAttributes attrs = "{" `T.append` T.concat (L.intersperse ", " $ map
6161
6262data TutorialDOperatorResult = QuitResult |
6363 DisplayResult StringType |
64+ DisplayIOResult (IO ()) |
6465 DisplayErrorResult StringType |
6566 QuietSuccessResult
6667
6768displayOpResult :: TutorialDOperatorResult -> IO ()
6869displayOpResult QuitResult = return ()
6970displayOpResult (DisplayResult out) = TIO.putStrLn out
71+displayOpResult (DisplayIOResult ioout) = ioout
7072displayOpResult (DisplayErrorResult err) = TIO.hPutStrLn stderr ("ERR: " `T.append` err)
7173displayOpResult QuietSuccessResult = return ()
TutorialD/Interpreter/RODatabaseContextOperator.hs+16−1
@@ -12,11 +12,13 @@ import TutorialD.Interpreter.DatabaseExpr
1212import Control.Monad.State
1313import qualified Data.Text as T
1414import ProjectM36.Relation.Show.Term
15+import ProjectM36.Relation.Show.Gnuplot
1516import qualified Data.Map as M
1617
1718--operators which only rely on database context reading
1819data RODatabaseContextOperator where
1920 ShowRelation :: RelationalExpr -> RODatabaseContextOperator
21+ PlotRelation :: RelationalExpr -> RODatabaseContextOperator
2022 ShowRelationType :: RelationalExpr -> RODatabaseContextOperator
2123 ShowConstraint :: StringType -> RODatabaseContextOperator
2224 ShowPlan :: DatabaseExpr -> RODatabaseContextOperator
@@ -51,10 +53,17 @@ showConstraintsP = do
5153 reservedOp ":constraints"
5254 constraintName <- option "" identifier
5355 return $ ShowConstraint (T.pack constraintName)
56+
57+plotRelExprP :: Parser RODatabaseContextOperator
58+plotRelExprP = do
59+ reserved ":plotexpr"
60+ expr <- relExprP
61+ return $ PlotRelation expr
5462
5563roDatabaseContextOperatorP :: Parser RODatabaseContextOperator
5664roDatabaseContextOperatorP = typeP
5765 <|> showRelP
66+ <|> plotRelExprP
5867 <|> showConstraintsP
5968 <|> showPlanP
6069 <|> quitP
@@ -68,6 +77,10 @@ evalRODatabaseContextOp context (ShowRelation expr) = do
6877 case runState (evalRelationalExpr expr) context of
6978 (Left err, _) -> DisplayErrorResult $ T.pack (show err)
7079 (Right rel, _) -> DisplayResult $ showRelation rel
80+
81+evalRODatabaseContextOp context (PlotRelation expr) = case runState (evalRelationalExpr expr) context of
82+ (Left err, _) -> DisplayErrorResult $ T.pack (show err)
83+ (Right rel, _) -> DisplayIOResult $ showPlottedRelation rel
7184
7285evalRODatabaseContextOp context (ShowConstraint name) =
7386 case name of
@@ -88,4 +101,6 @@ evalRODatabaseContextOp _ (Quit) = QuitResult
88101interpretRODatabaseContextOp :: DatabaseContext -> String -> TutorialDOperatorResult
89102interpretRODatabaseContextOp context tutdstring = case parse roDatabaseContextOperatorP "" tutdstring of
90103 Left err -> DisplayErrorResult (T.pack (show err))
91− Right parsed -> evalRODatabaseContextOp context parsed
104+ Right parsed -> evalRODatabaseContextOp context parsed
105+
106+
project-m36.cabal+3−2
@@ -7,7 +7,7 @@ Category: Relational Algebra
77Cabal-Version: >= 1.2
88
99Library
10− Build-Depends: base,mtl,containers,unordered-containers,parsec,hashable,haskeline,directory,uuid,cassava,text,bytestring,deepseq,deepseq-generics,vector,parallel,monad-parallel,transformers
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
1111 Exposed-Modules: ProjectM36.Error,
1212 ProjectM36.Transaction,
1313 ProjectM36.Transaction.Show,
@@ -23,7 +23,8 @@ Library
2323 ProjectM36.Relation.Show.HTML,
2424 ProjectM36.StaticOptimizer,
2525 ProjectM36.Relation.Show.Term,
26− ProjectM36.Relation.Show.CSV
26+ ProjectM36.Relation.Show.CSV,
27+ ProjectM36.Relation.Show.Gnuplot
2728 GHC-Options: -Wall -rtsopts
2829
2930Executable tutd