KJDRAW / COMPILE KNOWLEDGE PACKS
Compile knowledge packs
Turn licensed, versioned semantic rules and explicit engineering facts into deterministic editable CAD transactions.
Understand the pipeline
A KJDraw knowledge pack is declarative data, not a drawing dump or executable plugin. It declares source provenance, license flags, an ontology and bounded templates or rules. A semantic intent supplies the facts for one drawing. compileKnowledgeDrawing() validates both inputs and returns one deterministic CREATEBATCH argument plus evidence hashes:
licensed sources -> versioned pack -> semantic intent -> compiler -> CREATEBATCH -> KJD/DXF reopen
Keep project measurements in the intent, not in the reusable pack. Do not place original drawing binaries or copied entity arrays in a pack. Each source entry needs a stable ID, title, license and content hash; publish only material you are authorized to redistribute.
Compile a synthetic borehole
Create compile-knowledge.mjs in a project that has @kanjieteam/kjdraw installed. This complete example uses the packaged public geology pack and synthetic facts; it does not represent measured site data:
import {
KJDRAW_SEMANTIC_IR_SCHEMA,
createKJDrawSDK,
} from '@kanjieteam/kjdraw'
import { compileKnowledgeDrawing } from '@kanjieteam/kjdraw/knowledge-compiler'
import { KJDRAW_GEOLOGY_KNOWLEDGE_PACK } from '@kanjieteam/kjdraw/knowledge-packs/geology-core'
const intent = {
schema: KJDRAW_SEMANTIC_IR_SCHEMA,
packId: 'geology.core',
packVersion: '1.0.0',
drawing: { kind: 'borehole-column', title: 'Synthetic ZK-01', units: 'meter' },
objects: [
{ id: 'hole:zk-01', kind: 'borehole', properties: {
name: 'ZK-01', depth: 18, verticalScale: 1,
} },
{ id: 'layer:upper', kind: 'stratum', properties: {
top: 0, bottom: 6.5, lithology: 'clay',
} },
{ id: 'layer:lower', kind: 'stratum', properties: {
top: 6.5, bottom: 18, lithology: 'sand',
} },
],
relations: [
{ kind: 'contains-stratum', from: 'hole:zk-01', to: 'layer:upper' },
{ kind: 'contains-stratum', from: 'hole:zk-01', to: 'layer:lower' },
],
}
const compiled = compileKnowledgeDrawing({
pack: KJDRAW_GEOLOGY_KNOWLEDGE_PACK,
intent,
templateId: 'borehole-column',
rootObjectId: 'hole:zk-01',
expectedRevision: 0,
})
const sdk = createKJDrawSDK()
const drawing = sdk.createDocument({ documentId: 'knowledge-demo', units: 'meter' })
await sdk.executeCommand('CREATEBATCH', compiled.commandArgs, {
document: drawing,
expectedRevision: compiled.evidence.expectedRevision,
})
const kjd = await sdk.writeDocument(drawing, { format: 'KJD', version: '1' })
const dxf = await sdk.writeDocument(drawing, { format: 'DXF', version: '2018' })
const reopenedKjd = await sdk.readDocument(kjd, { format: 'KJD', version: '1' })
const reopenedDxf = await sdk.readDocument(dxf, { format: 'DXF', version: '2018' })
console.log({
evidence: compiled.evidence,
kjdEntities: reopenedKjd.listEntities().length,
dxfEntities: reopenedDxf.listEntities().length,
dxfValid: reopenedDxf.validate().valid,
})
Run node compile-knowledge.mjs. The compiler rejects a stratum gap or overlap, an undeclared object/relation kind, an unknown lithology lookup, an identity mismatch or an unsupported executable field before CAD mutation.
Author and register a pack
Use validateKnowledgePack(source) while developing one pack and KJKnowledgePackRegistry.register(source) when a host manages several redistributable packs. The minimum contract is:
schema, stable lowercaseid, semanticversion,titleanddomain;- explicit SPDX license, redistribution flag and training-permission flag;
- one or more content-hashed
sources; - declared semantic
objectKindsandrelationKinds; - bounded declarative
templatesandrulesconsumed by a compiler.
The generic compiler supports bounded expressions (get, arithmetic, concatenation and lookup), relation selection, continuity checks and native line, polyline, rectangle, rectangular hatch and text emission. It does not execute JavaScript stored in pack data. Domain compilers may consume additional validated rules through their published APIs.
Version every behavior change. Store the selected pack ID, version, content hash, intent hash, template ID and compiler evidence with regression results. A model should supply engineering facts only; the trusted host selects and hash-locks the knowledge pack.
Close the regression loop
For every published pack and representative intent, verify:
- two compiles produce identical command arguments and evidence hashes;
- the result applies as one
CREATEBATCHtransaction; - Undo removes the batch and Redo restores it;
- KJD and DXF both reopen and pass document validation;
- expected native entity, layer, hatch and text counts survive reopen;
- invalid, missing or discontinuous facts fail before mutation;
- provenance and redistribution declarations match the material actually published.
This proves the stated fixture and rules only. It does not turn synthetic facts into measured data or certify an arbitrary engineering drawing.