The dynamic model is actually a simple implementation of the object-oriented concept "everything is an object".
For example, if you see something from a distance, you don’t know what it is. When I got closer, I saw that it was a cow. When I got closer, I realized that the cow didn't move. It turned out to be a lifelike sculpture. In this case, the thing pre-exists, we can treat it as an object, understand what it is by giving it a type, and change it at any time when the type is found to be inappropriate.
The dynamic model is an object-oriented programming method that takes things as the starting point and can modify the type of objects at any time. The advantage of this programming method is that it is dynamic and can simulate a world that is changing all the time.
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.
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 "e;hello world"e"/> </actions> </Person>
If the above XML is a model, it can be used as an 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 + " say hello world";"/> </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.
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 + " say hello world";"/> </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".
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, XWorker's dynamic model engine is implemented using Java, Java is a meta-language, and JVM is a meta-system.
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.
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.
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.
In XWorker, the model can be edited through the model editor, the principle is as follows.
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.
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.
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.
The role of metamodel in dynamic model programming is to build class models.
In addition, the metamodel may be a basic structure in language and cognition. The reason for this speculation is because of the following two coincidences.
The metamodel has a certain philosophical significance, provided that it is really a basic structure in language and cognition.
Copyright © 2007-2019 XWorker.org 版权所有 沪ICP备08000575号