Summary of the invention
The object of this invention is to provide a kind of method, Apparatus and system TTCN-3 script being converted to Java source code file, pass through the present invention, can ensure to change the semantic semanteme regulation meeting standard TTCN-3 completely of post code, the Java source code further after conversion directly can be compiled as bytecode by Java compiler.
The invention discloses method TTCN-3 script being converted to Java source code, comprising:
Build the translater of TTCN-3 script;
Described TTCN-3 script is translated according to predefined translation rule by described translater, converts Java source code to;
In conjunction with described translation rule, build the Support Library run needed for Java source code; Described Support Library realizes the foundation class referred in translation;
The execution entrance of configuration Java source code, obtains the executable file of Java source code.
Preferably, described TTCN-3 script is translated according to predefined translation rule, converts Java source code to and comprises and carry out lexical analysis and grammatical analysis to TTCN-3 script, described lexical analysis and grammatical analysis further comprising the steps:
The mistake of morphology and grammer in TTCN-3 script described in School Affairs correction, analyzes morphology and syntactic structure, and generative grammar is set;
Syntax tree is scanned, carries out semantic analysis;
Be scanned across the syntax tree of semantic analysis, generate corresponding Java source code.
Preferably, described translater comprises step further: the symbol in record TTCN-3 script file code, provides the retrieval of symbol table, introduces operation.
In addition, described semantic analysis comprises carries out semantic test to the code in TTCN-3 script, associates the symbol in the code in described TTCN-3 script, stores the information generated required for Java source code, prepares for generating Java source code.
Translater carries out lexical analysis to TTCN-3 script file and grammatical analysis is by means of JavaCC and JJTree instrument, and realize by writing the file meeting BNF normal form, described BNF normal form can be cut out according to demand.
Whether wherein, semantic analysis performs in a certain order: for the modules A of input TTCN-3 script file, first judge A analyzed mistake; If then terminate the semantic analysis to A, then check each the possible introducing B existed in A if not; If the analyzed mistake of B, then all symbols all in B are incorporated in the symbol table of current block; If the not analyzed mistake of B, then first carry out semantic analysis to B, then be incorporated in the symbol table of current block by all symbols all in B; Each the possible introducing B existed in A is carrying out semantic analysis to A, and is identifying A for analyzing, terminating the semantic analysis to A after detecting.
The invention also discloses a kind of device TTCN-3 script being converted to Java source code, it is characterized in that, comprising:
Translater builds module, builds the translater of TTCN-3 script;
Translater, described TTCN-3 script is translated according to predefined translation rule by described translater, converts Java source code to;
Support Library builds module, in conjunction with described translation rule, builds the Support Library run needed for Java source code;
Support Library, described Support Library realizes the foundation class referred in translation;
Configuration collector, the execution entrance of configuration Java source code, obtains the executable file of Java source code.
Translater comprises analysis module TTCN-3 script being carried out to lexical analysis and grammatical analysis further, and described analysis module comprises:
Morphology syntax parsing module, the mistake of morphology and grammer in TTCN-3 script described in School Affairs correction, analyzes morphology and syntactic structure, and generative grammar is set;
Semantic module, scans syntax tree, carries out semantic analysis;
Code generation module, is scanned across the syntax tree of semantic analysis, generates corresponding Java source code.
Described translater comprises symbol table module further, and the symbol in record TTCN-3 script file code, provides the retrieval of symbol table, introduces operation.
In addition, described semantic module comprises further carries out semantic test to the code in TTCN-3 script, associate the symbol in the code in described TTCN-3 script, store the information generated required for Java source code, for generating the module that Java source code is prepared.
Wherein, translater carries out lexical analysis to TTCN-3 script file and grammatical analysis is by means of JavaCC and JJTree instrument, and realize by writing the file meeting BNF normal form, described BNF normal form can be cut out according to demand.
Whether preferably, described semantic analysis performs in order: for the modules A of input TTCN-3 script file, first judge A analyzed mistake; If then terminate the semantic analysis to A, then check each the possible introducing B existed in A if not; If the analyzed mistake of B, then all symbols all in B are incorporated in the symbol table of current block; If the not analyzed mistake of B, then first carry out semantic analysis to B, then be incorporated in the symbol table of current block by all symbols all in B; Each the possible introducing B existed in A is carrying out semantic analysis to A, and is identifying A for analyzing, terminating the semantic analysis to A after detecting.
The invention also discloses the scheme that TTCN-3 script is converted to Java source code by another, comprise step:
Build the translater of TTCN-3 script;
Described translater comprises described translater and completes the conversion of TTCN-3 script file code to syntax tree data structure; Symbol table in record TTCN-3 script file code, provides the retrieval of symbol table, introduces operation; Scanning syntax tree, carries out semantic test, associated nodes and corresponding symbol; Complete the conversion of module syntax tree to Java source code;
Build the Support Library run needed for Java source code; Described Support Library realizes the foundation class referred in translation;
Configuration collector, the execution entrance of configuration Java source code, obtains the executable file of Java source code.
And disclose a kind of system TTCN-3 script being converted to Java source code, described system comprises:
Translater builds module, builds the translater of TTCN-3 script;
Translater;
Support Library builds module, builds the Support Library run needed for Java source code;
Support Library, described Support Library realizes the foundation class referred in translation;
The execution entrance of configuration Java source code, obtains the executable file of Java source code.
Described translater comprises further:
Morphology syntax parsing module, morphology syntax parsing module, completes the conversion of TTCN-3 script file code to syntax tree data structure;
Symbol table module, the symbol table in record TTCN-3 script file code, provides the retrieval of symbol table, introduces operation;
Semantic module, scanning syntax tree, carries out semantic test, associated nodes and corresponding symbol;
Code generation module, completes the conversion of module syntax tree to Java source code.
Can find out, the protocol procedures that the present invention provides is less, gives Java compiler process by the intermediate code optimizing process of conventional compiler device.Meanwhile, once compile by Java language the characteristic run everywhere, this method can make TTCN-3 script run under different operating system.In addition, because TTCN-3 to Java itself is higher level lanquage, therefore, new TTCN-3 grammer is expanded also comparatively easy.When implementing, only need to make translater increase new grammer support, and in Java Support Library, add the java class of new definition.
Embodiment
Below in conjunction with the drawings and specific embodiments, the present invention is described in further detail:
Fig. 1 gives of the present invention set forth total execution flow process TTCN-3 script file (* .ttcn) being converted to Java source code file.First this process needs the translater of a structure TTCN-3 script file, and TTCN-3 script file can be converted to a Java source code file by this translater.
Then build the Support Library run needed for Java source code, described Support Library realizes the foundation class referred in translation, is compiled the Java source code that translation generates by Java compiler, performs the Java source code that described translation generates.
Wherein, translater performs the significant element of TTCN-3 script file to Java source code file transform; For specifically, this translater needs to comprise following function:
(1) lexical analysis is carried out, grammatical analysis to all TTCN-3 script files.The Main Function of this process gets rid of some fundamental errors in script file, the mistake check and correction that such as misspelling etc. are conventional, when errors are detected, sends error reporting in time in an informed way, facilitate commissioning staff to modify, and or carry out auto modification for frequent fault; This process finally understands generative grammar tree.
(2) syntax tree is scanned, carry out semantic analysis.Semantic analysis process mainly carries out semantic test to code.In addition, this process also can symbol in association code, stores the information required for generating code, for code generation process is prepared.
(3) be scanned across the syntax tree of semantic analysis, generate corresponding Java source code.In the specific implementation, a TTCN-3 file can be exchanged into a Java source file, and a TTCN-3 module can be mapped to a java class and gets on, and is so just easy to set up the corresponding relation between file and even statement.
Build the Support Library of Java, the Java source code after these Java source code files ensure that translation can correctly be compiled.
Code after combination translation and Support Library, and configure the execution entrance of whole Java source code, can the complete Java source code run be obtained.
From a structural point, this translater contains with lower module:
Morphology syntax parsing module: complete the conversion of TTCN-3 script file code to syntax tree data structure;
Symbol table module: the symbol table in record TTCN-3 script file code, provides the retrieval of symbol table, introduces operation;
Semantic module: analysis module scanning syntax tree, carries out semantic test, associated nodes and corresponding symbol;
Code generation module: complete the conversion of module syntax tree to Java source code.
structure morphology syntax parsing module
First translater carries out lexical analysis and grammatical analysis to TTCN-3 script file, by means of JavaCC and JJTree instrument, only needs to write the normal form file meeting instrument and specify, can generate corresponding morphology and the Java source code of syntax parsing.Wherein, JavaCC (JavaCompilerCompiler) is a grammatical analysis maker with Java exploitation.Concrete BNF normal form can carry out certain cutting according to oneself demand, for the syntactic element do not related in those TTCN-3 script files, then can be deleted.After normal form has write normal form file, instrument will generate corresponding node class and morphology syntax parsing device for the nonterminal symbol in BNF normal form.The normal form of the TTCN-3 of normative reference, its node class formation is shown in accompanying drawing 2.
In fig 2, Node interface is the interface that all node classes all need to realize, and it provides the access method to child nodes and father node.JjtGetChildNum () method obtains the number of child nodes, and jjtGetChild () method can obtain the child nodes of specifying, and jjtAccept () method makes Accessor Classes can access this node.
SimpleNode class achieves Node interface enterLocation, and it is the base class of other all nodes.This method with the addition of getStartPosition () method in SimpleNode class, to record the position of node in source code.Nodal information is then outputted to the printing device of specifying with the mode recurrence of text and gets on by dump () method.
StrcutFieldDef class and RecordDef class just inherit SimpleNode class, and they are exactly the node class generated by corresponding BNF normal form.After semantic analysis completes, each TTCN-3 code file will be converted into a syntax tree synthesized by node class example set.
After opening the VISITOR option of JJTree instrument, also can generate ParserVisitor interface, this interface is that syntax tree visitor needs to realize interface, and each node class is to should visit () method in interface.If need to access syntax tree, will realistic this interface existing.Specifically, in ensuing semantic module and code generation module, just need all the visit () methods realized in this interface, to access all syntax tree nodes.
All node class names and its extend information as shown in table 1:
The list of table 1 node class
structure symbol table module
According to TTCN-3 grammatical norm, static state during the semiotic function territory of TTCN-3 language, namely TTCN-3 script file code book body structure just determines the action scope of symbol in code.Each action scope unit comprises mutually, and in falling tree structure, all symbols inside father's action scope unit are all visible to its all sub-action scope.Because the semantic analysis stage needs to carry out a large amount of symbolic look-up accessing operations, therefore, symbol table, except ensureing efficient search capacity, also needs the symbol ensureing to find to come from correct action scope.
In the specific implementation, this programme will by a chain stack, the name space of maintain symbol.At a time its snapshot as shown in Figure 3, and when entering a new semiotic function territory, (as when entering a new function) will set up a new action scope object, and is set to current action scope.Exiting before prescope or newly-built action scope, all symbols newly added will be organized with the form of doubly linked list, and if when the symbolic name newly added exists hash-collision (or of the same name), the symbol newly added will cover former symbol.Do like this and can ensure when Hash lookup, the symbol of the always innermost layer action scope found.During concrete enforcement, for improving development efficiency, the Hash table that Java self can be used to provide supports class.
Fig. 4 provides class figure when symbol table is specifically implemented, and symbol table module is externally mutual by the mono-routine class of SymbolTbl.The multiple Scope object of Symbol object internal maintenance, they are organized with the form of storehouse.Semantic module is added on a new Scope object and storehouse by newScope () method, and is set to current action scope.After this, new symbol all adds this action scope by enter () method, and lookup () method is for retrieving all symbols in name space.When semantic module calls leave () method, when prescope object will be released, and symbols all inside it also discharges thereupon.
constructing semantic analysis module
Semantic module mainly carries out semantic test to syntax tree, needs to use symbol table to carry out recording indexes current sign information in checking process.Semantic module, as visitor, needs to realize ParserVisitor interface.To each node class, just need to realize corresponding with it visit () method.
the semantic analysis order of determination module
In TTCN-3, there is symbol introduce (by import keyword identification) mechanism, the symbol definition under this makes current block (module) can quote other modules.Such as, define TypeA in ModuleA, ModuleB introduces ModuleA, and defines the example InsA of TypeA.In order to verify the correctness in definition of InsA, check according to the definition of TypeA with regard to needing.In order to ensure that this inspection can be carried out smoothly, a kind of simple method ensures that the module of all references all completes semantic analysis.That is the ModuleA first scanned, and then scan ModuleB, just correctly can 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, topological sorting can be used to carry out determination module scanning sequency.Whether Fig. 5 gives a kind of feasible algorithm flow: for input TTCNModuleA, first judge A analyzed mistake; If then terminate the semantic analysis to A, then check each the possible introducing B existed in A if not; If the analyzed mistake of B, then all symbols all in B are incorporated in the symbol table of current block; If the not analyzed mistake of B, then first carry out semantic analysis to B, then be incorporated in the symbol table of current block by all symbols all in B; Each the possible introducing B existed in A is carrying out semantic analysis to A, and is identifying A for analyzing, terminating the semantic analysis to A after detecting.
the delay association of module internal symbol
Similar Java language, in a module, TTCN-3 allows the type first quoting rear statement, such as: following definition is legal.
This backward mode quoted can run into and cannot find in symbol table by TypeA when result in depth-first fashion scanning syntax tree.In order to avoid this situation, this method has declared IInstance interface (with reference to figure 3), and this node inherits ISymbolized interface, for the node of mainly above-mentioned containing type definition.When first time scans undefined symbol, this method first step only checks the uniqueness of this symbolic name, then puts it into one and does not complete in queue.After having scanned the syntax tree of current block, then second step association process is carried out to the IInstance example that this does not complete inside queue.
structure code translation module
Code generation module is responsible for generating a Java source code file for each syntax tree after semantic analysis terminates.Be similar to semantic analysis, code translation module needs to realize ParserVisitor interface.The Java source code meeting generated by translation module and Support Library are combined, and form final compiling result.The translation of code is according to following rule:
Type definition: for type definition node, generates a new class after translation, and such inherits class corresponding in Support Library.This category node comprises: TTCN3Module, ModuleControlPart, SubTypeDef, RecordDef, RecordOfDef, EnumDef, SignatureDef, ComponentDef, PortDef, TestCaseDef, AltstepDef (as shown in table 1).The class name generated is identical with type name in TTCN-3 code.Form because each TTCN-3 module is defined by the above-mentioned type, so the code after translation is combined into by these classes.Child node is comprised to the node of executable statement node, comprise TestCaseDef, AltstepDef, FunctionDefPart, ModuleControlPart, they all comprise executable code, therefore, the class after 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, acquisition MTC assembly method, acquisition System assembly method and some built-in functions (as ispresent () function), this method can realize in advance in the T3RTSystem class in operation support storehouse, and this class is a routine class of overall addressable list.If comprise the call statement of these methods in TTCN-3 code, this statement translation is the Java statement calling method of the same name in T3RTSystem class by translater.
Variable constant definition: in statement block, often can comprise local variable definition.When entering action scope, if run into local variable statement statement, this definition translation can be addVariable () call statement by translater.In this way, this local variable will join when code finally runs in the action scope at its place.In order to safeguard action scope, end up at each statement block, translater will be also the call statement that each local variable generates delVariable () method, previously adds this action scope ground local variable to empty.For the constant definition in statement block, translater adopts identical strategy.The organizational form of the local variable of concrete action scope is responsible for by 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 class corresponding with it in advance in operation support storehouse, respectively: IntegerType, FloatType, BooleanType, VerdictType, these classes are all bonding examples, are obtained such instance objects by getInstance ().When running into a constant value, translater will translate into the statement that form is <Class-Name>.getInstance () .newInstance (<InitValue>), wherein <Class-Name> is corresponding class name, and <InitValue> is initial value.
Control statement: control statement mainly comprises conditional statement (by if keyword identification), loop statement (by for, while keyword identification), after running into these statements, translater can be translated into control statement completely corresponding with it in Java.Also have some key words, as break key word, return key word, is also translated into corresponding Java key word.
Optional step statement: for optional step statement block, translater is translated into the do-while loop statement in Java, and whether circulation terminates by checking that snapshot state judges.Each conditional statement in optional step statement block is translated into the if statement in Java language.The interpretative system that repeat key word in optional step statement is corresponding is throw exception, and this can ensure once to jump out multilayer circulation.The structure of optional step is as shown in the table.
The table 2 optional step translation table of comparisons
Port method call: when running into call, during getcall, reply, getreply method call, translater is translated into the method call of corresponding Java, and it is good that these methods construct all in advance in the class in operation support storehouse.
operation support storehouse
Code after translation will combine compiling with operation support storehouse, and operation support storehouse realizes the foundation class referred in translation scheme, and complete the execution of code and the work such as the record of execution result that generate translation.
For the module (by module keyword identification) in TTCN-3 language, control the entity elements such as (by control keyword identification), test case (by testcase keyword identification), function (by function keyword identification), optional step (by altstep keyword identification), operation support storehouse is the base class that each element realizes that completes this element basic function.And be exactly the process of structure one to the concrete subclass that base class is expanded to the translation process of concrete element individuality.As shown in Figure 6, following entity elements is had to support class in operation support storehouse.
AbstractModule class is the base class of all block elements translation results in TTCN-3, achieves the management to elements such as the constant defined in module, control, behavior (i.e. test case, function and optional step), function signature (by signature keyword identification);
AbstractControl class is the base class of control element translation result in TTCN-3, achieves the functions such as control and measuring use-case execution;
AbstractTestcase class is the base class of control element translation result in TTCN-3, achieves the management to test case execution result and test suite (component);
AbstractFunction class is the base class of functional element translation result in TTCN-3, achieves the management to the variable in function, constant and rreturn value;
AbstractAltstep class is the base class of optional step element translation result in TTCN-3, achieves the management to the variable in optional step, constant;
Control, test case, function, optional step are all the executable in TTCN-3, and their subclass of all expansions all must realize run () method according to operational semantics.
In TTCN-3 language, define some basic data types, in addition, user can be expanded basic data type according to actual needs, retrain or instantiation, define 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, list type etc.).No matter each data type, be fundamental type or user defined type, belong to certain large class all uniquely.In operation support storehouse, for the large class of each data type establishes a base class, they are IntegerType, FloatType, BooleanType, VerdictType, CharstringType, RecordType, RecordOfType respectively.Each base class is expanded from TypeImpl class, and TypeImpl class achieves Type interface.All user defined types are all translated into the subclass of the base class to class large belonging to it, realize the expansion to base class or constraint.
In TTCN-3, all data values all belong to a specific data type, see Fig. 7 and Fig. 8, correspond to the object that extends ValueImpl class in translation scheme.ValueImpl class achieves Value interface, contains concrete data message.ValueImpl class also has a member territory achieving Type interface, the object that the data type of this member belonging to ValueImpl class is corresponding.The getType () method calling Value interface will return this member.Correspondingly, have individual newInstance () method in Type interface, call the method by the data value object of data type corresponding for establishment one, namely one achieves the example of Value interface.And belong to all subclasses achieving Type interface of a large class, even if the concrete data type representated by it is different, the example achieving Value interface created by calling its newInstance () method is all the object of same class.Such as, IntegerValueImpl class extends ValueImpl class and achieves 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 the object of an IntegerValueImpl class be returned.Corresponding also have the classes such as FloatValueImpl, BooleanValueImpl, VerdictValueImpl, CharstringValueImpl, RecordValueImpl, RecordOfImpl.
Service data type and data value are come according to the pattern of abstract factory in operation support storehouse, the newInstance () method setting up the Type example that a data value must use data type belonging to it corresponding creates, and ensure that type matching and compatible feasibility.
Assembly (by component keyword identification) is the core in TTCN-3, represents the entity performing TTCN-3 semanteme.All test cases, function, optional step all perform in specific assembly, and each assembly is exactly the clue of a semantic execution.Communication is realized by the mode of message or the invocation of procedure between assembly.Operation support storehouse Component class represents an assembly, and the expansion of Component class is from ValueImpl class, and namely Component is a special data value.In translation scheme, the definition of each assembly is translated into a subclass of ComponentType class, and the operation creating assembly is then realized by the newInstance () method calling corresponding ComponentType object.Component class manages the port, variable, constant and the timer that define in assembly.
Operation support storehouse adopts the mode of multithreading to support the parallel work-flow of multiple assembly in testing engineering, territory containing a Thread type in Component class, be used for the semantic operation that executive module needs to perform, by calling execute () method and specifies and performing the semantic operation that needs carry out.Component class also maintains a communication queue, under two principles of first in first out and Data Matching, and the communication between supporting assembly.The thread synchronization mechanism of operation support Cooley Java language safeguards the consistance of assembly data sharing.
T3RTSystem class adopts singleton pattern to realize, and is responsible for the execution controlling whole testing engineering, and collects the execution result of all test cases, safeguard execution journal information and carry out error handling processing.By the configuration of property file, the measurand adapter (SUTAdapter) that can use in nominative testing system, the execution starting point of coder/decoder (Coder/Decoder) and generating code.When in the code translating generation and operation support storehouse, other classes are carried out mutual, T3RTSystem class also plays intermediate axle beam action, thus generating code is hidden to the specific implementation in operation support storehouse as much as possible.
It should be noted that, the content such as information interaction, implementation between said apparatus and intrasystem each unit, due to the inventive method embodiment based on same design, particular content can see in the inventive method embodiment describe, repeat no more herein.
Apply specific case herein to set forth principle of the present invention and embodiment, the explanation of above embodiment just understands method of the present invention and core concept thereof for helping; Meanwhile, 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.