Schema
knowledgecomplex.schema — SchemaBuilder and vocab/text descriptors.
Public API. Never exposes rdflib, pyshacl, or owlrl objects.
Internal structure mirrors the 2x2 responsibility map: {topological, ontological} x {OWL, SHACL}
The core ontology defines KC:Element as the base class for all simplices, with KC:Vertex (k=0), KC:Edge (k=1), KC:Face (k=2) as subclasses. add_vertex_type / add_edge_type / add_face_type each declare a user type as a subclass of the appropriate simplex class and write to both internal OWL and SHACL graphs.
dump_owl() and dump_shacl() return merged (core + user) Turtle strings.
Codec
Bases: Protocol
Bidirectional bridge between element records and artifacts at URIs.
A codec pairs compile (map → territory) and decompile (territory → map) for a given element type. Registered on KnowledgeComplex instances via register_codec(), and inherited by child types.
Source code in knowledgecomplex/schema.py
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 | |
compile(element)
Write an element record to the artifact at its URI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
dict
|
Keys: id, type, uri, plus all attribute key-value pairs. |
required |
Source code in knowledgecomplex/schema.py
49 50 51 52 53 54 55 56 57 58 | |
decompile(uri)
Read the artifact at a URI and return an attribute dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI of the artifact to read. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Attribute key-value pairs suitable for add_vertex/add_edge/add_face kwargs. |
Source code in knowledgecomplex/schema.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
VocabDescriptor
dataclass
Returned by vocab(). Carries the allowed string values for an attribute. Generates both an OWL rdfs:comment annotation and a SHACL sh:in constraint when passed to add_*_type().
Source code in knowledgecomplex/schema.py
77 78 79 80 81 82 83 84 85 86 87 88 89 | |
TextDescriptor
dataclass
Returned by text(). Marks an attribute as a free-text string (no controlled vocabulary). Generates an OWL DatatypeProperty with xsd:string range and a SHACL property shape with sh:datatype xsd:string but no sh:in constraint.
Source code in knowledgecomplex/schema.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
SchemaBuilder
Author a knowledge complex schema: vertex types, edge types, face types.
Each add_*_type call declares a new OWL subclass of the appropriate KC:Element subclass (Vertex, Edge, or Face) and creates a corresponding SHACL node shape. Both OWL and SHACL graphs are maintained internally. dump_owl() / dump_shacl() return the full merged Turtle strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Short namespace token for user-defined classes and properties. Used to build IRI prefix: https://example.org/{namespace}# |
required |
Example
sb = SchemaBuilder(namespace="aaa") sb.add_vertex_type("spec") sb.add_edge_type("verification", ... attributes={"status": vocab("passing", "failing", "pending")}) sb.add_face_type("assurance") owl_ttl = sb.dump_owl() shacl_ttl = sb.dump_shacl()
Source code in knowledgecomplex/schema.py
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 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 243 244 245 246 247 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 | |
add_vertex_type(name, attributes=None, parent=None, bind=None)
Declare a new vertex type (OWL subclass of KC:Vertex + SHACL node shape).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Class name within the user namespace. |
required |
attributes
|
dict
|
Mapping of attribute name to descriptor (VocabDescriptor, TextDescriptor, or dict with "vocab"/"text" key and optional "required" flag). |
None
|
parent
|
str
|
Name of a registered vertex type to inherit from. |
None
|
bind
|
dict
|
Mapping of attribute names to fixed string values (sh:hasValue). |
None
|
Returns:
| Type | Description |
|---|---|
SchemaBuilder (self, for chaining)
|
|
Source code in knowledgecomplex/schema.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
add_edge_type(name, attributes=None, parent=None, bind=None)
Declare a new edge type (OWL subclass of KC:Edge + SHACL property shapes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Class name within the user namespace. |
required |
attributes
|
dict
|
Mapping of attribute name to descriptor (VocabDescriptor, TextDescriptor, or dict with "vocab"/"text" key and optional "required" flag). |
None
|
parent
|
str
|
Name of a registered edge type to inherit from. |
None
|
bind
|
dict
|
Mapping of attribute names to fixed string values (sh:hasValue). |
None
|
Returns:
| Type | Description |
|---|---|
SchemaBuilder (self, for chaining)
|
|
Source code in knowledgecomplex/schema.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
add_face_type(name, attributes=None, parent=None, bind=None)
Declare a new face type (OWL subclass of KC:Face + SHACL property shapes).
Attributes with required=False generate sh:minCount 0 constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Class name within the user namespace. |
required |
attributes
|
dict
|
Mapping of attribute name to descriptor (VocabDescriptor, TextDescriptor, or dict with "vocab"/"text" key and optional "required" flag). |
None
|
parent
|
str
|
Name of a registered face type to inherit from. |
None
|
bind
|
dict
|
Mapping of attribute names to fixed string values (sh:hasValue). |
None
|
Returns:
| Type | Description |
|---|---|
SchemaBuilder (self, for chaining)
|
|
Source code in knowledgecomplex/schema.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | |
describe_type(name)
Inspect a registered type's attributes, parent, and bindings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The registered type name. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Keys: name, kind, parent, own_attributes, inherited_attributes, all_attributes, bound. |
Source code in knowledgecomplex/schema.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
type_names(kind=None)
List registered type names, optionally filtered by kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
Filter by "vertex", "edge", or "face". |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
|
Source code in knowledgecomplex/schema.py
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
promote_to_attribute(type, attribute, vocab=None, text=None, required=True)
Atomically promote a discovered pattern to a first-class typed attribute.
Updates both OWL property definition and SHACL shape constraint for the named type. After calling this, dump_owl() and dump_shacl() both reflect the updated attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
The type name (must have been registered via add_*_type). |
required |
attribute
|
str
|
Attribute name to add or upgrade. |
required |
vocab
|
VocabDescriptor
|
Controlled vocabulary for the attribute. |
None
|
text
|
TextDescriptor
|
Free-text descriptor for the attribute. |
None
|
required
|
bool
|
If True, generates sh:minCount 1. Overrides the descriptor's own required flag. |
True
|
Returns:
| Type | Description |
|---|---|
SchemaBuilder (self, for chaining)
|
|
Source code in knowledgecomplex/schema.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | |
add_sparql_constraint(type_name, sparql, message)
Attach a sh:sparql constraint to the SHACL shape for type_name.
The sparql argument must be a SPARQL SELECT query that returns $this for each violating focus node. pyshacl evaluates this and reports the message for each returned row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The type name (must have been registered via add_*_type). |
required |
sparql
|
str
|
SPARQL SELECT query. Must bind $this to each violating node. |
required |
message
|
str
|
Human-readable message reported when the constraint is violated. |
required |
Returns:
| Type | Description |
|---|---|
SchemaBuilder (self, for chaining)
|
|
Source code in knowledgecomplex/schema.py
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |
add_query(name, operation, *, target_type=None)
Register a named topological query template on this schema.
The query is generated from a topological operation and optional type
filter, then stored internally. It is exported as a .sparql file
by :meth:export and automatically loaded by
:class:~knowledgecomplex.graph.KnowledgeComplex at runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Query template name (becomes the filename stem, e.g. |
required |
operation
|
str
|
Topological operation: |
required |
target_type
|
str
|
Filter results to this type (including subtypes via OWL class hierarchy). |
None
|
Returns:
| Type | Description |
|---|---|
SchemaBuilder (self, for chaining)
|
|
Example
sb.add_query("spec_coboundary", "coboundary", target_type="verification")
Source code in knowledgecomplex/schema.py
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | |
add_topological_constraint(type_name, operation, *, target_type=None, predicate='min_count', min_count=1, max_count=None, message=None)
Escalate a topological query to a SHACL constraint.
Generates a sh:sparql constraint that, for each focus node of
type_name, evaluates a topological operation and checks a cardinality
predicate. Delegates to :meth:add_sparql_constraint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
The type to constrain (must be registered). |
required |
operation
|
str
|
Topological operation: |
required |
target_type
|
str
|
Filter the topological result to this type. |
None
|
predicate
|
str
|
|
'min_count'
|
min_count
|
int
|
Minimum count (used by |
1
|
max_count
|
int
|
Maximum count (used by |
None
|
message
|
str
|
Custom violation message. Auto-generated if not provided. |
None
|
Returns:
| Type | Description |
|---|---|
SchemaBuilder (self, for chaining)
|
|
Example
sb.add_topological_constraint( ... "spec", "coboundary", ... target_type="verification", ... predicate="min_count", min_count=1, ... message="Every spec must have at least one verification edge", ... )
Source code in knowledgecomplex/schema.py
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 | |
dump_owl()
Return merged OWL graph (core + user schema) as a Turtle string.
Source code in knowledgecomplex/schema.py
1022 1023 1024 | |
dump_shacl()
Return merged SHACL graph (core shapes + user shapes) as a Turtle string.
Source code in knowledgecomplex/schema.py
1026 1027 1028 | |
export(path, query_dirs=None)
Export the schema to a directory as standard semantic web files.
Writes ontology.ttl (OWL) and shapes.ttl (SHACL). If query_dirs are provided, copies all .sparql files into a queries/ subdirectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Target directory. Created if it does not exist. |
required |
query_dirs
|
list[Path]
|
Directories containing .sparql query templates to include. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The export directory. |
Source code in knowledgecomplex/schema.py
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 | |
load(path)
classmethod
Load a schema from a directory containing ontology.ttl and shapes.ttl.
Reconstructs the type registry by inspecting OWL subclass triples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Directory containing ontology.ttl and shapes.ttl. |
required |
Returns:
| Type | Description |
|---|---|
SchemaBuilder
|
|
Source code in knowledgecomplex/schema.py
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 | |
vocab(*values, multiple=False)
Declare a controlled vocabulary for an attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*values
|
str
|
The allowed string values. |
()
|
multiple
|
bool
|
If True, allows multiple values (no sh:maxCount). If False (default), generates sh:maxCount 1. |
False
|
Returns:
| Type | Description |
|---|---|
VocabDescriptor
|
|
Example
vocab("adjacent", "opposite") vocab('adjacent', 'opposite') vocab("a", "b", "c", multiple=True) vocab('a', 'b', 'c', multiple=True)
Source code in knowledgecomplex/schema.py
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 | |
text(*, required=True, multiple=False)
Declare a free-text string attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required
|
bool
|
If True (default), generates sh:minCount 1. |
True
|
multiple
|
bool
|
If True, allows multiple values (no sh:maxCount). If False (default), generates sh:maxCount 1. |
False
|
Returns:
| Type | Description |
|---|---|
TextDescriptor
|
|
Example
text() text() text(required=False, multiple=True) text(required=False, multiple=True)
Source code in knowledgecomplex/schema.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |