Pattern¶
Architectural pattern representation.
Overview¶
The Pattern class represents reusable architectural patterns with proven success rates.
Class Documentation¶
upir.patterns.pattern.Pattern
dataclass
¶
An architectural pattern discovered from or applied to UPIRs.
Patterns represent common architectural structures that appear across multiple UPIR instances. They can be discovered through clustering or defined manually as templates.
A pattern includes a template structure, metadata about instances that match the pattern, and performance metrics for pattern effectiveness.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier for the pattern |
name |
str
|
Human-readable name (e.g., "streaming-etl", "api-gateway") |
description |
str
|
Detailed description of the pattern |
template |
Dict[str, Any]
|
Template structure with parameterizable components Format: { "components": [{"type": "...", "properties": {...}}], "connections": [{"from": "...", "to": "...", ...}], "parameters": {...} # Tunable parameters } |
instances |
List[str]
|
List of UPIR IDs that match this pattern |
success_rate |
float
|
Fraction of instances meeting their specifications (0-1) |
average_performance |
Dict[str, float]
|
Average metrics across instances Format: {"latency_p99": ..., "throughput_qps": ...} |
Example
pattern = Pattern( ... id="streaming-etl-1", ... name="Streaming ETL Pipeline", ... description="Event-driven data processing pipeline", ... template={ ... "components": [ ... {"type": "pubsub_source", "properties": {}}, ... {"type": "stream_processor", "properties": {}}, ... {"type": "bigquery_sink", "properties": {}} ... ], ... "parameters": {"window_size": 60, "parallelism": 10} ... }, ... instances=["upir-1", "upir-2", "upir-3"], ... success_rate=0.95, ... average_performance={"latency_p99": 150, "throughput_qps": 5000} ... )
References: - TD Commons: Pattern extraction and reuse - Design patterns: Template method pattern
Source code in upir/patterns/pattern.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Functions¶
__post_init__()
¶
Validate pattern fields.
Source code in upir/patterns/pattern.py
add_instance(upir_id, performance=None)
¶
Add a UPIR instance to this pattern.
Updates instances list and recalculates average performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upir_id
|
str
|
ID of the UPIR instance |
required |
performance
|
Dict[str, float]
|
Performance metrics for this instance |
None
|
Source code in upir/patterns/pattern.py
matches(feature_vector, threshold=0.8)
¶
Check if a feature vector matches this pattern.
Uses cosine similarity between the feature vector and the pattern's centroid (if available in template).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_vector
|
List[float]
|
Feature vector to check |
required |
threshold
|
float
|
Similarity threshold (0-1) |
0.8
|
Returns:
| Type | Description |
|---|---|
bool
|
True if similarity >= threshold |
Source code in upir/patterns/pattern.py
to_dict()
¶
Serialize pattern to dictionary.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary representation |
Source code in upir/patterns/pattern.py
from_dict(data)
classmethod
¶
Deserialize pattern from dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary containing pattern fields |
required |
Returns:
| Type | Description |
|---|---|
Pattern
|
Pattern instance |
Source code in upir/patterns/pattern.py
__str__()
¶
__repr__()
¶
Developer-friendly representation.
Usage Example¶
from upir.patterns.pattern import Pattern
from datetime import datetime
# Create pattern
pattern = Pattern(
id="streaming-etl-001",
name="Streaming ETL Pattern",
description="Real-time data pipeline with Pub/Sub -> Beam -> BigQuery",
template={
"components": [...],
"connections": [...],
"centroid": [...] # Feature vector
},
instances=[],
success_rate=0.95,
created_at=datetime.now(),
updated_at=datetime.now()
)
# Serialize
pattern_json = pattern.to_json()
See Also¶
- Pattern Extractor - Extract patterns from architectures
- Pattern Library - Store and retrieve patterns