source: MDService/trunk/xquery/cmd-model.xqm @ 248

Last change on this file since 248 was 248, checked in by ljo, 14 years ago

cmd-model.xqm - fixed small syntax error.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1module namespace cmd-model = "http://spraakbanken.gu.se/clarin/xquery/model";
2
3(:
4 $Id: cmd-model.xqm 248 2010-03-17 23:50:05Z ljo $
5:)
6
7import module namespace xdb="http://exist-db.org/xquery/xmldb";
8import module namespace util="http://exist-db.org/xquery/util";
9
10declare variable $cmd-model:cmdiDatabaseURI as xs:string := "xmldb:exist:///db";
11
12declare variable $cmd-model:commonFreqsPath as xs:string := "/db/common/clarin/freqs";
13declare variable $cmd-model:cmdiMirrorPath as xs:string := "/db/cmdi-mirror";
14
15declare variable $cmd-model:typeActorPath as xs:string := "MDGroup/Actors/Actor";
16declare variable $cmd-model:typeActorPath0 as xs:string := "Actor";
17declare variable $cmd-model:typeActorRolePath as xs:string := "MDGroup/Actors/Actor/Role";
18
19declare variable $cmd-model:docTypeModelTrees as xs:string := "ModelTrees";
20declare variable $cmd-model:docTypeTerms as xs:string := "Terms";
21declare variable $cmd-model:docTypeSuffix as xs:string := "Values";
22
23declare variable $cmd-model:xmlExt as xs:string := ".xml";
24
25declare function cmd-model:elem($collection as xs:string, $path as xs:string, $depth as xs:integer) as element() {
26  let $path-nodes := util:eval(fn:concat("collection('", $collection, "')//", $path))
27  let $path-count := count($path-nodes)
28   
29  let $subs := distinct-values($path-nodes/child::element()/name())
30  let $text-nodes := $path-nodes//text()
31  let $text-count := count($text-nodes)
32  let $text-count-distinct := count(distinct-values($text-nodes))
33  return 
34        <elem path="{fn:concat("//", $path)}" count="{$path-count}" count_text="{$text-count}"  count_distinct_text="{$text-count-distinct}" >
35        { if ($depth > 0) then
36            for $elname in $subs[. != '']
37            return
38              cmd-model:elem($collection, concat($path, '/', $elname), $depth - 1)
39          else 'maxdepth'
40        }</elem>
41};
42
43declare function cmd-model:paths($n) {
44        for $el in $n
45        return <elem name="{$el/name()}"> {
46        for $anc in $el/parent::element()
47        return util:node-xpath($anc)
48        }</elem>
49};
50
51(:
52
53:)
54declare function cmd-model:recurse-collections-model($collection as xs:string, $type-name as xs:string, $depth as xs:integer) as item()* {
55    let $children := xdb:get-child-collections($collection)
56    return
57      if (fn:exists($children)) then
58          let $child-results :=
59            for $child in $children
60            return
61              cmd-model:recurse-collections-model(fn:concat($collection, '/', xs:string($child)), $type-name, $depth),
62              $current := cmd-model:create-doc($collection, $type-name, $depth)
63          return ($current, $child-results)
64      else
65        cmd-model:create-doc($collection, $type-name, $depth)
66};
67
68(:
69
70:)
71declare function cmd-model:create-doc($collection as xs:string, $type-name as xs:string, $depth as xs:integer) as xs:string* {
72  (: if newer data available :)
73    cmd-model:store-result($collection, cmd-model:elem($collection, $type-name, $depth), $type-name, $depth)
74  (:else () :)
75};
76
77(:
78
79:)
80declare function cmd-model:get-result-doc($collection as xs:string, $type-name as xs:string, $depth as xs:integer) as item()* {
81  let $name-last := text:groups($type-name, "/(\w+)$")[last()],
82    $new-name := if (fn:empty($name-last)) then $type-name else $name-last,
83    $dummy := if (cmd-model:is-result-available($collection, fn:concat("/", $new-name, xs:string($depth)))) then
84    ()
85    else
86      cmd-model:create-doc($collection, $type-name, $depth)
87    return
88      fn:doc(fn:concat($collection, "/", $new-name, xs:string($depth), $cmd-model:xmlExt))
89};
90
91(:
92  Function for telling wether the result is already available or not.
93:)
94declare function cmd-model:is-result-available($collection as xs:string, $result-ref as xs:string) as xs:boolean {
95  fn:doc-available(fn:concat($collection, $result-ref, $cmd-model:xmlExt))
96};
97
98(:
99  Store the calculated frequencies for reuse.
100  If more than one collection is given the result is stored in the common
101  collection for reuse.
102:)
103declare function cmd-model:store-result($coll-names as xs:string+, $entries as element()*, $type-name as xs:string, $depth as xs:integer) as xs:string {
104  let $clarin-writer := fn:doc("/db/clarin/writer.xml"),
105    $dummy := xdb:login($cmd-model:cmdiDatabaseURI, $clarin-writer//write-user/text(), $clarin-writer//write-user-cred/text())
106    return
107   
108      if (fn:exists($coll-names[2])) then
109        (: Det gÀller fler Àn en samling. :)
110        xdb:store($cmd-model:commonFreqsPath, cmd-model:make-compound-doc-name($coll-names, $type-name, $depth), cmd-model:make-doc-element-of-type($type-name, $coll-names, $entries, $depth))
111      else
112        (: Det gÀller endast en samling. :)
113        let $dummy := util:log('debug', fn:concat('Stores ', $type-name, ' in ', $coll-names))
114        return xdb:store($coll-names, cmd-model:make-doc-name($coll-names, $type-name, xs:string($depth), fn:false()), cmd-model:make-doc-element-of-type($type-name, (), $entries, xs:string($depth)))
115};
116
117(:
118  Create document name for type () with or without collection path.
119:)
120declare function cmd-model:make-doc-name($coll-name as xs:string?, $type-name as xs:string, $depth as xs:string, $incl-path as xs:boolean) as xs:string {
121  let $doc-name := fn:concat($type-name, $depth, $cmd-model:xmlExt)
122  return
123    if ($incl-path) then
124      fn:concat($coll-name, "/", $doc-name)
125    else
126      $doc-name
127};
128
129(:
130  Create document name with md5-hash for selected collections (or types) for reuse.
131:)
132declare function cmd-model:make-compound-doc-name($coll-names as xs:string+, $type-name as xs:string, $depth as xs:string) as xs:string {
133  let $name-prefix := fn:concat($type-name, $depth)
134    return
135    fn:concat($name-prefix, "-", util:hash(string-join($coll-names, ""), "MD5"), $cmd-model:xmlExt)
136};
137
138(:
139  Skapa ett element av angiven typ.
140:)
141declare function cmd-model:make-element-of-type($type-name as xs:string, $count as xs:string, $text-count as xs:string, $text-types-count as xs:string, $value as xs:string) as element() {
142  element {$type-name} {
143
144      attribute count {$freq},
145      attribute text-count {$rank},
146      attribute text-types-count {$text-types},
147      text {$value} 
148  }
149};
150
151(:
152  Skapa ett dokumentelement av angiven typ.
153:)
154declare function cmd-model:make-doc-element-of-type($type-name as xs:string, $coll-names as xs:string*, $entries as element()*, $depth as xs:string) as element() {
155      let $depth-value := attribute depth {$depth},
156      $coll-names-value := if (fn:empty($coll-names)) then () else attribute colls {fn:string-join($coll-names, ",")}
157      return
158        element {cmd-model:get-doc-type-element-name($type-name)} {
159          $depth-value,
160          $coll-names-value,
161          attribute created {fn:current-dateTime()},
162          $entries
163        }
164};
165
166(:
167  Skapa elementnamn för dokumentet av typ.
168:)
169declare function cmd-model:get-doc-type-element-name($type-name as xs:string) as xs:string {
170  let $name-last := text:groups($type-name, "/(\w+)$")[last()]
171    return
172    if (fn:empty($name-last)) then $cmd-model:ModelTrees else $cmd-model:Terms
173
174};
Note: See TracBrowser for help on using the repository browser.