squiggle/src/components/DistBuilder.re

469 lines
15 KiB
ReasonML
Raw Normal View History

2020-02-26 10:38:29 +00:00
open BsReform;
2020-02-27 06:21:47 +00:00
open Antd.Grid;
2020-02-26 10:38:29 +00:00
module FormConfig = [%lenses
type state = {
2020-02-26 12:21:30 +00:00
guesstimatorString: string,
//
domainType: string, // Complete, LeftLimited(...), RightLimited(...), LeftAndRightLimited(..., ...)
2020-03-04 07:50:11 +00:00
xPoint: string,
xPoint2: string,
excludingProbabilityMass: string,
excludingProbabilityMass2: string,
2020-02-26 12:21:30 +00:00
//
unitType: string, // UnspecifiedDistribution, TimeDistribution(zero, unit)
zero: MomentRe.Moment.t,
unit: string,
2020-02-27 07:00:03 +00:00
//
2020-03-04 07:50:11 +00:00
sampleCount: string,
outputXYPoints: string,
truncateTo: string,
2020-02-26 10:38:29 +00:00
}
];
2020-03-04 07:50:11 +00:00
type options = {
sampleCount: int,
outputXYPoints: int,
truncateTo: option(int),
};
2020-02-26 10:38:29 +00:00
module Form = ReForm.Make(FormConfig);
let schema = Form.Validation.Schema([||]);
module FieldString = {
[@react.component]
let make = (~field, ~label) => {
<Form.Field
field
render={({handleChange, error, value, validate}) =>
<Antd.Form.Item label={label |> E.ste}>
<Antd.Input
value
onChange={BsReform.Helpers.handleChange(handleChange)}
onBlur={_ => validate()}
/>
</Antd.Form.Item>
}
/>;
};
};
2020-02-26 12:21:30 +00:00
module FieldFloat = {
[@react.component]
2020-03-04 07:50:11 +00:00
let make = (~field, ~label, ~className=Css.style([])) => {
2020-02-26 12:21:30 +00:00
<Form.Field
field
render={({handleChange, error, value, validate}) =>
<Antd.Form.Item label={label |> E.ste}>
2020-03-04 07:50:11 +00:00
<Antd.Input
2020-02-26 12:21:30 +00:00
value
2020-03-04 07:50:11 +00:00
onChange={BsReform.Helpers.handleChange(handleChange)}
2020-02-26 12:21:30 +00:00
onBlur={_ => validate()}
2020-02-27 07:43:37 +00:00
className
2020-02-26 12:21:30 +00:00
/>
</Antd.Form.Item>
}
/>;
};
};
2020-02-27 07:00:03 +00:00
module Styles = {
open Css;
let rows =
style([
selector(
">.ant-col:first-child",
[paddingLeft(em(0.25)), paddingRight(em(0.125))],
),
selector(
">.ant-col:last-child",
[paddingLeft(em(0.125)), paddingRight(em(0.25))],
),
selector(
">.ant-col:not(:first-child):not(:last-child)",
[paddingLeft(em(0.125)), paddingRight(em(0.125))],
),
]);
let parent =
2020-02-27 07:17:10 +00:00
style([
selector(".ant-input-number", [width(`percent(100.))]),
selector(".anticon", [verticalAlign(`zero)]),
]);
2020-02-27 07:00:03 +00:00
let form = style([backgroundColor(hex("eee")), padding(em(1.))]);
let dist = style([padding(em(1.))]);
let spacer = style([marginTop(em(1.))]);
2020-02-27 07:43:37 +00:00
let groupA =
style([
selector(
".ant-input-number-input",
[backgroundColor(hex("fff7db"))],
),
]);
let groupB =
style([
selector(
".ant-input-number-input",
[backgroundColor(hex("eaf4ff"))],
),
]);
2020-02-27 07:00:03 +00:00
};
2020-02-27 06:21:47 +00:00
module DemoDist = {
[@react.component]
2020-03-04 07:50:11 +00:00
let make = (~guesstimatorString, ~domain, ~unit, ~options) => {
2020-02-27 06:21:47 +00:00
<Antd.Card title={"Distribution" |> E.ste}>
<div className=Styles.spacer />
<div>
2020-03-04 07:50:11 +00:00
{switch (domain, unit, options) {
| (Some(domain), Some(unit), Some(options)) =>
2020-03-04 09:55:01 +00:00
let distPlus =
DistPlusIngredients.make(~guesstimatorString, ~domain, ~unit, ())
|> DistPlusIngredients.toDistPlus(
~sampleCount=options.sampleCount,
~outputXYPoints=options.outputXYPoints,
~truncateTo=options.truncateTo,
);
switch (distPlus) {
| Some(distPlus) => <DistPlusPlot distPlus />
| _ =>
"Correct Guesstimator string input to show a distribution."
|> E.ste
};
2020-03-04 07:50:11 +00:00
| _ =>
"Nothing to show. Try to change the distribution description."
|> E.ste
}}
2020-02-27 06:21:47 +00:00
</div>
</Antd.Card>;
};
};
2020-02-26 10:38:29 +00:00
[@react.component]
let make = () => {
2020-02-27 07:17:10 +00:00
let (reloader, setRealoader) = React.useState(() => 1);
2020-02-26 10:38:29 +00:00
let reform =
Form.use(
~validationStrategy=OnDemand,
~schema,
~onSubmit=({state}) => {None},
2020-02-26 12:21:30 +00:00
~initialState={
guesstimatorString: "mm(5 to 20, floor(normal(20,2)), [.5, .5])",
2020-03-02 10:13:52 +00:00
domainType: "Complete",
2020-03-04 07:50:11 +00:00
xPoint: "50.0",
xPoint2: "60.0",
excludingProbabilityMass2: "0.5",
excludingProbabilityMass: "0.3",
2020-02-26 12:21:30 +00:00
unitType: "UnspecifiedDistribution",
zero: MomentRe.momentNow(),
unit: "days",
2020-03-04 07:50:11 +00:00
sampleCount: "1000",
outputXYPoints: "2000",
truncateTo: "500",
2020-02-26 12:21:30 +00:00
},
2020-02-26 10:38:29 +00:00
(),
);
let onSubmit = e => {
e->ReactEvent.Synthetic.preventDefault;
reform.submit();
};
2020-03-04 07:50:11 +00:00
let xPoint = reform.state.values.xPoint |> Js.Float.fromString;
let xPoint2 = reform.state.values.xPoint2 |> Js.Float.fromString;
let excludingProbabilityMass =
reform.state.values.excludingProbabilityMass |> Js.Float.fromString;
let excludingProbabilityMass2 =
reform.state.values.excludingProbabilityMass2 |> Js.Float.fromString;
let zero = reform.state.values.zero;
let unit = reform.state.values.unit;
let domainType = reform.state.values.domainType;
let unitType = reform.state.values.unitType;
let guesstimatorString = reform.state.values.guesstimatorString;
let sampleCount = reform.state.values.sampleCount |> Js.Float.fromString;
let outputXYPoints =
reform.state.values.outputXYPoints |> Js.Float.fromString;
let truncateTo = reform.state.values.truncateTo |> Js.Float.fromString;
2020-02-26 12:55:19 +00:00
let domain =
2020-03-04 07:50:11 +00:00
switch (domainType) {
| "Complete" => Some(DistTypes.Complete)
| "LeftLimited"
when
!Js.Float.isNaN(xPoint)
&& !Js.Float.isNaN(excludingProbabilityMass) =>
Some(LeftLimited({xPoint, excludingProbabilityMass}))
| "RightLimited"
when
!Js.Float.isNaN(xPoint2)
&& !Js.Float.isNaN(excludingProbabilityMass2) =>
Some(RightLimited({xPoint, excludingProbabilityMass}))
| "LeftAndRightLimited"
when
!Js.Float.isNaN(xPoint)
&& !Js.Float.isNaN(excludingProbabilityMass)
&& !Js.Float.isNaN(xPoint2)
&& !Js.Float.isNaN(excludingProbabilityMass2) =>
Some(
LeftAndRightLimited(
{xPoint, excludingProbabilityMass},
{xPoint, excludingProbabilityMass},
),
2020-02-26 12:55:19 +00:00
)
2020-03-04 07:50:11 +00:00
| _ => None
2020-02-26 12:55:19 +00:00
};
let unit =
2020-03-04 07:50:11 +00:00
switch (unitType) {
| "UnspecifiedDistribution" => Some(DistTypes.UnspecifiedDistribution)
2020-02-26 12:55:19 +00:00
| "TimeDistribution" =>
2020-03-04 07:50:11 +00:00
Some(
TimeDistribution({zero, unit: unit |> TimeTypes.TimeUnit.ofString}),
)
| _ => None
2020-02-26 12:55:19 +00:00
};
2020-03-04 07:50:11 +00:00
let options =
switch (sampleCount, outputXYPoints, truncateTo) {
| (_, _, _)
when
!Js.Float.isNaN(sampleCount)
&& !Js.Float.isNaN(outputXYPoints)
&& !Js.Float.isNaN(truncateTo)
&& sampleCount > 10.
&& outputXYPoints > 10.
&& truncateTo > 10. =>
Some({
sampleCount: sampleCount |> int_of_float,
outputXYPoints: outputXYPoints |> int_of_float,
truncateTo: truncateTo |> int_of_float |> E.O.some,
})
| _ => None
};
2020-02-26 12:55:19 +00:00
2020-02-27 06:21:47 +00:00
let demoDist =
React.useMemo1(
2020-03-04 07:50:11 +00:00
() => <DemoDist guesstimatorString domain unit options />,
2020-02-27 06:21:47 +00:00
[|
reform.state.values.guesstimatorString,
reform.state.values.domainType,
2020-03-04 07:50:11 +00:00
reform.state.values.xPoint,
reform.state.values.xPoint2,
reform.state.values.xPoint2,
reform.state.values.excludingProbabilityMass,
reform.state.values.excludingProbabilityMass2,
2020-02-27 06:21:47 +00:00
reform.state.values.unitType,
reform.state.values.zero |> E.M.format(E.M.format_standard),
reform.state.values.unit,
2020-03-04 07:50:11 +00:00
reform.state.values.sampleCount,
reform.state.values.outputXYPoints,
reform.state.values.truncateTo,
2020-02-27 07:17:10 +00:00
reloader |> string_of_int,
2020-02-27 06:21:47 +00:00
|],
);
2020-02-27 07:17:10 +00:00
let onRealod = _ => {
setRealoader(_ => reloader + 1);
};
2020-02-26 12:55:19 +00:00
2020-02-27 07:00:03 +00:00
<div className=Styles.parent>
2020-02-26 12:55:19 +00:00
<div className=Styles.spacer />
2020-02-27 06:21:47 +00:00
demoDist
2020-02-26 12:55:19 +00:00
<div className=Styles.spacer />
2020-02-27 07:17:10 +00:00
<Antd.Card
title={"Distribution Form" |> E.ste}
extra={
<Antd.Button
icon=Antd.IconName.reload
shape=`circle
onClick=onRealod
/>
}>
2020-02-26 12:55:19 +00:00
<Form.Provider value=reform>
<Antd.Form onSubmit>
2020-02-27 07:00:03 +00:00
<Row _type=`flex className=Styles.rows>
2020-02-27 08:04:54 +00:00
<Col span=12>
2020-02-27 07:00:03 +00:00
<FieldString
field=FormConfig.GuesstimatorString
label="Guesstimator String"
/>
</Col>
</Row>
<Row _type=`flex className=Styles.rows>
2020-02-27 06:21:47 +00:00
<Col span=4>
2020-02-26 12:55:19 +00:00
<Form.Field
field=FormConfig.DomainType
render={({handleChange, value}) =>
<Antd.Form.Item label={"Domain Type" |> E.ste}>
<Antd.Select value onChange={e => e |> handleChange}>
<Antd.Select.Option value="Complete">
{"Complete" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="LeftLimited">
2020-02-26 12:58:56 +00:00
{"Left Limited" |> E.ste}
2020-02-26 12:55:19 +00:00
</Antd.Select.Option>
<Antd.Select.Option value="RightLimited">
2020-02-26 12:58:56 +00:00
{"Right Limited" |> E.ste}
2020-02-26 12:55:19 +00:00
</Antd.Select.Option>
<Antd.Select.Option value="LeftAndRightLimited">
2020-02-26 12:58:56 +00:00
{"Left And Right Limited" |> E.ste}
2020-02-26 12:55:19 +00:00
</Antd.Select.Option>
</Antd.Select>
</Antd.Form.Item>
}
/>
2020-02-27 06:21:47 +00:00
</Col>
2020-02-27 07:43:37 +00:00
{<>
<Col span=4>
<FieldFloat
field=FormConfig.XPoint
2020-02-27 08:50:46 +00:00
label="Left X-point"
2020-02-27 07:43:37 +00:00
className=Styles.groupA
/>
</Col>
<Col span=4>
<FieldFloat
field=FormConfig.ExcludingProbabilityMass
2020-02-27 08:50:46 +00:00
label="Left Excluding Probability Mass"
2020-02-27 07:43:37 +00:00
className=Styles.groupA
/>
</Col>
</>
|> E.showIf(
E.L.contains(
reform.state.values.domainType,
2020-02-27 08:50:46 +00:00
["LeftLimited", "LeftAndRightLimited"],
2020-02-27 07:43:37 +00:00
),
)}
{<>
<Col span=4>
<FieldFloat
field=FormConfig.XPoint2
2020-02-27 08:50:46 +00:00
label="Right X-point"
2020-02-27 07:43:37 +00:00
className=Styles.groupB
/>
</Col>
<Col span=4>
<FieldFloat
field=FormConfig.ExcludingProbabilityMass2
2020-02-27 08:50:46 +00:00
label="Right Excluding Probability Mass"
2020-02-27 07:43:37 +00:00
className=Styles.groupB
/>
</Col>
</>
|> E.showIf(
E.L.contains(
reform.state.values.domainType,
2020-02-27 08:50:46 +00:00
["RightLimited", "LeftAndRightLimited"],
2020-02-27 07:43:37 +00:00
),
)}
2020-02-27 06:21:47 +00:00
</Row>
2020-02-27 07:00:03 +00:00
<Row _type=`flex className=Styles.rows>
2020-02-27 06:21:47 +00:00
<Col span=4>
2020-02-26 12:55:19 +00:00
<Form.Field
field=FormConfig.UnitType
render={({handleChange, value}) =>
2020-02-27 07:43:37 +00:00
<Antd.Form.Item label={"Unit Type" |> E.ste}>
2020-02-26 12:55:19 +00:00
<Antd.Select value onChange={e => e |> handleChange}>
<Antd.Select.Option value="UnspecifiedDistribution">
2020-02-26 12:58:56 +00:00
{"Unspecified Distribution" |> E.ste}
2020-02-26 12:55:19 +00:00
</Antd.Select.Option>
<Antd.Select.Option value="TimeDistribution">
2020-02-26 12:58:56 +00:00
{"Time Distribution" |> E.ste}
2020-02-26 12:55:19 +00:00
</Antd.Select.Option>
</Antd.Select>
</Antd.Form.Item>
}
/>
2020-02-27 06:21:47 +00:00
</Col>
2020-02-28 10:29:17 +00:00
{<>
<Col span=4>
<Form.Field
field=FormConfig.Zero
render={({handleChange, value}) =>
<Antd.Form.Item label={"Zero Point" |> E.ste}>
<Antd_DatePicker
value
onChange={e => {
e |> handleChange;
_ => ();
}}
/>
</Antd.Form.Item>
}
/>
</Col>
<Col span=4>
<Form.Field
field=FormConfig.Unit
render={({handleChange, value}) =>
<Antd.Form.Item label={"Unit" |> E.ste}>
<Antd.Select value onChange={e => e |> handleChange}>
<Antd.Select.Option value="days">
{"Days" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="hours">
{"Hours" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="milliseconds">
{"Milliseconds" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="minutes">
{"Minutes" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="months">
{"Months" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="quarters">
{"Quarters" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="seconds">
{"Seconds" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="weeks">
{"Weeks" |> E.ste}
</Antd.Select.Option>
<Antd.Select.Option value="years">
{"Years" |> E.ste}
</Antd.Select.Option>
</Antd.Select>
</Antd.Form.Item>
}
/>
</Col>
</>
|> E.showIf(
E.L.contains(
reform.state.values.unitType,
["TimeDistribution"],
),
)}
2020-02-27 07:00:03 +00:00
</Row>
<Row _type=`flex className=Styles.rows>
<Col span=4>
2020-03-04 07:50:11 +00:00
<FieldFloat field=FormConfig.SampleCount label="Sample Count" />
2020-02-27 07:00:03 +00:00
</Col>
<Col span=4>
2020-03-04 07:50:11 +00:00
<FieldFloat
2020-02-27 07:00:03 +00:00
field=FormConfig.OutputXYPoints
label="Output XY-points"
/>
</Col>
<Col span=4>
2020-03-04 07:50:11 +00:00
<FieldFloat field=FormConfig.TruncateTo label="Truncate To" />
2020-02-27 06:21:47 +00:00
</Col>
</Row>
2020-02-27 07:17:10 +00:00
<Antd.Button
_type=`primary icon=Antd.IconName.reload onClick=onRealod>
{"Update Distribution" |> E.ste}
</Antd.Button>
2020-02-26 12:55:19 +00:00
</Antd.Form>
</Form.Provider>
2020-02-27 06:21:47 +00:00
</Antd.Card>
2020-02-27 07:00:03 +00:00
<div className=Styles.spacer />
2020-02-26 12:55:19 +00:00
</div>;
2020-02-28 10:29:17 +00:00
};