001/**
002 * Copyright (C) 2007-2009, 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 */
020package org.dllearner.server;
021
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.Iterator;
025import java.util.Map;
026import java.util.Random;
027import java.util.Set;
028
029import org.dllearner.core.AbstractComponent;
030import org.dllearner.core.AbstractKnowledgeSource;
031import org.dllearner.core.AbstractCELA;
032import org.dllearner.core.AbstractClassExpressionLearningProblem;
033import org.dllearner.core.AbstractReasonerComponent;
034import org.dllearner.kb.OWLFile;
035import org.dllearner.kb.sparql.SparqlKnowledgeSource;
036import org.dllearner.kb.sparql.SparqlQuery;
037
038/**
039 * Stores the state of a DL-Learner client session.
040 * 
041 * @author Jens Lehmann
042 *
043 */
044public class ClientState {
045
046        // stores the mapping between component IDs and component
047        // (note that this allows us to keep all references to components even
048        // if they are not used anymore e.g. a deleted knowledge source)
049        private Map<Integer,AbstractComponent> componentIDs = new HashMap<>();
050        
051        private Set<AbstractKnowledgeSource> knowledgeSources = new HashSet<>();
052        
053        private Map<Integer, SparqlQuery> queryIDs = new HashMap<>();
054        
055        private AbstractClassExpressionLearningProblem learningProblem;
056        
057        private AbstractReasonerComponent reasonerComponent;
058        
059        private AbstractCELA learningAlgorithm;
060
061        private Random rand=new Random();
062        
063        private boolean isAlgorithmRunning = false;
064        
065        private int generateComponentID(AbstractComponent component) {
066                int id;
067                do {
068                        id = rand.nextInt();
069                } while(componentIDs.keySet().contains(id));
070                componentIDs.put(id, component);
071                return id;              
072        }
073        
074        private int generateQueryID(SparqlQuery query) {
075                int id;
076                Random rand = new Random();
077                do {
078                        id = rand.nextInt();
079                } while (queryIDs.keySet().contains(id));
080                queryIDs.put(id, query);
081                return id;
082        }
083        
084        public int addQuery(SparqlQuery query){
085                return this.generateQueryID(query);
086        }
087        
088        public SparqlQuery getQuery(int id){
089                return queryIDs.get(id);
090        }
091        
092        /**
093         * @return the isAlgorithmRunning
094         */
095        @Deprecated
096        public boolean isAlgorithmRunning() {
097                return isAlgorithmRunning;
098        }
099
100        /**
101         * @param isAlgorithmRunning the isAlgorithmRunning to set
102         */
103        @Deprecated
104        public void setAlgorithmRunning(boolean isAlgorithmRunning) {
105                this.isAlgorithmRunning = isAlgorithmRunning;
106        }
107        
108//      public Component getComponent(Class<? extends Component> componentClass) throws UnknownComponentException {
109//              if(learningProblem.getClass().equals(componentClass))
110//                      return learningProblem;
111//              else if(learningAlgorithm.getClass().equals(componentClass))
112//                      return learningAlgorithm;
113//              else if(reasonerComponent.getClass().equals(componentClass))
114//                      return reasonerComponent;
115//              else if(KnowledgeSource.class.isAssignableFrom(componentClass)) {
116//                      
117//                      
118//                      for(KnowledgeSource ks : knowledgeSources) {
119//                              if(ks.getClass().equals(componentClass))
120//                                      return ks;
121//                      }
122//                      throw new UnknownComponentException(componentClass.getName());
123//              } else
124//                      throw new UnknownComponentException(componentClass.getName());
125//      }
126
127        /**
128         * Removes a knowledge source with the given URL (independant of its type).
129         * @param url URL of the OWL file or SPARQL Endpoint.
130         * @return True if a knowledge source was deleted, false otherwise.
131         */
132        public boolean removeKnowledgeSource(String url) {
133                Iterator<AbstractKnowledgeSource> it = knowledgeSources.iterator(); 
134                while(it.hasNext()) {
135                        AbstractKnowledgeSource source = it.next();
136                        if((source instanceof OWLFile && ((OWLFile)source).getURL().toString().equals(url))
137                                || (source instanceof SparqlKnowledgeSource && ((SparqlKnowledgeSource)source).getURL().toString().equals(url)) ) {
138                                it.remove();
139                                return true;
140                        }
141                }
142                return false;
143        }
144
145        /**
146         * @return the learningProblem
147         */
148        public AbstractClassExpressionLearningProblem getLearningProblem() {
149                return learningProblem;
150        }
151
152        /**
153         * @param learningProblem the learningProblem to set
154         */
155        public int setLearningProblem(AbstractClassExpressionLearningProblem learningProblem) {
156                this.learningProblem = learningProblem;
157                return generateComponentID(learningProblem);
158        }
159
160        /**
161         * @return the reasonerComponent
162         */
163        public AbstractReasonerComponent getReasonerComponent() {
164                return reasonerComponent;
165        }
166
167        /**
168         * Sets the reasoner component and creates the corresponding
169         * <code>ReasonerComponent</code> instance.
170         * 
171         * @param reasonerComponent the reasonerComponent to set
172         */
173        public int setReasonerComponent(AbstractReasonerComponent reasonerComponent) {
174                this.reasonerComponent = reasonerComponent;
175//              reasoningService = new ReasonerComponent(reasonerComponent);
176                return generateComponentID(reasonerComponent);
177        }
178
179        /**
180         * @return the learningAlgorithm
181         */
182        public AbstractCELA getLearningAlgorithm() {
183                return learningAlgorithm;
184        }
185
186        /**
187         * @param learningAlgorithm the learningAlgorithm to set
188         */
189        public int setLearningAlgorithm(AbstractCELA learningAlgorithm) {
190                this.learningAlgorithm = learningAlgorithm;
191                return generateComponentID(learningAlgorithm);
192        }
193
194        /**
195         * @param id A component ID.
196         * @return The component associated with this ID.
197         * @see java.util.Map#get(java.lang.Object)
198         */
199        public AbstractComponent getComponent(int id) {
200                return componentIDs.get(id);
201        }
202
203        /**
204         * Adds a knowledge source to the client session. Use the 
205         * returned value to refer to this knowledge source.
206         * @param ks The knowledge source to add.
207         * @return The component ID for the newly added knowledge source.
208         */
209        public int addKnowledgeSource(AbstractKnowledgeSource ks) {
210                knowledgeSources.add(ks);
211                return generateComponentID(ks);
212                
213        }
214
215        public boolean removeKnowledgeSource(int componentID) {
216                return knowledgeSources.remove(componentIDs.get(componentID));
217        }
218        
219        /**
220         * @return the knowledgeSources
221         */
222        public Set<AbstractKnowledgeSource> getKnowledgeSources() {
223                return knowledgeSources;
224        }
225}