Home | History | Annotate | Download | only in grxmlcompile
      1 /* FILE:        netw_arc.h
      2  *  DATE MODIFIED:    31-Aug-07
      3  *  DESCRIPTION:    Part of the  SREC graph compiler project source files.
      4  *
      5  *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
      6  *                                                                           *
      7  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
      8  *  you may not use this file except in compliance with the License.         *
      9  *                                                                           *
     10  *  You may obtain a copy of the License at                                  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0                           *
     12  *                                                                           *
     13  *  Unless required by applicable law or agreed to in writing, software      *
     14  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
     15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
     16  *  See the License for the specific language governing permissions and      *
     17  *  limitations under the License.                                           *
     18  *                                                                           *
     19  *---------------------------------------------------------------------------*/
     20 
     21 #ifndef __netw_arc_h__
     22 #define __netw_arc_h__
     23 
     24 #undef assert
     25 #define assert(X)
     26 #include <cstdio>
     27 
     28 class GRXMLDoc;
     29 class NUANArc
     30 {
     31 public:
     32     friend class SubGraph;
     33     /* Constructors */
     34 
     35     /* Create arc with only input and output labels
     36     */
     37     NUANArc (int iLabel, int oLabel)
     38     {
     39         inputLabel= iLabel;
     40         outputLabel= oLabel;
     41         centre= -1;
     42         left= -1;
     43         right= -1;
     44         return;
     45     };
     46 
     47     /* Create arc with full data
     48     */
     49     NUANArc (int iLabel, int oLabel, int from, int to)
     50     {
     51         inputLabel= iLabel;
     52         outputLabel= oLabel;
     53         fromId= from;
     54         toId= to;
     55         left= -1;
     56         right= -1;
     57         centre= -1;
     58         return;
     59     };
     60 
     61     /* Copy an arc
     62     */
     63     NUANArc (NUANArc *arcsrc)
     64     {
     65         inputLabel= arcsrc->inputLabel;
     66         outputLabel= arcsrc->outputLabel;
     67         fromId= arcsrc->fromId;
     68         toId= arcsrc->toId;
     69         left= arcsrc->left;
     70         right= arcsrc->right;
     71         centre= arcsrc->centre;
     72         return;
     73     };
     74 
     75     /* Create arc based on another arc
     76     */
     77     NUANArc (NUANArc *arcsrc, int offset, int startId, int newStartId, int endId, int newEndId)
     78     {
     79         inputLabel= arcsrc->inputLabel;
     80         outputLabel= arcsrc->outputLabel;
     81         if (arcsrc->fromId == startId && newStartId >= 0)
     82             fromId= newStartId;
     83         else
     84             fromId= arcsrc->fromId + offset;
     85         if (arcsrc->toId == endId && newEndId >= 0)
     86             toId= newEndId;
     87         else
     88             toId= arcsrc->toId + offset;
     89         left= -1;
     90         right= -1;
     91         centre= -1;
     92         return;
     93     };
     94 
     95     /*  Assign non-terminal vertices
     96     */
     97     void AssignFromId (int Id)
     98     {
     99         fromId= Id;
    100     };
    101 
    102     /*  Assign non-terminal vertices
    103     */
    104     void AssignToId (int Id)
    105     {
    106         toId= Id;
    107     };
    108 
    109     void AssignInput (int Id)
    110     {
    111         inputLabel= Id;
    112     };
    113 
    114     void AssignOutput (int Id)
    115     {
    116         outputLabel= Id;
    117     };
    118 
    119     /*  Assign centre context
    120     */
    121     void AssignCentre (int centreData) { centre= centreData; };
    122 
    123     /*  Assign left context
    124     */
    125     void AssignLeft (int leftData) { left= leftData; };
    126 
    127     /*  Assign right context
    128     */
    129     void AssignRight (int rightData) { right= rightData; };
    130 
    131     /* Access functions */
    132     /* Get input label
    133     */
    134     int GetInput() { return inputLabel; };
    135 
    136     /* Get output label
    137     */
    138     int GetOutput()  { return outputLabel; };
    139 
    140     /* Get from Vertex
    141     */
    142     int GetFromId()  { return fromId; };
    143 
    144     /* Get to Vertex
    145     */
    146     int GetToId()  { return toId; };
    147 
    148     /* Get centre context
    149     */
    150     int GetCentre()  { return centre; };
    151 
    152     /* Get left context
    153     */
    154     int GetLeft()  { return left; };
    155 
    156     /* Get right context
    157     */
    158     int GetRight()  { return right; };
    159 
    160     /*  Transduction
    161     */
    162     int Transduce (int iLabel)
    163     {
    164         if (inputLabel == iLabel)
    165             return outputLabel;
    166         else
    167             return -1;
    168     };
    169 
    170     /*  Similarity checks
    171     */
    172     int Compare (NUANArc *test)
    173     {
    174         if (fromId > test->fromId)
    175             return 1;
    176         else if (fromId < test->fromId)
    177             return -1;
    178         else if (toId > test->toId)
    179             return 1;
    180         else if (toId < test->toId)
    181             return -1;
    182         else if (inputLabel > test->inputLabel)
    183             return 1;
    184         else if (inputLabel < test->inputLabel)
    185             return -1;
    186         else if (outputLabel > test->outputLabel)
    187             return 1;
    188         else if (outputLabel < test->outputLabel)
    189             return -1;
    190         else
    191             return 0;
    192         }
    193 
    194     int CompareSymbol (NUANArc *test)
    195     {
    196         if (inputLabel > test->inputLabel)
    197             return 1;
    198         else if (inputLabel < test->inputLabel)
    199             return -1;
    200         else if (outputLabel > test->outputLabel)
    201             return 1;
    202         else if (outputLabel < test->outputLabel)
    203             return -1;
    204         else
    205             return 0;
    206         }
    207 
    208     int CompareReverse (NUANArc *test)
    209     {
    210         if (toId > test->toId)
    211             return 1;
    212         else if (toId < test->toId)
    213             return -1;
    214         else if (fromId > test->fromId)
    215             return 1;
    216         else if (fromId < test->fromId)
    217             return -1;
    218         else if (inputLabel > test->inputLabel)
    219             return 1;
    220         else if (inputLabel < test->inputLabel)
    221             return -1;
    222         else if (outputLabel > test->outputLabel)
    223             return 1;
    224         else if (outputLabel < test->outputLabel)
    225             return -1;
    226         else
    227             return 0;
    228     }
    229 
    230     int CompareForMin (NUANArc *test)
    231     {
    232         if (fromId > test->fromId)
    233             return 1;
    234         else if (fromId < test->fromId)
    235             return -1;
    236         else if (inputLabel > test->inputLabel)
    237             return 1;
    238         else if (inputLabel < test->inputLabel)
    239             return -1;
    240         else if (outputLabel > test->outputLabel)
    241             return 1;
    242         else if (outputLabel < test->outputLabel)
    243             return -1;
    244         else if (toId > test->toId)
    245             return 1;
    246         else if (toId < test->toId)
    247             return -1;
    248         else
    249             return 0;
    250     }
    251 
    252     int CompareWithContext (NUANArc *test)
    253     {
    254         if (fromId > test->fromId)
    255             return 1;
    256         else if (fromId < test->fromId)
    257             return -1;
    258         else if (toId > test->toId)
    259             return 1;
    260         else if (toId < test->toId)
    261             return -1;
    262         else if (inputLabel > test->inputLabel)
    263             return 1;
    264         else if (inputLabel < test->inputLabel)
    265             return -1;
    266         else if (outputLabel > test->outputLabel)
    267             return 1;
    268         else if (outputLabel < test->outputLabel)
    269             return -1;
    270         else if (left > test->left)
    271             return 1;
    272         else if (left < test->left)
    273             return -1;
    274         else if (right > test->right)
    275             return 1;
    276         else if (right < test->right)
    277             return -1;
    278         else
    279             return 0;
    280     }
    281 
    282     bool IsSame (NUANArc *test)
    283     {
    284         if (inputLabel == test->inputLabel && outputLabel == test->outputLabel && fromId == test->fromId && toId == test->toId)
    285             return true;
    286         else
    287             return false;
    288     };
    289 
    290     bool HasSameLabels (NUANArc *test)
    291     {
    292         if (inputLabel == test->inputLabel && outputLabel == test->outputLabel)
    293             return true;
    294         else
    295             return false;
    296     };
    297 
    298     bool HasSameLabelsAndTo (NUANArc *test)
    299     {
    300         if (inputLabel == test->inputLabel && outputLabel == test->outputLabel && toId == test->toId)
    301             return true;
    302         else
    303             return false;
    304     };
    305 
    306     bool HasSameLabelsAndFrom (NUANArc *test)
    307     {
    308         if (inputLabel == test->inputLabel && outputLabel == test->outputLabel && fromId == test->fromId)
    309             return true;
    310         else
    311             return false;
    312     };
    313 
    314     /* Print
    315     */
    316     void Print()
    317     {
    318         printf ("%d %d %d %d (%d)\n", fromId, toId, inputLabel, outputLabel, centre);
    319         return;
    320     };
    321     void PrintText()
    322     {
    323         printf ("%d %d %c %d (%d)\n", fromId, toId, inputLabel, outputLabel, centre);
    324         return;
    325     };
    326 
    327     void Dump (GRXMLDoc &p_Doc );
    328 
    329 
    330 protected:
    331     int     inputLabel;     /*  input label */
    332     int     outputLabel;    /*  output label */
    333     int     fromId;         /*  from node */
    334     int     toId;           /*  to node */
    335     int     centre;         /*  left context  */
    336     int     left;           /*  left context  */
    337     int     right;          /*  right context */
    338 };
    339 
    340 #endif // __netw_arc_h__
    341