Through broker.localPersistence.* services, we can access functions related to local data storage, i.e in the device.

The data types that can be persisted locally by these functions in mat|r are the following:

Basic := Integer | Boolean | Double | String | Date | Model

Recursive := Array <Basic> | Array <Recursive> | Map <Basic> | Map <Recursive>

PUBLIC METHODS

void broker.localPersistence.save(String key, Any valueToSave)

It saves a value in the local data storage, in the key specified as argument.

Arguments  
key String: key where the value will be saved.
valueToSave [Basic, Recursive]: Value to be saved in the specified key.
Exceptionss
LocalPersistenceInternalException – Has been an exception in local persistence service, in method save for key key.

Any broker.localPersistence.get(String key)

It retrieves a value from the local data storage.

Arguments  
key String: key of the value to be retrieved.
Return  
[Basic, Recursive] Value corresponding to a specified key.
Exceptions
LocalPersistenceInternalException – Has been an exception in local persistence service, in method get for key key.
LocalPersistenceKeyNotFound – Local persistence service has not found a value for key key.

Any broker.localPersistence.get(String key, Any defaultValue)

It retrieves a value from the local data storage, returning a value by default, unless a value for the specified key is found.

Arguments  
key String: key of the value to be retrieved .
defaultValue [Basic, Recursive]: value by default.
Return  
[Basic, Recursive] Value corresponding to a specified key.
Excepciones
LocalPersistenceInternalException – Has been an exception in local persistence service, in method get for key key.
LocalPersistenceKeyNotFound – Local persistence service has not found a value for key key.

Bool broker.localPersistence.hasKey(String key)

It returns true if the key specified in key exists.

Arguments  
key String: key to be consulted in the local data storage.
Return  
Bool It returns true if the specified key exists.

void broker.localPersistence.remove(String key)

It deletes the value corresponding to the specified key from the local data storage.

Arguments  
key String: key of the value to be deleted.
Exceptions
LocalPersistenceKeyNotFound – Local persistence service has not found a value for key key.

void broker.localPersistence.removeAll()

It deletes the local data storage totally.

Example:

Experience Main {
    String name label("Main name")
    Decision rule1 action("MainContext.addTask")  label("Add Task")
    Decision rule2 action("MainContext.deleteList")  label("Delete task list")
}

Model Task {
    String nameTask
    String descriptionTask
}

RuleContext MainContext {

  Rule addTask {
    Array<Task> taskList = []
    if (broker.localPersistence.hasKey("taskList")) { //retrieve task list
        tasksList =  broker.localPersistence.get("taskList")
    } 
    Task newTask = Task()
    newTask.nameTask = "new task -> milk"
    newTask.descriptionTask = "buy milk"
    taskList.add(newTask)
    //persist list in local persistence
    broker.localPersistence.save("taskList", taskList)
  }

  Rule deleteList {
    if (broker.localPersistence.hasKey("taskList")) {
        //delete task list
        broker.localPersistence.remove("taskList")
    }
  }

}