Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

HMF Codegen — Hivecore Model Framework

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").

What gets generated

Kotlin (MetamodelCodeGenerator)

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.

TypeScript (TypeScriptCodeGenerator)

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 union

Conventions:

  • 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 extends references are always valid.

Usage

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/path

The consumer is responsible for populating its MetamodelRegistry before invoking the runner.

Building

./gradlew build --no-configuration-cache
./gradlew test --no-configuration-cache

Requires JDK 21 and Gradle 9.x.

Dependencies

  • hmf — hmf-schema and hmf-runtime (composed via includeBuild("../hmf")).
  • Kotlin 2.1.10, kotlin-logging 6.0.3.

Related projects

  • hmf — schema and runtime that this generator targets.

License

See LICENSE and NOTICE.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages