Stacking rules
Tag your cargo with classes, then keep named sets of rules about which class may rest on which, and how many of a class one tower may hold.
When to use this
- Some of your goods must never carry certain others: nothing heavy on glass, no pallet on a carton.
- Some goods may stack, but only so high: glass at most two high.
- You keep such rules per kind of trip, for example one set for a trailer and one for a sea container.
- If each product may only stack on itself, you need no classes: set
sameSkuStackingon the lines (see Constraints).
Cargo classes
A cargo class is a label for a kind of cargo, such as glass or heavy. You choose its key when you save it; the key is fixed afterwards and is what SKUs and rules name. The name is what people see and can change. A job can also use a key you have not saved, for that job only; see The class of a line.
curl -X POST "https://api.loadoptimizer.ai/v2/cargo-classes" \
-H "X-Api-Key: ck_live_..." -H "Content-Type: application/json" \
-d '{ "key": "glass", "name": "Glass", "note": "Glass panels and mirrors" }'
A key is 1 to 64 characters: lowercase letters, digits, _ and -, starting with a letter or a digit. A name is unique in your account, ignoring case. A key or a name another class already has is refused with 409 cargo_class_key_taken or cargo_class_name_taken, and resource names that class: { "type": "cargo_class", "id": "glass" }, the id being the class's key.
PUT /v2/cargo-classes/{key} replaces the name and the note; a note you leave out is cleared. The read-only members (revision, createdAt, createdBy, updatedAt, updatedBy) in the body are ignored, and key may stay in it as long as it is the key in the path (another key is a 400), so a body you fetched can be edited and sent back.
Tag a SKU
A SKU in your SKU library takes its class in classes, a list of at most one key. Saving a SKU with a class needs the permission cargo_classes.read as well. To find the SKUs of a class, filter the library's entries with ?cargoClass=glass.
Delete a class
A class is deleted for good. The delete is refused with 409 cargo_class_in_use while SKUs still have the class or rule sets name it, and the answer carries both counts: skuCount (SKUs that have the class) and ruleSetCount (your rule sets with at least one rule that names it). Both are always there, 0 when none, so you can show what the delete would touch before anyone confirms it. A 412 for a stale If-Match is answered before a 409.
Give the SKUs another class first; they always block the delete. To go ahead while rule sets name the class, confirm it with ?removeFromRuleSets=true:
curl -X DELETE "https://api.loadoptimizer.ai/v2/cargo-classes/glass?removeFromRuleSets=true" \
-H "X-Api-Key: ck_live_..." -H "If-Match: \"3\""
Every rule that names the class then leaves your rule sets, and each changed set's revision adds 1. That cannot be undone: creating a class with the same key later does not bring the rules back. The parameter takes true or false (false, or leaving it out, is the plain delete); anything else is a 400 validation_failed with the pointer removeFromRuleSets. The delete needs cargo_classes.delete and stacking_rule_sets.update.
Rule sets
A rule set is a named list of stacking rules over your classes. A pair of classes it does not name is allowed, so a new class may rest on anything until a rule says otherwise. The diagram shows one rule set with three rules and, for each rule, towers it allows and refuses.
Two kinds of rule:
directOn: a box of classuppermay not rest directly on a box of classlower. A rule is one way:heavyonglasssays nothing aboutglassonheavy. It is about boxes in contact: with a box of another class between them, aheavybox may stand aboveglass.towerLimits: a tower is the boxes standing on one another in one stack, from the bottom box up. A limit applies to a tower as soon as a box of classupperrests directly on a box of classlower. That tower may then hold at mostmaxCountboxes of classupper, anywhere in it, below thelowerbox too. With one class on itself the bottom box counts as well, somaxCount: 1means no stacking of that class on itself.
The two classes of a tower limit may differ, as in rule 3 of the diagram: upper heavy, lower carton and maxCount 2, because a carton gives way under too much weight. Read each tower from the bottom up. A tower of carton, heavy, heavy is fine: a heavy box rests on the carton, and the tower holds two heavy boxes. A tower of carton, heavy, heavy, heavy is not allowed, and neither is heavy, carton, heavy, heavy: a heavy box rests on the carton, and the tower holds three heavy boxes, the one under the carton included. A tower of heavy, heavy, heavy, carton is fine, because no heavy box rests on the carton, so the limit does not apply. The count is over the whole tower, not only the boxes above the carton.
The rule set of the diagram:
{
"name": "Sea container",
"directOn": [ { "upper": "heavy", "lower": "glass" } ],
"towerLimits": [
{ "upper": "glass", "lower": "glass", "maxCount": 2 },
{ "upper": "heavy", "lower": "carton", "maxCount": 2 }
]
}
A pair appears at most once in a list and never in both lists; every class must be one your account has. A PUT replaces the name and both lists in full. The read-only members (id, revision, createdAt, createdBy, updatedAt, updatedBy) in the body are ignored, so a body you fetched can be edited and sent back. A name is unique in your account, ignoring case; one another rule set has is a 409 stacking_rule_set_name_taken, and resource names that rule set: { "type": "stacking_rule_set", "id": "..." }.
In a job
A job applies classes and rules in two places: each cargo line says what class it is, and constraints.stackingRules says what may rest on what.
The class of a line. An inline cargo line takes its class in classes, a list of at most one key; a line that references a SKU takes the SKU's class, and sending classes beside libraryId is a 400 ambiguous_source. The key can be one your account has saved, or any other key of the same form: the job then uses it for that job only, and it is not saved to your account or counted against your cargo class limit. A key that equals a saved key is that class, with the same rules and overrides. A job carries at most 200 distinct keys, its inline lines' and its SKUs' together; the line that brings the 201st is a 400 validation_failed at /cargo/{i}/classes/0, or at /cargo/{i}/libraryId when its SKU brings it. In the job's own rules and overrides, a key must be carried by a cargo line of the job or be saved; a key that is neither is a 400 unknown_cargo_class at the rule's pointer, so a misspelt rule is caught. SKUs and saved rule sets still take only saved classes. A line without a class stacks as before, governed by its own allowStackingOnTop and sameSkuStacking.
For example, two inline lines can carry a key you have never saved, and the job's rules can limit it:
{
"cargo": [
{ "classes": ["glass"], ... },
{ "classes": ["glass"], ... }
],
"constraints": {
"stackingRules": {
"towerLimits": [ { "upper": "glass", "lower": "glass", "maxCount": 2 } ]
}
}
}
The rules. constraints.stackingRules takes one of two forms, never both. A member name the contract does not define, such as directsOn, is not rejected: it is ignored, so a typo would run the job without those rules. POST /v2/jobs/validate answers it with an unknown_member warning at that member (see Validate first); POST /v2/jobs carries no warning. Inline, it holds the rules themselves, in the shape of a rule set:
{
"constraints": {
"stackingRules": {
"directOn": [ { "upper": "heavy", "lower": "glass" } ],
"towerLimits": [ { "upper": "glass", "lower": "glass", "maxCount": 3 } ]
}
}
}
By reference, it names a saved rule set by ruleSetId, optionally with overrides:
{
"constraints": {
"stackingRules": {
"ruleSetId": "0199e2b4-6c1d-7a3e-9b20-5f1d3c8e4a71",
"overrides": [ { "upper": "glass", "lower": "glass", "rule": "limited", "maxCount": 3 } ]
}
}
}
Plan. A job that names a class on a plan without cargo classes, or a ruleSetId on a plan without stacking rule sets, is a 403 libraries_not_entitled, answered before any lookup. Its detail says which one the plan lacks. The same goes for POST /v2/jobs/validate.
Sending ruleSetId beside directOn or towerLimits is a 400 ambiguous_source at /constraints/stackingRules. A ruleSetId that names no rule set your account has (unknown, deleted, another account's, or not in the standard 36-character form) is a 400 stacking_rule_set_not_found at /constraints/stackingRules/ruleSetId. Permissions. A ruleSetId needs stacking_rule_sets.read. A job that names a class itself needs cargo_classes.read: a non-empty classes on an inline cargo line, or a non-empty directOn, towerLimits or overrides. A SKU reference brings its class under sku_libraries.read alone. A missing permission is a 403 permission_denied.
Overrides. An override changes one pair for this job only and replaces whatever the rule set says about that pair. Its rule is allowed (the pair may stack), forbidden (upper may not rest directly on lower) or limited (allowed, with at most maxCount boxes of class upper per tower). maxCount is required with limited and not allowed otherwise. A pair appears at most once in overrides. The saved rule set itself does not change.
Which rules apply. Only rules whose two classes both occur in the job apply: an inline rule or a rule of the set about a class the job does not carry is ignored, while a key no cargo line of the job carries and your account has not saved is a 400. allowStackingOnTop: false on a line still wins over any rule: nothing rests on that line, whatever the rules allow. sameSkuStacking on a line (see Constraints) applies as well as the rules, and the strictest wins. Rules only take stacking away: they forbid a pair or cap a tower of boxes that allowStackingOnTop lets stack, and never switch it on. When no line of a job allows anything on top of it, the rules have nothing to act on and POST /v2/jobs/validate warns stacking_disabled at /cargo; see Validate-only warnings.
Reading it back. GET /v2/jobs/{id}/input returns the rules as applied, inline, under constraints.stackingRules, and names the saved rule set in stackingRuleSet (its ruleSetId, name and revision at submit, and the overrides you sent, in the order you sent them with rule in its canonical lowercase spelling, so you can show which pairs the order changed even after the set itself has changed or been deleted; [] when you sent none; null for inline rules or none). Rules you sent of which none applied read back as two empty lists (directOn and towerLimits), which differs from a job sent without constraints, where constraints is absent. A later change to the set never changes a job you submitted. Sending a read-back request again whose lines carry classes (a SKU's class too, which the read-back writes on the line) needs cargo_classes.read. A class on a line need not still be saved: a deleted one is used as a key for that job only, and a rule or override about a class that no line carries must still be a class you have saved. Every placement and unplaced item in the result carries the classes of its line.
Changing a class or a rule set safely
Every class and rule set has a revision, which the ETag header returns in quotes, such as "7". Send it back as If-Match with a PUT or DELETE: if someone changed the resource in between, the answer is 412 revision_mismatch with currentRevision, and nothing changes. Read it again and reapply your change. Without If-Match your change applies to whatever is stored.
Who made a change. A cargo class and a rule set, and each item of their lists, return createdAt and createdBy, and updatedAt and updatedBy for the last change, each as an actor: a user, with via: agent through an AI agent, or an API key. See Users, API keys and who did what.
Plans and limits
Cargo classes and rule sets come with company libraries. How many you may keep depends on your plan; see Limits.
Key fields
| Field | Where | Notes |
|---|---|---|
key |
CargoClass |
Fixed once saved. The value a SKU puts in classes and a rule in upper or lower. |
classes |
SKU entry, cargo line | At most one key. On a SKU it is the SKU's class; on an inline cargo line it is the line's own; a line that references a SKU takes the SKU's. |
constraints.stackingRules |
Job request | The rules a job applies: directOn and towerLimits inline, or a saved rule set by ruleSetId, never both. See In a job. |
overrides |
Job request, with ruleSetId |
Per pair, for this job only: rule is allowed, forbidden or limited (with maxCount). |
stackingRuleSet |
Job input | The saved rule set a job used: ruleSetId, name, revision and the overrides the order sent, as sent; null for inline rules or none. |
classes |
Placement, unplaced item | The class of the cargo line: empty, or one key. |
directOn |
StackingRuleSet |
Forbidden pairs: upper may not rest directly on lower. |
towerLimits |
StackingRuleSet |
Allowed pairs with a maximum number of upper boxes per tower, 1 to 1000. |
createdBy / updatedBy |
both | Who created it and who made the last change, as actors. The set's list items carry them too. |
revision |
both | Returned in quotes in the ETag header; send it as If-Match. |
Notes & caveats
- Deleting a class can change rule sets. With
removeFromRuleSets=true, every rule set that named the class gets a newrevision; anIf-Matchwith the oldETagthen answers412. - Kept until you delete them. Classes and rule sets stay while your account exists; see Retention.
Related
- SKU libraries: where a SKU gets its class.
- Constraints: stacking per box with
allowStackingOnTop. - Errors & troubleshooting:
cargo_class_in_use,revision_mismatch,unknown_cargo_classand the other codes.