Basic Class Example

Updated: 2026-03-31

GPSS-Plus allows external logic to be incorporated through classes written in Javascript.

They are used to incorporate specific logic that is not part of the model: calculations, business rules, or libraries from a particular domain (chemistry, logistics, optimization, computation, visualization, etc.).

The model defines the system's behavior; the class handles specific operations.

Nature of Classes

These classes are integrated into the model as logical resources and are executed by entities as they move through the model.

  • They do not have an API.
  • They do not create VEs or directly modify a SAVEVALUE, as this would break the model's consistency without proper control.
  • They do not modify the model's flow or time.
  • Classes are synchronous: they execute at the exact moment they are invoked.

Class Contract

This first example shows the simplest case: a Sum class that receives two values, accumulates a value, processes them, and returns a result.

This is the Javascript file containing the class used in this example:

class Sum {

  constructor(name) {
    this.name = name;
	this.acc = 0;
  }

  accString() {
    return "'ACC: "+this.acc+"'";
  }

	
  exec(input) {
    const output = {};
	if (input.acc) this.acc += input.acc;
    output.out = input.a + input.b + this.acc;
	if (input.a<0) output.error="exec: Negative number"; 
    return output;
  }
}

The class follows the contract expected by the engine:

  • Constructor method.
  • exec(input) method that receives an object and returns an object.
  • A method for retrieving internal data, accessible through the SNA E$(instance,method), which takes no parameters and returns a string.
  • If exec returns the "error" property in the output object, the model will stop.

All classes must include at least the constructor and the exec method.

 

Using the Class from the Model:

To execute its logic from the model, follow these steps:

1.- Load the class using LOAD_CLASS (the URL must support CORS):

LOAD_CLASS https://gpss-plus.com/imports_js/Sum.js

Instantiate the class using INSTANCE_CLASS:

INSTANCE_CLASS {name:sum, class:Sum}

And call the exec method with the input we need using EXEC_CLASS:

assign inputs,{a:10,b:20,acc:D$N}
EXEC_CLASS {name:sum,in:V$inputs,savevalue:data} ; optional assign:data

The external resource does not alter the model's flow: it receives data, processes it, and returns a result that the model uses through a savevalue or an assign in the invoking entity.

Additionally, its internal data can be accessed through the SNA:

E$(instance,method)

 

 

Example

/*

  	 	Injected Resources
 
*/


POSITION {NAME:POS1,X:620,Y:360}
GRAPHIC {NAME:Text1,TYPE:TEXT,X:373,Y:268,TEXT:"DSL Graphic",font:"24"} ; Create text graphic
Facility {NAME:fac1,X:373,Y:365,capacity:3}


LOAD_CLASS https://gpss-plus.com/imports_js/Sum.js
INSTANCE_CLASS {name:sum, class:Sum}

START 2000




;*****************************************************
GENERATE 10,3 {NAME:GEN1,X:111,Y:366}
ADVANCE 5 {TO:fac1}


assign inputs,{a:10,b:20,acc:D$N}
EXEC_CLASS {name:sum,in:V$inputs,savevalue:data}

Move {name:Text1,text:"Result X$(data.out) \nACC: E$(sum,acc) \nACCstr: E$(sum,accString)"}

seize fac1
	ADVANCE 20,20
release fac1

ADVANCE 20 {TO:POS1}

ENDGENERATE 1