Graph theory-based approach to XML data binding

A technique in accordance with one embodiment of the invention automatically generates class interfaces for regular expressions based on graphs that correspond to the regular expressions. According to one embodiment of the invention, a graph is automatically generated based on a regular expression. Strongly connected components within the graph are automatically identified. For each strongly connected component within the graph, a separate method is generated within a class interface for the regular expression. In one embodiment of the invention, if a strongly connected component contains a cycle, then the method corresponding to that strongly connected component is generated to return a List of “T,” where “T” is a type that is as specific as the contents of the strongly typed component permit.

BACKGROUND

A regular expression is a description of a pattern composed from combinations of symbols and operators. For example, the following text is a regular expression:

The above regular expression represents that a “Name” consists of exactly one “Firstname,” followed by zero or one “Middlenames” (the “zero or one” being denoted by the question mark in the expression), followed by exactly one “Lastname.”

The above regular expression also can be represented in the form of a tree.FIG. 1is a diagram that illustrates a tree structure that represents a regular expression. The illustrated tree indicates that the regular expression is a “SEQUENCE” of three nodes: a “Firstname” node, an “OPTIONAL” node, and a “Lastname” node. The presence of the “OPTIONAL” node indicates that child nodes of that node are not mandatory within instances that conform to the regular expression. Thus, the “OPTIONAL” node corresponds functionally to the question mark that is associated with the “Middlename” in the regular expression discussed above. Because the “Middlename” node follows the “OPTIONAL” node, instances that conform to the regular expression can, but do not need to, contain any “Middlename.”

When a regular expression is used to indicate the structure of an Extensible Markup Language (XML) document, the regular expression is called a “schema.” An XML document conforms to a schema if the elements within that XML document follow the structure indicated in the schema. For example, the following XML data conforms to the regular expression discussed above:

<Name><Firstname>Kohsuke</Firstname><Lastname>Kawaguchi</Lastname></Name>
The XML data comprises exactly one “Firstname” element (“Kohsuke”) followed by exactly one “Lastname” element (“Kawaguchi”) as required by the regular expression. The XML data conforms to the structure of the regular expression despite the absence of a “Middlename” element, because the regular expression requires zero or one “Middlenames.” In this case, the XML data comprises zero “Middlename” elements, which is acceptable. XML data that conforms to a schema is often called a “valid instance” with respect to that schema.

Taken together, multiple regular expressions such as the one discussed above may be seen as defining a “type system.” For example, the regular expression discussed above defines the structure of a “Name” type in such a type system. Regular expressions can be used to describe data types.

It is often useful to generate programs that read or write XML data that conforms to a regular expression. For example, one might write a JAVA program that contains a class specifically designed to read instances of the “Name” type from one or more XML documents. Such a class might have an interface similar to the following:

Using constraints available within the JAVA type system, the above class represents the constraints of the type system defined by the regular expression. To read a “Firstname,” “Lastname,” or “Middlename” element from an XML document, a computer program may invoke the appropriate “getFirstname,” “getLastname,” or “getMiddlename” method of the “Name” class. Within the program code, each method may be implemented specifically to read and return the appropriate type of element. For example, the interface of the “getFirstname” method specifically indicates that the “getFirstname” method is to return data of a “Firstname” type. Thus, the type system defined in the XML document is preserved in the return types of the methods.

Such classes are very useful. Because these classes are so useful, it is beneficial to attempt to automate, to the extent possible, the generation of the interface of these classes and the interfaces of these classes' methods. A computer program that receives a type system, such as one or more regular expressions (i.e., a schema), and attempts to automatically generate class and methods that correspond to the type system, is called a “schema compiler.” The process of generating class and methods that correspond to such a type system is called “data binding.”

Sometimes the automatic generation of class and methods is relatively straightforward. However, complications can arise when the regular expressions to which the interfaces correspond are more complex.

For example, one might define a type “X” in the following manner:

In plain English, this complex regular expression reads as, “X consists of an arbitrary combination of A, B, and C, but there must be at least one of them,” as that is typically the intention of the schema author when he writes a regular expression like this. The following sequences are all of those which conform to the constraints of this complex regular expression: “A,” “AB,” “AC,” “ABC,” “B,” “BC,” and “C.”

