]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.selection/scl/Simantics/District/Selection.scl
Implementation of non-UI parts of network element queries
[simantics/district.git] / org.simantics.district.selection / scl / Simantics / District / Selection.scl
diff --git a/org.simantics.district.selection/scl/Simantics/District/Selection.scl b/org.simantics.district.selection/scl/Simantics/District/Selection.scl
new file mode 100644 (file)
index 0000000..4f1d270
--- /dev/null
@@ -0,0 +1,89 @@
+import "http://www.simantics.org/ElementSelection-1.0" as ES
+import "Simantics/DB"
+
+importJava "org.simantics.district.selection.ElementSelector" where
+    data ElementSelector
+
+    """
+    Get a selection resource represented as an ElementSelector object.
+    """
+    getSelector :: Resource -> <ReadGraph> ElementSelector
+    
+    """
+    Get the name of the selection from an element selector object.
+    """
+    @JavaName getName
+    getSelectorName :: ElementSelector -> String
+    
+    """
+    Get an expression that represents the selection from an element selector object.
+    """
+    @JavaName getExpression
+    getSelectorExpression :: ElementSelector -> String
+    
+    """
+    Get the selection resource form an element selector object.
+    """
+    @JavaName getResource
+    getSelectorResource :: ElementSelector -> Resource
+
+    """
+    Get the elements selected by an element selector object.
+
+    `selectedElements = selectElementsFrom elementSelector model`
+    """
+    @JavaName selectElementsFrom
+    selectElementsFrom :: ElementSelector -> Resource -> <ReadGraph> [Resource]
+
+data Generator = Model | Diagram Resource | Explicit [Resource]
+data Selector = All | NLowest String Integer | NHighest String Integer
+data Condition = PropertyCondition String (Maybe Double) (Maybe Double)
+               | Region Resource | Route Resource
+               | Conjunction [Condition] | Disjunction [Condition] | Negation [Condition]
+               | Not Condition
+data Selection = Selection Generator Selector (Maybe Condition)
+
+"""
+Write a selection expression into the database.
+
+`selectionResource = writeSelection parentResource selectionName selection`
+"""
+writeSelection :: Resource -> String -> Selection -> <WriteGraph> Resource
+writeSelection root name (Selection generator selector maybeCondition) = let
+    selection = newEntity [hasName name, hasLabel name, hasType ES.Selection, hasParent root]
+    generator = match generator with
+        Model -> newEntity [hasType ES.Generator.Model]
+        Diagram diagram -> newEntity [hasType ES.Generator.Diagram, hasStatement ES.Generator.HasDiagram diagram]
+        Explicit elements -> let
+            g = newEntity [hasType ES.Generator.Explicit]
+            for elements (claim g ES.Generator.HasSelectedElement)
+            in g
+    claim selection ES.Selection.HasGenerator generator
+    selector = match selector with
+        All -> newEntity [hasType ES.Selector.All]
+        NLowest propertyName count -> newEntity [hasType ES.Selector.NLowest,
+                                                 hasProperty ES.PropertySelector.HasSelectionPropertyName propertyName,
+                                                 hasProperty ES.PropertySelector.HasResultCount count]
+        NHighest propertyName count -> newEntity [hasType ES.Selector.NHighest,
+                                                  hasProperty ES.PropertySelector.HasSelectionPropertyName propertyName,
+                                                  hasProperty ES.PropertySelector.HasResultCount count]
+    claim selection ES.Selection.HasSelector selector
+    for maybeCondition \condition -> claim selection ES.Selection.HasCondition $ writeCondition condition
+    in selection
+  where
+    writeCondition condition = match condition with
+            PropertyCondition propertyName lowerLimit upperLimit -> let
+                cond = newEntity [hasType ES.PropertyCondition, hasProperty ES.PropertyCondition.HasPropertyName propertyName]
+                for lowerLimit \limit -> claimRelatedValue cond ES.PropertyCondition.HasLowerLimit limit
+                for upperLimit \limit -> claimRelatedValue cond ES.PropertyCondition.HasUpperLimit limit
+                in cond
+            Region region -> newEntity [hasType ES.RegionCondition, hasProperty ES.RegionCondition.HasRegion region]
+            Route route -> newEntity [hasType ES.RouteCondition, hasProperty ES.RouteCondition.HasRoute route]
+            Conjunction conds -> writeAggregateCondition ES.Conjunction conds
+            Disjunction conds -> writeAggregateCondition ES.Disjunction conds
+            Negation conds -> writeAggregateCondition ES.Negation conds
+            Not cond -> writeAggregateCondition ES.Negation [cond]
+    writeAggregateCondition condType conds = let
+        cond = newEntity [hasType condType]
+        for conds \c -> claim cond ES.HasSubcondition $ writeCondition c
+        in cond