/** * @see #get(Class, String, java.util.Map) * @param klass * @param name * @param <T> * @return * @throws ConfigurationException */ public <T> T get(Class<T> klass, String name) throws ConfigurationException { return get(klass, name, null); }
/** * Returns a default set of LuceneOptions. * * @return a default set of LuceneOptions */ public static LuceneOptions getDefaultOptions() { try { return new Configurator(new Configuration()).get(LuceneOptions.class, "plaintext"); } catch (ConfigurationException e) { throw new RuntimeException(e); } }
@Override public String describeDisambiguator() { if (!config.hasPath("disambiguator")){ return "none"; } String disambigName = config.getString("disambiguator"); try { Map dc = configurator.getConfig(Disambiguator.class, disambigName).root().unwrapped(); String phraseName = null; if (dc.containsKey("phraseAnalyzer")) { phraseName = (String) dc.get("phraseAnalyzer"); } if (phraseName == null || phraseName.equals("default")) { phraseName = configurator.getConf().get().getString("phrases.analyzer.default"); } dc.put("phraseAnalyzer", phraseName); return disambigName + "=" + dc.toString(); } catch (ConfigurationException e) { throw new IllegalStateException(e); } }
name = resolveComponentName(klass, name); Config config = getConfig(klass, name); Map<String, Object> cache = components.get(klass); String key = makeCacheKey(name, runtimeParams); synchronized (cache) { if (cache.containsKey(key)) { return (T) cache.get(key); } else { Pair<Provider, T> pair = constructInternal(klass, name, config, runtimeParams); if (pair.getLeft().getScope() == Provider.Scope.SINGLETON) { cache.put(key, pair.getRight());
@Override public PhraseAnalyzer get(String name, Config config, Map<String, String> runtimeParams) throws ConfigurationException { if (!config.getString("type").equals("anchortext")) { return null; } PhraseAnalyzerDao paDao = getConfigurator().construct( PhraseAnalyzerDao.class, name, config.getConfig("dao"), new HashMap<String, String>()); LocalPageDao lpDao = getConfigurator().get(LocalPageDao.class, config.getString("localPageDao")); LocalLinkDao llDao = getConfigurator().get(LocalLinkDao.class, config.getString("localLinkDao")); PrunedCounts.Pruner<String> phrasePruner = getConfigurator().construct( PrunedCounts.Pruner.class, null, config.getConfig("phrasePruner"), null); PrunedCounts.Pruner<Integer> pagePruner = getConfigurator().construct( PrunedCounts.Pruner.class, null, config.getConfig("pagePruner"), null); return new AnchorTextPhraseAnalyzer(paDao, lpDao, llDao, phrasePruner, pagePruner); } }
protected static void configureBase(Configurator configurator, BaseSRMetric sr, Config config) throws ConfigurationException { Config rootConfig = configurator.getConf().get(); File path = new File(rootConfig.getString("sr.metric.path")); sr.setDataDir(FileUtils.getFile(path, sr.getName(), sr.getLanguage().getLangCode())); // initialize normalizers sr.setSimilarityNormalizer(configurator.get(Normalizer.class, config.getString("similaritynormalizer"))); sr.setMostSimilarNormalizer(configurator.get(Normalizer.class, config.getString("mostsimilarnormalizer"))); boolean isTraining = rootConfig.getBoolean("sr.metric.training"); if (isTraining) { sr.setReadNormalizers(false); } if (config.hasPath("buildMostSimilarCache")) { sr.setBuildMostSimilarCache(config.getBoolean("buildMostSimilarCache")); } try { sr.read(); } catch (IOException e){ throw new ConfigurationException(e); } LOG.info("finished base configuration of metric " + sr.getName()); } }
@Override public boolean runDiagnostic(PrintWriter writer) { Config config = null; try { config = env.getConfigurator().getConfig(WpDataSource.class, null); } catch (ConfigurationException e) { throw new IllegalStateException(e); } boolean passed = true; try { WpDataSource ds = env.getConfigurator().get(WpDataSource.class); ds.getConnection().close(); writer.write("Connection to database succeeded. Active configuration:\n"); } catch (Exception e) { writer.write("Connection to database FAILED! Active configuration:\n"); passed = false; } for (Map.Entry<String, ConfigValue > entry : config.entrySet()) { writer.write("\t" + entry.getKey() + ": " + entry.getValue().render() + "\n"); } return passed; } }
public Config getMetricConfig(String name) throws ConfigurationException { return env.getConfigurator().getConfig(SRMetric.class, name); }
configurator = new Configurator(configuration);
public SRBuilder(Env env, String metricName, Language language) throws ConfigurationException { this.env = env; this.language = language; this.config = env.getConfiguration(); this.srDir = new File(config.get().getString("sr.metric.path")); datasetNames = config.get().getStringList("sr.dataset.defaultsets"); // Properly resolve the default metric name. this.metricName = env.getConfigurator().resolveComponentName(SRMetric.class, metricName); if (!srDir.isDirectory()) { srDir.mkdirs(); } }
@Override public SRMetric create() { try { Map<String, String> runtimeParams = new HashMap<String, String>(); runtimeParams.put("language", language.getLangCode()); return configurator.construct(SRMetric.class, name, config, runtimeParams); } catch (ConfigurationException e) { throw new RuntimeException(e); } }
@Override public PhraseAnalyzer get(String name, Config config, Map<String, String> runtimeParams) throws ConfigurationException { if (!config.getString("type").equals("stanford")) { return null; } PhraseAnalyzerDao paDao = getConfigurator().construct( PhraseAnalyzerDao.class, name, config.getConfig("dao"), new HashMap<String, String>()); LocalPageDao lpDao = getConfigurator().get(LocalPageDao.class, config.getString("localPageDao")); File path = new File(config.getString("path")); PrunedCounts.Pruner<String> phrasePruner = getConfigurator().construct( PrunedCounts.Pruner.class, null, config.getConfig("phrasePruner"), null); PrunedCounts.Pruner<Integer> pagePruner = getConfigurator().construct( PrunedCounts.Pruner.class, null, config.getConfig("pagePruner"), null); return new StanfordPhraseAnalyzer(paDao, lpDao, phrasePruner, pagePruner, path); } }
File path = new File(conf.getConf().getFile("download.path"), "wikidata.json.bz2"); if (!path.isFile()) { File tf = File.createTempFile("wikidata.json", null); WikidataDao wdDao = conf.get(WikidataDao.class); UniversalPageDao upDao = conf.get(UniversalPageDao.class); MetaInfoDao metaDao = conf.get(MetaInfoDao.class); LanguageSet langs = conf.get(LanguageSet.class);
@Override public boolean runDiagnostic(PrintWriter writer) { Config config = null; try { config = env.getConfigurator().getConfig(WpDataSource.class, null); } catch (ConfigurationException e) { throw new IllegalStateException(e); } boolean passed = true; try { WpDataSource ds = env.getConfigurator().get(WpDataSource.class); ds.getConnection().close(); writer.write("Connection to database succeeded. Active configuration:\n"); } catch (Exception e) { writer.write("Connection to database FAILED! Active configuration:\n"); passed = false; } for (Map.Entry<String, ConfigValue > entry : config.entrySet()) { writer.write("\t" + entry.getKey() + ": " + entry.getValue().render() + "\n"); } return passed; } }
name = resolveComponentName(klass, name); Config config = getConfig(klass, name); Map<String, Object> cache = components.get(klass); String key = makeCacheKey(name, runtimeParams); synchronized (cache) { if (cache.containsKey(key)) { return (T) cache.get(key); } else { Pair<Provider, T> pair = constructInternal(klass, name, config, runtimeParams); if (pair.getLeft().getScope() == Provider.Scope.SINGLETON) { cache.put(key, pair.getRight());
/** * Constructs a new factory that creates an sr metric with a particular name from the config * file. The overrides take precedence over any configuration parameters, and are relative to * the innermost configuration block for an SR metric (i.e. the nested dictionary with key * the name of the metric). * * @param language * @param configurator * @param name Name of metric from configuration file * @param configOverrides Optional configuration overrides, or null. * @throws ConfigurationException */ public ConfigMonolingualSRFactory(Language language, Configurator configurator, String name, Map<String, Object> configOverrides) throws ConfigurationException { this.config = ConfigFactory.empty(); if (configOverrides != null) { config = config.withFallback(ConfigFactory.parseMap(configOverrides)); } config = config.withFallback(configurator.getConfig(SRMetric.class, name)); this.configurator = configurator; this.name = name; this.language = language; }
/** * Returns the config object associated with the given class and name. * @param klass The generic interface or superclass, not the specific implementation. * @param name The name of the class as it appears in the config file. If name is null, * the configurator tries to guess by looking for a "default" entry in * the config that provides the name for a default implementation or, if * there is exactly one implementation returning it. Otherwise, if name is * null it throws an error. * @return The requested config object. * @throws ConfigurationException */ public Config getConfig(Class klass, String name) throws ConfigurationException { if (!providers.containsKey(klass)) { throw new ConfigurationException("No registered providers for components with class " + klass); } ProviderSet pset = providers.get(klass); name = resolveComponentName(klass, name); String path = pset.path + "." + name; if (!conf.get().hasPath(path)) { throw new ConfigurationException("Configuration path " + path + " does not exist"); } return conf.get().getConfig(path); }
/** * Get a specific named instance of the component with the specified class. * This method can only be used when there is exactly one instance of the component. * * @param klass The generic interface or superclass, not the specific implementation. * @return The requested component. */ public <T> T get(Class<T> klass) throws ConfigurationException { return get(klass, null); }