Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / urs / exploit / SchemasProgressionMacro.groovy @ 1217

History | View | Annotate | Download (9.5 kB)

1
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3
// @author mdecorde
4
// @author sheiden
5
// STANDARD DECLARATIONS
6
package org.txm.macro.urs.exploit
7

    
8
import groovy.transform.Field
9

    
10
import org.jfree.chart.JFreeChart
11
import org.jfree.chart.editor.ChartEditor
12
import org.kohsuke.args4j.*
13
import org.txm.Toolbox
14
import org.txm.annotation.urs.*
15
import org.txm.chartsengine.r.core.RChartsEngine
16
import org.txm.macro.urs.AnalecUtils
17
import org.txm.progression.core.chartsengine.jfreechart.themes.highcharts.renderers.ProgressionItemSelectionRenderer
18
import org.txm.progression.core.functions.Progression
19
import org.txm.rcp.Application
20
import org.txm.rcp.IImageKeys
21
import org.txm.rcp.swt.widget.parameters.*
22
import org.txm.searchengine.cqp.corpus.*
23
import org.txm.searchengine.cqp.corpus.query.CQLQuery
24

    
25
import visuAnalec.elements.*
26

    
27
def scriptName = this.class.getSimpleName()
28
def parent
29
def selection = []
30
if (!(corpusViewSelection instanceof CQPCorpus)) {
31
        println "** $scriptName please select a Corpus to run the macro"
32
        return;
33
}
34
selection << corpusViewSelection // only one corpus for progression
35
parent = corpusViewSelection
36

    
37
// BEGINNING OF PARAMETERS
38
@Field @Option(name="schema_ursql", usage="TYPE@PROP=VALUE", widget="String", required=true, def="CHAINE")
39
                String schema_ursql
40
@Field @Option(name="minimum_schema_size", usage="Minimum size needed to consider a schema", widget="Integer", required=true, def="3")
41
                int minimum_schema_size
42
@Field @Option(name="maximum_schema_size", usage="Maximum size needed to consider a schema", widget="Integer", required=true, def="9999999")
43
                int maximum_schema_size
44
@Field @Option(name="schema_property_display", usage="PROP", widget="String", required=true, def="REF")
45
                String schema_property_display
46
@Field @Option(name="unit_ursql", usage="TYPE@PROP=VALUE", widget="String", required=false, def="")
47
                String unit_ursql
48
@Field @Option(name="limit_distance_in_schema", usage="Unit distance in schema (0 = no selection, 1 = first after limit, -1 = last before limit, etc.)", widget="Integer", required=true, def="0")
49
                int limit_distance_in_schema
50
@Field @Option(name="limit_cql", usage="CQL to build structure limits", widget="Query", required=true, def="")
51
                limit_cql
52
@Field @Option(name="strict_inclusion", usage="Units must be strictly included into corpus matches", widget="Boolean", required=true, def="true")
53
                boolean strict_inclusion
54
@Field @Option(name="limit_distance", usage="Unit distance to structure limit (0 = no selection, 1 = first after limit, -1 = last before limit, etc.)", widget="Integer", required=true, def="0")
55
                int limit_distance
56
@Field @Option(name="struct_name", usage="Structure to display", widget="String", required=true, def="div")
57
                String struct_name
58
@Field @Option(name="struct_prop", usage="Structure property to display", widget="String", required=true, def="n")
59
                String struct_prop
60
@Field @Option(name="line_width", usage="line width", widget="Integer", required=true, def="1")
61
                int line_width = 1
62
@Field @Option(name="cummulative", usage="bande width", widget="Boolean", required=true, def="true")
63
                Boolean cummulative = true
64
@Field @Option(name="bande_width", usage="bande width", widget="Float", required=true, def="0.02f")
65
                float bande_width = 0.02f
66
//@Field @Option(name="show_lines", usage="Show progression lines", widget="Boolean", required=true, def="true")
67
//                Boolean show_lines
68
@Field @Option(name="show_title", usage="Show progression title", widget="Boolean", required=true, def="true")
69
                Boolean show_title
70
@Field @Option(name="show_legend", usage="Show progression legend", widget="Boolean", required=true, def="true")
71
                Boolean show_legend
72
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
73
                debug
74
if (!ParametersDialog.open(this)) return
75
        if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
