001    /**
002     * Copyright (C) 2007-2008, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     * 
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner 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, see <http://www.gnu.org/licenses/>.
018     *
019     */
020    
021    package org.dllearner.tools.ore;
022    
023    import java.awt.Color;
024    import java.awt.Component;
025    import java.awt.Cursor;
026    import java.awt.event.ActionListener;
027    import java.awt.event.MouseEvent;
028    import java.awt.event.MouseListener;
029    import java.util.Map;
030    import java.util.Set;
031    
032    import javax.swing.JLabel;
033    import javax.swing.JMenu;
034    import javax.swing.JPopupMenu;
035    import javax.swing.ToolTipManager;
036    
037    import org.dllearner.core.owl.Description;
038    import org.dllearner.core.owl.Individual;
039    import org.dllearner.core.owl.NamedClass;
040    import org.dllearner.core.owl.Negation;
041    import org.dllearner.core.owl.ObjectAllRestriction;
042    import org.dllearner.core.owl.ObjectCardinalityRestriction;
043    import org.dllearner.core.owl.ObjectSomeRestriction;
044    import org.dllearner.core.owl.Thing;
045    
046    /**
047     * Label that might have menu items when clicked on it.
048     * @author Lorenz Buehmann
049     *
050     */
051    public class DescriptionLabel extends JLabel implements MouseListener{
052            /**
053             * 
054             */
055            private static final long serialVersionUID = 1L;
056    
057            private static final int MOVE_TO_CLASS = 0;
058            private static final int MOVE_FROM_CLASS = 1;
059            private static final int ADD_CLASS = 2;
060            private static final int REMOVE_CLASS = 3;
061            private static final int ADD_PROPERTY = 4;
062            private static final int REMOVE_RANGE_PROPERTY = 5;
063            private static final int DELETE_PROPERTY = 6;
064            private static final int REMOVE_NOT_RANGE_PROPERTY = 7;
065    
066            private final Description desc;
067            private Individual ind;
068            private ORE ore;
069            private JPopupMenu menu;
070            private String mode;
071            private String descriptionLabel;
072    
073            private String baseURI;
074            private Map<String, String> prefixes;
075    
076            /**
077             * constructor.
078             * 
079             * @param d
080             * @param mode
081             */
082            public DescriptionLabel(Description d, String mode) {
083                    super();
084                    this.desc = d;
085                    this.mode = mode;
086                    setForeground(Color.red);
087                    addMouseListener(this);
088    
089            }
090            
091            /**
092             * initialize description label with solution.
093             */
094            public void init(){
095                    baseURI = ore.getBaseURI();
096                    prefixes = ore.getPrefixes();
097                    
098                    setText(((Description) desc).toManchesterSyntaxString(ore.getBaseURI(), ore.getPrefixes()));
099                    menu = new JPopupMenu();
100                    ToolTipManager.sharedInstance().setDismissDelay(7000);
101                    //negative example solutions
102                    if(mode.equals("neg")){
103                            if(!(desc instanceof Negation)){
104                                    if(desc instanceof NamedClass){                                                                                                                         //1. description is a named class
105                                            descriptionLabel = desc.toManchesterSyntaxString(baseURI, prefixes);
106                                            menu.add(new DescriptionMenuItem(REMOVE_CLASS, descriptionLabel, desc));              //1.a remove class assertion
107                                            
108                                            JMenu dme = new JMenu("move class assertion " + descriptionLabel + " to ...");                  //1.b move individual
109                                            for(NamedClass nc : ore.getpossibleClassesMoveTo(ind)){
110                                                    DescriptionMenuItem move = new DescriptionMenuItem(MOVE_TO_CLASS, nc.toManchesterSyntaxString(baseURI, prefixes), (NamedClass) desc);
111                                                    move.setName(nc.toString());
112                                                    dme.add(move);
113                                                    Set<NamedClass> complements = ore.getComplements(nc, ind);                                                        //check for complement error
114                                                    if(!(complements.isEmpty() || (complements.size() == 1 && complements.toArray()[0].toString().equals(desc.toString())))){
115                                                            move.setEnabled(false);
116                                                            StringBuffer strBuf = new StringBuffer();
117                                                            strBuf.append("<html>class assertion not possible because individual<br> " 
118                                                                                    + "is still asserted to its complements:<br><BLOCKQUOTE>");
119                                                            
120                                                            for(NamedClass n: complements){
121                                                                    strBuf.append("<br><b>" + n + "</b>");
122                                                            }
123                                                            strBuf.append("</BLOCKQUOTE></html>");
124            
125                                                    
126                                                            move.setToolTipText(strBuf.toString());
127                                                    }
128                                            }
129                                            menu.add(dme);
130                                    } else if(desc instanceof ObjectSomeRestriction){                                                                                                               //2. description is a object some restriction
131                                            String propertyName = ((ObjectSomeRestriction) desc).getRole().toString(baseURI, prefixes);
132                                            String propertyRange = ((ObjectSomeRestriction) desc).getChild(0).toManchesterSyntaxString(baseURI, prefixes);
133                                            menu.add(new DescriptionMenuItem(DELETE_PROPERTY, propertyName , desc));                                                //2.a remove all property assertions
134                                            if (!(desc.getChild(0) instanceof Thing)){ 
135                                                    menu.add(new DescriptionMenuItem(REMOVE_RANGE_PROPERTY, propertyRange, desc));                          //2.b remove property assertions with objects in range
136                                            }
137                                    } else if(desc instanceof ObjectAllRestriction){                                                                                                                //3. description is a object all restriction
138                                            if (!(desc.getChild(0) instanceof Thing)) {
139                                                    JMenu dme = new JMenu("add property assertion " + ((ObjectAllRestriction) desc).getRole().toKBSyntaxString(baseURI, prefixes)   //3.a add property assertion with object not in range
140                                                                    + " with object ...");
141                                                    for (Individual i : ore.getIndividualsNotInPropertyRange((ObjectAllRestriction) desc, ind)){
142                                                            dme.add(new DescriptionMenuItem(ADD_PROPERTY, i.toManchesterSyntaxString(baseURI, prefixes), desc));
143                                                    }
144                                                            menu.add(dme);
145                                            }
146                                    }
147            
148                            } else if(desc instanceof Negation){
149                                    if(desc.getChild(0) instanceof NamedClass){                                                                                                                     //4. description is a negated named class
150                                            DescriptionMenuItem addItem = new DescriptionMenuItem(ADD_CLASS, desc.getChild(0).toManchesterSyntaxString(baseURI, prefixes), desc.getChild(0));
151                                            menu.add(addItem);                                                                                                                                                              //4.a add class assertion
152                                            Set<NamedClass> complements = ore.getComplements(desc.getChild(0), ind);                                          //check for complement errors
153                                            if(!complements.isEmpty()){
154                                                    addItem.setEnabled(false);
155                                                    StringBuffer strBuf = new StringBuffer();
156                                                    strBuf.append("<html>class assertion not possible because individual<br> " 
157                                                                              + "is still asserted to its complements:<br><BLOCKQUOTE>");
158                                                    
159                                                    for(NamedClass n: complements){
160                                                            strBuf.append("<br><b>" + n + "</b>");
161                                                    }
162                                                    strBuf.append("</BLOCKQUOTE></html>");
163    
164                                            
165                                                    addItem.setToolTipText(strBuf.toString());
166                                            }
167                                    }
168                            }
169                    } else if(mode.equals("pos")){//positive example solutions
170                            if(!(desc instanceof Negation)){
171                                    if(desc instanceof NamedClass){
172                                            DescriptionMenuItem add = new DescriptionMenuItem(ADD_CLASS, desc.toManchesterSyntaxString(baseURI, prefixes), desc);
173                                            Set<NamedClass> complements = ore.getComplements(desc, ind);
174                                            if(!(complements.isEmpty())){
175                                                    add.setEnabled(false);
176                                                    StringBuffer strBuf = new StringBuffer();
177                                                    strBuf.append("<html>class assertion not possible because individual<br> " 
178                                                                             + "is still asserted to its complements:<br><BLOCKQUOTE>");
179                                                    
180                                                    for(NamedClass n: complements){
181                                                            strBuf.append("<br><b>" + n + "</b>");
182                                                    }
183                                                    strBuf.append("</BLOCKQUOTE></html>");
184    
185                                            
186                                                    add.setToolTipText(strBuf.toString());
187                                            }
188                                            menu.add(add);
189                                            
190                                            Set<NamedClass> moveClasses = ore.getpossibleClassesMoveFrom(ind);
191                                            if(moveClasses.size() > 0){
192                                                    JMenu move = new JMenu("move to " + desc.toManchesterSyntaxString(baseURI, prefixes) + " from ...");
193                                                    for (NamedClass m : moveClasses){System.out.println("hier" + m);
194                                                            DescriptionMenuItem item = new DescriptionMenuItem(MOVE_FROM_CLASS, m.toManchesterSyntaxString(baseURI, prefixes), desc);
195                                                            item.setName(m.toString());
196                                                            move.add(item);
197                                                            
198                                                            if(!(complements.isEmpty() || (complements.size() == 1 && complements.toArray()[0].toString().equals(m.toString())))){
199                                                                    move.setEnabled(false);
200                                                                    StringBuffer strBuf = new StringBuffer();
201                                                                    strBuf.append("<html>moving class is not possible because individual<br> "
202                                                                                             + "is still asserted to its complements:<br><BLOCKQUOTE>");
203                                                                    
204                                                                    for(NamedClass n: complements){
205                                                                            strBuf.append("<br><b>" + n + "</b>");
206                                                                    }
207                                                                    strBuf.append("</BLOCKQUOTE></html>");
208                    
209                                                            
210                                                                    move.setToolTipText(strBuf.toString());
211                                                            }
212                                                    }
213                                                    menu.add(move);
214                                                                    
215                                            }
216                                            
217                                            
218                                    } else if(desc instanceof ObjectSomeRestriction){
219                                            JMenu dme = new JMenu("add property assertion " + ((ObjectSomeRestriction) desc).getRole()
220                                                            + " with object ...");
221                                            for (Individual i : ore.getIndividualsInPropertyRange((ObjectSomeRestriction) desc, ind)){
222                                                    dme.add(new DescriptionMenuItem(ADD_PROPERTY, i.toManchesterSyntaxString(baseURI, prefixes), desc));
223                                            }
224                                                    menu.add(dme);
225                                            
226                                    } else if(desc instanceof ObjectAllRestriction){
227                                            if (!(desc.getChild(0) instanceof Thing)) {
228                                                    menu.add(new DescriptionMenuItem(REMOVE_NOT_RANGE_PROPERTY, ((ObjectAllRestriction) desc).getChild(0).toString(baseURI, prefixes), desc));
229                                                    menu.add(new DescriptionMenuItem(DELETE_PROPERTY, ((ObjectAllRestriction) desc).getRole().toString(baseURI, prefixes), desc));
230                                            }
231                                    }
232                                            
233                                    
234                            }
235                            
236                    } else if(desc instanceof Negation){
237                            if(desc.getChild(0) instanceof NamedClass){
238                                    descriptionLabel = desc.toManchesterSyntaxString(baseURI, prefixes);
239                                    menu.add(new DescriptionMenuItem(REMOVE_CLASS, descriptionLabel, desc));   
240                            }
241                    }
242            }
243    
244            
245            /**
246             * returns actual description.
247             * @return desc Description
248             */
249            public Description getDescription(){
250                    return desc;
251            }
252            
253            /**
254             * adds action listeners to menu items.
255             * @param aL ActionListener
256             */
257            public void addActionListeners(ActionListener aL){
258                    for(Component c : menu.getComponents()){
259                            if(c instanceof DescriptionMenuItem){
260                                    ((DescriptionMenuItem) c).addActionListener(aL);
261                            } else if(c instanceof JMenu){
262                                    for(int i = 0; i < ((JMenu) c).getItemCount(); i++){
263                                            ((JMenu) c).getItem(i).addActionListener(aL);
264                                    }
265                            }
266                                    
267                            
268                    }
269            
270                    
271            }
272    
273            public void setIndOre(ORE ore, Individual ind) {
274                    this.ore = ore;
275                    this.ind = ind;
276            }
277    
278            public void mouseClicked(MouseEvent e) {
279                    menu.show(this.getParent(), getLocation().x, getLocation().y + 50);
280    
281            }
282    
283            /**
284             * Underlining label when mouse over.
285             */
286            public void mouseEntered(MouseEvent e) {
287                    setText("<html><u>"
288                                    + ((Description) desc).toManchesterSyntaxString(ore
289                                                    .getBaseURI(), ore.getPrefixes()) + "</u></html>");
290                    setCursor(new Cursor(Cursor.HAND_CURSOR));
291                    if(desc instanceof ObjectCardinalityRestriction){
292                            setToolTipText("ObjectCardinality repair not available at present");
293                    }
294    
295            }
296    
297            /**
298             * Removing underlining when mosue relased.
299             */
300            public void mouseExited(MouseEvent e) {
301                    setText(((Description) desc).toManchesterSyntaxString(ore.getBaseURI(),
302                                    ore.getPrefixes()));
303                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
304            }
305    
306            public void mousePressed(MouseEvent e) {
307                    // TODO Auto-generated method stub
308    
309            }
310    
311            public void mouseReleased(MouseEvent e) {
312                    // TODO Auto-generated method stub
313    
314            }
315    }