Code generators that produce typed APIs from an HMF metamodel.
Given a MetamodelRegistry populated with MetaClass and MetaAssociation
definitions, the generators emit:
- Kotlin — typed interfaces and
HMFObject-wrapping implementations. - TypeScript — interface declarations and a discriminated-union type of concrete metaclass names, suitable for ID-based REST APIs.
This repo is a sibling build to hmf and is composed in via
includeBuild("../hmf-codegen").
Type-safe interfaces over ModelElement, with implementations that delegate
to an HMFObject and HMFEngine:
// Interface
interface Element : ModelElement {
var aliasIds: List<String>
var declaredName: String?
val isLibraryElement: Boolean // derived, read-only
val name: String? // derived, read-only
fun effectiveName(): String?
}
// Implementation (excerpt)
class ElementImpl(wrapped: HMFObject, engine: HMFEngine) : Element {
override var declaredName: String?
get() = wrapped.getProperty("declaredName") as? String
set(value) { engine.setProperty(wrapped.id!!, "declaredName", value) }
override val name: String?
get() = engine.getProperty(wrapped.id!!, "name") as? String // triggers derivation
}A factory class is generated alongside the impls so callers can construct typed instances by metaclass name.
export interface Element {
'@id': string;
'@type': Metaclass;
declaredName?: string;
readonly name?: string;
owner?: ElementRef;
readonly ownedElements: ElementRef[];
}
export type Metaclass =
| 'Element'
| 'Package'
// ... only concrete classes appear in the unionConventions:
- Association ends become
ElementRef(ID-based references) for use with REST APIs that exchange entities by ID. - Optionality uses
?on the property name, not| undefined. - Only concrete (non-abstract) classes appear in the metaclass union.
- Output is topologically sorted so
extendsreferences are always valid.
The generators are driven by CodeGeneratorRunner with a mode flag. A
downstream project (such as a metamodel module) wires Gradle tasks like:
// In a consumer build.gradle.kts
tasks.register<JavaExec>("generateMetamodelCode") {
mainClass.set("org.openmbee.hmf.framework.codegen.CodeGeneratorRunner")
classpath = sourceSets.main.get().runtimeClasspath
systemProperty("hmf.codegen.mode", "kotlin")
}
tasks.register<JavaExec>("generateTypeScriptTypes") {
mainClass.set("org.openmbee.hmf.framework.codegen.CodeGeneratorRunner")
classpath = sourceSets.main.get().runtimeClasspath
systemProperty("hmf.codegen.mode", "typescript")
// Optional override of the output directory:
// systemProperty("hmf.codegen.ts.outputDir", "/custom/path")
}Then:
./gradlew generateMetamodelCode --no-configuration-cache
./gradlew generateTypeScriptTypes --no-configuration-cache
./gradlew generateTypeScriptTypes -PtsOutputDir=/custom/pathThe consumer is responsible for populating its MetamodelRegistry before
invoking the runner.
./gradlew build --no-configuration-cache
./gradlew test --no-configuration-cacheRequires JDK 21 and Gradle 9.x.
- hmf —
hmf-schemaandhmf-runtime(composed viaincludeBuild("../hmf")). - Kotlin 2.1.10, kotlin-logging 6.0.3.
- hmf — schema and runtime that this generator targets.