Parameters and rules

Parameters are what an action asks for. Rules are what it does with the answers. Together they decide the form a person sees, the arguments an agent must supply, and the exact edits the action commits. This page covers parameter types and constraints, defaults and dropdown options, conditional parameter defaults, the rule kinds and how they compile into one edit per object, and how to apply an action to many objects at once

Parameter typesScalars, objects, lists of objects, attachments
DefaultsStatic
RulesCreate, modify, delete and function
BatchMany parameter sets in one call

In the platform today

Built
  • Parameters and rules run in the platform service, defined on action types in ontology.yaml
  • Parameter types are object (with objectType, the value being the key, or with interface, an object of any implementing type given as { objectType, key }, { _type, _key } or "type:key"), objects, string, integer, double, boolean, date, timestamp, and attachment or attachments, which take file ids from POST /api/v2/files with an optional accept list of content types (see attachments), with required, values, min, max, a valueType and a static default
  • Rules are modify (object, set), create (type, set; without the key in set, a generated key is used, with an optional keyPrefix), delete (object) and function (name, args), whose function returns edits as { op, type, key, set }
  • Values come from parameters and their properties, currentUser, now, fixed values, add, sub, addHours, template, registered functions, one field of a function's result ({ function, args, property }), { append: [list, value] }, which adds a value or a list of values to a list (a null adds nothing), and { writeback: name, field: output }, an output of a writeback call made before the edits
  • No edit may leave a property marked required: true on its type empty: a create must set it and a modify cannot clear it, or the run is refused with ACTION_VALIDATION_FAILED.REQUIRED_PROPERTY before anything is committed
  • A delete leaves a _deleted marker that keeps the object deleted while the source still has its row, and a create of a key the source does not hold leaves a _created marker that keeps it through a full sync, as described on object storage
  • An apply edits at most 10,000 objects
  • :runBatch applies parameter sets one after another, as shown below
On request
  • The other parameter types, defaults, options, parameter defaults, groups, link rules, created references, expressions, atomic batches, object set parameters and inline edits

Parameters

Each parameter has an ID and a type, and can be required, limited to a list of values or a range, and given a static default

TypeValueForm control
stringTextText field, or a dropdown when it has values
integer, doubleNumbers, with value type units such as litres or °CNumber field with unit
booleanTrue or falseSwitch
date, timestampCalendar dates and instantsDate and time pickers
objectOne object of a named type, the value being its key; with interface, an object of any implementing typeObject picker with search
objectsA list of object keysMulti-select
attachment, attachmentsOne file id, or a list, uploaded first with POST /api/v2/files, up to files.maxSize (200 MB by default), limited to the content types in accept. The file belongs to the object once the action applies, and is stored under the lake's root in your bucket. On request, files are scanned for malware before they can be attachedFile drop, or the phone camera

Constraints

  • Required. required: true
  • Ranges. min and max for numbers and dates
  • Values. A values list, or a valueType whose constraints apply, such as lot-code

Constraints are checked in the form as the person types and again at submit, against the snapshot the action reads. A client that skips the form, such as a script or an agent, gets the same checks and the same messages

Defaults

A parameter can have a static default. On request, defaults from the person, the time or earlier answers, filtered dropdowns of objects, and fields that change with earlier answers

Rules

Rules turn parameters into edits

RuleEffectNotes
createCreates an object{ type, set }. Without the type's key in set, a generated key is used, with an optional keyPrefix
modifySets properties on one object{ object, set }
deleteDeletes an object{ object }. The object stays deleted while the source still has its row
functionDelegates edits to a registered function{ name, args }. The function returns edits as { op, type, key, set }, which join the other rules' edits in rule order

Value sources

Every value a rule writes comes from one of a fixed set of sources, which is what makes an action's effect readable from its definition

SourceSyntax
A parameter{ parameter: reason }
A property of an object parameter{ parameter: batch, property: productId }
A static value{ value: On hold }, or a bare literal
The current user or time{ currentUser: id }, { now: true }
Arithmetic and text{ add: [a, b] }, { sub: [a, b] }, { addHours: [ts, n] }, { template: "…" }
A function result{ function: run_litres, args: … }, or one field of it with property
A list with values added{ append: [ { parameter: inspection, property: photos }, { parameter: photo } ] }
An output of a writeback call made before the edits{ writeback: po, field: poNumber }

