/*
   * Analyse the descriptor and any <repository>s that might be part of it
   */
  private void analyze(Repository repository) throws Exception {
    info("== Analyzing feature descriptor ==");
    info(" - read %s", file.getAbsolutePath());

    features.add(repository.getFeatures());

    // add the repositories from the plugin configuration
    if (repositories != null) {
      for (String uri : repositories) {
        getLog().info(String.format(" - adding repository from %s", uri));
        Repository dependency = new RepositoryImpl(URI.create(translateFromMaven(uri)));
        schemaCheck(dependency, URI.create(uri));
        features.add(dependency.getFeatures());
        validateBundlesAvailable(dependency);
        analyzeExports(dependency);
      }
    }

    for (URI uri : repository.getRepositories()) {
      Artifact artifact = (Artifact) resolve(uri.toString());
      Repository dependency =
          new RepositoryImpl(new File(localRepo.getBasedir(), localRepo.pathOf(artifact)).toURI());

      schemaCheck(dependency, uri);
      getLog()
          .info(
              String.format(
                  " - adding %d known features from %s", dependency.getFeatures().length, uri));
      features.add(dependency.getFeatures());
      // we need to do this to get all the information ready for further processing
      validateBundlesAvailable(dependency);
      analyzeExports(dependency);
    }
  }
示例#2
0
 public void train(int maxIter) {
   features.setEmpiricalWeight(sampleList.size());
   for (int i = 0; i < maxIter; ++i) {
     features.updateModelWeight(sampleList);
     if (features.isConvergence(0.435)) {
       break;
     }
   }
 }
示例#3
0
 public void loadData(String path) throws IOException {
   BufferedReader br = new BufferedReader(new FileReader(new File(path)));
   String line = br.readLine();
   while (line != null) {
     String[] segs = line.split("\\s");
     String label = segs[0];
     List<String> featureList = new ArrayList<String>();
     for (int i = 1; i < segs.length; ++i) {
       featureList.add(segs[i]);
       features.addFeature(label, segs[i]);
     }
     features.setMaxFeaturePerSample(featureList.size());
     Sample sample = new Sample(label, featureList);
     sampleList.add(sample);
     labelSet.add(label);
     line = br.readLine();
   }
 }
  /*
   * Validate if all imports for a feature are being matched with exports
   */
  private void validateImportsExports(Feature feature) throws Exception {
    Map<Clause, String> imports = new HashMap<Clause, String>();
    Set<Clause> exports = new HashSet<Clause>();
    for (Dependency dependency : feature.getDependencies()) {
      if (featureExports.containsKey(dependency.getName())) {
        exports.addAll(featureExports.get(dependency.getName()));
      } else {
        validateImportsExports(features.get(dependency.getName(), dependency.getVersion()));
      }
    }
    for (String bundle : getBundleLocations(feature)) {
      Manifest meta = manifests.get(bundles.get(bundle));
      exports.addAll(getExports(meta));
      for (Clause clause : getMandatoryImports(meta)) {
        imports.put(clause, bundle);
      }
    }

    // setting up the set of required imports
    Set<Clause> requirements = new HashSet<Clause>();
    requirements.addAll(imports.keySet());

    // now, let's remove requirements whenever we find a matching export for them
    for (Clause element : imports.keySet()) {
      if (systemExports.contains(element.getName())) {
        debug("%s is resolved by a system bundle export or provided bundle", element);
        requirements.remove(element);
        continue;
      }
      for (Clause export : exports) {
        if (matches(element, export)) {
          debug("%s is resolved by export %s", element, export);
          requirements.remove(element);
          continue;
        }
        debug("%s is not resolved by export %s", element, export);
      }
    }

    // if there are any more requirements left here, there's a problem with the feature
    if (!requirements.isEmpty()) {
      warn("Failed to validate feature %s", feature.getName());
      for (Clause entry : requirements) {
        warn("No export found to match %s (imported by %s)", entry, imports.get(entry));
      }
      throw new Exception(
          String.format(
              "%d unresolved imports in feature %s", requirements.size(), feature.getName()));
    }
    info("    OK: imports resolved for %s", feature.getName());
    featureExports.put(feature.getName(), exports);
  }
示例#5
0
 // calculate p(y|x)=sum_i(alpha_i*f_i(x,y))/sum_y(sum_i(alpha_i*f_i(x,y))).
 public Map<String, Double> predict(List<String> x) {
   Map<String, Double> probMap = new HashMap<String, Double>();
   double sum = 0;
   for (String label : labelSet) {
     double prob = 0;
     for (String feature : x) {
       prob += features.getModelWeight(label, feature);
     }
     prob = Math.exp(prob);
     probMap.put(label, prob);
     sum += prob;
   }
   for (String label : probMap.keySet()) {
     probMap.put(label, probMap.get(label) / sum);
   }
   return probMap;
 }