Statistics
| Revision:

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

History | View | Annotate | Download (10.6 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.core.preferences.ChartsEnginePreferences
16
import org.txm.chartsengine.r.core.RChartsEngine
17
import org.txm.macro.urs.AnalecUtils
18
import org.txm.progression.core.chartsengine.jfreechart.themes.highcharts.renderers.ProgressionItemSelectionRenderer
19
import org.txm.progression.core.functions.Progression
20
import org.txm.rcp.Application
21
import org.txm.rcp.IImageKeys
22
import org.txm.rcp.swt.widget.parameters.*
23
import org.txm.searchengine.cqp.CQPSearchEngine
24
import org.txm.searchengine.cqp.corpus.*
25
import org.txm.searchengine.cqp.corpus.query.CQLQuery
26
import org.txm.chartsengine.rcp.*;
27
import visuAnalec.elements.*
28

    
29
def scriptName = this.class.getSimpleName()
30

    
31
def selection = []
32
for (def s : corpusViewSelections) {
33
        if (s instanceof CQPCorpus) selection << s
34
        else if (s instanceof Partition) selection.addAll(s.getParts())
35
}
36

    
37
if (selection.size() == 0) {
38
        println "** $scriptName: please select a Corpus or a Partition in the Corpus view: "+corpusViewSelections
39
        return false
40
} else {
41
        for (def c : selection) c.compute(false)
42
}
43

    
44
// BEGINNING OF PARAMETERS
45
@Field @Option(name="sep", usage="Schemas and units selection part", widget="Separator", required=false, def="Selection")
46
String sep
47
@Field @Option(name="schema_ursql", usage="TYPE@PROP=VALUE", widget="String", required=true, def="CHAINE")
48
                String schema_ursql
49
@Field @Option(name="minimum_schema_size", usage="Minimum size needed to consider a schema", widget="Integer", required=true, def="3")
50
                int minimum_schema_size
51
@Field @Option(name="maximum_schema_size", usage="Maximum size needed to consider a schema", widget="Integer", required=true, def="9999999")
52
                int maximum_schema_size
53
@Field @Option(name="schema_property_display", usage="PROP", widget="String", required=true, def="REF")
54
                String schema_property_display
55
@Field @Option(name="unit_ursql", usage="TYPE@PROP=VALUE", widget="String", required=false, def="")
56
                String unit_ursql
57
@Field @Option(name="position_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")
58
                int position_in_schema
59
@Field @Option(name="cql_limit", usage="CQL to build structure limits", widget="Query", required=false, def="")
60
                cql_limit
61
@Field @Option(name="strict_inclusion", usage="Units must be strictly included into corpus matches", widget="Boolean", required=true, def="true")
62
                boolean strict_inclusion
63
@Field @Option(name="position_in_matches", usage="Unit distance to structure limit (0 = no selection, 1 = first after limit, -1 = last before limit, etc.)", widget="Integer", required=true, def="0")
64
                int position_in_matches
65
@Field @Option(name="sep2", usage="Progression command parameters", widget="Separator", required=false, def="Progression")
66
                String sep2
67
@Field @Option(name="struct_name", usage="Structure to display", widget="String", required=true, def="div")
68
                String struct_name
69
@Field @Option(name="struct_prop", usage="Structure property to display", widget="String", required=true, def="n")
70
                String struct_prop
71
@Field @Option(name="line_width", usage="line width", widget="Integer", required=true, def="1")
72
                int line_width = 1
73
@Field @Option(name="cummulative", usage="bande width", widget="Boolean", required=true, def="true")
74
                Boolean cummulative = true
75
@Field @Option(name="bande_width", usage="bande width", widget="Float", required=true, def="0.02f")
76
                float bande_width = 0.02f
77
//@Field @Option(name="show_lines", usage="Show progression lines", widget="Boolean", required=true, def="true")
78
//                Boolean show_lines
79
@Field @Option(name="show_title", usage="Show progression title", widget="Boolean", required=true, def="true")
80
                Boolean show_title
81
@Field @Option(name="show_legend", usage="Show progression legend", widget="Boolean", required=true, def="true")
82
                Boolean show_legend
83
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
84
                debug
85
if (!ParametersDialog.open(this)) return
86
        if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
87

    
88
def CQI = CQPSearchEngine.getCqiClient()
89

    
90

    
91

    
92
def queries = []
93
def queryResults = []
94
def informations = []
95
def styles = []
96
for (def corpus : selection) {
97

    
98
        def cql_limit_matches = null;
99
        if (cql_limit != null && !cql_limit.getQueryString().equals("\"\"")) {
100
                def limitssubcorpus = corpus.createSubcorpus(cql_limit, corpus.getName().toUpperCase())
101
                cql_limit_matches = limitssubcorpus.getMatches();
102
                limitssubcorpus.delete();
103
        } else {
104
                cql_limit_matches = corpus.getMatches()
105
        }
106
        
107
        mainCorpus = corpus.getMainCorpus()
108

    
109
        def word = mainCorpus.getWordProperty()
110
        def analecCorpus = URSCorpora.getCorpus(mainCorpus)
111

    
112
        def errors = AnalecUtils.isPropertyDefined(Schema.class, analecCorpus, schema_ursql)
113
        if (errors.size() > 0) {
114
                println "** The $schema_ursql schema URSQL cannot be computed in the corpus with types: $errors."
115
                return;
116
        }
117
        
118
        if (schema_property_display.length() > 0) {
119
                errors = AnalecUtils.isPropertyDefined(Schema.class, analecCorpus, AnalecUtils.getFilterParameters(schema_ursql)[0], schema_property_display).size()
120
                if (errors > 0) {
121
                        println "Error: some Schema types don't contain the $schema_property_display property: $errors"
122
                        return
123
                }
124
        }
125

    
126
        def allUnits = [:]
127
        def allHighlightedUnits = [:]
128
        def allSchemas = AnalecUtils.selectSchemasInCorpus(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size)
129

    
130
        if (allSchemas.size() == 0) {
131
                println "No schema match for '$schema_ursql' selection. Aborting"
132
                return
133
        }
134

    
135
        allUnits = AnalecUtils.groupAllUnitesInElements(debug, allSchemas)
136
        if ((unit_ursql != null && unit_ursql.length() > 0) || (cql_limit != null && !cql_limit.getQueryString().equals("\"\""))) {
137
                if (debug) println "Building selection of units to highlight..."
138
                allHighlightedUnits = AnalecUtils.groupAllUnitesInElements(debug, allSchemas, unit_ursql)
139
        }
140

    
141
        if (position_in_schema > 0) allHighlightedUnits = AnalecUtils.filterUniteByInclusionInSchema(debug, allSchemas, position_in_schema)
142

    
143
        if (allUnits.size() == 0) {
144
                println "No unit match for '$unit_ursql' selection. Aborting"
145
                return
146
        }
147

    
148
        if (debug) println "allUnits=${allUnits.size()}"
149

    
150
        for (Schema schema : allSchemas) {
151
                //println "schema="+schema.getProps()
152
                def selectedUnits = allUnits[schema]
153

    
154
                if (selectedUnits.size() == 0) continue;
155
                if (debug) println "all schema units size: "+selectedUnits.size()
156

    
157
                selectedUnits = AnalecUtils.filterUniteByInclusion(debug, selectedUnits, corpus.getMatches(), strict_inclusion, 0)
158

    
159
                if (selectedUnits.size() == 0) continue;
160
                if (debug) println "all schema units in corpus size: "+selectedUnits.size()
161

    
162
                def selectedAndHighlightedUnits = new HashSet() // faster to find items
163
                if (allHighlightedUnits != null && allHighlightedUnits.containsKey(schema)) selectedAndHighlightedUnits.addAll(allHighlightedUnits[schema])
164

    
165
                if (cql_limit_matches != null) {
166
                        if (debug) println "corpus matches: "+corpus.getMatches()
167
                        if (debug) println "filter cql_limit_matches=${cql_limit_matches} with "+selectedAndHighlightedUnits.size()+" units."
168
                        selectedAndHighlightedUnits = AnalecUtils.filterUniteByInclusion(debug, selectedAndHighlightedUnits, cql_limit_matches, strict_inclusion, position_in_matches)
169
                        if (debug) println "selectedAndHighlightedUnits=${selectedAndHighlightedUnits.size()}"
170
                }
171

    
172
                def query = schema.getProp(schema_property_display)
173
                if (query == null) query = "<null>"
174
                if (query.length() == 0) query = "<empty>"
175

    
176
                query = new CQLQuery(query)
177

    
178
                int[] starts = new int[selectedUnits.size()];
179
                int[] ends = new int[selectedUnits.size()];
180
                def unitsinformations = []
181
                def unitsstyles = []
182
                int n = 0;
183
                for (Unite unite : selectedUnits) {
184
                        starts[n] = unite.getDeb()
185
                        ends[n] = unite.getFin()
186
                        unitsinformations << AnalecUtils.toString(CQI, word, unite);
187

    
188
                        if (selectedAndHighlightedUnits.contains(unite)) unitsstyles << 1.3d // bigger
189
                        else unitsstyles << null;
190

    
191
                        n++
192
                }
193

    
194

    
195

    
196
                def queryResult = new FakeQueryResult(corpus.getID(), corpus, query, starts, ends, null)
197

    
198
                queries << query
199
                queryResults << queryResult
200
                informations << unitsinformations
201
                styles << unitsstyles
202
        }
203
        
204
        try {
205
                def struct = corpus.getStructuralUnit(struct_name);
206
                def struct_p = null;
207
                if (struct != null) {
208
                        struct_p = struct.getProperty(struct_prop)
209
                }
210
                
211
                Progression progression = new Progression(corpus);
212
                progression.setParameters(queries,
213
                                struct, struct_p,        ".*",
214
                                cummulative, line_width, false, bande_width)
215
        
216
                progression.setQueryResults(queryResults); // new
217
                progression.setParameter(ChartsEnginePreferences.SHOW_LEGEND, show_legend);
218
                progression.setParameter(ChartsEnginePreferences.SHOW_TITLE, show_title);
219
                
220
                progression.compute()
221
                if (!progression.stepStructuralUnits() || monitor.isCanceled())        return
222
                        monitor.worked(20)
223
                if (!progression.stepFinalize() || monitor.isCanceled()) return
224
                        monitor.worked(20)
225
        
226
                monitor.syncExec(new Runnable() {
227
                                        @Override
228
                                        public void run() {
229
                                                try {
230
                                                        //def charteditorpart = org.txm.chartsengine.rcp.SWTChartsComponentsProvider.openEditor(Application.swtComponentProvider.createProgressionChartEditorPart(IImageKeys.getImage(IImageKeys.ACTION_PROGRESSION), progression, progression.isMonochrome(), progression.isMonostyle(), progression.isDoCumulative()))
231
                                                        def charteditorpart = org.txm.chartsengine.rcp.editors.ChartEditor.openEditor(progression);
232
//                                                        JFreeChart chart = charteditorpart.getChart()
233
//                                                        def plot = chart.getXYPlot()
234
//        
235
//                                                        ProgressionItemSelectionRenderer renderer = plot.getRenderer();
236
//                                                        //renderer.setBaseItemLabelsVisible(true)
237
//                                                        //renderer.setBaseLinesVisible(false)
238
//                                                        //renderer.setBaseSeriesVisible(SchemasProgressionMacro.this.show_lines, true);
239
//                                                        //renderer.setItemLabelsVisible(true)
240
//                                                        //renderer.setLinesVisible(false)
241
//                                                        renderer.setAdditionalLabelInformation(informations)
242
//                                                        renderer.setAdditionalShapeScales(styles)
243
//                                                        chart.getLegend().setVisible(SchemasProgressionMacro.this.show_legend)
244
//                                                        chart.getTitle().setVisible(SchemasProgressionMacro.this.show_title)
245
        
246
                                                } catch(Exception e) {e.printStackTrace()}
247
                                        }
248
                                })
249
        
250
        } catch(Exception e) {
251
                e.printStackTrace()
252
                return false
253
        }
254
}
255

    
256
//println ""+queries.size()+" selected schemas: "+queries
257