At first glance, it might seem that this complex regular expression could be expressed in simpler terms. However, the constraints defined by this complex regular expression are not the same as the constraints defined by either of the following other simpler regular expressions:

The sequences “AB,” “AC,” “BC,” and “ABC,” which conform to the complex regular expression discussed previously, don't conform to the first of these other regular expressions. Additionally, the empty sequence, which doesn't conform to the complex regular expression discussed previously, conforms to the second of these other regular expressions.

Unfortunately, existing schema compilers do not handle complex regular expressions in an optimal manner. For example, if an existing schema compiler received, as input, the complex regular expression discussed above, the existing schema converter might generate the following class and methods:

Class X {List<Object> getContent( );}
The above interfaces are not very specific. The method “getContent” would merely read and return a list of elements of non-specific “Object” types. In JAVA, “Object” is the most general type. When data is stored in an “Object” type, the more specific information that might have been available concerning that data's original type is not preserved.

Yet, one of the prime reasons that data is stored in an XML document in the first place is so that the specific types (e.g., “Firstname,” “Middlename,” “Lastname”) of the data stored therein are defined. Failing to preserve the specific types of data specified within an XML document tends to defeat the very reasons why the data was stored in XML format in the first place. Thus, the non-specific “getContent” method is not very useful.

Existing schema compilers are limited in effectiveness by their inability to generate, automatically, type-specific methods based on complex regular expressions.

SUMMARY

A technique in accordance with one embodiment of the invention automatically generates class interfaces for regular expressions based on graphs that correspond to the regular expressions. According to one embodiment of the invention, a graph is automatically generated based on a regular expression. Strongly connected components within the graph are automatically identified. For each strongly connected component within the graph, a separate method is generated within a class interface for the regular expression. If a strongly connected component contains a cycle, then the method corresponding to that strongly connected component is generated to return a List of “T,” where “T” is a type that is as specific as the contents of the strongly typed component permit.

Using this technique for generating a class interface allows the same class interface to be generated for all regular expressions that are substantially the same, even if those regular expressions are different in form. Regular expressions that are different in form may be reduced to the same graphs, from which the same class interfaces may be derived. Thus, the above techniques provide a high degree of consistency. The above techniques can be used to convert any regular expression, of any degree of complexity, into a class interface that comprises methods with return types that are as specific as the graph corresponding to the regular expression permits.

DETAILED DESCRIPTION OF EMBODIMENT(S)

Overview

According to techniques described herein, graphs are derived automatically from regular expressions. In fact, the same graph sometimes can be derived from multiple regular expressions that are different in form. Such a graph preserves type-specific information represented by a regular expression while avoiding much of the complexity that inheres in that regular expression's form. Once a graph has been derived from a regular expression, a class interface with appropriate type-specific methods can be derived automatically from the graph.

Example Graph

FIG. 2is a diagram that illustrates an example of a graph automatically generated based on a regular expression, according to an embodiment of the invention. Although techniques described herein refer to generating graphs, embodiments of the invention do not require that such graphs actually be generated in a visible form. Such graphs may be generated and represented by data structures within the memory of a computer system, for example, without ever being displayed to a human being. The graph is an intermediate representation of certain aspects of a regular expression. Class and methods are generated based on the intermediate representation. It is not necessary for the intermediate representation to be displayed.

The graph illustrated inFIG. 2comprises nodes202-216. Node202corresponds to “A,” node204corresponds to “B,” node206corresponds to “C,” node208corresponds to “D,” node210corresponds to “E,” and node212corresponds to “F.” Node214is a “source” node, and node216is a “sink” node. The graph corresponds to at least the following regular expression:

Sequences that conform to this regular expression comprise zero or more (denoted by the asterisk in the regular expression) sequences of “ABC,” followed by exactly one “E,” followed by zero or more sequences of “DE,” followed by exactly one “F.” Other different regular expressions also may correspond to the graph ofFIG. 2.

