2.2 KModel

A KModel text is a model: objects of the types of one metamodel, with their values. A file has the extension .kmodel. The grammar of the language is src/main/antlr/io/karpfen/dsl/grammar/kmodel/Kmodel.g4 in the karpfen-dsl-tools repository.

The examples of this section are models of the lamp metamodel:

type "Bulb" "A light bulb that burns with watts, up to maxWatts" {
    prop("maxWatts", "number")
    prop("watts", "number")
    prop("lit", "boolean")
}
type "Switch" "A wall switch" {
    prop("position", "string")
    prop("presses", list("string"))
}
type "Lamp" "A lamp with a bulb, wired to a switch" {
    prop("mode", "string")
    has("bulb", "Bulb")
    knows("control", "Switch")
}
type "Room" "The room the lamp stands in" {
    has("lamp", "Lamp")
    has("switch", "Switch")
}

2.2.1 Objects#

make object "key":"Type" { ... } makes an object of a type. The braces hold its statements: prop statements give values to its properties, has statements embed objects, and knows statements refer to objects. The statements may come in any order, and an object may have none.

make object "wallSwitch":"Switch" {
    prop("position") -> "down"
}

A text holds exactly one object at its top, the root object. Every other object is embedded in it, directly or deeper. A complete model starts with an object of the root type of the metamodel, see 2.1.5 The root type. The readers accept a root object of any type of the metamodel, so the small examples of this section start with a lamp, a bulb or a switch.

2.2.2 Keys#

The first string of make object is the key of the object, its id. A knows statement names an object by its key, and a run names the object a state machine runs on by its key. No two objects of a text have the same key, embedded objects included.

The empty key "" makes an object without an id. Several objects may have the empty key, but no knows statement can refer to them.

make object "lamp":"Lamp" {
    has("bulb") -> make object "":"Bulb" {
        prop("watts") -> "0"
    }
}

2.2.3 Property values#

prop("key") -> "value" gives the property key a value. The value is always written as a string and read by the type of the property.

TypeValueExamples
stringAny text."down", ""
numberA number as Java reads a double."40", "-2.5", "1e3"
booleantrue or false, in any case."true", "FALSE"
make object "bulb":"Bulb" {
    prop("maxWatts") -> "40"
    prop("watts") -> "0"
    prop("lit") -> "false"
}

A list property gets one statement per element, in the order of the list. A single statement gives a list of one element. A list without a statement is empty.

make object "wallSwitch":"Switch" {
    prop("position") -> "down"
    prop("presses") -> "up"
    prop("presses") -> "down"
}

A property without a statement has no value in the model.

2.2.4 Embedded objects#

has("key") -> make object ... embeds a new object in the relation key. The embedded object is written in place, with its own statements.

make object "living room":"Room" {
    has("lamp") -> make object "lamp":"Lamp" {
        has("bulb") -> make object "bulb":"Bulb" {
            prop("maxWatts") -> "40"
        }
    }
    has("switch") -> make object "wallSwitch":"Switch" {
    }
}

The embedded object should have the type the relation declares. A list relation gets one has statement per object.

2.2.5 References#

knows("key") -> "id" refers to the object whose key is id. The object may stand anywhere in the model, before or after the statement, and must have the type the relation declares.

make object "living room":"Room" {
    has("lamp") -> make object "lamp":"Lamp" {
        knows("control") -> "wallSwitch"
    }
    has("switch") -> make object "wallSwitch":"Switch" {
    }
}

A list relation gets one knows statement per object it refers to. This metamodel has list relations of both kinds:

type "Lamp" "A lamp" {
    prop("mode", "string")
}
type "Switch" "A wall switch" {
    knows("lamps", list("Lamp"))
}
type "Room" "A room with several lamps" {
    has("lamps", list("Lamp"))
    has("switch", "Switch")
}
make object "hall":"Room" {
    has("lamps") -> make object "ceiling":"Lamp" {
    }
    has("lamps") -> make object "wall":"Lamp" {
    }
    has("switch") -> make object "hallSwitch":"Switch" {
        knows("lamps") -> "ceiling"
        knows("lamps") -> "wall"
    }
}

2.2.6 Rules of a model#

A model must keep these rules. 2.4.4 KModel lists the code of each problem.

  • Every object is of a type of the metamodel.
  • Every prop names a property of the type of its object, every has an embedding relation (has in KMeta) and every knows a referring relation (knows in KMeta).
  • A number property gets a number and a boolean property true or false.
  • A property or relation that is not a list gets at most one statement.
  • No two objects have the same key, unless the key is empty.
  • A knows names the key of an object of the model, and that object has the type of the relation.
  • An object embedded with has has the type of the relation. Another type is reported as a warning.

Source: this page on GitHub at commit 3b6029f, refreshed 16 November 2026. The whole reference as a PDF.