On request, more parameter types and rules, such as lists, upserts, links and expressions

How rules compile

Rules run in declaration order, and their edits commit together with one action log entry. No edit may leave a property marked required: true on its type empty: a create must set it and a modify cannot clear it, or the run is refused with ACTION_VALIDATION_FAILED.REQUIRED_PROPERTY before anything is committed

Here are the rules of place-hold, used by a QA lead after a failed quality check. They hold the batch and, through a function, update the product's count of batches on hold

parameters:
  batch:  { type: object, objectType: batch, required: true }
  reason: { type: string, required: true }
rules:
  - modify: { object: { parameter: batch },
              set: { hold: true, qaStatus: On hold, holdReason: { parameter: reason } } }
  - function: { name: product_hold_edits, args: { batch: { parameter: batch }, hold: true } }

Function rules

Declarative rules cover most actions. When the edits depend on logic, such as a loop over silos to plan a draw, a create that happens only if a threshold is crossed, or edits across many types computed from one input, use a function rule. The function receives the parameters, reads the Ontology at the action's snapshot with the caller's permissions, and returns an edit set. It changes nothing by itself: the action service validates the returned edits and commits them like any other

import {
  EditFunction, EditSet, FunctionError,
} from "@marklandstreet/functions";
import { Silo } from "@marklandstreet/functions";

export const planSiloDraw: EditFunction<{ litres: number }> = async ({ litres }, ctx) => {
  const silos = await ctx.mso.objects(Silo)
    .where({ status: { $in: ["Holding", "Drawing"] } })
    .fetchAll({ orderBy: { hoursToLimit: "asc" } });   // milk nearest its 72 h limit first

  const edits = new EditSet();
  let remaining = litres;
  for (const s of silos) {
    if (remaining <= 0) break;
    const take = Math.min(s.volume - s.committedLitres, remaining);
    if (take <= 0) continue;
    edits.modify(s, { committedLitres: s.committedLitres + take });
    remaining -= take;
  }
  if (remaining > 0) throw new FunctionError(`The silos are short by ${remaining} L`);
  return edits;
};

A FunctionError is shown in the form as the action's message, like a failed criterion. Any other error fails the apply with FUNCTION_FAILED and commits nothing. On request, function rules run with the 60-second default timeout. See edits from functions for testing them

Batch applies and inline edits

:runBatch applies one action to many parameter sets in one call

Many parameter sets

POST /api/v2/actions/{action}:runBatch
{
  "requests": [
    { "batch": "B-0843", "reason": "Same silo draw as B-0841" },
    { "batch": "B-0841", "reason": "QA-7710 underfill" }
  ]
}

Each item in requests is one parameter set. The platform service applies them one after another and answers with a result per item, so one item that fails its conditions does not stop the others

{
  "results": [
    { "index": 0, "logId": "alog_01M5Y3M0WKA9KQFS7PE615KXJQ",
      "result": "APPLIED", "action": "place-hold", "objectKey": "B-0843", "edits": 1,
      "writeback": [ { "system": "production-sheet", "status": "delivered" },
                     { "system": "order-desk", "status": "delivered" } ] },
    { "index": 1, "result": "FAILED",
      "code": "ACTION_VALIDATION_FAILED", "reason": "CONDITION_FAILED",
      "detail": "B-0841 is already on hold" }
  ],
  "applied": 1
}

Each item gets its own action log entry. On request, all-or-nothing batches, actions over a whole object set, and inline table edits

Limitations

  • Edits from declarative rules and function rules join in rule order and commit together, with one action log entry
  • An apply edits at most 10,000 objects
  • :runBatch applies its items one after another and is not all-or-nothing: an item that fails leaves the items before it applied
ItemDefault
Attachment size200 MB, set with files.maxSize
Objects one file can be attached to10, set with files.maxObjects
Edits per apply10,000

Related