-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartBeautify.fs
83 lines (67 loc) · 2.49 KB
/
SmartBeautify.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module SmartBeautify
open CommonTypes
open DrawModelType
open DrawModelType.SymbolT
open DrawModelType.BusWireT
open Symbol
open Optics
open Optic
open Operators
open SmartHelpers
open SmartPortOrder
open SmartSizeSymbol
module Constants =
let maxPairsToReorder = 10
/// Optimizes Symbols.
let optimizeSyms
(helpers: ExternalSmartHelpers)
(bBoxes: Map<CommonTypes.ComponentId, BoundingBox>)
(model: BusWireT.Model)
=
let updateBBoxes (wModel: BusWireT.Model) (bBoxes: Map<ComponentId, BoundingBox>) =
bBoxes |> Map.map (fun sId _ -> getBoundingBox wModel.Symbol sId)
let symsToResize =
model.Symbol.Symbols
|> Map.filter (fun id sym ->
match sym.Component.Type with
| Custom _ as customComp -> true
| _ -> false)
|> Map.toList
|> List.map snd
|> Set.ofList
let folder (currModel, currBB, currSet) _ =
let sym, newModel =
Set.toArray currSet
|> Array.map (fun sym -> sym, optimiseSymbol currModel sym currBB helpers)
|> Array.minBy (fun (_, m) -> totalLengthOfWires m.Wires)
newModel, updateBBoxes newModel currBB, Set.remove sym currSet
let model', _, _ = ((model, bBoxes, symsToResize), symsToResize) ||> Set.fold folder
model'
/// Reorders Symbol.
let reorderPairs (smartHelpers: ExternalSmartHelpers) (model: BusWireT.Model) =
// Gets Symbol Pairs to Beautify.
let getSymsToReorder =
let keepSymPair (symPair: Symbol * Symbol) =
match symbolMatch (fst symPair), symbolMatch (snd symPair) with
| Custom _, Custom _
| Custom _, And
| And, Custom _ -> true
| _ -> false
getConnSyms model |> List.filter keepSymPair
// Truncate pairs to reorder based on number of swaps.
let pairs =
getSymsToReorder
|> List.sortByDescending (fun (symA, symB) -> (symReorderPair model symA symB) |> optSwaps |> fst)
|> List.truncate Constants.maxPairsToReorder
(model, pairs)
||> List.fold (fun model' (symA, symB) ->
let syms' = model'.Symbol.Symbols
let symA', symB' = syms'[symA.Id], syms'[symB.Id]
reOrderPorts model' symA' symB' smartHelpers)
/// Header Function.
let smartBeautify
(wModel: BusWireT.Model)
(boundingBoxes: Map<CommonTypes.ComponentId, BoundingBox>)
(smartHelpers: ExternalSmartHelpers)
: BusWireT.Model =
wModel |> reorderPairs smartHelpers |> optimizeSyms smartHelpers boundingBoxes