-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.go
51 lines (41 loc) · 971 Bytes
/
component.go
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
package builder
import "math/rand"
const PropertySkipChance float64 = 0.5
type Component struct {
Name string `json:"name"`
Required bool `json:"required"`
Properties []Property `json:"properties"`
}
// Reduce returns a slice of randomly selected Properties.
func (c *Component) Reduce() []Property {
var props []Property
for _, p := range c.Properties {
if !p.Required && rand.Float64() <= PropertySkipChance {
continue
}
props = append(props, p)
}
if len(props) <= 0 {
i := rand.Intn(len(c.Properties))
props = append(props, c.Properties[i])
}
return props
}
func (c *Component) Atomize() []Atom {
var atoms []Atom
props := c.Reduce()
for _, p := range props {
attr := p.Reduce()
for _, a := range attr {
v := a.Reduce()
atom := Atom{
PropertyLabel: p.Name,
WeightFactor: v.WeightFactor,
ValueFactor: v.ValueFactor,
String: v.Name,
}
atoms = append(atoms, atom)
}
}
return atoms
}