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