Skip to content

Commit

Permalink
Update deserialization docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Sep 6, 2024
1 parent 70d84db commit f659576
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions docs/scheduling-and-constraints/procedural/constraints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ just return it. They can be created with some provided static constructor functi
@ConstraintProcedure
class BatteryAboveZero: Constraint {
override fun run(plan: Plan, simResults: SimulationResults) = Violations.inside(
plan.resource("/battery_soc", Real.deserialize()).lessThan(0).highlightTrue()
plan.resource("/battery_soc", Real.deserializer()).lessThan(0).highlightTrue()
)
}
```
Expand All @@ -37,7 +37,7 @@ public class BatterAboveZero implements Constraint {
@Override
public Violations run(@NotNull Plan plan, @NotNull SimulationResults simResults) {
return Violations.inside(
plan.resource("/battery_soc", Real.deserialize()).lessThan(0).highlightTrue()
plan.resource("/battery_soc", Real.deserializer()).lessThan(0).highlightTrue()
);
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ For example, to violate whenever `MyActivity` occurs when `/my/resource < 0`, yo
@ConstraintProcedure
class MyConstraint: GeneratorConstraint() {
override fun generate(plan: Plan, simResults: SimulationResults) {
val myResource = simResults.resource("/my/resource", Real.deserialize()).cache()
val myResource = simResults.resource("/my/resource", Real.deserializer()).cache()
for (activity in plan.directives("MyActivity")) {
if (myResource.sample(activity.startTime) < 0)
violate(Violation(activity.interval))
Expand All @@ -86,7 +86,7 @@ class MyConstraint: GeneratorConstraint() {
public class MyConstraint extends GeneratorConstraint {
@Override
public void generate(@NotNull Plan plan, @NotNull SimulationResults simResults) {
final var myResource = simResults.resource("/my/resource", Real.deserialize()).cache();
final var myResource = simResults.resource("/my/resource", Real.deserializer()).cache();
for (final var activity: plan.directives("MyActivity")) {
if (myResource.sample(activity.startTime) < 0)
violate(Violation(activity.interval));
Expand All @@ -107,7 +107,7 @@ normally.
@ConstraintProcedure
class BatteryAboveZero: GeneratorConstraint() {
override fun generate(plan: Plan, simResults: SimulationResults) {
simResults.resource("/battery_soc", Real.deserialize())
simResults.resource("/battery_soc", Real.deserializer())
.greaterThan(0)

// Only works in a generator constraint!
Expand Down Expand Up @@ -135,7 +135,7 @@ class MyConstraint: GeneratorConstraint() {
override fun message() = "MyActivity cannot start when /my/resource < 0"

override fun generate(plan: Plan, simResults: SimulationResults) {
val myResource = simResults.resource("/my/resource", Real.deserialize()).cache()
val myResource = simResults.resource("/my/resource", Real.deserializer()).cache()
for (activity in plan.directives("MyActivity")) {
if (myResource.sample(activity.startTime) < 0)
violate(Violation(activity.interval))
Expand All @@ -157,7 +157,7 @@ public class MyConstraint extends GeneratorConstraint {

@Override
public void generate(@NotNull Plan plan, @NotNull SimulationResults simResults) {
final var myResource = simResults.resource("/my/resource", Real.deserialize()).cache();
final var myResource = simResults.resource("/my/resource", Real.deserializer()).cache();
for (final var activity: plan.directives("MyActivity")) {
if (myResource.sample(activity.startTime) < 0)
violate(Violation(activity.interval));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ You can query instances the same way as directives, using `simResults.instances(

You can query resources with `simResults.resource("/my/resource", <deserializer>)`. Unlike activities, there is no option
to use a default deserializer; you must pick one, because it determines the profile type. Each profile type provides a
deserializer for you to use, so for example, you can get a string resource with `simResults.resource("/my/string", Strings.deserialize())`.
deserializer for you to use, so for example, you can get a string resource with `simResults.resource("/my/string", Strings.deserializer())`.

If you made your own data structure for your resource (say, `V`), you'll probably want to use the `Constants` profile.
But unfortunately you have to do any deserialization yourself. If you don't want to, you can just use `.resource("/my/object", Constants::new)`,
but this will return `Constants<SerializedValue>`. To do proper deserialization, call `Constants.deserialize`:
but this will return `Constants<SerializedValue>`. To do proper deserialization, call `Constants.deserializer`:

<Tabs groupId="lang">
<TabItem value="kt" label="Kotlin">
Expand All @@ -67,7 +67,7 @@ but this will return `Constants<SerializedValue>`. To do proper deserialization,
data class Point2(x: Double, y: Double) {}

// note the use of . instead of :: here! v
val myResource = plan.resource("/my/object", Constants.deserialize {
val myResource = plan.resource("/my/object", Constants.deserializer {
val fields = it.asMap().get()
Point2(fields.get("x").asReal().get(), fields.get("y").asReal().get())
})
Expand All @@ -81,7 +81,7 @@ val myResource = plan.resource("/my/object", Constants.deserialize {
record Point2(double x, double y) {}

// note the use of . instead of :: here! v
final var myResource = plan.resource("/my/object", Constants.deserialize($ -> {
final var myResource = plan.resource("/my/object", Constants.deserializer($ -> {
final var fields = it.asMap().get();
return new Point2(fields.get("x").asReal().get(), fields.get("y").asReal().get());
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Highlighting is often used to iterate through the regions when a certain conditi
<TabItem value="kt" label="Kotlin">

```kotlin
val myResource = plan.resource("my_resource", Real.deserialize())
val myResource = plan.resource("my_resource", Real.deserializer())
for (interval in myResource.highlightEqualTo(3)) {
// Do something with the interval
// `interval` is JUST an interval, the original segment has been lost.
Expand All @@ -77,7 +77,7 @@ for (interval in myResource.highlightEqualTo(3)) {
<TabItem value="java" label="Java">

```java
final var myResource = plan.resource("my_resource", Real.deserialize());
final var myResource = plan.resource("my_resource", Real.deserializer());
for (final var interval: myResource.highlightEqualTo(3)) {
// Do something with the interval
// `interval` is JUST an interval, the original segment has been lost.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ but sometimes a collect call is hidden inside another operation. Let's take the
<TabItem value="kt" label="Kotlin">

```kotlin
val n1 = plan.resource("int_resource_1", Numbers.deserialize())
val n2 = plan.resource("int_resource_2", Numbers.deserialize())
val n1 = plan.resource("int_resource_1", Numbers.deserializer())
val n2 = plan.resource("int_resource_2", Numbers.deserializer())

// Construct a very expensive profile that we should only evaluate once.
val superExpensiveProfile = n1.map2Values(n2) { l, r, _ -> ackermann(l, r) }
Expand All @@ -29,8 +29,8 @@ val superExpensiveProfile = n1.map2Values(n2) { l, r, _ -> ackermann(l, r) }
<TabItem value="java" label="Java">

```java
final var n1 = plan.resource("int_resource_1", Numbers.deserialize());
final var n2 = plan.resource("int_resource_2", Numbers.deserialize());
final var n1 = plan.resource("int_resource_1", Numbers.deserializer());
final var n2 = plan.resource("int_resource_2", Numbers.deserializer());

// Construct a very expensive profile that we should only evaluate once.
final var expensiveProfile = n1.map2Values(n2, (l, r, _) -> ackermann(l, r));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use it in a for-each loop (which calls `.collect` under the hood). For example,
<TabItem value="kt" label="Kotlin">

```kotlin
val myResource = plan.resource("my_resource", Real.deserialize())
val myResource = plan.resource("my_resource", Real.deserializer())
for (interval in myResource.highlightEqualTo(3)) {
// do something with the interval
}
Expand All @@ -27,7 +27,7 @@ for (interval in myResource.highlightEqualTo(3)) {
<TabItem value="java" label="Java">

```java
final var myResource = plan.resource("my_resource", Real.deserialize());
final var myResource = plan.resource("my_resource", Real.deserializer());
for (final var interval: myResource.highlightEqualTo(3)) {
// do something with the interval
}
Expand Down

0 comments on commit f659576

Please sign in to comment.