Class PmmlInferenceEngine
- java.lang.Object
-
- com.codedstream.otterstream.inference.engine.LocalInferenceEngine<org.jpmml.evaluator.Evaluator>
-
- com.codedstream.otterstream.pmml.PmmlInferenceEngine
-
- All Implemented Interfaces:
InferenceEngine<org.jpmml.evaluator.Evaluator>
public class PmmlInferenceEngine extends LocalInferenceEngine<org.jpmml.evaluator.Evaluator>
PMML (Predictive Model Markup Language) implementation ofLocalInferenceEngine.This engine provides inference capabilities for PMML models using the JPMML (Java PMML) library. PMML is an XML-based standard for representing predictive models, supporting various model types including regression, decision trees, neural networks, and ensemble models.
Supported PMML Features:
- Model Types: Regression, decision trees, neural networks, ensemble models
- Data Preparation: Automatic type conversion and value preparation
- Output Types: Target fields, output fields, computed results
- Batch Processing: Sequential batch inference (non-optimized)
Model Loading:
ModelConfig config = ModelConfig.builder() .modelPath("model.pmml") .modelId("credit-scoring") .build(); PmmlInferenceEngine engine = new PmmlInferenceEngine(); engine.initialize(config);Inference Example:
Map<String, Object> inputs = new HashMap<>(); inputs.put("age", 35); inputs.put("income", 75000.0); inputs.put("credit_score", 720); inputs.put("loan_amount", 25000.0); InferenceResult result = engine.infer(inputs); double riskScore = (double) result.getOutput("risk_score"); String prediction = (String) result.getOutput("prediction");Input Field Preparation:
The engine automatically prepares input values using PMML field definitions:
- Type conversion according to PMML field data types
- Missing value handling
- Outlier treatment
- Discretization and normalization (if defined in PMML)
Output Structure:
Results include both target fields (primary predictions) and output fields (derived metrics). Computable results are automatically resolved:
- Target Fields: Primary model predictions
- Output Fields: Derived metrics, probabilities, scores
- Computed Values: Automatically resolved from Computable objects
Batch Inference:
Batch inference is implemented as sequential single inferences, as PMML/JMML doesn't natively support batch processing. Outputs are aggregated with index suffixes (e.g., "risk_score_0", "risk_score_1").
Capabilities:
Feature Supported Notes Batch Inference No Sequential processing only Native Batching No No native batch optimization Max Batch Size 1 Single inference at a time GPU Support Yes Depends on underlying hardware Dependencies:
Requires JPMML (Java PMML) library: - org.jpmml:pmml-evaluator (runtime) - org.jpmml:pmml-model (runtime)
Thread Safety:
JPMML
Evaluatorinstances are not thread-safe. For concurrent inference, create separate engine instances or synchronize access toinfer(java.util.Map<java.lang.String, java.lang.Object>)andinferBatch(java.util.Map<java.lang.String, java.lang.Object>[])methods.Performance Considerations:
- PMML inference is generally slower than native formats (ONNX, TensorFlow)
- Batch processing is sequential - consider parallel execution for throughput
- Complex data transformations in PMML can add overhead
- Since:
- 1.0.0
- Author:
- Nestor Martourez, Sr Software and Data Streaming Engineer @ CodedStreams
- See Also:
LocalInferenceEngine,Evaluator,ModelEvaluatorFactory
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.codedstream.otterstream.inference.engine.InferenceEngine
InferenceEngine.EngineCapabilities
-
-
Field Summary
-
Fields inherited from class com.codedstream.otterstream.inference.engine.LocalInferenceEngine
initialized, loadedModel, modelConfig, modelLoader
-
-
Constructor Summary
Constructors Constructor Description PmmlInferenceEngine()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the PMML inference engine and releases resources.InferenceEngine.EngineCapabilitiesgetCapabilities()Gets the engine's capabilities for PMML inference.ModelMetadatagetMetadata()Gets metadata about the loaded PMML model.InferenceResultinfer(Map<String,Object> inputs)Performs single inference on the provided inputs using the PMML model.InferenceResultinferBatch(Map<String,Object>[] batchInputs)Performs batch inference by sequentially processing multiple input sets.voidinitialize(ModelConfig config)Initializes the PMML inference engine by loading and parsing a PMML model file.-
Methods inherited from class com.codedstream.otterstream.inference.engine.LocalInferenceEngine
getModelConfig, isReady, loadModelDirectly
-
-
-
-
Method Detail
-
initialize
public void initialize(ModelConfig config) throws InferenceException
Initializes the PMML inference engine by loading and parsing a PMML model file.The initialization process:
- Loads PMML file from the configured path
- Parses XML into
PMMLobject - Creates
Evaluatorinstance usingModelEvaluatorFactory - Verifies model integrity
Dependency Note:
Requires
pmml-evaluatorlibrary. Ensure the following dependency:<dependency> <groupId>org.jpmml</groupId> <artifactId>pmml-evaluator</artifactId> <version>1.6.3</version> </dependency>- Specified by:
initializein interfaceInferenceEngine<org.jpmml.evaluator.Evaluator>- Overrides:
initializein classLocalInferenceEngine<org.jpmml.evaluator.Evaluator>- Parameters:
config- model configuration containing the PMML file path- Throws:
InferenceException- if model loading or initialization failsIllegalStateException- if required JPMML libraries are missing
-
infer
public InferenceResult infer(Map<String,Object> inputs) throws InferenceException
Performs single inference on the provided inputs using the PMML model.The inference process:
- Prepares input values using PMML field definitions
- Executes model evaluation
- Extracts target and output fields
- Resolves
Computableresults if present - Returns structured inference results
Input Preparation:
Input values are automatically prepared using
InputField.prepare(Object), which handles:- Data type conversion
- Missing value substitution
- Value transformations defined in PMML
- Validation against field constraints
Output Extraction:
Extracts both target fields (primary predictions) and output fields (additional metrics). Computable results are automatically resolved to their final values.
- Specified by:
inferin interfaceInferenceEngine<org.jpmml.evaluator.Evaluator>- Specified by:
inferin classLocalInferenceEngine<org.jpmml.evaluator.Evaluator>- Parameters:
inputs- map of input field names to values- Returns:
- inference result containing predictions and timing information
- Throws:
InferenceException- if inference fails or inputs are invalid
-
inferBatch
public InferenceResult inferBatch(Map<String,Object>[] batchInputs) throws InferenceException
Performs batch inference by sequentially processing multiple input sets.Note: PMML/JMML doesn't support native batch processing, so this method processes inputs sequentially. Outputs are aggregated with index suffixes to maintain per-sample traceability.
Output Format:
Outputs are aggregated into a single map with indexed keys:
{ "risk_score_0": 0.75, "prediction_0": "APPROVED", "risk_score_1": 0.92, "prediction_1": "REJECTED" }Performance Warning:
This implementation doesn't provide batch optimization. For high-throughput scenarios, consider:
- Parallel execution across multiple engine instances
- Using a different model format with native batch support
- Implementing custom batching logic
- Specified by:
inferBatchin interfaceInferenceEngine<org.jpmml.evaluator.Evaluator>- Specified by:
inferBatchin classLocalInferenceEngine<org.jpmml.evaluator.Evaluator>- Parameters:
batchInputs- array of input maps, each representing one sample- Returns:
- aggregated inference result containing all batch outputs
- Throws:
InferenceException- if any inference in the batch fails
-
getCapabilities
public InferenceEngine.EngineCapabilities getCapabilities()
Gets the engine's capabilities for PMML inference.Note: PMML engines have limited capabilities compared to other formats like ONNX:
- Batch Inference: Not supported natively
- Native Batching: No batch optimization
- Max Batch Size: Limited to 1 (sequential processing)
- GPU Support: Available but depends on hardware
- Specified by:
getCapabilitiesin interfaceInferenceEngine<org.jpmml.evaluator.Evaluator>- Specified by:
getCapabilitiesin classLocalInferenceEngine<org.jpmml.evaluator.Evaluator>- Returns:
- engine capabilities indicating limited batch support
-
close
public void close() throws InferenceExceptionCloses the PMML inference engine and releases resources.Note: JPMML
Evaluatordoesn't implementAutoCloseable, so resources are released by nullifying references and relying on garbage collection. No native resources need explicit cleanup.- Specified by:
closein interfaceInferenceEngine<org.jpmml.evaluator.Evaluator>- Overrides:
closein classLocalInferenceEngine<org.jpmml.evaluator.Evaluator>- Throws:
InferenceException- if cleanup fails (unlikely for PMML)
-
getMetadata
public ModelMetadata getMetadata()
Gets metadata about the loaded PMML model.TODO: Implement PMML metadata extraction. Potential metadata includes:
- Model type (regression, decision tree, neural network, etc.)
- Input field definitions and data types
- Output/target field information
- Model version and creation timestamp
- Training data characteristics
- Returns:
- model metadata (currently returns null, override for implementation)
-
-