1.Major Classes of X-Meta Engines

    The X-Meta engine is an implementation of the dynamic model. Several main Java classes are listed below.

2.Thing

2.1.org.xmeta.Thing

    In the X-Meta engine, the model is called Thing, and the corresponding Java class is org.xmeta.Thing. Usually in the X-Meta engine models,  things are the same thing here.

2.2.The Structure of Things

    A model is made up of attributes and sub-nodes, so a thing is made up of attributes and sub-things.

2.2.1.Attribute

    The attributes of a thing are stored in the form of key-value, where the name of the attribute is a string and the value of the attribute can be any Java object.

//--------Related operations of attributes-----------
//Set attribute, where set and put methods work the same way
thing.set(String name, Object value);
thing.put(String name, Object value);

//get attribute
Object value = thing.get(String name)
//Getting attributes: A method with type conversion
String value =thing.getString(String name);
int value = thing.getInt(String name);
int value = thing.getInt(String name, int defualtValue);
double value = thing.getDouble(String name);
double value = thing.getDouble(String name, double defualtValue);
//Other similar approaches
xxxx value = thing.getXxx(String name);
xxxx value = thing.getXxx(String name, xxxx, defualtValue);

2.2.2.Child Thing

    Child thing is a child node of a thing, stored in a List.

//--------Relevant Operation of Subthings-----------
//Add child nodes
thing.addChild(Thing child);
thing.addChild(Thing child, int index);

//Get a list of all child nodes
List<Thing> childs = thing.getChilds();
//Get the corresponding list of child nodes according to the name of the object
List<Thing> childs = thing.getChilds(String thingName);

2.3.The Behavior of Things

    Models can also be transformed into actions, that is to say, everything can be transformed into actions, and actions are executable procedures, so certain sub-things of a thing can be regarded as its actions.

    In the X-Meta engine, we agree that the first sub-object of a thing named actions is the behavior definition node of the thing, and the first sub-object named actions is the concrete behavior.

    What's the name of the thing? See the description of the thing below, and how the action of the thing is carried out. See the action below.

2.4.Description of things

    What is a thing? What is its structure? What kind of behavior does it have? It can be explained by descriptive method. Even if one thing is used to explain another thing, then if thing A is used to explain thing B, then thing A is also called the descriptor of thing B.

    The descriptive relationship between things is stored in an attribute of things called descriptors. For example, if thing A is the descriptor of thing B, then:

Thing a = new Thing();
Thing b = new Thing();

//Setting thing a as the descriptor of thing b, where "a" is the path of thing a
b.put("descriptors", "a");

//Get the first descriptor
Thng descritpor = b.getDescriptor();
//Get all descriptors defined by themselves
 List<Thing> descriptors = b.getDescriptors();
//Get all descriptors, including the inheritance of descriptors
List<Thing> descriptors = b.getAllDescriptors();

    It should be noted here that the object model defines the descriptive relationship between things, but does not define a specific interpretation method, which means that you can implement it according to specific needs.

2.4.1.The thing name and name of thing

    The value of a thing's name attribute is its name, and the name of the descriptor of a thing is its thing name.

    In this case, the object name corresponds to the class name of the object. For example, the object A is the descriptor of the object B, and the object name of the object B is A.

2.4.2.Descriptive behavior

    In the dynamic model, the descriptor can describe the behavior. For example, if thing A is the descriptor of thing B, then part or all of the behavior of thing B can be known by thing A.

    The concrete realization method is that things inherit the behavior of their descriptors. For example, thing A is the descriptor of thing B. If thing A has an action named sayHello, then thing B also has an action named sayHello. Although thing B itself does not define an action named sayHello, sayHello behavior is inherited from thing A.

2.4.3.Describing attributes and sub-things

    In dynamic models, descriptors can describe attributes and sub-things. For example, if thing A is the descriptor of thing B, then part or all of the attributes and sub-things of thing B can be known by thing A.

2.4.4.Descriptor's limitations

    A thing can regard any thing as its descriptor, even its own, the number is unlimited, and the descriptor of a thing can be modified at any time.

    If there are multiple descriptors of a thing, their paths are separated by English commas in the descriptors attribute, and the order of inheritance of actions is the order in which the descriptors attribute appears.

2.5.Inheritance between things.

