Programmatically invoking rules

All types of rules can be accessed programmatically within a Java application. For example, a profiler can be used to determine the behavior an application should exhibit depending on the current user, or an action can return content to your application for further processing before the content is displayed. Rules are mapped to content spots, and because a content spot is an implementation of a JavaBean, it can be programmatically declared and implemented.

To programmatically invoke a rule, follow these steps:

  1. Instantiate the bean. If the class name of your content spot was ProfilerSpot, you would instantiate the bean using one of the following options:
    com.ibm.websphere.personalization.ContentSpot contentSpot = 
      new com.ibm.websphere.personalization.ContentSpot(“ProfilerSpot”);

    or

    ProfilerSpot contentSpot = new ProfilerSpot();
  2. Call the method setRequest() and pass the HttpServletRequest object or an object that implements PznRequestObjectInterface. This makes current information stored in the request object known to the Personalization Runtime Engine and the Resource Engine.
    contentSpot.setRequest(request); 
  3. Trigger the rule and, if applicable, get the content from the rule. Use one of the following methods, depending on the type of rule:
    • Select content actions, bindings and recommendation actions are mapped to content spots and return content from a resource.
      • getRuleContent() - returns an array of results
      • getRuleContent(int which) - returns the resource at the given index
    • Profilers are mapped to empty content spots which do not declare a return type.
      • boolean isProfiledAs(string value) - returns true or false depending on whether the string passed to the method matches a profile given to the user
      • String getProfile(integer value) - returns the profile in the location specified by the integer passed

        A user may have more than one profile. For example, a user may fit profiles named "young," "hip," and "sporty"; when looking for the profile at location 0, "young" is returned.

      • String[] getProfiles() - returns an array of profiles.

        For example, if a user is in profiles "young," "hip," and "sporty," this method will return an array of the profiles.

      • boolean isProfiledAsAll(String[] profiles) - returns True or False depending on whether all the profiles match all the profiles in the list of profiles passed to the method
      • boolean isProfiledAsAny(String[] profiles) - returns True or False depending on whether the profile is in the list of profiles passed to the method
    • Update actions and email actions are mapped to empty content spots.
      1. trigger() - runs the rule