The directed edges in the graph ofFIG. 2indicate the order in which the symbols of the nodes ofFIG. 2occur within sequences that conform to the corresponding regular expression. For example, the directed edge leading from node202to node204indicates that “B” occurs after “A” in a conforming sequence. Similarly, the directed edges leading from node206to nodes202and node210indicate that “C” may be followed by either “A” or “E” in a conforming sequence. According to one embodiment of the invention, the graph is generated in such a way that any alphabet occurs at most once, so that there are never two nodes that have the same label.

According to one embodiment of the invention, a graph of the kind shown inFIG. 2is automatically generated based on a regular expression to which that graph corresponds. For example, a computer program may automatically generate such a graph by parsing and analyzing the corresponding regular expression.

Some information in the regular expression might not be, and does not need to be, preserved in the graph that is derived from the regular expression. Thus, the fact that a particular symbol in the regular expression might occur zero times in a conforming sequence does not need to be preserved in the graph corresponding to the regular expression. The fact that such a symbol could occur more than zero times in a conforming sequence is enough to merit the inclusion, in the graph, of a node that corresponds to that symbol. It is not necessarily possible, or necessary, to reconstruct the exact regular expression from which a graph is derived based only on the information represented in the graph.

Strongly Connected Component Decomposition

According to one embodiment of the invention, after a graph has been generated automatically based on a regular expression, the graph is “decomposed” automatically into separate “strongly connected components.” A strongly connected component is defined as either (a) a set of one or more nodes in which a cycle exists or (b) a single node that is not part of any cycle. “Decomposing” a graph into strongly connected components means identifying all of the strongly connected components that exist in the graph. A computer program may perform the decomposition automatically, for example.

FIG. 3is a diagram that illustrates an example of a graph in which strongly connected components have been identified, according to an embodiment of the invention. Because nodes202,204, and206are in a cycle, these nodes are considered to be a part of strongly connected component302. Because nodes208and210are in a cycle, these nodes are considered to be a part of strongly connected component304. Node212is not a part of any cycle, so node212is itself considered to be a strongly connected component306. “Source” node214and “sink” node216are special nodes which are not considered to be part of any strongly connected component.

The graph expresses an order between the strongly connected components. As can be seen in the graph ofFIG. 3, strongly connected component304follows strongly connected component302, and strongly connected component306follows strongly connected component304. Although there may exist different paths through the graph, each sequence that conforms to the regular expression to which the graph corresponds will comprise a symbol from at least one node in each strongly connected component. Symbols from different strongly connected components will occur in an order relative to each other that is consistent with the graph's representation of the ordering of the strongly connected components.

Some strongly connected components may form a “cut set” of a graph. In graph theory terminology, a set of nodes forms a “cut set” if a graph becomes disjoint after removing the nodes in the set from the graph. In the graph ofFIG. 3, all strongly connected components302-306are cut sets, but in some other graphs, some strongly connected components might not be cut sets. According to one embodiment of the invention, if a strongly connected component is a cut set, then the property generated from that strongly connected component is “mandatory,” in that every instance which conforms to the graph's corresponding regular expression must include least one element that matches at least one member of that strongly connected component. In Java, this translates to a method that must receive and return a value other than “null” (which represents a “missing value”). For example, if the type corresponding to a strongly connected component is “int,” then whether the type is mandatory or not can make a difference between the component's method's return type will be the primitive “int” (which implies that the method can't receive or return “null”) or “java.lang.Integer” (which implies that the method can receive and return “null”).

Graph-Based Class Interface Generation

According to one embodiment of the invention, each strongly connected component in the graph corresponds to a separate method that is to be generated automatically in a class interface for the regular expression from which the graph was derived. According to one embodiment of the invention, for each such strongly connected component in the graph, a corresponding method, with an appropriate return type, is automatically generated. For example, a computer program may automatically generate the methods based on the graph.

In one embodiment of the invention, if a strongly connected component comprises a cycle, then the return type of the corresponding method is set to be of type “List.” Alternatively, if a strongly connected component does not comprise any cycle, then the return type of the corresponding method is set to be of a specific type that corresponds to the type of the node in that strongly connected component.

