001package com.astrolabsoftware.FinkBrowser.Januser;
002
003import com.Lomikel.Utils.Pair;
004import com.Lomikel.Utils.LomikelException;
005
006// Java
007import java.util.Map;
008import java.util.HashMap;
009
010/** <code>Classifier</code> classifies sources.
011  * @opt attributes
012  * @opt operations
013  * @opt types
014  * @opt visibility
015  * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
016public abstract class Classifier {
017  
018  public static Classifier instance(Type type) throws LomikelException {
019    return instance(type, "");
020    }
021    
022  public static Classifier instance(String name) throws LomikelException {
023    String[] fullName = name.split("=");
024    if (fullName.length == 2) {
025      return instance(fullName[0], fullName[1]);
026      }
027    else if (fullName.length == 1) {
028      return instance(name, "");
029      }
030    else {
031      throw new LomikelException("Unknown Classifier name " + name);
032      }
033    }
034 
035  public static Classifier instance(String name,
036                                    String flavor) throws LomikelException {
037    return instance(Type.valueOf(name), flavor);
038    }
039    
040  /** Give singleton {@link Classifier} for each type and flavor.
041    * @param type The {@link Classifier} {@link Type}.
042    * @param flavor The {@link Classifier} flavor.
043    * @throws LomikelException If {@link Classifier} cannot be created. */
044  public static Classifier instance(Type   type,
045                                    String flavor) throws LomikelException {
046    Pair<Type, String> signature = Pair.of(type, flavor);
047    if (!_classifiers.containsKey(signature)) {
048      Classifier cl;
049      switch (type) {
050        case FINK_PORTAL:
051          cl = new FinkPortalClassifier();
052          break;
053        case FINK:
054          cl = new FinkClassifier();
055          break;
056        case XMATCH:
057          cl = new XMatchClassifier();
058          break;
059        case FEATURES:
060          cl = new FeaturesClassifier();
061          break;
062        case LIGHTCURVES:
063          cl = new LightCurvesClassifier();
064          break;
065        case TAG:
066          cl = new TagClassifier();
067          break;
068        default:
069          throw new LomikelException("Unknown Classifier Type " + type);
070        }
071      cl.setType(type);
072      cl.setFlavor(flavor);
073      _classifiers.put(signature, cl);
074      }
075    return _classifiers.get(signature);
076    }
077    
078  /** Classify <em>source</em> and expand them to alerts (if requested).
079    * It should register classes corresponding to specified <tt>objectId</tt>
080    * using {@link FinkGremlinRecipies#registerSoI(Classifiers, String, String, double, String, String, boolean, String)}.
081    * @param recipies   The {@link FinkGremlinRecipies} caller.
082    * @param oid        The <tt>objectId</tt> of source to be added.
083    * @throws LomikelException If anything fails. */
084  public abstract void classify(FinkGremlinRecipies recipies,
085                                String              oid) throws LomikelException;
086  
087  /** Set {@link Classifier} flavor.
088    * @param flavor The {@link Classifier} flavor. */
089  protected void setFlavor(String flavor) {
090    _flavor = flavor;
091    }  
092    
093  protected void setType(Type type) {
094    _type = type;
095    }    
096  
097  public String flavor() {
098    return _flavor;
099    }
100  
101  public String name() {
102    return _type.name();
103    }
104    
105  public String toString() {
106    return name() + "[" + flavor() + "]";
107    }
108    
109  private Type _type;  
110    
111  private String _flavor;
112  
113  private static Map<Pair<Type, String>, Classifier> _classifiers = new HashMap<>();
114  
115  /** The type of {@link Classifier}. Each {@link Type} can exist in many flavors. */
116  public static enum Type {
117    FINK_PORTAL,
118    FINK,
119    XMATCH,
120    FEATURES,
121    LIGHTCURVES,
122    TAG
123    }
124
125  }