code: public static void trainIfNeeded(DistanceMetric dm, DataSet dataset, boolean parallel)
      {
          if(!(dm instanceof TrainableDistanceMetric))
              return;
          TrainableDistanceMetric tdm = (TrainableDistanceMetric) dm;
          if(!tdm.needsTraining())
              return;
          if(dataset instanceof RegressionDataSet)
              tdm.train((RegressionDataSet) dataset, parallel);
          else if(dataset instanceof ClassificationDataSet)
              tdm.train((ClassificationDataSet) dataset, parallel);
          else
              tdm.train(dataset, parallel);
      }
docs: Static helper method for training a distance metric only if it is needed.
  This method can be safely called for any Distance Metric.
  
  @param dm the distance metric to train
  @param dataset the data set to train from
  @param parallel {@code true} if multiple threads should be used for
  training. {@code false} if it should be done in a single-threaded manner.