Skip to content

LOGIC

Custom routing written in Python: sends each entity to one of several outgoing links by your own rule when chance or a condition is not enough.

Routes an entity through a user-supplied, plain (non-BaseProcess) class defined in nagasim.core.custom_logic, or first in the module the caller passes as run_model(logic=...). The class only needs an execute(self, entity, ctx) -> int method returning the outgoing link index; execute(self, entity) (no ctx) is also accepted.

Family: control. Python builder: m.logic(...).

Typical uses

Use LOGIC when a routing rule is too complex for DECIDE: a rule that needs loops, several attributes, a lookup or state kept between entities. You write the rule as a small Python class, and the block uses the number it returns as the index of the outgoing link.

Behaviour and rules

The class needs an execute(self, entity, ctx) method that returns the link index. A method execute(self, entity) without ctx is also accepted. The model file only names the class in logic_class. The class is looked up first in the module the caller passes with run_model(logic=...) or naga run --logic, then in the default module custom_logic.

A model file cannot name a module or a path, so a file alone cannot make the engine run Python. The caller chooses the module. A class that neither module has stops the run with an error.

A new instance of the class is created for every entity. Anything the rule must remember between entities therefore belongs in model variables or on the context, not in the instance. The returned index must be a valid link of the block, or the run stops with an error. The block takes no time.

Ports

  • Inputs: any number of incoming links.
  • Outputs: several, one for each branch; the block picks the link by its index at run time.

Parameters

Parameter Type Default Required Quantity Unit key Description
logic_class str 'SampleLogic' Name of a class in nagasim.core.custom_logic implementing execute(entity, ctx) -> int.
hook str '' Inside an item with code: call its on_entry (route by its answer) or its on_exit (then go on by the first link). Empty: use logic_class.

Statistics

Every run fills system and entity_summary (see the KPI reference). The example below also fills:

Frame Columns
material_flow_summary Run, From, To, Entities

Problem codes

A check of the model reports these about this block:

Code Meaning
E_ITEM_HOOK An item's code is wired wrong: a LOGIC hook outside an item with code, a hook its class does not define, a timer or signals without their hook, a watched name that is not a variable, item code at the top of the file, or an "item" routing rule on a block outside an item with code.
E_LOGIC_CLASS_UNKNOWN A LOGIC block names a class the logic module (nagasim.core.custom_logic) does not define, so a run from an agent tool would fail on it.
E_VIEWER_MODEL A model cannot be shared as a browser viewer as it is: it holds agent Python, a LOGIC block, something a plugin registered, a data source that was not frozen into the copy, or it is too large.

Example

The snippet seeds itself; its output is from a run made when this page was generated.

from nagasim import build as nb

m = nb.Model(time_limit=100)
logic = m.logic()                              # the stock SampleLogic: always link 0
m.create(distribution=nb.Constant(value=2)) >> logic
logic >> m.delay(distribution=nb.Constant(value=1)) >> m.dispose()
logic >> m.dispose()

results = m.run(seed=1)
print(int(results.kpi("system/Total Entities Completed")), "entities, all sent down link 0")
50 entities, all sent down link 0

Used in

Bundled models: Palletizing line.

Guides: How do I route entities by a rule I write in Python?, Coming from AnyLogic, Coming from Arena, Coming from salabim, Coming from SimPy, How do I share a viewer or a player? and 3 more.

See also