]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/scl/Simantics/District.scl
56e8edb45367a08ef6e5e2f481e0d3c8a3c36f4d
[simantics/district.git] / org.simantics.district.network / scl / Simantics / District.scl
1 import "Simantics/DB"
2 import "Simantics/Ontologies"
3 import "Simantics/Variables"
4 import "http://www.simantics.org/DistrictNetwork-1.0" as DN
5
6 import "Map" as Map
7 import "MSet" as MSet
8
9 importJava "org.simantics.district.network.DistrictNetworkUtil" where
10     calculateDistance :: Resource -> Resource -> <ReadGraph> Double
11
12 importJava "org.simantics.district.network.ModelledCRS" where
13     xToLongitude :: Double -> <Proc> Double
14     yToLatitude :: Double -> <Proc> Double
15     longitudeToX :: Double -> <Proc> Double
16     latitudeToY :: Double -> <Proc> Double
17
18 findBrokenConnections :: Resource -> <ReadGraph, Proc> ()
19 findBrokenConnections diagram = do
20     elements = objectsWithType diagram L0.ConsistsOf DIA.DefinedElement
21     print $ "Found " + show (length elements) + " elements from diagram"
22     symbols = MMap.create ()
23     iter (\el -> do
24         symbol = singleObject el L0.InstanceOf
25         print $ "Found symbol " + uriOf symbol
26         ccs = collectionToList $ objects_ symbol L0.ConsistsOf
27         connectionPointRelations = filter (\cc -> isSubrelationOf cc  STR.IsConnectedTo) ccs
28         print $ "Found " + show (length connectionPointRelations) + " cprs"
29         iter (\cpr -> do
30             connectionPoints = objectsWithType el cpr DIA.Connector
31             print $ "Found " + show (length connectionPoints) + " cps"
32             if length connectionPoints > 1 /*|| length connectionPoints == 0*/
33             then do
34                 // get existing map
35                 print $ "Found " + show (length connectionPoints) + " connections for element " + nameOf el
36                 instances = match MMap.get symbols (uriOf symbol) with
37                     Just m -> m
38                     Nothing -> do
39                         set = MSet.create ()
40                         MMap.put symbols (uriOf symbol) set
41                         set
42                 ignore $ MSet.add instances $ singleObject el MOD.ElementToComponent
43             else ()
44             ()
45         ) connectionPointRelations
46     ) elements
47     MMap.iter (\key val -> do
48         print key
49         MSet.iter (\v -> print (nameOf v)) val
50         ()
51     ) symbols 
52     ()
53
54 translateAllElements :: Resource -> <WriteGraph, Proc> ()
55 translateAllElements diagram = do
56     print $ "Translating elements of diagram " + uriOf diagram 
57     elements = objectsWithType diagram L0.ConsistsOf DIA.DefinedElement
58     print $ "Found " + show (length elements) + " elements from diagram"
59     iter (\el -> do
60         connectionPoints = objectsWithType el STR.IsConnectedTo DIA.Connector
61         print $ "Found " + show (length connectionPoints) + " connections for element " + nameOf el
62     ) elements
63
64
65 translateElement :: Resource -> <WriteGraph, Proc> ()
66 translateElement elem = do
67     connectionPoints = objectsWithType elem STR.IsConnectedTo DIA.Connector
68     if length connectionPoints < 5
69     then do //this we can handle
70         areConnected = MSet.create ()
71         iter (\cp -> do
72             aconnector = singleObject cp  DIA.AreConnected
73             //otherElem
74             ()
75         ) []
76         //if length areConnected == 2
77         //then do // this we can handle - only two symbols
78         //    ()
79         //else ()
80         ()
81     else ()
82     ()
83
84 importJava "org.simantics.district.network.DistrictNetworkUtil" where
85     createVertex :: Resource -> Vector Double -> Double -> Resource -> <WriteGraph, Proc> Resource
86     createEdge :: Resource -> Resource -> Vector Double -> <WriteGraph, Proc> Resource
87
88 """
89 Tries to look for the Resource representing the configuration component
90 mapped to the specified district network diagram element.
91 """
92 possibleMappedComponent :: Resource -> <ReadGraph> Maybe Resource
93 possibleMappedComponent r = match possibleObject r DN.MappedComponent with
94     Nothing -> Nothing
95     Just me -> possibleObject me MOD.ElementToComponent
96
97 """
98 Tries to look for the Variable representing the configuration component
99 mapped to the specified district network diagram element.
100
101 The variable returned is by default the active experiment context variable
102 but if that is not available then a configuration context variable is returned.
103 """
104 possibleMappedComponentVariable :: Resource -> <ReadGraph> Maybe Variable
105 possibleMappedComponentVariable r = match possibleMappedComponent r with
106     Nothing -> Nothing
107     Just mc -> do
108         mcv = resourceVariable mc
109         match possibleActiveVariable mcv with
110             Nothing -> Just mcv
111             a       -> a
112
113 """
114 """
115 possibleMappedComponentPropertyValue :: Serializable a => Typeable a => String -> Resource -> <ReadGraph> Maybe a
116 possibleMappedComponentPropertyValue propName r = match possibleMappedComponentVariable r with
117     Nothing -> Nothing
118     Just mv -> possiblePropertyValue mv propName
119
120 """
121 """
122 mappedComponentPropertyValue :: Serializable a => Typeable a => a -> String -> Resource -> <ReadGraph> a
123 mappedComponentPropertyValue def propName r = match possibleMappedComponentVariable r with
124     Nothing -> def
125     Just mv -> match possiblePropertyValue mv propName with
126         Nothing -> def
127         Just v  -> v
128
129 """
130 Returns possible district network element that is mapped to the
131 specified model configuration component. The DN element is found
132 using the following relation path:
133
134     domain model configuration component ---[MOD.ComponentToElement]---> domain model diagram element
135                                          ----[DN.MappedFromElement]----> district network diagram element
136 """
137 possibleDNElementMappedToComponent :: Resource -> <ReadGraph> Maybe Resource
138 possibleDNElementMappedToComponent mappedComponent = match possibleObject mappedComponent MOD.ComponentToElement with
139     Nothing      -> Nothing
140     Just element -> possibleObject element DN.MappedFromElement
141
142 """
143 Returns all district network elements that are found to be mapped the
144 specified mapped model configuration components by
145 [possibleDNElementMappedToComponent](possibleDNElementMappedToComponent).
146
147 The result list can be smaller than the input resource list, even empty.
148 """
149 dnElementsMappedToComponents :: [Resource] -> <ReadGraph> [Resource]
150 dnElementsMappedToComponents mappedComponents = mapMaybe possibleDNElementMappedToComponent mappedComponents
151
152
153 importJava "org.simantics.district.network.DistrictNetworkUtil" where
154     createNetworkDiagram :: Resource -> Resource -> String -> Resource -> Resource -> Resource -> Resource -> Resource -> <WriteGraph, Proc> Resource
155     changeMappingType :: Resource -> [Resource] -> <WriteGraph, Proc> ()