Understanding dynamic models

1. Introduction

    We know that the real world is constantly changing, so can a program system also change continuously like the real world at runtime? The programming theory of dynamic model is to solve this problem.

    In order to solve the problem of system dynamic changes, the dynamic model introduces the concept of things, where things here also refer to things in daily spoken language. For example, an egg, after hatching, becomes a bird. We can also describe this example as, this thing is an egg, which becomes a bird after hatching. Here, we use the concept of things (things) to connect eggs and birds.

    After introducing the concept of things, there is also the basic concept of dynamic model.

2. Basic concepts

  • Model (Thing)
    Models are structured data, and models can be used to represent various things, sometimes called models things. The structured data here refers to tree-structured data, such as XML and JSON.
     
  • Action
    The dynamic model believes that any model can be transformed into an action, and the action is equivalent to the function and method in programming, and the action can be executed. Dynamic models assume that any model can be transformed into an action.
     
  • ActionContext
    Sometimes also called variable context. Used to store variables and maintain execution state during action execution.
     
  • World
    The container for the model. Generally, the model is obtained through World. Concepts correspond to things, and the container containing various things is called the world.

3. Object-oriented

    The two concepts of things and actions are abstract. To be able to program, you need to use these two concepts to implement object-oriented programming methods.

3.1. Object

    We need to use the model to represent the object, the following is the method to use the model to represent the object.

<Person name="zhangsan" age="40">
     <actions>
         <GroovyAction name="helloWorld" code="println &quote;hello world&quote"/>
     </actions>
</Person>

    If the above XML is a model, it can be used as an object:

  • Properties of an object
    Nodes in XML can have their own attributes and child nodes, which can be regarded as attributes of objects.
     
  • Object Behavior
    In the dynamic model, each node in XML is regarded as an independent model, and the dynamic model stipulates that any model can be converted into actions for execution, so some sub-nodes of a model can be artificially specified as object behaviors.
    For example, in the above XML, the sub-nodes under the actions sub-node are specified as the behavior of the object, then the object whose name is zhangsan has a behavior named helloWorld.

3.2. Type of object

    A descriptor in a dynamic model is equivalent to a class in an object-oriented model.

    In a dynamic model, a model can use any model as its descriptor, and the model inherits the behavior of the describer.

    For example, the description of the <Person name="zhangsan.../> model below is the <thing name="Person".../> model

<?xml version='1.0' encoding='utf-8'?>

<Person name="Zhangsan" descriptors="xworker.doc.dyanmicmodel.examples.Person" age="40"
      sex="0">
     <Child name="Xiaoming" age="10" sex="0"/>
     <Child name="Lili" age="5" sex="1"/>
</Person>
<?xml version='1.0' encoding='utf-8'?>

<thing name="Person" descriptors="xworker.lang.MetaDescriptor3">
     <actions>
         <GroovyAction name="sayHello" descriptors="xworker.groovy.GroovyAction" code="println self.name + &quot; say hello world&quot;;"/>
     </actions>
     <attribute name="name"/>
     <attribute name="age"/>
     <attribute name="sex" inputtype="select">
         <value name="male" value="0"/>
         <value name="female" value="1"/>
     </attribute>
     <attribute name="description" inputtype="html"/>
     <thing name="Child" extends="xworker.doc.dyanmicmodel.examples.Person"/>
</thing>

    In the above example, the describer of zhagnsan is Person. Person can be used to describe the behavior, attributes and child nodes of zhangsan.

  • Behavior
    Person defines the sayHello behavior and Zhang San also inherits this behavior.
  • Attributes
    The <attribute nam="xxx".../> child node of Person can be used to indicate which attributes zhangsan has.
  • Child nodes
    The <thing name="xxx" .../> child node of Person can be used to indicate which child nodes zhagnsan has.

3.3. Inheritance

    In a dynamic model, a model can inherit any other model except itself, thereby inheriting the behavior of the inherited model. If the model is used as a describer, it also inherits the description of attributes and child nodes.

    Inheritance is useful for complex models and frameworks. For example, in the following model, the Child child node inherits the root node Person, so Child is also Person, and it also has the behaviors, attributes and child nodes described by Person.

<?xml version='1.0' encoding='utf-8'?>

<thing name="Person" descriptors="xworker.lang.MetaDescriptor3">
     <actions>
         <GroovyAction name="sayHello" descriptors="xworker.groovy.GroovyAction" code="println self.name + &quot; say hello world&quot;;"/>
     </actions>
     <attribute name="name"/>
     <attribute name="age"/>
     <attribute name="sex" inputtype="select">
         <value name="male" value="0"/>
         <value name="female" value="1"/>
     </attribute>
     <attribute name="description" inputtype="html"/>
     <thing name="Child" extends="xworker.doc.dyanmicmodel.examples.Person"/>
</thing>

    The extends attribute in the above XML code is used to indicate inheritance, such as: extends="xworker.doc.dyanmicmodel.examples.Person".

4. How to run the model

4.1. Dynamic model engine, metalanguage and metasystem

    The dynamic model needs to be realized by other programming languages. The realization of the dynamic model is called the dynamic model engine, and the language and system that realize the dynamic model are called meta-language and meta-system. For example, Worker's dynamic model engine is implemented using Java, Java is a meta-language, and JVM is a meta-system.

