001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.scenarios.standard;
025    
026    import java.awt.Color;
027    import java.awt.Graphics2D;
028    import java.awt.Rectangle;
029    
030    import se.liu.ida.critiquer.activities.Activity;
031    import se.liu.ida.critiquer.activities.ActivityConsistencyCheck;
032    import se.liu.ida.critiquer.activities.parameters.ActivityParameter;
033    import se.liu.ida.critiquer.activities.parameters.LocationParameter;
034    import se.liu.ida.critiquer.activities.parameters.Parameter;
035    import se.liu.ida.critiquer.constraints.StandardConstraint;
036    import se.liu.ida.critiquer.constraints.TextCritic;
037    import se.liu.ida.critiquer.gui.TaskView;
038    import se.liu.ida.critiquer.gui.View;
039    import se.liu.ida.critiquer.mics.Comparer;
040    import se.liu.ida.critiquer.mics.Utils;
041    
042    /**
043     * 
044     * Verify that there is a destination in the activity that should come before
045     * and a static location in the after activity, or a static location in the
046     * beforeActivity and a starting location in the afterActivity
047     * 
048     * @author olale
049     * 
050     */
051    public class CorrectOrdering extends StandardConstraint implements ActivityConsistencyCheck,TextCritic {
052    
053            /**
054             * 
055             */
056            private static final long serialVersionUID = 1L;
057            private Activity inconsistentBeforeActivity;
058            private Activity inconsistentAfterActivity;
059            private String information;
060    
061            /**
062         * 
063         */
064            public CorrectOrdering() {
065                    super();
066                    Activity.addStaticConsistencyChecker(this);
067            }
068    
069            /*
070         * (non-Javadoc)
071         * 
072         * @see se.liu.ida.critiquer.activities.ActivityConsistencyCheck#paramValueCheck(se.liu.ida.critiquer.activities.Activity,
073         *      se.liu.ida.critiquer.activities.parameters.Parameter, T)
074         */
075            public <T> boolean paramValueCheck(Activity a, Parameter<T> p, T newValue) {
076                    // TODO Auto-generated method stub
077                    return true;
078            }
079    
080            /*
081         * (non-Javadoc)
082         * 
083         * @see se.liu.ida.critiquer.activities.ActivityConsistencyCheck#childOfCheck(se.liu.ida.critiquer.activities.Activity,
084         *      se.liu.ida.critiquer.activities.Activity)
085         */
086            public boolean childOfCheck(Activity parentActivity, Activity Child) {
087                    // TODO Auto-generated method stub
088                    return true;
089            }
090    
091            /**
092         * 
093         * Checks the ordering consistency
094         * 
095         * @see se.liu.ida.critiquer.activities.ActivityConsistencyCheck#orderingCheck(se.liu.ida.critiquer.activities.Activity,
096         *      se.liu.ida.critiquer.activities.Activity)
097         */
098            public boolean orderingCheck(Activity beforeActivity, Activity afterActivity) {
099                    boolean case1 = checkCase1(beforeActivity, afterActivity);
100                    boolean case2 = checkCase2(beforeActivity, afterActivity);
101    
102                    boolean ok = case1 || case2;
103                    if (!ok) {
104                            this.inconsistentBeforeActivity=beforeActivity;
105                            this.inconsistentAfterActivity=afterActivity;
106                            information = "There must be either a an end location or a stationary in "+beforeActivity+
107                            " and a stationary location or a start location in "+afterActivity;
108                    } else {
109                            this.inconsistentBeforeActivity=null;
110                            this.inconsistentAfterActivity=null;
111                    }
112                    setConsistent(ok);
113                    return ok;
114            }
115    
116            /**
117         * Case 1: There is a destination in the beforeActivity an a stationary
118         * location in the afterActivity
119         */
120            private boolean checkCase1(Activity beforeActivity, Activity afterActivity) {
121                    return Utils.findFirst(beforeActivity.getParams(), new Comparer<ActivityParameter>() {
122    
123                            public boolean check(ActivityParameter obj) {
124                                    boolean check = false;
125                                    if (obj instanceof LocationParameter) {
126                                            LocationParameter locParam = (LocationParameter) obj;
127                                            if (locParam.getType() == LocationParameter.Type.DEST_LOC) {
128                                                    check = true;
129                                            }
130                                    }
131                                    return check;
132                            }
133    
134                    }) != null && Utils.findFirst(afterActivity.getParams(), new Comparer<ActivityParameter>() {
135    
136                            public boolean check(ActivityParameter obj) {
137                                    boolean check = false;
138                                    if (obj instanceof LocationParameter) {
139                                            LocationParameter locParam = (LocationParameter) obj;
140                                            if (locParam.getType() == LocationParameter.Type.STATIC_LOC) {
141                                                    check = true;
142                                            }
143                                    }
144                                    return check;
145                            }
146    
147                    }) != null;
148            }
149    
150            /**
151         * Case 2: There is a stationary parameter in beforeActivity and a starting
152         * location in afterActivity
153         */
154            private boolean checkCase2(Activity beforeActivity, Activity afterActivity) {
155                    return Utils.findFirst(beforeActivity.getParams(), new Comparer<ActivityParameter>() {
156    
157                            public boolean check(ActivityParameter obj) {
158                                    boolean check = false;
159                                    if (obj instanceof LocationParameter) {
160                                            LocationParameter locParam = (LocationParameter) obj;
161                                            if (locParam.getType() == LocationParameter.Type.STATIC_LOC) {
162                                                    check = true;
163                                            }
164                                    }
165                                    return check;
166                            }
167    
168                    }) != null && Utils.findFirst(afterActivity.getParams(), new Comparer<ActivityParameter>() {
169    
170                            public boolean check(ActivityParameter obj) {
171                                    boolean check = false;
172                                    if (obj instanceof LocationParameter) {
173                                            LocationParameter locParam = (LocationParameter) obj;
174                                            if (locParam.getType() == LocationParameter.Type.ORIG_LOC) {
175                                                    check = true;
176                                            }
177                                    }
178                                    return check;
179                            }
180    
181                    }) != null;
182            }
183    
184            /* (non-Javadoc)
185             * @see se.liu.ida.critiquer.activities.AbstractParamChangedListener#paramChanged(se.liu.ida.critiquer.activities.Activity, se.liu.ida.critiquer.activities.parameters.Parameter)
186             */
187            @Override
188            public <T> void paramChanged(Activity activity, Parameter<T> p) {
189                    // TODO Auto-generated method stub
190                    
191            }
192    
193            /**
194             *
195             * Describe this constraint
196             * 
197             * @see se.liu.ida.critiquer.constraints.SingletonConstraint#getDescription()
198             */
199            public String getDescription() {
200                    return "Ensure that there is a sensible ordering of activities with respect to locations";
201            }
202    
203            
204            
205            /**
206             * 
207             * Make this view applicable to the task view
208             * 
209             * @see se.liu.ida.critiquer.constraints.StandardConstraint#initApplicableViews()
210             */
211            @Override
212            protected void initApplicableViews() {
213                    applicableViews.add(TaskView.class);
214            }
215    
216            /* (non-Javadoc)
217             * @see se.liu.ida.critiquer.gui.ViewRenderingListener#viewUpdated(se.liu.ida.critiquer.gui.View, java.awt.Graphics2D)
218             */
219            public void viewUpdated(View v, Graphics2D g2) {
220                    if (inconsistentBeforeActivity!=null && inconsistentAfterActivity!=null) {
221                            Color oldColor = g2.getColor();
222                            g2.setColor(getColor());
223                            Rectangle area1 = v.getActivityArea(inconsistentBeforeActivity).getBounds();
224                            Rectangle area2 = v.getActivityArea(inconsistentAfterActivity).getBounds();
225                            g2.drawLine((int)area1.getMaxX(), (int)area1.getCenterY(), (int)area2.getX(), (int)area2.getCenterY());
226                            g2.setColor(oldColor);
227                    }
228            }
229    
230            /* (non-Javadoc)
231             * @see se.liu.ida.critiquer.constraints.TextCritic#getText()
232             */
233            public String getText() {
234                    return information;
235            }
236    
237    }