]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.selection/scl/Simantics/District/Selection.scl
Update SCL interface for element selection
[simantics/district.git] / org.simantics.district.selection / scl / Simantics / District / Selection.scl
1 import "http://www.simantics.org/ElementSelection-1.0" as ES
2 import "Simantics/DB"
3
4 importJava "org.simantics.district.selection.ElementSelector" where
5     data ElementSelector
6
7     """
8     Get a selection resource represented as an ElementSelector object.
9     """
10     getSelector :: Resource -> <ReadGraph> ElementSelector
11     
12     """
13     Get the name of the selection from an element selector object.
14     """
15     @JavaName getName
16     getSelectorName :: ElementSelector -> String
17     
18     """
19     Get an expression that represents the selection from an element selector object.
20     """
21     @JavaName getExpression
22     getSelectorExpression :: ElementSelector -> String
23     
24     """
25     Get the selection resource form an element selector object.
26     """
27     @JavaName getResource
28     getSelectorResource :: ElementSelector -> Resource
29
30     @JavaName selectElementsFrom
31     selectElementsFrom' :: ElementSelector -> Resource -> <ReadGraph> SelectionResult
32
33 """
34 Get the elements selected by an element selector object.
35
36 `selectedElements = selectElementsFrom elementSelector model`
37 """
38 selectElementsFrom :: ElementSelector -> Resource -> <ReadGraph> [Resource]
39 selectElementsFrom s r = selectedElements $ selectElementsFrom' s r
40
41 importJava "org.simantics.district.selection.ElementSelector$SelectionResult" where
42     data SelectionResult
43     
44     "Get the list of selected elements"
45     @JavaName elements
46     selectedElements :: SelectionResult -> [Resource]
47     "Number of selected elements with equal selection criteria (for n lowest/n highest selections)"
48     tailCount :: SelectionResult -> Integer
49     "Actual number of elements with selection criteria equal to the last selected element (for n lowest/n highest selections)"
50     tailSize :: SelectionResult -> Integer
51
52 data Generator = Model | Diagram Resource | Explicit [Resource]
53 data Selector = All | NLowest String Integer | NHighest String Integer
54 data Condition = PropertyCondition String (Maybe Double) (Maybe Double)
55                | Region Resource | Route Resource
56                | Conjunction [Condition] | Disjunction [Condition] | Negation [Condition]
57                | Not Condition
58 data Selection = Selection Generator Selector (Maybe Condition)
59
60 """
61 Write a selection expression into the database.
62
63 `selectionResource = writeSelection parentResource selectionName selection`
64 """
65 writeSelection :: Resource -> String -> Selection -> <WriteGraph> Resource
66 writeSelection root name (Selection generator selector maybeCondition) = let
67     selection = newEntity [hasName name, hasLabel name, hasType ES.Selection, hasParent root]
68     generator = match generator with
69         Model -> newEntity [hasType ES.Generator.Model]
70         Diagram diagram -> newEntity [hasType ES.Generator.Diagram, hasStatement ES.Generator.HasDiagram diagram]
71         Explicit elements -> let
72             g = newEntity [hasType ES.Generator.Explicit]
73             for elements (claim g ES.Generator.HasSelectedElement)
74             in g
75     claim selection ES.Selection.HasGenerator generator
76     selector = match selector with
77         All -> newEntity [hasType ES.Selector.All]
78         NLowest propertyName count -> newEntity [hasType ES.Selector.NLowest,
79                                                  hasProperty ES.PropertySelector.HasSelectionPropertyName propertyName,
80                                                  hasProperty ES.PropertySelector.HasResultCount count]
81         NHighest propertyName count -> newEntity [hasType ES.Selector.NHighest,
82                                                   hasProperty ES.PropertySelector.HasSelectionPropertyName propertyName,
83                                                   hasProperty ES.PropertySelector.HasResultCount count]
84     claim selection ES.Selection.HasSelector selector
85     for maybeCondition \condition -> claim selection ES.Selection.HasCondition $ writeCondition condition
86     in selection
87   where
88     writeCondition condition = match condition with
89             PropertyCondition propertyName lowerLimit upperLimit -> let
90                 cond = newEntity [hasType ES.PropertyCondition, hasProperty ES.PropertyCondition.HasPropertyName propertyName]
91                 for lowerLimit \limit -> claimRelatedValue cond ES.PropertyCondition.HasLowerLimit limit
92                 for upperLimit \limit -> claimRelatedValue cond ES.PropertyCondition.HasUpperLimit limit
93                 in cond
94             Region region -> newEntity [hasType ES.RegionCondition, hasProperty ES.RegionCondition.HasRegion region]
95             Route route -> newEntity [hasType ES.RouteCondition, hasProperty ES.RouteCondition.HasRoute route]
96             Conjunction conds -> writeAggregateCondition ES.Conjunction conds
97             Disjunction conds -> writeAggregateCondition ES.Disjunction conds
98             Negation conds -> writeAggregateCondition ES.Negation conds
99             Not cond -> writeAggregateCondition ES.Negation [cond]
100     writeAggregateCondition condType conds = let
101         cond = newEntity [hasType condType]
102         for conds \c -> claim cond ES.HasSubcondition $ writeCondition c
103         in cond