2.5.1.Extend

    In the dynamic model, a thing can inherit other things besides itself, thus possessing the behavior of the inherited thing. For example, if thing B inherits thing A, then thing B has the behavior of thing A.

2.5.2.Representation of inheritance

    The inheritance relationship between things is expressed by attribute extends. If thing B inherits thing A, then the path of thing A is set in the attribute of thing B.

Thing a = new Thing();
Thing b = new Thing();

//Set things B to inherit things a, where "a" is the path of things a
b.put("extends", "a");

//Get an inheritance list of things defined by themselves
List<Thing> extendThings = b.getExtends();
//Get all inheritance lists of things, including inheritance of inherited things
List<Thing> extendThings = b.getAllExtends();

2.5.3.Inheritance between descriptors

    If a thing is a descriptor, then it plays the role of describing the structure (attributes and sub-things) and behavior of other things, and if it also inherits other things, it also inherits the structure and behavior described by the inherited things.

    For example, Person is a model, which inherits the Animal model, while Tom is an instance of Person. If the Animal defines an attribute named age, then Tom also has the attribute of age, where Person inherits the definition of the age attribute of Animal.   

2.5.4.Restrictions on inheritance

    A thing can inherit many things, the number is not limited, because inheriting oneself is meaningless, so one thing can not inherit oneself. If one thing inherits many things, then it is separated by English commas in the extends attribute, and the order of inheritance behavior is priority according to the order appearing in extends.

2.6The role of descriptor and inheritance.

    Through description and inheritance, almost all the concepts of object-oriented can be realized, but it can be seen that there are no concepts of class and object in the matter model.

3.Action

    Thing model can also be run, it is achieved through action.

3.1Anything can be transformed into actions, and actions are executable procedures

    In Thing, there is a getAction () method for transforming things into actions, which can be executed.

//Conversion from Things to Actions
Action action = thing.getAction();

//Actions are executable, where ActionContext is the variable context
action.run(ActionContext actionContext)

    Everything here has a getAction method, but not everything transformed into action execution is meaningful, action execution follows some rules.

3.2org.xmeta.Action

    The Java class corresponding to the action is org.xmeta.Action.

3.3The Principle of Action Execution

    The principle of action execution is relatively simple, that is, if the name of the thing corresponding to the action is JavaAction, then Java code is considered to be executed, otherwise the run behavior of the corresponding thing is executed.

    The corresponding code snippet in the X-Meta engine is:

