Summary of the invention
The purpose of this invention is to provide a kind of method, Apparatus and system that the TTCN-3 script is converted to the Java source code file, by the present invention, can guarantee to change the semanteme regulation that the post code semanteme meets standard TTCN-3 fully, further the Java source code after the conversion can directly be compiled as bytecode by the Java compiler.
The invention discloses the method that the TTCN-3 script is converted to the Java source code, comprising:
Make up the translater of TTCN-3 script;
Described translater is translated described TTCN-3 script according to predefined translation rule, convert the Java source code to;
In conjunction with described translation rule, make up the required support storehouse of operation Java source code; The foundation class that described support storehouse refers in realizing translating;
Dispose the execution entrance of Java source code, obtain the executable file of Java source code.
Preferably, described TTCN-3 script is translated according to predefined translation rule, converts the Java source code to and comprises the TTCN-3 script is carried out lexical analysis and grammatical analysis that described lexical analysis and grammatical analysis further may further comprise the steps:
The mistake of morphology and grammer is analyzed morphology and syntactic structure in verification and the described TTCN-3 script of correction, the generative grammar tree;
Syntax tree is scanned, carry out semantic analysis;
Scanning generates corresponding Java source code through the syntax tree of semantic analysis.
Preferably, described translater further comprises step: the symbol in the record TTCN-3 script file code provides retrieval, the introducing operation of symbol table.
In addition, described semantic analysis comprises carries out semantic test to the code in the TTCN-3 script, the symbol in the code in the related described TTCN-3 script, and storage generates the needed information of Java source code, prepares for generating the Java source code.
The TTCN-3 script file is carried out lexical analysis with translater and grammatical analysis is by means of JavaCC and JJTree instrument, realizes that by writing the file that meets the BNF normal form described BNF normal form can be cut out according to demand.
Whether wherein, semantic analysis is carried out in a certain order: for the modules A of input TTCN-3 script file, at first judge A analyzed mistake; If then finish the semantic analysis to A, then check if not each the possible introducing B that exists among the A; If the analyzed mistake of B then is incorporated into all among B symbols in the symbol table of front module; If the not analyzed mistake of B is then carried out semantic analysis to B first, again all among B symbols are incorporated in the symbol table of front module; Each the possible introducing B that exists in to A detect complete after, A is being carried out semantic analysis, and sign A was for analyzing, end is to the semantic analysis of A.
The invention also discloses and a kind of the TTCN-3 script is converted to the device of Java source code, it is characterized in that, comprising:
Translater makes up module, makes up the translater of TTCN-3 script;
Translater, described translater is translated described TTCN-3 script according to predefined translation rule, convert the Java source code to;
Support the storehouse to make up module, in conjunction with described translation rule, make up the required support storehouse of operation Java source code;
Support the storehouse, the foundation class that described support storehouse refers in realizing translating;
Dispose collector, dispose the execution entrance of Java source code, obtain the executable file of Java source code.
Translater further comprises the analysis module that the TTCN-3 script is carried out lexical analysis and grammatical analysis, and described analysis module comprises:
Morphology syntax parsing module, the mistake of morphology and grammer is analyzed morphology and syntactic structure in verification and the described TTCN-3 script of correction, the generative grammar tree;
Semantic module scans syntax tree, carries out semantic analysis;
Code generation module, scanning generates corresponding Java source code through the syntax tree of semantic analysis.
Described translater further comprises the symbol table module, and the symbol in the record TTCN-3 script file code provides retrieval, the introducing operation of symbol table.
In addition, described semantic module further comprises carries out semantic test to the code in the TTCN-3 script, symbol in the code in the related described TTCN-3 script, storage generates the needed information of Java source code, the module of preparing for generating the Java source code.
Wherein, the TTCN-3 script file is carried out lexical analysis with translater and grammatical analysis is by means of JavaCC and JJTree instrument, realizes that by writing the file that meets the BNF normal form described BNF normal form can be cut out according to demand.
Whether preferably, described semantic analysis is carried out in order: for the modules A of input TTCN-3 script file, at first judge A analyzed mistake; If then finish the semantic analysis to A, then check if not each the possible introducing B that exists among the A; If the analyzed mistake of B then is incorporated into all among B symbols in the symbol table of front module; If the not analyzed mistake of B is then carried out semantic analysis to B first, again all among B symbols are incorporated in the symbol table of front module; Each the possible introducing B that exists in to A detect complete after, A is being carried out semantic analysis, and sign A was for analyzing, end is to the semantic analysis of A.
The invention also discloses another TTCN-3 script be converted to the scheme of Java source code, comprise step:
Make up the translater of TTCN-3 script;
Described translater comprises that described translater finishes TTCN-3 script file code to the conversion of syntax tree data structure; Symbol table in the record TTCN-3 script file code provides retrieval, the introducing operation of symbol table; The scanning syntax tree carries out semantic test, associated nodes and corresponding symbol; Finish the module syntax tree to the conversion of Java source code;
Make up the required support storehouse of operation Java source code; The foundation class that described support storehouse refers in realizing translating;
Dispose collector, dispose the execution entrance of Java source code, obtain the executable file of Java source code.
And disclose and a kind of the TTCN-3 script is converted to the system of Java source code, described system comprises:
Translater makes up module, makes up the translater of TTCN-3 script;
Translater;
Support the storehouse to make up module, make up the required support storehouse of operation Java source code;
Support the storehouse, the foundation class that described support storehouse refers in realizing translating;
Dispose the execution entrance of Java source code, obtain the executable file of Java source code.
Described translater further comprises:
Morphology syntax parsing module, morphology syntax parsing module is finished TTCN-3 script file code to the conversion of syntax tree data structure;
The symbol table module, the symbol table in the record TTCN-3 script file code provides retrieval, the introducing operation of symbol table;
Semantic module, the scanning syntax tree carries out semantic test, associated nodes and corresponding symbol;
Code generation module is finished the module syntax tree to the conversion of Java source code.
Can find out that the scheme flow process that the present invention provides is less, give the Java compiler processes with the intermediate code optimizing process of conventional compiler device.Simultaneously, by Java language once the compiling everywhere the operation characteristic, this method can be so that the TTCN-3 script moves under different operating system.In addition, because TTCN-3 is higher level lanquage to Java itself, therefore, it is also comparatively easy to expand new TTCN-3 grammer.When implementing, only need to make translater increase new grammer support, and in Java support storehouse, add the java class of new definition.
Embodiment
The present invention is described in further detail below in conjunction with the drawings and specific embodiments:
Fig. 1 has provided the total execution flow process that TTCN-3 script file (* .ttcn) is converted to the Java source code file of setting forth of the present invention.This process at first needs to make up the translater of a TTCN-3 script file, and this translater can be converted to the TTCN-3 script file Java source code file.
Then make up the required support storehouse of operation Java source code, the foundation class that described support storehouse refers in realizing translating compiles the Java source code that translation generates by the Java compiler, carries out the Java source code that described translation generates.
Wherein, translater is to carry out the TTCN-3 script file to the significant element of Java source code file conversion; For specifically, this translater need to comprise following function:
(1) all TTCN-3 script files is carried out lexical analysis, grammatical analysis.The Main Function of this process is some fundamental errors of getting rid of in the script file, such as conventional mistake check and correction such as misspellings, when detecting mistake, in time send error reporting in the mode of notice, make things convenient for the commissioning staff to make amendment, and or automatically revise for frequent fault; This process is finally understood the generative grammar tree.
(2) syntax tree is scanned, carry out semantic analysis.Semantic analysis process mainly is that code is carried out semantic test.In addition, the symbol of this process in also can association code, the needed information of storage generating code is for code generation process is prepared.
(3) scanning generates corresponding Java source code through the syntax tree of semantic analysis.In the specific implementation, a TTCN-3 file can be exchanged into a Java source file, and a TTCN-3 module can correspond to a java class and get on, and so just is easy to set up the corresponding relation between file and even statement.
Make up the support storehouse of Java, these Java source code file have guaranteed that the Java source code after the translation can correctly be compiled.
Make up the code after translating and support the storehouse, and dispose the execution entrance of whole Java source code, can obtain complete moved Java source code.
From a structural point, this translater has comprised with lower module:
Morphology syntax parsing module: finish TTCN-3 script file code to the conversion of syntax tree data structure;
The symbol table module: the symbol table in the record TTCN-3 script file code provides retrieval, the introducing operation of symbol table;
Semantic module: analysis module scanning syntax tree, carry out semantic test, associated nodes and corresponding symbol;
Code generation module: finish the module syntax tree to the conversion of Java source code.
Structure morphology syntax parsing module
Translater at first carries out lexical analysis and grammatical analysis to the TTCN-3 script file, by means of JavaCC and JJTree instrument, only need to write the normal form file that meets the instrument regulation, can generate corresponding morphology and the Java source code of syntax parsing.Wherein, JavaCC (Java CompilerCompiler) is a grammatical analysis maker of developing with Java.Concrete BNF normal form can be carried out certain cutting according to own demand, for the syntactic element that does not relate in those TTCN-3 script files, then it can be deleted.After normal form had been write the normal form file, instrument will generate for the nonterminal symbol in the BNF normal form corresponding node class and morphology syntax parsing device.The normal form of the TTCN-3 of normative reference, its node class formation is seen accompanying drawing 2.
In accompanying drawing 2, the Node interface is that all node classes all need the interface realized, and it provides the access method to child nodes and father node.JjtGetChil dNum () method is obtained the number of child nodes, and jjtGetChild () method can be obtained the child nodes of appointment, and jjtAccept () method is so that the Accessor Classes can access this node.
The SimpleNode class has realized Node interface enterLocation, and it is the base class of other all nodes.This method has been added getStartPosition () method in the SimpleNode class, with the position of record node in source code.Dump () method then gets on nodal information with the printing device that the mode recurrence of text outputs to appointment.
StrcutFieldDef class and RecordDef class have just been inherited the SimpleNode class, and they are exactly the node class that is generated by corresponding BNF normal form.After semantic analysis was finished, each TTCN-3 code file will be converted into one by the synthetic syntax tree of node class example set.
Behind the VISITOR option of having opened the JJTree instrument, also can generate the ParserVisitor interface, this interface is that the syntax tree visitor need to realize interface, each node class is to a visit () method in should interface.If need the access syntax tree, will realistic existing this interface.Specifically, in ensuing semantic module and code generation module, just need all the visit () methods in this interface of realization, to access all syntax tree nodes.
All node class names with and extend information as shown in table 1:
The tabulation of table 1 node class
Structure symbol table module
According to the TTCN-3 grammatical norm, static state during the semiotic function territory of TTCN-3 language, namely TTCN-3 script file code book body structure has just determined the action scope of symbol in the code.Each action scope unit comprises mutually, is downtree shape structure, and all sub-action scopes are all as seen to it for all symbols of father's action scope unit the inside.Because the semantic analysis stage need to be carried out a large amount of symbolic look-up accessing operations, therefore, symbol table needs also to guarantee that except guaranteeing the efficient search capacity symbol that finds comes from correct action scope.
In the specific implementation, this programme will be by a chain stack, the name space of maintain symbol.At a time its snapshot as shown in Figure 3, when entering a new function (as) will set up a new action scope object when entering a new semiotic function territory, and is set to current action scope.Withdrawing from before prescope or newly-built action scope, the symbol of all new addings will be organized with the form of doubly linked list, if and the new symbolic name that adds is when existing hash-collision (or of the same name), the new symbol that adds will cover former symbol.Do like this can guarantee when Hash lookup, the symbol of the always innermost layer action scope that finds.During implementation, for improving development efficiency, the Hash table that can use Java self to provide is supported class.
Class figure when Fig. 4 provides the symbol table implementation, the symbol table module is externally mutual by the single routine class of SymbolTbl.Symbol object internal maintenance a plurality of Scope objects, they are organized with the form of storehouse.Semantic module is added on the new Scope object and storehouse by newScope () method, and is set to current action scope.After this, new symbol all passes through enter () method and adds this action scope, and lookup () method is used for all symbols in the retrieval name space.When semantic module was called leave () method, when the prescope object will be released, and all symbols in its inside also discharged thereupon.
The constructing semantic analysis module
Semantic module is mainly carried out semantic test to syntax tree, needs in the checking process to come recording indexes current sign information with symbol table.Semantic module needs to realize the ParserVisitor interface as the visitor.To each node class, just need to realize with it visit () method of correspondence.
The semantic analysis order of determination module
Exist symbol to introduce (by import key word sign) mechanism in TTCN-3, this makes when front module (module) can quote symbol definition under other modules.For example defined TypeA among the ModuleA, ModuleB has introduced ModuleA, and has defined the example InsA of TypeA.In order to verify the correctness in definition of I nsA, just need to check according to the definition of TypeA.In order to guarantee that this inspection can carry out smoothly, a kind of simple method is that the module that guarantees all references has all been finished semantic analysis.That is to say the first ModuleA of scanning, and then scanning ModuleB, just can correctly introduce the symbol table of ModuleA, and the situation that symbol definition can not find can not occur.In the specific implementation, in order to address this problem, can come the determination module scanning sequency with topological sorting.Whether Fig. 5 has provided a kind of feasible algorithm flow: for input TTCNModuleA, at first judge A analyzed mistake; If then finish the semantic analysis to A, then check if not each the possible introducing B that exists among the A; If the analyzed mistake of B then is incorporated into all among B symbols in the symbol table of front module; If the not analyzed mistake of B is then carried out semantic analysis to B first, again all among B symbols are incorporated in the symbol table of front module; Each the possible introducing B that exists in to A detect complete after, A is being carried out semantic analysis, and sign A was for analyzing, end is to the semantic analysis of A.
The delay of module internal symbol is related
Similar Java language, in a module, TTCN-3 allows to quote first the type of rear statement, and for example: following definition is legal.
This backward mode of quoting can run into and can't find in symbol table by TypeA when having caused depth-first fashion scanning syntax tree.For fear of this situation, this method has been declared IInstance interface (with reference to figure 3), and this node has been inherited the ISymbolized interface, for mainly be the node of above-mentioned containing type definition.When scanning undefined symbol for the first time, this method first step only checks the uniqueness of this symbolic name, then puts it into one and does not finish in the formation.Scanning behind the syntax tree of front module, again this IInstance example of not finishing the formation the inside is being carried out the second step association process.
Structure code translation module
Code generation module is responsible for generating a Java source code file for each syntax tree after semantic analysis finishes.Be similar to semantic analysis, the code translation module need to realize the ParserVisitor interface.The meeting of Java source code and the support storehouse that generate by translation module combine, and form final compiling result.The translation of code is according to following rule:
Type definition: for the type definition node, generate a new class after the translation, such has been inherited and has supported class corresponding in the storehouse.This category node comprises: TTCN3Module, ModuleControlPart, SubTypeDef, RecordDef, RecordOfDef, EnumDef, SignatureDef, ComponentDef, PortDef, TestCaseDef, AltstepDef (as shown in table 1).The class name that generates is identical with type name in the TTCN-3 code.Because each TTCN-3 module is comprised of the above-mentioned type definition, the code after the translation is combined into by these classes.Comprise the node of executable statement node for child node, comprise TestCaseDef, AltstepDef, FunctionDefPart, ModuleControlPart, they all comprise executable code, therefore, the class after the translation also has corresponding executing method run ().
Built-in method: for the built-in method of TTCN-3, comprise: create method, start method, stop method, execute method, connect method, map method, log method, obtain the MTC assembly method, obtain System assembly method and some built-in functions (such as ispresent () function), this method can realize in advance in the T3RTSystem in operation support storehouse class that this class is a routine class of overall addressable list.If comprise the call statement of these methods in the TTCN-3 code, translater is translated as this statement the Java statement that calls method of the same name in the T3RTSystem class.
Variable constant definition: in statement block, often can comprise the local variable definition.When entering action scope, if run into local variable statement statement, translater can be an addVariable () call statement with this definition translation.In this way, this local variable will join in the action scope at its place when code finally moves.In order to safeguard action scope, in each statement block ending, translater also will generate for each local variable the call statement of delVariable () method, before adds this action scope ground local variable to empty.For the constant definition in the statement block, translater adopts identical strategy.The organizational form of the local variable of concrete action scope is responsible for by the operation support storehouse, and translater is not responsible for this.
Data value: for the definition of constant value, comprise shaping value, floating point values, Boolean and decision content, this method will realize corresponding with it class in advance in the operation support storehouse, be respectively: IntegerType, FloatType, BooleanType, VerdictType, these classes all are the bonding examples, obtain such instance objects by getInstance ().When running into a constant value, translater will be translated into form and be<Class-Name〉statement of .getInstance () .newInstance (<InitValue 〉), wherein<and Class-Name〉be corresponding class name,<InitValue〉be initial value.
Control statement: control statement mainly comprises conditional statement (by if key word sign), loop statement (by for, while key word sign), and after running into these statements, translater can be translated among the Java fully corresponding with it control statement.Also have some key words, such as the break key word, the return key word also is translated into corresponding Java key word.
Optional step statement: for optional step statement block, translater is translated into do-while loop statement among the Java with it, and whether circulation finishes by checking that snapshot state judges.Each conditional statement in the optional step statement block is translated into the if statement in the Java language.The interpretative system that repeat key word in the optional step statement is corresponding is throw exception, and this can guarantee once to jump out multilayer circulation.The structure in optional step is as shown in the table.
The optional step translation of table 2 table of comparisons
The port method call: when running into call, getcall, reply, during the getreply method call, translater is translated into the method call of corresponding Java, these methods all in advance in the class in operation support storehouse structure good.
The operation support storehouse
Code after the translation will combine compiling with the operation support storehouse, the foundation class that refers in the operation support storehouse realization translation scheme, and finish the execution of the code that translation is generated and the work such as record of execution result.
For entity elements such as the module in the TTCN-3 language (by module key word sign), control (by control key word sign), test case (by testcase key word sign), function (by function key word sign), optional steps (by altstep key word sign), a base class of finishing this element basic function is realized for each element in the operation support storehouse.And to the process of the translation process of the concrete element individuality concrete subclass that to be exactly one of structure expand base class.As shown in Figure 6, there is following entity elements to support class in the operation support storehouse.
The AbstractModule class is the base class of all block elements translation results among the TTCN-3, has realized the management to elements such as the constant that defines in the module, control, behavior (being test case, function and optional step), function signature (by signature key word sign);
The AbstractControl class is the base class of control element translation result among the TTCN-3, has realized the functions such as control and measuring use-case execution;
The AbstractTestcase class is the base class of control element translation result among the TTCN-3, has realized the management to test case execution result and test suite (component);
The AbstractFunction class is the base class of functional element translation result among the TTCN-3, has realized the management to the variable in the function, constant and rreturn value;
The AbstractAltstep class is the base class of optional step element translation result among the TTCN-3, has realized the management to the variable in the optional step, constant;
Control, test case, function, optional step all are the bodies carried out among the TTCN-3, and all subclasses of expanding them all must realize run () method according to operational semantics.
In the TTCN-3 language, defined some basic data types, in addition, the user can be according to actual needs to basic data type expanded, constraint or instantiation, define the user data type.All data types are divided into several large classes (comprising shaping, floating type, Boolean type, decision content type, character string type, recordable type, tabulation type etc.).Each data type no matter be fundamental type or user defined type, all belongs to certain large class uniquely.In the operation support storehouse, set up a base class for the large class of each data type, they are respectively IntegerType, FloatType, BooleanType, VerdictType, CharstringType, RecordType, RecordOfType.Each base class is expanded the class from TypeImpl, and the TypeImpl class has realized the Type interface.All user defined types all are translated into the subclass to the base class of large class under it, realize expansion or constraint to base class.
In TTCN-3, all data values all belong to a specific data type, referring to Fig. 7 and Fig. 8, in translation scheme corresponding to an object of expanding the ValueImpl class.The ValueImpl class has realized the Value interface, has comprised concrete data message.The ValueImpl class also has a member territory of realizing the Type interface, and this member is object corresponding to data type under the ValueImpl class.Call the getType () method of Value interface and will return this member.Correspondingly, individual newInstance () method is arranged in the Type interface, call the data value object that the method will create the data type of a correspondence, be i.e. an example of realizing the Value interface.And belong to a large class realization all subclasses of Type interface, even the concrete data type of its representative is different, by call realization that its newInstance () method creates the example of Value interface all be the object of same class.Such as, the IntegerValueImpl class has been expanded the ValueImpl class and has been realized the IntegerValue interface, representative belongs to the data value of the data type of the large class of shaping, calls the getType () method of IntegerValueImpl class, will return the object of an IntegerType class; And call the newInstance () method of IntegerType and subclass thereof, will return the object of an IntegerValueImpl class.Corresponding in addition FloatValueImpl, BooleanValueImpl, VerdictValueImpl, CharstringValueImpl, the classes such as RecordValueImpl, RecordOfImpl.
Service data type and data value are come according to the pattern of abstract factory in the operation support storehouse, set up a data value and must use the newInstance () method of Type example corresponding to its affiliated data type to create, guaranteed type matching and compatible feasibility.
Assembly (by component key word sign) is the core among the TTCN-3, has represented the entity of carrying out the TTCN-3 semanteme.All test cases, function, optional step all carry out in specific assembly, and each assembly is exactly the clue of a semantic execution.Mode by message or the invocation of procedure between the assembly realizes communication.The operation support storehouse represents an assembly with the Component class, and the Component class is expanded from the ValueImpl class, and namely Component is a special data value.In the translation scheme, the definition of each assembly is translated into a subclass of ComponentType class, and the operation that creates assembly then realizes by the newInstance () method of calling corresponding ComponentType object.The Component class is being managed port, variable, constant and the timer that defines in assembly.
The operation support storehouse adopts the mode of multithreading to support the parallel work-flow of a plurality of assemblies in the testing engineering, the territory of containing a Thread type in the Component class, be used for the semantic operation that executive module need to carry out, specify and carry out the semantic operation that to carry out by calling execute () method.The Component class is also being safeguarded a communication queue, under two principles of first in first out and Data Matching, and the communication between the supporting assembly.The operation support Cooley safeguards that with the thread synchronization mechanism of Java language inter-module shares data's consistency.
The T3RTSystem class adopts single routine pattern to realize, is responsible for the execution of the whole testing engineering of control, and collects the execution result of all test cases, safeguards execution journal information and carries out error handling processing.By the configuration of property file, the measurand adapter (SUT Adapter) that uses in can the nominative testing system, the execution starting point of coder/decoder (Coder/Decoder) and generating code.Other classes are carried out when mutual in code that translation generates and operation support storehouse, and the T3RTSystem class also plays the intermediate axle beam action, thereby as much as possible to the specific implementation in the hiding operation support storehouse of generating code.
Need to prove, the contents such as the information interaction between said apparatus and intrasystem each unit, implementation since with the inventive method embodiment based on same design, particular content can referring to the narration among the inventive method embodiment, repeat no more herein.
Used specific case herein principle of the present invention and embodiment are set forth, the explanation of above embodiment just is used for helping to understand method of the present invention and core concept thereof; Simultaneously, for one of ordinary skill in the art, according to thought of the present invention, all will change in specific embodiments and applications, in sum, this description should not be construed as limitation of the present invention.