76

    
77
def CQI = CQPSearchEngine.getCqiClient()
78

    
79
def limit_cql_matches = null;
80
if (limit_cql != null && !limit_cql.getQueryString().equals("\"\"")) {
81
        def limitssubcorpus = parent.createSubcorpus(limit_cql, parent.getName().toUpperCase())
82
        limit_cql_matches = limitssubcorpus.getMatches();
83
        limitssubcorpus.delete();
84
} else {
85
        limit_cql_matches = parent.getMatches()
86
}
87

    
88
def queries = []
89
def queryResults = []
90
def informations = []
91
def styles = []
92
for (def corpus : selection) {
93

    
94
        mainCorpus = corpus.getMainCorpus()
95

    
96
        def word = mainCorpus.getWordProperty()
97
        def analecCorpus = URSCorpora.getCorpus(mainCorpus.getName())
98

    
99
        if (schema_property_display.length() > 0) {
100
                def errors = AnalecUtils.isPropertyDefined(Schema.class, analecCorpus, schema_ursql, schema_property_display).size()
101
                if (errors > 0) {
102
                        println "Error: some Schema types don't contain the $schema_property_display property: $errors"
103
                        return
104
                }
105
        }
106

    
107
        def allUnits = [:]
108
        def allHighlightedUnits = [:]
109
        def allSchemas = AnalecUtils.selectSchemas(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size)
110

    
111
        if (allSchemas.size() == 0) {
112
                println "No schema match for '$schema_ursql' selection. Aborting"
113
                return
114
        }
115

    
116
        allUnits = AnalecUtils.groupAllUnitesInElements(debug, allSchemas)
117
        if ((unit_ursql != null && unit_ursql.length() > 0) || (limit_cql != null && !limit_cql.getQueryString().equals("\"\""))) {
118
                if (debug) println "Building selection of units to highlight..."
119
                allHighlightedUnits = AnalecUtils.groupAllUnitesInElements(debug, allSchemas, unit_ursql)
120
        }
121

    
122
        if (limit_distance_in_schema > 0) allHighlightedUnits = AnalecUtils.filterUniteByInclusionInSchema(debug, allSchemas, limit_distance_in_schema)
123

    
124
        if (allUnits.size() == 0) {
125
                println "No unit match for '$unit_ursql' selection. Aborting"
126
                return
127
        }
128

    
129
        if (debug) println "allUnits=${allUnits.size()}"
130

    
131
        for (Schema schema : allSchemas) {
132
                //println "schema="+schema.getProps()
133
                def selectedUnits = allUnits[schema]
134

    
135
                if (selectedUnits.size() == 0) continue;
136
                if (debug) println "all schema units size: "+selectedUnits.size()
137

    
138
                selectedUnits = AnalecUtils.filterUniteByInclusion(debug, selectedUnits, corpus.getMatches(), strict_inclusion, 0)
139

    
140
                if (selectedUnits.size() == 0) continue;
141
                if (debug) println "all schema units in corpus size: "+selectedUnits.size()
142

    
143
                def selectedAndHighlightedUnits = new HashSet() // faster to find items
144
                if (allHighlightedUnits != null && allHighlightedUnits.containsKey(schema)) selectedAndHighlightedUnits.addAll(allHighlightedUnits[schema])
145

    
146
                if (limit_cql_matches != null) {
147
                        if (debug) println "corpus matches: "+parent.getMatches()
148
                        if (debug) println "filter limit_cql_matches=${limit_cql_matches} with "+selectedAndHighlightedUnits.size()+" units."
149
                        selectedAndHighlightedUnits = AnalecUtils.filterUniteByInclusion(debug, selectedAndHighlightedUnits, limit_cql_matches, strict_inclusion, limit_distance)
150
                        if (debug) println "selectedAndHighlightedUnits=${selectedAndHighlightedUnits.size()}"
151
                }
152

    
153
                def query = schema.getProp(schema_property_display)
154
                if (query == null) query = "<null>"
155
                if (query.length() == 0) query = "<empty>"
156

    
157
                query = new CQLQuery(query)
158

    
159
                int[] starts = new int[selectedUnits.size()];
160
                int[] ends = new int[selectedUnits.size()];
161
                def unitsinformations = []
162
                def unitsstyles = []
163
                int n = 0;
164
                for (Unite unite : selectedUnits) {
165
                        starts[n] = unite.getDeb()
166
                        ends[n] = unite.getFin()
167
                        unitsinformations << AnalecUtils.toString(CQI, word, unite);
168

    
169
                        if (selectedAndHighlightedUnits.contains(unite)) unitsstyles << 1.3d // bigger
170
                        else unitsstyles << null;
171

    
172
                        n++
173
                }
174

    
175

    
176

    
177
                def queryResult = new FakeQueryResult(corpus.getID(), corpus, query, starts, ends, null)
178

    
179
                queries << query
180
                queryResults << queryResult
181
                informations << unitsinformations
182
                styles << unitsstyles
183
        }
184
}
185

    
186
//println ""+queries.size()+" selected schemas: "+queries
187

    
188
def corpus = parent
189
try {
190
        def struct = corpus.getStructuralUnit(struct_name);
191
        def struct_p = null;
192
        if (struct != null) {
193
                struct_p = struct.getProperty(struct_prop)
194
        }
195
        
196
        Progression progression = new Progression(corpus, queries,
197
                        struct, struct_p,        ".*",
198
                        cummulative, false, false,
199
                        line_width, false, bande_width)
200

    
201
        progression.stepQueries(queryResults); // new
202

    
203
        if (!progression.stepStructuralUnits() || monitor.isCanceled())        return
204
                monitor.worked(20)
205
        if (!progression.stepFinalize() || monitor.isCanceled()) return
206
                monitor.worked(20)
207

    
208
        monitor.syncExec(new Runnable() {
209
                                @Override
210
                                public void run() {
211
                                        try {
212
                                                def charteditorpart = SWTChartsComponentProvider.openEditor(Application.swtComponentProvider.createProgressionChartEditorPart(IImageKeys.getImage(IImageKeys.ACTION_PROGRESSION), progression, progression.isMonochrome(), progression.isMonostyle(), progression.isDoCumulative()))
213
                                                JFreeChart chart = charteditorpart.getChart()
214
                                                def plot = chart.getXYPlot()
215

    
216
                                                ProgressionItemSelectionRenderer renderer = plot.getRenderer();
217
                                                //renderer.setBaseItemLabelsVisible(true)
218
                                                //renderer.setBaseLinesVisible(false)
219
                                                //renderer.setBaseSeriesVisible(SchemasProgressionMacro.this.show_lines, true);
220
                                                //renderer.setItemLabelsVisible(true)
221
                                                //renderer.setLinesVisible(false)
222
                                                renderer.setAdditionalLabelInformation(informations)
223
                                                renderer.setAdditionalShapeScales(styles)
224
                                                chart.getLegend().setVisible(SchemasProgressionMacro.this.show_legend)
225
                                                chart.getTitle().setVisible(SchemasProgressionMacro.this.show_title)
226

    
227
                                        } catch(Exception e) {e.printStackTrace()}
228
                                }
229
                        })
230

    
231
} catch(Exception e) {
232
        e.printStackTrace()
233
        return false
234
}