File: EngineRunner.java

package info (click to toggle)
libgrinvin-core-java 1.2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 3,904 kB
  • ctags: 5,009
  • sloc: java: 23,494; xml: 423; makefile: 15
file content (238 lines) | stat: -rw-r--r-- 8,092 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* EngineRunner.java
 * =========================================================================
 * This file is part of the GrInvIn project - http://www.grinvin.org
 * 
 * Copyright (C) 2005-2008 Universiteit Gent
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * A copy of the GNU General Public License can be found in the file
 * LICENSE.txt provided with the source distribution of this program (see
 * the META-INF directory in the source jar). This license can also be
 * found on the GNU website at http://www.gnu.org/licenses/gpl.html.
 * 
 * If you did not receive a copy of the GNU General Public License along
 * with this program, contact the lead developer, or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.grinvin.conjecture.engine;

import java.awt.EventQueue;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

import org.grinvin.conjecture.filter.GraphFilter;
import org.grinvin.expr.Expression;
import org.grinvin.generators.GeneratorException;
import org.grinvin.generators.graphs.FilteredInvariantListGeneratorSink;
import org.grinvin.generators.graphs.GraphGeneratorInstance;
import org.grinvin.generators.graphs.InvariantListGeneratorSink;
import org.grinvin.worksheet.WorksheetModel;
import org.grinvin.invariants.Invariant;
import org.grinvin.invariants.InvariantValue;
import org.grinvin.list.invariants.DefaultInvariantList;
import org.grinvin.list.graphs.GraphListElement;

/**
 * Object which can be used to run a given conjecturing engine in a background thread.
 * <p>
 * Allows listeners to be notified when the run starts and when it ends.
 */
public class EngineRunner implements Runnable {
    
    //
    private final Engine engine;
    
    //
    private final WorksheetModel worksheetModel;
    
    /**
     * Return the associated engine.
     */
    public Engine getEngine() {
        return engine;
    }
    
    //
    private final EventListenerList listenerList = new EventListenerList();
    
    //
    private static final Class<ChangeListener> CHANGE_LISTENER_CLASS = ChangeListener.class;
    
    //
    private final ChangeEvent EVENT = new ChangeEvent(this);
    
    /**
     * Register an object which is informed of changes in the
     * 'running' state of this runner.
     */
    public void addChangeListener(ChangeListener l) {
        listenerList.add(CHANGE_LISTENER_CLASS, l);
    }
    
    /**
     * Remove a change listener.
     */
    public void removeChangeListener(ChangeListener l) {
        listenerList.remove(CHANGE_LISTENER_CLASS, l);
    }
    
    /**
     * Notify every listener of a change in state.
     */
    protected void fireStateChanged() {
        Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length-2; i>=0; i-=2)
            // if (listeners[i]==CHANGE_LISTENER_CLASS)
            ((ChangeListener)listeners[i+1]).stateChanged(EVENT);
    }
    
    /**
     * Construct a runner object for the given engine.
     * 
     * @param engine Engine to be used
     * @param worksheetModel Model which determines the input for this EngineRunner
     */
    public EngineRunner(Engine engine,
            WorksheetModel worksheetModel) {
        this.engine = engine;
        this.worksheetModel = worksheetModel;
    }
    
    //
    private Expression result;
    
    /**
     * Return the latest result generated by the engine (may be null).
     */
    public Expression getResult() {
        return this.result;
    }
    
    //
    public enum Status {
        STOPPED,
        GENERATOR,
        INVARIANTS,
        CONJECTURE;
    }
    
    //
    private Status status = Status.STOPPED;
    
    //
    public Status getStatus() {
        return status;
    }
    
    //
    private void setStatus(Status status) {
        this.status = status;
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                fireStateChanged();
            }
        });
    }
    
    /**
     * Extract the invariant values from the table and run the engine. Changes are
     * reported to all listeners in the event dispatching thread, even when this
     * method is run in a different thread (which is generally a good idea).
     */
    public void run() {
        worksheetModel.writeStateToHistory(); //history
        this.result = null;
        List<Invariant> allowedInvariantList = new DefaultInvariantList();
        for(Invariant inv : worksheetModel.getInvariantList())
            if(engine.allows(inv))
                allowedInvariantList.add(inv);
        int numberOfInvariants = allowedInvariantList.size();
        int numberOfGenerators = worksheetModel.getGeneratorInstanceList().size();
        int numberOfRows = worksheetModel.getGraphList().size();
        
        if (numberOfInvariants == 0 || numberOfGenerators + numberOfRows == 0)
            return; // nothing to do
        
        setStatus(Status.GENERATOR);
        
        // gather invariants
        Invariant[] invariants = new Invariant[numberOfInvariants];
        for (int i = 0; i < numberOfInvariants; i++)
            invariants[i] = allowedInvariantList.get(i);

        // make sure the invariants are lexically sorted
        Arrays.sort(invariants, new InvariantIdComparator());
        
        // get filter
        GraphFilter filter = worksheetModel.getFilter();
        
        // gather invariant values from generators
        InvariantListGeneratorSink sink = new FilteredInvariantListGeneratorSink(invariants, filter);
        for (GraphGeneratorInstance instance : worksheetModel.getGeneratorInstanceList()) {
            try {
                instance.generate(sink);
            } catch (GeneratorException ex) {
                Logger.getLogger("org.grinvin.generators").log(Level.WARNING, "Failed to generate graphs from generator '" + instance.getName() + "' (" + instance.getId() + ")", ex);
                setStatus(Status.STOPPED);
                return;
            }
        }
        List<InvariantValue[]> values = sink.getListOfInvariantValues();

        setStatus(Status.INVARIANTS);
        
        // get invariant values from graph table
        for (GraphListElement gle : worksheetModel.getGraphList()) {
            if(filter.accepts(gle.getBundle())){
                InvariantValue[] graphvalues = new InvariantValue[numberOfInvariants];
                for (int i=0; i < numberOfInvariants; i++)
                    graphvalues[i] = gle.getInvariant(invariants[i]);
                values.add(graphvalues);
            }
        }
        
        // create array with values
        InvariantValue[][] valuesArray = new InvariantValue[values.size()][invariants.length];
        int i=0;
        for(InvariantValue[] ivs : values){
            valuesArray[i++] = ivs;
        }
               
        // compute conjecture
        setStatus(Status.CONJECTURE);
        this.result = engine.run(valuesArray);
        
        // that's all folks'
        setStatus(Status.STOPPED);
    }
    
    private static class InvariantIdComparator implements Comparator<Invariant> {
        
        InvariantIdComparator() {
            // avoids creating accessor type
        }
        
        public int compare(Invariant inv1, Invariant inv2) {
            return inv1.getId().compareTo(inv2.getId());
        }
        
    }

}