5 include "Simantics/DB" hiding (resourceId)
6 import "http://www.simantics.org/Layer0-1.1" as L0
7 import "http://www.simantics.org/Modeling-1.2" as MOD
8 import "StringBuilder" as StringBuilder
11 "Converts a value to string in a read transtion."
12 gshow :: Resource -> a -> <Proc,ReadGraph> String
13 "Appends the string representation of the value to the string builder in a read transaction."
14 gappend :: Resource -> StringBuilder.T -> a -> <Proc,ReadGraph> StringBuilder.T
15 graphPrecedence :: a -> Integer
17 gshow ctx v = StringBuilder.toString (gappend ctx StringBuilder.new v)
18 gappend ctx sb v = StringBuilder.appendString sb $ gshow ctx v
21 instance (GShow a) => GShow (Par a) where
22 gappend ctx sb (Par outerPrec v) = do
23 prec = graphPrecedence v
31 instance GShow String where
34 instance GShow Integer where
36 graphPrecedence v = if v >= 0 then 0 else 100
38 instance GShow Long where
40 graphPrecedence v = if v >= 0 then 0 else 100
42 instance GShow Float where
44 graphPrecedence v = if v >= 0 then 0 else 100
46 instance GShow Double where
48 graphPrecedence v = if v >= 0 then 0 else 100
50 instance GShow Boolean where
53 instance GShow DoubleArray where
54 gappend _ sb v = sb << "toDoubleArray " <+ fromDoubleArray v
57 importJava "org.simantics.utils.bytes.Base64" where
59 encodeBase64 :: ByteArray -> String
61 decodeBase64 :: String -> ByteArray
63 instance GShow ByteArray where
64 gappend _ sb v = sb << "decodeBase64 " <+ (encodeBase64 v)
68 importJava "org.simantics.db.layer0.util.ExtendedUris" where
69 getPrimaryFunctionalReference :: Resource -> <ReadGraph> Maybe (Resource,Resource)
70 getSecondaryFunctionalReference :: Resource -> <ReadGraph> Maybe (Resource,Resource)
73 NamedChildOf ResourceId String
74 | PropertyOf ResourceId String
76 | ContextRelativeUri String
79 importJava "gnu.trove.set.hash.THashSet" where
82 createResourceSet :: () -> /*<Proc> not necessary in our case*/ ResourceSet
84 addResourceSet :: ResourceSet -> Resource -> /*<Proc> not necessary in our case*/ Boolean
86 resourceId :: Resource -> Resource -> <ReadGraph> Maybe ResourceId
87 resourceId ctx r = loop r
89 guardSet = createResourceSet ()
90 loop r = if addResourceSet guardSet r
92 (guard (r == ctx) >> return (ContextRelativeUri ""))
94 (guard (r == getRootLibrary ()) >> return (AbsoluteUri "http:/"))
97 (p,rel) <- getPrimaryFunctionalReference r
99 relName <- possibleNameOf rel
100 return $ simplifyResourceIdHead $ PropertyOf path relName
104 p <- possibleObject r L0.PartOf
106 name <- possibleNameOf r
107 return $ simplifyResourceIdHead $ NamedChildOf path name
111 (p,rel) <- getSecondaryFunctionalReference r
113 relName <- possibleNameOf rel
114 return $ simplifyResourceIdHead $ PropertyOf path relName
121 importJava "org.simantics.databoard.util.URIStringUtils" where
122 escape :: String -> String
124 simplifyResourceIdHead (NamedChildOf (AbsoluteUri uri) name) = AbsoluteUri (uri + "/" + escape name)
125 simplifyResourceIdHead (NamedChildOf (ContextRelativeUri uri) name) = ContextRelativeUri (uri + "/" + escape name)
126 simplifyResourceIdHead (PropertyOf (AbsoluteUri uri) name) = AbsoluteUri (uri + "#" + escape name)
127 simplifyResourceIdHead (PropertyOf (ContextRelativeUri uri) name) = ContextRelativeUri (uri + "#" + escape name)
128 simplifyResourceIdHead id = id
130 instance Show ResourceId where
131 sb <+ NamedChildOf parent name = sb << "fromJust (possibleResourceChild " <+ Par 0 parent << " " <+ name << ")"
132 sb <+ ContextRelativeUri uri = sb << "modelResource " <+ uri
133 sb <+ AbsoluteUri uri = sb << "resource " <+ uri
137 instance GShow Resource where
138 gappend ctx sb r = sb <+ fromJust (resourceId ctx r)
139 graphPrecedence v = 1
141 instance (GShow a) => GShow [a] where
142 gappend ctx sb l = do
146 if (i>0) then sb << ", " else sb
153 instance GShow () where
156 instance (GShow a, GShow b) => GShow (a, b) where
157 gappend ctx sb (x, y) = do
164 instance (GShow a, GShow b, GShow c) => GShow (a, b, c) where
165 gappend ctx sb (x, y, z) = do
174 instance (GShow a, GShow b, GShow c, GShow d) => GShow (a, b, c, d) where
175 gappend ctx sb (x, y, z, w) = do
186 instance (GShow a) => GShow (Maybe a) where
187 gappend ctx sb Nothing = sb << "Nothing"
188 gappend ctx sb (Just x) = do
190 gappend ctx sb (Par 0 x)
191 graphPrecedence (Just x) = 1
192 graphPrecedence Nothing = 0
194 instance (GShow a, GShow b) => GShow (Either a b) where
195 gappend ctx sb (Left x) = do
197 gappend ctx sb (Par 0 x)
198 gappend ctx sb (Right x) = do
200 gappend ctx sb (Par 0 x)
201 graphPrecedence _ = 1