]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.selection/scl/Simantics/District/Selection.scl
4f1d2705e4a8b114cdccfa35c2b20b08d0914b98
[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     """
31     Get the elements selected by an element selector object.
32
33     `selectedElements = selectElementsFrom elementSelector model`
34     """
35     @JavaName selectElementsFrom
36     selectElementsFrom :: ElementSelector -> Resource -> <ReadGraph> [Resource]
37
38 data Generator = Model | Diagram Resource | Explicit [Resource]
39 data Selector = All | NLowest String Integer | NHighest String Integer
40 data Condition = PropertyCondition String (Maybe Double) (Maybe Double)
41                | Region Resource | Route Resource
42                | Conjunction [Condition] | Disjunction [Condition] | Negation [Condition]
43                | Not Condition
44 data Selection = Selection Generator Selector (Maybe Condition)
45
46 """
47 Write a selection expression into the database.
48
49 `selectionResource = writeSelection parentResource selectionName selection`
50 """
51 writeSelection :: Resource -> String -> Selection -> <WriteGraph> Resource
52 writeSelection root name (Selection generator selector maybeCondition) = let
53     selection = newEntity [hasName name, hasLabel name, hasType ES.Selection, hasParent root]
54     generator = match generator with
55         Model -> newEntity [hasType ES.Generator.Model]
56         Diagram diagram -> newEntity [hasType ES.Generator.Diagram, hasStatement ES.Generator.HasDiagram diagram]
57         Explicit elements -> let
58             g = newEntity [hasType ES.Generator.Explicit]
59             for elements (claim g ES.Generator.HasSelectedElement)
60             in g
61     claim selection ES.Selection.HasGenerator generator
62     selector = match selector with
63         All -> newEntity [hasType ES.Selector.All]
64         NLowest propertyName count -> newEntity [hasType ES.Selector.NLowest,
65                                                  hasProperty ES.PropertySelector.HasSelectionPropertyName propertyName,
66                                                  hasProperty ES.PropertySelector.HasResultCount count]
67         NHighest propertyName count -> newEntity [hasType ES.Selector.NHighest,
68                                                   hasProperty ES.PropertySelector.HasSelectionPropertyName propertyName,
69                                                   hasProperty ES.PropertySelector.HasResultCount count]
70     claim selection ES.Selection.HasSelector selector
71     for maybeCondition \condition -> claim selection ES.Selection.HasCondition $ writeCondition condition
72     in selection
73   where
74     writeCondition condition = match condition with
75             PropertyCondition propertyName lowerLimit upperLimit -> let
76                 cond = newEntity [hasType ES.PropertyCondition, hasProperty ES.PropertyCondition.HasPropertyName propertyName]
77                 for lowerLimit \limit -> claimRelatedValue cond ES.PropertyCondition.HasLowerLimit limit
78                 for upperLimit \limit -> claimRelatedValue cond ES.PropertyCondition.HasUpperLimit limit
79                 in cond
80             Region region -> newEntity [hasType ES.RegionCondition, hasProperty ES.RegionCondition.HasRegion region]
81             Route route -> newEntity [hasType ES.RouteCondition, hasProperty ES.RouteCondition.HasRoute route]
82             Conjunction conds -> writeAggregateCondition ES.Conjunction conds
83             Disjunction conds -> writeAggregateCondition ES.Disjunction conds
84             Negation conds -> writeAggregateCondition ES.Negation conds
85             Not cond -> writeAggregateCondition ES.Negation [cond]
86     writeAggregateCondition condType conds = let
87         cond = newEntity [hasType condType]
88         for conds \c -> claim cond ES.HasSubcondition $ writeCondition c
89         in cond