-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitExample.cs
78 lines (68 loc) · 2.66 KB
/
UnitExample.cs
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
using System;
using System.Collections.Generic;
using Grpc.Core;
using ProtoLCA;
using static DemoApp.Util;
namespace DemoApp {
/// <summary>
/// This example writes the unit groups, their default flow properties, and
/// units to the console output.
/// </summary>
class UnitExample : Example {
private readonly Channel channel;
public UnitExample(Channel channel) {
this.channel = channel;
}
public string Description() {
return "List the available units of measurement";
}
public void Run() {
var triples = Examples.GetUnitTriples(channel);
foreach (var group in UnitGroupsOf(triples)) {
var property = DefaultPropertyOf(group, triples);
if (property == null) {
Log($" .. ERROR: unit group {group.Name} " +
"has no default flow property");
continue;
}
Log($"\n .. + unit group '{group.Name}' with default flow" +
$" property '{property.Name}': ");
foreach (var unit in UnitsOf(group, triples)) {
Log($" .. - {unit.Name}; {unit.ConversionFactor}");
}
}
}
private List<ProtoUnitGroup> UnitGroupsOf(
List<(ProtoUnit, ProtoUnitGroup, ProtoFlowProperty)> triples) {
var groups = new List<ProtoUnitGroup>();
var handled = new HashSet<string>();
foreach (var (_, group, _) in triples) {
if (handled.Contains(group.Id))
continue;
groups.Add(group);
handled.Add(group.Id);
}
groups.Sort((g1, g2) => String.Compare(g1.Name, g2.Name));
return groups;
}
private ProtoFlowProperty DefaultPropertyOf(ProtoUnitGroup group,
List<(ProtoUnit, ProtoUnitGroup, ProtoFlowProperty)> triples) {
foreach (var (_, otherGroup, property) in triples) {
if (String.Equals(group.Id, otherGroup.Id))
return property;
}
return null;
}
private List<ProtoUnit> UnitsOf(ProtoUnitGroup group,
List<(ProtoUnit, ProtoUnitGroup, ProtoFlowProperty)> triples) {
var units = new List<ProtoUnit>();
foreach (var (unit, otherGroup, _) in triples) {
if (String.Equals(group.Id, otherGroup.Id)) {
units.Add(unit);
}
}
units.Sort((u1, u2) => String.Compare(u1.Name, u2.Name));
return units;
}
}
}