4.2. How to run the model

   The dynamic model is an object-oriented programming method. The model can be regarded as an object, and running a model can be regarded as executing the behavior of an object.

  1. In the dynamic model, the behavior of the model is also a model. To execute the behavior of the model, it needs to be converted into an action for execution.
  2. When the model is converted into an action execution, the behavior whose name is run is executed. To execute the behavior of the model named run, go back to step 1.
  3. The above is an iterative process. To terminate the iteration, some models need to be executed directly by the engine instead of continuing to recursively execute the behavior named run.

    For example, the dynamic model engine of XWorker is implemented in Java, and the model whose class name is JavaAction is directly executed by the engine. Among them, the function of JavaAction is to execute a certain Java method, or to compile and execute the code given in the model.

5. Metamodel

    The metamodel is a data structure accidentally discovered in programming. It can solve the problem of model editing and also has a little philosophical meaning.

5.1. The principle of editing models

    In XWorker, the model can be edited through the model editor, the principle is as follows.

  1. In object-oriented, objects can be instantiated through classes. In the model editor, classes are used to dynamically generate an edit form, and users can edit a model through the form.
  2. Take the class as an object, and you can edit it in the model editor through its (as an object) class.

    The above is an iterative process. To terminate the iteration, an object is needed, which is its own class. It can edit itself or any other class by itself. This object is the metamodel.

5.2. Metamodel discovery process

    In the dynamic model, objects are represented by tree-structured data. For example, XML can be used to represent an object, so an object can be represented as XML.

    When using XML to represent an object, we define the structure of the object in the following way.

  • Use <thing name="xxx"/> to represent an XML node.
  • Use <attribute name="xxx"/> to represent an attribute in an XML node.

    For example, the following XML.

<Person name="Zhangsan" age="40">
     <Child name="Xiaoming" age="10"/>
</Person>

    The structure of this XML is as follows.

<thing name="Person">
     <attribute name="name"/>
     <attribute name="age"/>
     <thing name="Child">
         <attribute name="name"/>
         <attribute name="age"/>
     </thing>
</thing>

    Through the above definition of XML structure, then we can calculate any XML structure, we can find that the calculated result is still XML, and then we can iterate the calculation, and finally find that it can be iterated to an XML, this The XML is as follows.

<thing name="thing"">
     <attribute name="name"/>
     <thing name="attribute">
         <attribute name="name"/>
     </thing>
     <thing name="thing">
         <attribute name="name/>
         <thing name="attribute">
             <attribute name="name"/>
         </thing>
         <thing name="thing">
         ....
         </thing>
     </thing>
</thing>

    This is an XML with the same nodes at each layer and infinite layers.

    Since this XML has infinite layers, it cannot be expressed directly. In order to be able to express it, we introduce the concept of inheritance, so that it can be simplified into the following XML.

<thing name="thing"">
     <attribute name="name"/>
     <attribute name="extends"/>
     <thing name="attribute">
         <attribute name="name"/>
     </thing>
     <thing name="thing" extends="_root"/>
</thing>

    Here we add an extends attribute, in <thing name="thing" extends="_root"/> child node, extends="_root" attribute indicates this node Inherit the root node, so that this child node also has child nodes under the root node, so logically it is an XML with infinite layers.    

    Because it is a structure of any XML structure, in an XML editor that uses the structure of XML to edit XML, it can be used to directly or indirectly edit any other XML, so it can As the first model in the system, the metamodel.

5.3. Philosophical significance of metamodel

    The nature of the meta-model coincides with Lao Tzu's "the beginning of the nameless world, the mother of all things named" and "Tao begets one, one begets two, two begets three, and three begets all things", because the meta-model can be used as a system The first model in , while using the metamodel to define classes is mainly about naming. In addition, there is a coincidence between the metamodel and the nature of "God", because the metamodel is the first model in the system, and other models can be edited (created) directly or indirectly through it.

    The above coincidence shows that the metamodel has a certain philosophical significance, but we do not have the ability to prove it. We hope that professional philosophers related to language and cognition can care about the metamodel.

    We now believe that metamodel may be a basic structure in language and cognition, because we believe that the structure of language and cognitive methods will affect worldview, and the above coincidence is related to worldview, so metamodel Probably some basic structure of language and cognition.

6. Conclusion

    As mentioned in the introduction, the theory of dynamic models aims to solve the problem of dynamic changes in the system, but the dynamic model does not completely solve this problem. Because the dynamic model is an abstract programming method. To really become a practical programming method, it needs to be implemented in other programming languages.

    The dynamic model mainly solves the situation that the objects in the system are mutable, and it is realized by introducing the concept of "things". With the concept of things, the role of objects can be changed at any time. Since we often use roles to describe an object, when the role of the object changes, what the object is also changes, although the object itself is still that object. And when the objects in a system are mutable, then theoretically the system is also mutable.

    The metamodel in the dynamic model provides a way to edit the model, making it possible to edit various models while the system is running. In addition, the metamodel may have some philosophical significance.

    Although the dynamic model is a bit abstract, it is relatively simple, so it is relatively free, and can even be used to develop new programming methods. For details, please refer to XWorker, which is an example of a dynamic model.

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号