if(useOtherAction){
	//If an action refers to another action, then execute the cited action
	Bindings callerBindings = context.getScope(context.getScopesSize() - 2);
	try{
		context.push(callerBindings);
		result = outerAction.run(context, parameters, isSubAction);
	}finally{
		context.pop();
	}
}else if(isJava){
	//If the action is native, that is, Java action, then it is invoked through the reflection mechanism. 
if(actionClass != null){
	if(method != null){
		result = method.invoke(actionClass, new Object[]{context});
	}else{
		logger.info("Java action method is null, " + getThing().getMetadata().getPath());
		}
	}
}else{
	//If it weren't Java, then the run behavior of the current action object could be either self-defined or its descriptor-defined.
//This approach is important to support other languages, such as Groovy, JavaScript and other scripting languages.
if(isSelfInterpretationType){
	//The action of self type, when an action is a sub-action, and the variable self needs to be itself, then it needs to be defined as this type.
	if(attributeTemplate){
		//Attribute template, the action attributes can be set to the FreeMarker template, when executed, using the template to generate real attribute values
		Thing fthing = (Thing) thing.run("processAttributeTemplate", context, (Map<String, Object>) null, isSubAction, true);
		if(fthing != null){
			fthing.run("run", context, (Map<String, Object>) null, isSubAction, true);
		}
	}else{
		result = thing.run("run", context, (Map<String, Object>) null, isSubAction, true);
	}
}else{
	//Get something's own behavior run, and then execute it
	Thing actionThing = thing.getActionThing("run");
	if(actionThing != null){
		Action ac = actionThing.getAction();
		result = ac.run(context, null, caller, isSubAction);
	}
}

3.4ActionContext

    The Java class corresponding to the variable context is org.xmeta.ActionContext. The purpose of the variable context is variables, including temporary variables and parameters in the course of action execution. The X-Meta engine is equivalent to a virtual machine, which is realized by action and variable context.

    Commonly used methods for variable context:

//Declare variable context
ActionContext actionContext = new ActionContext();

//Setting and reading variables
actionContext.put(String key,  Object value);
Object value = actionContext.get(String key);

//Stack-pressing and stack-out operations
Bindings bindings = actionContext.push();
actionContext.pop();

//Get the relative global variable range
Bindings globalScope = actionContext.getScope(0);
//Get the current local variable range
Bindings localScope = actionContext.getScope();

//Setting and reading variables for Bindings
bindings.put(String key, Object value);
Object value = bindings.get(Stirng key);

//Get thread state
int status = actionContext.getStatus();

3.4.1Calling effect of functions and methods

    ActionContext and action can be used to achieve the effect of calling functions and methods, that is, when an action is executed, a Bindings can be pushed into a stack, then parameters and temporary variables can be stored in Bindings, and then the stack pops up when the action is completed.

3.4.2Implementation of Control Statement Model

    The control statement can be realized by status of ActionContext. The optional values of status are RUNNING, RETURN, CANCEL, BREAK, CONTINUE and EXCEPTION. Using status, the return, break, continue and exception throwing and handling in programming language can be realized by using the object model.

3.5How to call an action

    There are two ways to call action to run, one is to run directly, the other is to be executed as behaviour.

3.5.1Execute the behaviour of thing

    From the definition of things, we can know that a thing's behavior is a child of its node, or inherited from its descriptor and inheritor, in which the behavior of things is defined by things, so we can get the definition of things by things, and then execute it.

    The behavior of things is performed through the doAction method, such as:

//Execute the behaviour xxx
thing.doAction("xxx" , actionContext);

    The implementation core code of doAction:

Thing actionThing = getActionThing(name);
Action action = actionThing.getAction();

Bindings bindings = context.push(null);
bindings.caller = this;
bindings.put("self", this);
		
try{
	return action.run(context);
}finally{
	context.pop();
}

    Here you can see that in doAction, the object model is placed in the context of the variable with the name of the self variable, so when the action is executed, you can know who is calling it.

4.World

    The container of things is the world. In the X-Meta engine, the world is used to manage things. Everything can be obtained through the World.

4.1org.xmeta.World

    The corresponding Java class in the world is org.xmeta.World, which is a single instance of the World object, World = World. getInstance ().

4.2Grouping of things

    To facilitate the management of classes, things are grouped according to ThingManager and Category.

4.2.1ThingManager

    The Java interface corresponding to the ThingManager is org.xmeta.ThingManager. ThingManager is an interface that stores things in different media, such as file system, Jar and class path, database and memory. Each medium corresponds to its own ThingManager implementation.

    Since the Thing Manager is the first level grouping of things, the Thing Manager has an additional function that can be used as a project.

4.2.2Category

    The Java corresponding to the directory interface is org.xmeta.Category. The directory is the specific grouping under the Things Manager. For example, the file directory corresponding to the directory under the file system can put things in different file directories to distinguish.

4.3Path

    The world is unique (single instance), and things are unique. Everything is unique, so it can be accessed through its path.

4.3.1Path Rules of Things

    The path of things is <directory>+the name of things, such as xworker.lang.MetaThing, which is saved in the file system, xworker.lang is the relative directory xworker/lang, and the encoding type of MetaThing+is the file name, such as MetaThing.xer.txt or MetaThing.xml.

4.3.2Path Rules of Attributes and Subthings

    The sub-path rules are as follows:

/                :Separator for child things.
@xxx             :Find sub-things with ID XXX
@?xxx            :Find by comparing the ID or name attributes of the child.
@n               :N is a number, if no sub-things identified as the corresponding number are found, then look for the nth sub-things.
xxx@             :Look for a list of sub-items named xxx, if no empty list is returned, not null.
xxx@n            :Find a child of the nth thing named xxx, and return it first if it is identified as n.     
#xxx             :Find an attribute named XXX。
$n                :The same @n is used to distinguish @xx, $n is only used to find the nth child.

4.3.3Examples of paths

World world = World.getInstance();

//Get things
Thing thing = world.getThing("xworker.lang.MetaThing");

//Getting Subthings Directly
Thing child = world.getThing("xworker.lang.MetaThing/@thing");

//get attribute
Object value = thing.get("#name");

//Get the first child named thing
Thing child = thing.getThing("thing@0");

 

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