Skip to content

Metamodelling with ChatGPT

I thought I'd try ChatGPT's metamodelling capabilities by prompting it to create a small Ecore metamodel for component-based systems (i.e. components/connectors/ports) in the Emfatic textual notation. Below are my prompts and ChatGPT's responses. I have included the code (metamodel) responses only as ChatGPT's commentary wasn't particularly interesting.

TLDR

With a few prompts, ChatGPT was able to produce a syntactically-correct metamodel in Emfatic.

Please write the metamodel of a domain-specific language for modelling component-based systems, in the Emfatic notation.

Although ChatGPT doesn't seem to be aware of the syntax of Emfatic, it produces a very sensible minimal metamodel in an object-oriented-like language.

Package componentBasedSystem;

Class Component
    Attribute name: String;
    Attribute type: String;
    Attribute description: String;
    Reference parent: Component;
    Reference children: [Component];

Class Connection
    Attribute name: String;
    Attribute type: String;
    Attribute description: String;
    Reference source: Component;
    Reference target: Component;

Adding Ports

How about extending the metamodel with support for ports?

Extend the metamodel so that it also supports component ports.

GhatGPT adds a new Port class to the metamodel, and redirects connections from components to ports (!).

Class Component
    Attribute name: String;
    Attribute type: String;
    Attribute description: String;
    Reference parent: Component;
    Reference children: [Component];
    Reference ports: [Port];

Class Connection
    Attribute name: String;
    Attribute type: String;
    Attribute description: String;
    Reference source: Port;
    Reference target: Port;

Class Port
    Attribute name: String;
    Attribute type: String;
    Attribute description: String;
    Reference component: Component;

Fixing the Syntax

The structure of the metamodel looks OK but the syntax isn't quite right.

Use "attr" instead of "Attribute", "ref" instead of "Reference" and "class" instead of "Class". Also, instead of "[X]", use "X[*]".

ChatGPT fixes the metamodel accordinly.

Package componentBasedSystem;

class Component
    attr name: String;
    attr type: String;
    attr description: String;
    ref parent: Component;
    ref children: Component[*];
    ref ports: Port[*];

class Connection
    attr name: String;
    attr type: String;
    attr description: String;
    ref source: Port;
    ref target: Port;

class Port
    attr name: String;
    attr type: String;
    attr description: String;
    ref component: Component;

The syntax is not quite right yet and needs a bit more tweaking.

Use "package" instead of "Package". Use "attr String name" instead of "attr name: String". Also apply to all other fields. Use curly braces instead of indentation.

ChatGPT happily corrects these issues.

package componentBasedSystem {
  class Component {
    attr String name;
    attr String type;
    attr String description;
    ref Component parent;
    ref Component[*] children;
    ref Port[*] ports;
  }

  class Connection {
    attr String name;
    attr String type;
    attr String description;
    ref Port source;
    ref Port target;
  }

  class Port {
    attr String name;
    attr String type;
    attr String description;
    ref Component component;
  }
}

We're getting there but in Emfatic package declarations don't have curly braces.

Package declarations don't need curly braces.

ChatGPT happily removes the package declaration braces.

package componentBasedSystem
  class Component {
    attr String name;
    attr String type;
    attr String description;
    ref Component parent;
    ref Component[*] children;
    ref Port[*] ports;
  }

  class Connection {
    attr String name;
    attr String type;
    attr String description;
    ref Port source;
    ref Port target;
  }

  class Port {
    attr String name;
    attr String type;
    attr String description;
    ref Component component;
  }

Package delcarations end with a semicolon in Emfatic.

But they need a semicolon after the package name.

No problem. Semicolon duly added.

package componentBasedSystem;
  class Component {
    attr String name;
    attr String type;
    attr String description;
    ref Component parent;
    ref Component[*] children;
    ref Port[*] ports;
  }

  class Connection {
    attr String name;
    attr String type;
    attr String description;
    ref Port source;
    ref Port target;
  }

  class Port {
    attr String name;
    attr String type;
    attr String description;
    ref Component component;
  }

How about we get rid of the extra indentation for classes?

Outdent class definitions.

Absolutely, and ChatGPT produces a first version of the metamodel that the Emfatic parser can actually parse.

package componentBasedSystem;

class Component {
  attr String name;
  attr String type;
  attr String description;
  ref Component parent;
  ref Component[*] children;
  ref Port[*] ports;
}

class Connection {
  attr String name;
  attr String type;
  attr String description;
  ref Port source;
  ref Port target;
}

class Port {
  attr String name;
  attr String type;
  attr String description;
  ref Component component;
}

Adding a PortType Enumeration

In the version of the metamodel so far, ports can have any type. How about we constrain this a bit more?

Only three port types are supported: Integer, String and Boolean. Update the metamodel accordingly.

ChatGPT adds a new PortType enumeration (it even gets the Emfatic keyword for enumerations right) with three literals, and updates Port.type accordingly.

package componentBasedSystem;

class Component {
  attr String name;
  attr String type;
  attr String description;
  ref Component parent;
  ref Component[*] children;
  ref Port[*] ports;
}

class Connection {
  attr String name;
  attr String type;
  attr String description;
  ref Port source;
  ref Port target;
}

class Port {
  attr String name;
  attr PortType type;
  attr String description;
  ref Component component;
}

enum PortType {
  Integer,
  String,
  Boolean
}

As it's not aware of the syntax rules of Emfatic, it needs a bit more guidance to get the enumeration syntax right.

All enumeration literals should end with a semicolon.

Issue fixed and a syntactically-valid metamodel is produced as a result.

package componentBasedSystem;

class Component {
  attr String name;
  attr String type;
  attr String description;
  ref Component parent;
  ref Component[*] children;
  ref Port[*] ports;
}

class Connection {
  attr String name;
  attr String type;
  attr String description;
  ref Port source;
  ref Port target;
}

class Port {
  attr String name;
  attr PortType type;
  attr String description;
  ref Component component;
}

enum PortType {
  Integer;
  String;
  Boolean;
}

Next Steps

  • Every time I ask it to create a metamodel, ChatGPT comes up with a slightly different initial syntax (e.g. it uses the entity keyword instead of class, or List<Component> instead of[Component]). Perhaps a more specific initial prompt can help with this.
  • Despite a few attempts, I haven't been able to guide ChatGPT to enrich the metamodel with more advanced features such as opposite or containment references yet.