Handling user-defined data types for ruleset parameters

Specialized toolkits - release 4.3.1.0-prod20190605 > com.ibm.streams.rules 2.1.2 > Handling user-defined data types for ruleset parameters

For ruleset parameters that use primitive Java data types, the ODMRulesetExecutor operator automatically maps the attributes in an input tuple to ruleset input parameter and maps ruleset output parameters to attributes in an output tuple. If the ruleset parameters contain user-defined Java data types, you must provide a Java class that can map the incoming tuple into any ODM input Java data types. You must also provide a Java class to map the ODM output Java data types to an IBM Streams tuple.

Procedure

  1. Create a class that extends the SimpleRulesetExecutionHandler class. For example:
    
      public class CustomRulesetExecutionHandler extends SimpleRulesetExecutionHandler
    
  2. Create a class that extends the SimpleParameterMapping class. For example:
    
      public class CustomParameterMapping extends SimpleParameterMapping
    
  3. In CustomRulsetExecutionHandler class, override the createTupleParameterMapping() method.
    
       import com.ibm.streams.operator.OutputTuple;
       import com.ibm.streams.operator.StreamingInput;
       import com.ibm.streams.operator.StreamingOutput;
       import com.ibm.streams.operator.Tuple;
       import com.ibm.streams.rules.odm.SimpleRulesetExecutionHandler;
       import com.ibm.streams.rules.odm.TupleRulesetParameterMapping;
       public class CustomRulesetExecutionHandler extends SimpleRulesetExecutionHandler {
         @Override
         protected TupleRulesetParameterMapping createTupleParameterMapping() {
           StreamingInput<Tuple> inputPort = getOperatorContext().getStreamingInputs().get(0);
           StreamingOutput<OutputTuple> outputPort = getOperatorContext().getStreamingOutputs().get(0);
         return new CustomRulesetMapping(inputPort, outputPort,
         getRulesetSignature());
       }
       }
    
  4. In the CustomParameterMapping class, override the mapToInputParameters and mapToTuple methods. The mapToInputParameters method maps input tuples to ruleset input parameters and returns input parameters that are used as input argument for executing the ruleset. The mapToTuple method maps the response object that contains the ruleset output parameters to output tuples.
    
       import ilog.rules.res.model.IlrRulesetParameter;
       import java.util.Collection;
       import java.util.Map;
       import java.util.Map.Entry;
       import miniloan.Borrower;
       import miniloan.Loan;
       import com.ibm.streams.operator.OutputTuple;
       import com.ibm.streams.operator.StreamingInput;
       import com.ibm.streams.operator.StreamingOutput;
       import com.ibm.streams.operator.Tuple;
       import com.ibm.streams.rules.odm.SimpleParameterMapping;
       
       public class CustomParameterMapping extends SimpleParameterMapping {
         public CustomParameterMapping(StreamingInput<Tuple> inputPort,
           StreamingOutput<OutputTuple> outputPort,
           Collection<IlrRulesetParameter> rulesetSignature) {
           super(inputPort, outputPort, rulesetSignature);
       }
      
      @Override
      public Map<String, ?> mapToInputParameters(Tuple tuple) throws Exception {
        Borrower bow = new Borrower();
        Loan loan = new Loan();
        Map<String, Object> inputParameters = (Map<String, Object>) super
          .mapToInputParameters(tuple);
        bow.setCreditScore(tuple.getInt("creditScore"));
        bow.setName(tuple.getString("name"));
        bow.setYearlyIncome(tuple.getInt("yearlyIncome"));
        loan.setAmount(tuple.getInt("amount"));
        loan.setDuration(tuple.getInt("dur"));
        loan.setYearlyInterestRate(tuple.getFloat("yearlyInterestRate"));
        inputParameters.put("borrower", bow);
        inputParameters.put("loan", loan);
        return inputParameters;
      }
      
      @Override
      public void mapToTuple(StreamingOutput<OutputTuple> outputPort,
        OutputTuple tup, Map<String, ?> outMap) throws Exception {
          super.mapToTuple(outputPort, tup, outMap);
          for (Entry<String, ?> entry : outMap.entrySet()) {
            if (entry.getKey().contains("loan")) {
              if (entry.getValue() != null) {
                Loan loanOut = (Loan) entry.getValue();
                tup.setString("ApprovalStatus", loanOut.getApprovalStatus());
                tup.setBoolean("Approved", loanOut.isApproved());
                tup.setInt("Amount", loanOut.getAmount());
              }
            }
        }
      }
      }
    
  5. Create a JAR file that contains CustomRulesetExecutionHandler and CustomParameterMapping classes and save it to a location that is accessible to your SPL application.
  6. In your SPL application, use the ODMRulesetExecutor operator and specify the following parameters:
    • xomLibrary parameter that points to the JAR file that you created in step 5
    • rulesetExecutionHandlerLibrary parameter that points to the location where the execution handler class file is located.
    • rulesetExecutionHandlerClassName parameter that points to the execution handler class CustomRulesetExecutionHandler.

Examples


composite CustomMapping{
  graph
    (stream<rstring strVal> Beacon_1_out0) as Beacon_1 = Beacon() {}
    (stream<rstring strVal> ODMRulesetExecutor_2_out0)
    as ODMRulesetExecutor_2 = ODMRulesetExecutor(Beacon_1_out0) {
      param
        rulesetPath : "/ruleapp/rules" ;
        ruleAppDirectory : "directory_where_rules_are_deployed";
        xomLibrary:"jar_file_containing_xom";
        rulesetExecutionHandlerLibrary :"jar_file_containing_ruleset_execution_handler" ;
        rulesetExecutionHandlerClassName : "mycompany.MyRulesetExecutionHandler" ;
    }
    () as Custom_3 = Custom(ODMRulesetExecutor_2_out0) {}
}