Clique Inference
knowledgecomplex.clique — Clique complex and flagification methods.
Two workflows for inferring higher-order simplices from the edge graph:
Generic exploration (fill_cliques)
Discover what higher-order structure exists before knowing the semantics.
Fill in generic simplices for all cliques up to a given order. Inspect
what shows up, then decide what types to declare.
Typed inference (infer_faces)
Once you've declared a face type with semantic meaning, fill in all
instances of that type from the edge graph. The face type is required —
you declare the type, then run inference to populate it.
find_cliques is a pure query that returns vertex cliques without
modifying the complex.
Typical workflow::
# Phase 1: Explore — what triangles exist?
sb.add_face_type("_clique")
kc = KnowledgeComplex(schema=sb)
# ... add vertices and edges ...
result = fill_cliques(kc, max_order=2)
# Phase 2: Inspect
for fid in result[2]:
edge_types = {kc.element(e).type for e in kc.boundary(fid)}
print(f"{fid}: {edge_types}")
# Phase 3: Typed inference with a real schema
sb2 = SchemaBuilder(namespace="ex")
sb2.add_face_type("operation", attributes={...})
kc2 = KnowledgeComplex(schema=sb2)
# ... add vertices and edges ...
infer_faces(kc2, "operation", edge_type="performs")
find_cliques(kc, k=3, *, edge_type=None)
Find all k-cliques of KC vertices in the edge graph.
A k-clique is a set of k vertices where every pair is connected by an edge. This is a pure query — it does not modify the complex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kc
|
KnowledgeComplex
|
|
required |
k
|
int
|
Clique size (default 3 for triangles). |
3
|
edge_type
|
str
|
Only consider edges of this type when building the adjacency graph. |
None
|
Returns:
| Type | Description |
|---|---|
list[frozenset[str]]
|
Each element is a frozenset of k vertex IDs. |
Source code in knowledgecomplex/clique.py
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 | |
infer_faces(kc, face_type, *, edge_type=None, id_prefix='face', dry_run=False)
Infer and add faces of a declared type from 3-cliques in the edge graph.
Finds all triangles (3-cliques of KC vertices), resolves the 3 boundary
edges for each, and calls kc.add_face() with the specified type.
Skips triangles that already have a face.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kc
|
KnowledgeComplex
|
|
required |
face_type
|
str
|
A registered face type to assign to inferred faces. |
required |
edge_type
|
str
|
Only consider edges of this type when finding triangles. |
None
|
id_prefix
|
str
|
Prefix for auto-generated face IDs (e.g. |
'face'
|
dry_run
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
IDs of newly added (or would-be) faces. |
Raises:
| Type | Description |
|---|---|
SchemaError
|
If face_type is not a registered face type. |
Source code in knowledgecomplex/clique.py
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
fill_cliques(kc, max_order=2, *, edge_type=None, id_prefix='clique')
Fill generic simplices for all cliques up to max_order.
Discovers what higher-order structure exists without requiring semantic
type declarations. For k=2 (faces), uses the first declared face type.
For k>2, uses _assert_element directly with the base kc:Element
type — these are generic, untyped simplices.
This is an exploration tool. Once you've inspected the structure,
declare typed face types and use :func:infer_faces for semantic
inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kc
|
KnowledgeComplex
|
|
required |
max_order
|
int
|
Maximum simplex dimension to fill (default 2 = faces). |
2
|
edge_type
|
str
|
Only consider edges of this type when finding cliques. |
None
|
id_prefix
|
str
|
Prefix for auto-generated IDs. |
'clique'
|
Returns:
| Type | Description |
|---|---|
dict[int, list[str]]
|
Mapping from dimension to list of newly added element IDs.
E.g. |
Source code in knowledgecomplex/clique.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
dry_run_check(kc, boundary_ids)
Check if an element with this boundary already exists.
Source code in knowledgecomplex/clique.py
391 392 393 394 395 396 397 398 399 | |