Pattern Matching Functions in a Project

Patterns can be deployed and undeployed dynamically in any agent that receives events such as a query agent or inference agent.

In a startup rule function you can start the service. You can also create, register, instantiate, configure, and deploy a pattern. In a shutdown rule function you can undeploy a pattern (which also unregisters the pattern). However you can perform these operations in different parts of your code, depending on the need.

See the online function reference in the HTML documentation for a list of functions.

Note: Each agent has a separate instance of the pattern service. The patterns are not distributed. They are not cluster-aware.

Start the Pattern Matcher Service

In a startup rule function, start the pattern service, so it’s running when the engine starts:

Pattern.Service.start();

You can start the Pattern Matcher service at any time after the processing unit starts. You can stop it any time before the processing unit stops. However it is generally advisable to start it in a startup rule function and stop the service in a shutdown rule function.

Send Events to the Service

You must send events to the service explicitly, for example in a rule that executes when the event is asserted. The actions (then block) would contain the following:

Pattern.IO.toPattern(MyEvent);

The pattern service automatically routes events of the specified type to all subscribing patterns that have been deployed in the agent.

You can invoke this call anywhere in the context of an agent, for example in a rule.

This call returns immediately because the actual work of routing to the pattern instances and the processing is done by other threads.

Create the Pattern String

The name of this String (MyPatternString) in the example) must be unique within the service. For example:

String MyPatternString = "define pattern /My/Pattern/URI \n" +
      " using /Ontology/EventA as a \n" +
      " and /Ontology/EventB as b \n" +
      " and /Ontology/EventC as c \n" +
      " with a.name and b.name and c.text = $ParamName \n" +
      " starts with a then b then c";

The value for bind variable $ParamName is provided in the Pattern.Manager.SetParameterString() function (see step ).

Register the Pattern

Register the pattern string and get the pattern URI, which must be unique:

String MyPatternURI = Pattern.Manager.register(MyPatternString);

The pattern URI is any unique string. You can use slashes or dashes or simple text depending on how you want to organize the patterns using meaningful names. If the URI contains spaces, wrap the whole URI in double quotes ("my name")

The pattern URI is also used to unregister the pattern.

Instantiate the Pattern Instance

Instantiate the pattern instance and set any bind variable values. You can instantiate a registered pattern multiple times, using a different instance name in each case and, as needed, different values for the pattern variables.

Object MyPatternInstance = Pattern.Manager.instantiate(MyPatternURI);
Pattern.Manager.setParameterString(MyPatternInstance, "ParamName", "ParamValue");

Set the Closure

The closure distinguishes one pattern instance from another:

      Pattern.Manager.setClosure(MyPatternInstance, "This is MyPatternInstance");

The closure for a pattern is used by listeners (callback functions), to distinguish one instantiated pattern instance from another.

Set the Listeners

Set the completion (success) listener and failure listener. See Success and Failure Listeners (Callback Functions) for more on these callback functions.

      Pattern.Manager.setCompletionListener(MyPatternInstance,
   "/RuleFunctions/MyPatternSuccess");
      Pattern.Manager.setFailureListener(MyPatternInstance,
   "/RuleFunctions/PatternSc2Failure");

Deploy the Pattern Instance

Pattern.Manager.deploy(MyPatternInstance, "DeployedPatternInstanceName");

The instance name is used to undeploy the instance.

Undeploy and Unregister a Pattern

Before shutting down the service undeploy and unregister the patterns.

Pattern.Manager.undeploy("MyPatternInstance");
Pattern.Manager.unregister("MyPatternURI");

Stop the Pattern Service

You can stop the Pattern Matcher service at any time before the processing unit stops (and you can also start it again) depending on need. It is generally advisable to stop the service in a shutdown rule function so that the service stops before the processing unit itself stops.

Pattern.Service.stop();