For example, in the graph ofFIG. 3, there are three strongly connected components302-306. Strongly connected component302comprises a cycle of three nodes, so, within a class interface, a method “getABC” may be generated with return type “List.” Strongly connected component304comprises a cycle of two nodes, so within the class interface, a method “getDE” may be generated with return type “List.” Strongly connected component306comprises only one node that is not involved in any cycle, so within the class interface, a method “getF” may be generated with a more specific return type “F.” Return type “F” corresponds to the type of node212in strongly connected component306. It is certain that this method will only read XML elements that are of type “F,” so the return type of this method can be more specific.

Based on the graph shown inFIG. 3, the following example class interface might be automatically generated:

Using the above techniques for generating a class interface allows the same class interface to be generated for all regular expressions that are substantially the same, even if those regular expressions are different in form. Regular expressions that are different in form may be reduced to the same graphs, from which the same class interfaces may be derived. Thus, the above techniques provide a high degree of consistency. The above techniques can be used to convert any regular expression, of any degree of complexity, into a class interface that comprises methods with return types that are as specific as the graph corresponding to the regular expression permits.

Example Flow

FIG. 4is a flow diagram that illustrates an example of a graph-based technique for automatically generating a class interface that corresponds to a regular expression, according to an embodiment of the invention. Such a technique may be performed automatically by a computer program, for example.

In block402, for each unique symbol that occurs within the regular expression, a separate node is generated for that symbol. For example, the regular expression might be:

In this case, although the symbol “E” appears twice in the regular expression, only one node is generated for that symbol. Based on this regular expression, six nodes would be generated, such as is shown inFIG. 2. Thus, a set of nodes is generated. In order to generate a graph, the nodes need to be connected.

In block404, for each node in the set of nodes, that node is connected to one or more other nodes in the set of nodes. The nodes are connected with directed edges that indicate an order between the connected nodes. The connections between the nodes are based on the order in which symbols corresponding to the nodes occur within the regular expression relative to other symbols in the regular expression. Thus, a graph is automatically generated based on the regular expression.

For example, in the graph shown inFIG. 2, node202is connected to node204by a directed edge that leads from node202to node204. This directed edge is placed in the graph because, in the corresponding regular expression, the symbol “A,” which corresponds to node202, can occur before symbol “B,” which corresponds to node204.

For another example, in the graph shown inFIG. 2, node206is connected to node202by a directed edge that leads from node206to node202. This directed edge is placed in the graph because, in the corresponding regular expression, the symbol “C,” which corresponds to node206, can occur before symbol “A,” which corresponds to node202. The reason why symbol “C” can occur before symbol “A” is because the regular expression indicates (via an asterisk) that the sequence “ABC” can occur more than once in a sequence that conforms to the regular expression. Consequently, if the sequence “ABC” repeats, then symbol “C” may occur before symbol “A.”

In block406, the graph is decomposed into a set of strongly connected components. Algorithms that decompose a graph into a set of strongly connected components are well known and are not described in great detail herein.

In block408, for each strongly connected component in the graph, a determination is made as to whether that strongly connected component contains a cycle.

In block410, for each strongly connected component in the graph, a determination is made as to whether that strongly connected component is a cut set, as described above.

In block412, a class interface is automatically generated for the regular expression. For example, if the regular expression represents the structure to which a “Name” entity must adhere, then the initial class interface, unpopulated by methods yet, might take the form:

Class Name {}

In block414, for each strongly connected component in the graph, a method for that strongly connected component is generated based on whether that strongly connected component comprises a cycle, and based on whether that strongly connected component is a cut set. Each such method is placed within the class interface generated in block412.

In one embodiment of the invention, if a strongly connected component comprises a cycle, then the method generated for that strongly connected component is given a “List” return type. Alternatively, if the strongly connected component does not comprise a cycle, then the method generated for that strongly connected component is given a return type that is specific to and depends on a type that is associated with a node within the strongly connected component.

For example, in response to a determination that strongly connected component302ofFIG. 3comprises a cycle, a method with a return type “List” may be generated automatically and placed within the class interface. For example, the method, including return type, might take the form:

