00001 # Copyright (C) 1997-2004 The CDG Team <cdg@nats.informatik.uni-hamburg.de> 00002 # 00003 # This file is free software; as a special exception the author gives 00004 # unlimited permission to copy and/or distribute it, with or without 00005 # modifications, as long as this notice is preserved. 00006 # 00007 # This program is distributed in the hope that it will be useful, but 00008 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the 00009 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00010 00011 ## ---------------------------------------------------------------------------- 00012 ## YadaWordgraph - bundled information about a wordgraph. 00013 ## This class represents a wordgraph in a YadaGrammar. Scanning a YadaGrammar 00014 ## (see YadaGrammar::scan()) results in bunch of YadaWordgraph s being constructed. 00015 ## \ingroup YadaConfiguration 00016 ## 00017 ## \author Michael Daum 00018 ## 00019 ## $Id: YadaWordgraph.tcl,v 1.10 2004/02/26 14:23:37 ddreyer Exp $ 00020 ## ---------------------------------------------------------------------------- 00021 class YadaWordgraph { 00022 00023 # variables ---------------------------------------------------------------- 00024 00025 ## name of the wordgraph 00026 public variable name "" 00027 00028 ## filename which this wordgraph has been extracted from 00029 public variable file "" 00030 00031 ## linenumber in the file where this wordgraph has been defined 00032 public variable lineNo 0 00033 00034 ## flag indicating whether the wordgraph is trivial (linear) or not 00035 public variable isTrivial false 00036 00037 ## flag indicating whether wordgraph has been selected 00038 public variable isSelected 0 00039 00040 ## list of word arcs making up the wordgraph 00041 public variable arcs {} 00042 00043 ## number of edges in the related annotation 00044 public variable noAnnoEdges 0 00045 00046 ## number of words in the related annotation 00047 public variable noAnnoWords 0 00048 00049 # methods ------------------------------------------------------------------ 00050 public method readXmlNode {latticeNode}; ## \type DomNode 00051 public method writeXmlNode {file}; ## \type FileHandle 00052 public method readXmlAnno {annoNode}; ## \type DomNode 00053 public method getWords {} 00054 public method clone {args}; ## \type TclList 00055 public method toDOM {} 00056 public method fromDOM {rootNode}; ## \type domNode 00057 00058 constructor {args} {}; ## \type TclList 00059 }; 00060 00061 ## ---------------------------------------------------------------------------- 00062 ## constructor 00063 ## ---------------------------------------------------------------------------- 00064 body YadaWordgraph::constructor {args} { 00065 eval configure $args 00066 } 00067 00068 ## ---------------------------------------------------------------------------- 00069 ## Serializes Object into a DOM-XML-Node 00070 ## @return root DOM node representing the wordgraph 00071 ## ---------------------------------------------------------------------------- 00072 body YadaWordgraph::toDOM {} { 00073 set valAttrib "val" 00074 set document [dom createDocument "yadaWordgraph"] 00075 set root [$document documentElement] 00076 00077 #dom createNodeCmd elementNode name 00078 dom createNodeCmd elementNode file 00079 #dom createNodeCmd elementNode lineNo 00080 #dom createNodeCmd elementNode noAnnoWords 00081 #dom createNodeCmd elementNode noAnnoEdges 00082 #dom createNodeCmd elementNode isSelected 00083 #dom createNodeCmd elementNode isTrivial 00084 dom createNodeCmd elementNode arc 00085 00086 $root nodeValue 00087 $root setAttribute name $name 00088 $root setAttribute lineNo $lineNo 00089 $root setAttribute isTrivial $isTrivial 00090 $root setAttribute isSelected $isSelected 00091 $root setAttribute noAnnoEdges $noAnnoEdges 00092 $root setAttribute noAnnoWords $noAnnoWords 00093 00094 00095 $root appendFromScript { 00096 file -$valAttrib $file 00097 foreach {to from word} $arcs { 00098 arc -from $from -to $to -word $word 00099 } 00100 } 00101 00102 return $root 00103 } 00104 00105 ## ---------------------------------------------------------------------------- 00106 ## Initialize wordgraph with DOM node (that saves the object state) 00107 ## @param rootNode root DOM-Node (Tag <yadaWordgraph>) 00108 ## ---------------------------------------------------------------------------- 00109 body YadaWordgraph::fromDOM {rootNode} { 00110 set valAttrib "val" 00111 00112 #set nodeName [$rootNode selectNodes "name"] 00113 00114 #puts "Node-Name: $nodeName" 00115 #set name [$nodeName getAttribute $valAttrib] 00116 set name [$rootNode getAttribute name] 00117 #puts "Set name $name" 00118 00119 set nodeFile [$rootNode selectNodes "file"] 00120 set file [$nodeFile getAttribute $valAttrib] 00121 #puts "Set file $file" 00122 00123 #set nodeLineNo [$rootNode selectNodes "lineNo"] 00124 #set lineNo [$nodeLineNo getAttribute $valAttrib] 00125 set lineNo [$rootNode getAttribute lineNo] 00126 #puts "Set lineNo $lineNo" 00127 00128 #set nodeIsTrivial [$rootNode selectNodes "isTrivial"] 00129 #set isTrivial [$nodeIsTrivial getAttribute $valAttrib] 00130 set isTrivial [$rootNode getAttribute isTrivial] 00131 #puts "Set isTrivial $isTrivial" 00132 00133 #set nodeIsSelected [$rootNode selectNodes "isSelected"] 00134 #set isSelected [$nodeIsSelected getAttribute $valAttrib] 00135 set isSelected [$rootNode getAttribute isSelected] 00136 #puts "Set isSelected $isSelected" 00137 00138 set nodeArcs [$rootNode selectNodes "arc"] 00139 00140 set arcs {} 00141 foreach nodeArc $nodeArcs { 00142 lappend arcs [$nodeArc getAttribute from] [$nodeArc getAttribute to]\ 00143 [$nodeArc getAttribute word] 00144 } 00145 #puts "Set arcs $arcs" 00146 00147 00148 #set nodeNoAnnoEdges [$rootNode selectNodes "noAnnoEdges"] 00149 #set noAnnoEdges [$nodeNoAnnoEdges getAttribute $valAttrib] 00150 set noAnnoEdges [$rootNode getAttribute noAnnoEdges] 00151 #puts "Set noAnnoEdges $noAnnoEdges" 00152 00153 #set nodeNoAnnoWords [$rootNode selectNodes "noAnnoWords"] 00154 #set noAnnoWords [$nodeNoAnnoWords getAttribute $valAttrib] 00155 set noAnnoWords [$rootNode getAttribute noAnnoWords] 00156 #puts "Set noAnnoWords $noAnnoWords" 00157 00158 } 00159 00160 ## ---------------------------------------------------------------------------- 00161 ## readXmlAnno 00162 ## ---------------------------------------------------------------------------- 00163 body YadaWordgraph::readXmlAnno {annoNode} { 00164 if {[$annoNode nodeName] != "annotation"} { 00165 error "ERROR: `$annoNode' is not an annotation node" 00166 } 00167 00168 set noAnnoEdges [$annoNode getAttribute "noedges"] 00169 set noAnnoWords [$annoNode getAttribute "nowords"] 00170 } 00171 00172 ## ---------------------------------------------------------------------------- 00173 ## readXmlNode 00174 ## ---------------------------------------------------------------------------- 00175 body YadaWordgraph::readXmlNode {latticeNode} { 00176 if {[$latticeNode nodeName] != "lattice"} { 00177 error "ERROR: `$latticeNode' is not a lattice node" 00178 } 00179 00180 # get the lattice id 00181 configure -name [$latticeNode getAttribute "id"] 00182 00183 # get the file info 00184 set node [$latticeNode descendant 1 "file"] 00185 if {$node == ""} { 00186 error "ERROR: illegal xml lattice format" 00187 } 00188 set file [$node getAttribute "name"] 00189 set lineNo [$node getAttribute "lineNo"] 00190 00191 # is it a trivial wordgraph 00192 set node [$latticeNode descendant 1 "boolean" "name" "trivial"] 00193 if {$node == ""} { 00194 error "ERROR: illegal xml lattice format" 00195 } 00196 set isTrivial [$node getAttribute "value"] 00197 00198 # scan the arcs 00199 set arcNodes [$latticeNode getElementsByTagName "arc"] 00200 if {$arcNodes == ""} { 00201 error "ERROR: illegal xml lattice format" 00202 } 00203 set arcs "" 00204 foreach node $arcNodes { 00205 set from [$node getAttribute "from"] 00206 set to [$node getAttribute "to"] 00207 set word "[$node getAttribute word]" 00208 lappend arcs $from $to "$word" 00209 } 00210 } 00211 00212 ## ---------------------------------------------------------------------------- 00213 ## writeXmlNode 00214 ## ---------------------------------------------------------------------------- 00215 body YadaWordgraph::writeXmlNode {file} { 00216 00217 puts $file "<lattice id=\"$name\">" 00218 puts $file " <file name=\"$file\" lineNo=\"$lineNo\"/>" 00219 puts $file " <boolean name=\"trivial\" value=\"$isTrivial\"/>" 00220 foreach {from to word} $arcs { 00221 puts $file " <arc from=\"$from\" to=\"$to\" word=\"$word\"/>" 00222 } 00223 puts $file "</lattice>" 00224 } 00225 00226 ## ---------------------------------------------------------------------------- 00227 ## getWords 00228 ## ---------------------------------------------------------------------------- 00229 body YadaWordgraph::getWords {} { 00230 set words "" 00231 foreach {from to word} "$arcs" { 00232 lappend words "$word" 00233 } 00234 00235 return "$words" 00236 } 00237 00238 ## ---------------------------------------------------------------------------- 00239 ## clone 00240 ## ---------------------------------------------------------------------------- 00241 body YadaWordgraph::clone {args} { 00242 00243 # build the clone 00244 set newWordgraph [YadaWordgraph ::#auto \ 00245 -name $name \ 00246 -file $file \ 00247 -lineNo $lineNo \ 00248 -isTrivial $isTrivial \ 00249 -arcs $arcs] 00250 00251 eval $newWordgraph configure $args 00252 00253 return $newWordgraph 00254 }