In the above method, <T> represents the most specific “ancestor” type (in the type hierarchy) of all of the types of the elements that can be placed in the list. Sometimes <T> is <Object>, when the types of the elements (in this case, A, B, and C) that can be placed in the list have no more specific common ancestor type. Other times, <T> is a type that is more specific than <Object>.

Similarly, in response to a determination that strongly connected component304ofFIG. 3comprises a cycle, a method with a return type “List” may be generated automatically and placed within the class interface. For example, the method, including return type, might take the form:

If the type of “D” and the type of “E” were both descendant types of type <Foo>, and if <Foo> was the most specific type that was an ancestor of the types of both “D” and “E,” then <T> would be <Foo> in this case.

In contrast, in response to a determination that strongly connected component306ofFIG. 3doesn't comprise a cycle, a method with a return type “F” (i.e., the type that is associated with node212) may be generated automatically and placed within the class interface. For example, the method, including return type, might take the form:

After the class interface and the methods within have been automatically generated, a schema compiler can automatically generate code that implements the functionality of each method. For example, a schema compiler might automatically implement the method “getABC” to get one or more symbols that comprise “A,” “B,” and “C,” and return those symbols in a list structure. For another example, a schema compiler might automatically implement the method “getF” to get exactly one “F” symbol and return that symbol as an “F” type. More practically speaking, the methods can be implemented to read XML elements from an XML document that conforms to the regular expression based on which the graph was generated.

Hardware Overview

FIG. 5is a block diagram that illustrates a computer system500upon which an embodiment of the invention may be implemented. Computer system500includes a bus502for facilitating information exchange, and one or more processors504coupled with bus502for processing information. Computer system500also includes a main memory506, such as a random access memory (RAM) or other dynamic storage device, coupled to bus502for storing information and instructions to be executed by processor504. Main memory506also may be used for storing temporary variables or other intermediate information during execution of instructions by processor504. Computer system500may further include a read only memory (ROM)508or other static storage device coupled to bus502for storing static information and instructions for processor504. A storage device510, such as a magnetic disk or optical disk, is provided and coupled to bus502for storing information and instructions.

In computer system500, bus502may be any mechanism and/or medium that enables information, signals, data, etc., to be exchanged between the various components. For example, bus502may be a set of conductors that carries electrical signals. Bus502may also be a wireless medium (e.g. air) that carries wireless signals between one or more of the components. Bus502may also be a medium (e.g. air) that enables signals to be capacitively exchanged between one or more of the components. Bus502may further be a network connection that connects one or more of the components. Overall, any mechanism and/or medium that enables information, signals, data, etc., to be exchanged between the various components may be used as bus502.

Bus502may also be a combination of these mechanisms/media. For example, processor504may communicate with storage device510wirelessly. In such a case, the bus502, from the standpoint of processor504and storage device510, would be a wireless medium, such as air. Further, processor504may communicate with ROM508capacitively. In this instance, the bus502would be the medium (such as air) that enables this capacitive communication to take place. Further, processor504may communicate with main memory506via a network connection. In this case, the bus502would be the network connection. Further, processor504may communicate with display512via a set of conductors. In this instance, the bus502would be the set of conductors. Thus, depending upon how the various components communicate with each other, bus502may take on different forms. Bus502, as shown inFIG. 5, functionally represents all of the mechanisms and/or media that enable information, signals, data, etc., to be exchanged between the various components.

The term “machine-readable medium” as used herein refers to any non-transitory medium that participates in providing data that causes a machine to operate in a specific fashion. In an embodiment implemented using computer system500, various machine-readable media are involved, for example, in providing instructions to processor504for execution. Such a medium may take many forms, including but not limited to, non-volatile media and volatile media. Non-volatile media includes, for example, optical or magnetic disks, such as storage device510. Volatile media includes dynamic memory, such as main memory506.

Processor504may execute the received code as the code is received and/or stored in storage device510or other non-volatile storage for later execution. In this manner, computer system500may obtain application code in the form of a carrier wave.