Example #1
0
  /** Test default package versions. */
  public static void testDefaultPackageVersion() throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setProperty("Bundle-Version", "1.2.3");
    a.setProperty("Export-Package", "test.refer");
    Jar jar = a.build();

    Manifest m = jar.getManifest();
    Parameters exports =
        Processor.parseHeader(m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE), null);
    Map<String, String> attrs = exports.get("test.refer");
    assertNotNull(attrs);
    assertEquals("1.2.3", attrs.get("version"));
  }
Example #2
0
  public void train(int[] instanceLengths, String trainfile, File train_forest) throws IOException {

    // System.out.print("About to train. ");
    // System.out.print("Num Feats: " + pipe.dataAlphabet.size());

    int i = 0;
    for (i = 0; i < options.numIters; i++) {

      System.out.print(" Iteration " + i);
      // System.out.println("========================");
      // System.out.println("Iteration: " + i);
      // System.out.println("========================");
      System.out.print("[");

      long start = System.currentTimeMillis();

      trainingIter(instanceLengths, trainfile, train_forest, i + 1);

      long end = System.currentTimeMillis();
      // System.out.println("Training iter took: " + (end-start));
      System.out.println("|Time:" + (end - start) + "]");
    }

    params.averageParams(i * instanceLengths.length);
  }
Example #3
0
  public void train(int[] instanceLengths, int[] ignore, String trainfile, File train_forest)
      throws IOException {

    int i = 0;
    for (i = 0; i < options.numIters; i++) {

      System.out.print(" Iteration " + i);
      System.out.print("[");

      long start = System.currentTimeMillis();

      trainingIter(instanceLengths, ignore, trainfile, train_forest, i + 1);

      long end = System.currentTimeMillis();
      // System.out.println("Training iter took: " + (end-start));
      System.out.println("|Time:" + (end - start) + "]");
    }
    params.averageParams(i * countActualInstances(ignore));
    //	 afm 06-04-08
    if (options.separateLab) {
      LabelClassifier oc =
          new LabelClassifier(
              options, instanceLengths, ignore, trainfile, train_forest, this, pipe);
      try {
        classifier = oc.trainClassifier(100);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
Example #4
0
 @Override
 public void render(Parameters blockParameters, Writer w, RenderHints hints)
     throws FrameworkException {
   log.debug("Error rendering " + blockParameters);
   switch (getType()) {
     case BODY:
       try {
         decorateIntro(hints, w, "error");
         w.write("<h1>" + error.status);
         w.write(": ");
         CharTransformer escape = new Xml(Xml.ESCAPE);
         w.write(escape.transform(error.exception.getMessage()));
         w.write(" ");
         w.write(escape.transform(url));
         w.write("</h1>");
         w.write("<pre>");
         HttpServletRequest request = blockParameters.get(Parameter.REQUEST);
         error.getErrorReport(w, request, escape);
         w.write("</pre>");
         decorateOutro(hints, w);
       } catch (IOException eio) {
         throw new FrameworkException(eio.getMessage(), eio);
       }
       break;
     default:
   }
 }
Example #5
0
 /** Prints the classifier. */
 public void print() {
   System.out.print("  Act: " + action + "   Conf:  ");
   for (int i = 0; i < rep.length; i++) {
     rep[i].print();
   }
   if (parameters != null) parameters.print();
 } // end print
Example #6
0
 public void actionPerformed(ActionEvent e) {
   String defaultHost = Parameters.getParameter("telnet.defaultHost", "localhost");
   String hostname = JOptionPane.showInputDialog(Edit.getFrame(), "Hostname:", defaultHost);
   if (hostname != null) {
     Edit.openFile("telnet://" + hostname);
   }
 }
Example #7
0
 /**
  * Prints the classifier to the specified file.
  *
  * @param fout is the file output where the classifier has to be printed.
  */
 public void print(PrintWriter fout) {
   fout.print(" ");
   for (int i = 0; i < rep.length; i++) {
     rep[i].print(fout);
   }
   fout.print("  " + action + "  ");
   if (parameters != null) parameters.print(fout);
 }
Example #8
0
 public void loadModel(String file) throws Exception {
   ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
   params.parameters = (double[]) in.readObject();
   pipe.dataAlphabet = (Alphabet) in.readObject();
   pipe.typeAlphabet = (Alphabet) in.readObject();
   in.close();
   pipe.closeAlphabets();
 }
Example #9
0
  /**
   * Test if the implementation of "AnnotatedProviderInterface", which is annotated with OSGi
   * R6 @ProviderType, causes import of the api package to use the provider version policy
   */
  public static void testProviderTypeR6() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setPrivatePackage("test.versionpolicy.implemented.osgi");
    b.setProperty("build", "123");

    Jar jar = b.build();
    assertTrue(b.check());
    Manifest m = jar.getManifest();
    m.write(System.err);

    Domain d = Domain.domain(m);
    Parameters params = d.getImportPackage();
    Attrs attrs = params.get("test.version.annotations.osgi");
    assertNotNull(attrs);
    assertEquals("[1.2,1.3)", attrs.get("version"));
  }
Example #10
0
  /**
   * Tests if the implementation of the EventHandler (which is marked as a ConsumerType) causes the
   * import of the api package to use the consumer version policy.
   */
  public static void testConsumerType() throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setPrivatePackage("test.versionpolicy.uses");
    a.setExportPackage("test.versionpolicy.api");
    a.setProperty("build", "123");
    Jar jar = a.build();
    assertTrue(a.check());
    Manifest m = jar.getManifest();
    m.write(System.err);
    Domain d = Domain.domain(m);

    Parameters parameters = d.getImportPackage();
    Attrs attrs = parameters.get("test.versionpolicy.api");
    assertNotNull(attrs);
    assertEquals("[1.2,2)", attrs.get("version"));
  }
Example #11
0
 @SuppressWarnings("unchecked")
 protected void setBlockParametersForRender(State state, Parameters blockParameters) {
   ServletRequest request = state.getRequest();
   String prefix = getPrefix(state);
   log.debug("prefix " + prefix);
   blockParameters.setAutoCasting(true);
   for (Map.Entry<String, String[]> entry :
       ((Map<String, String[]>) request.getParameterMap()).entrySet()) {
     String key = entry.getKey();
     if (key.startsWith(prefix)) {
       log.trace("setting" + entry);
       blockParameters.setIfDefined(key.substring(prefix.length()), entry.getValue());
     }
   }
   if (log.isDebugEnabled()) {
     log.debug("Set " + blockParameters);
   }
 }
Example #12
0
 @Override
 public String getProcessUrl(
     String path, Map<String, ?> parameters, Parameters frameworkParameters, boolean escapeAmps)
     throws FrameworkException {
   HttpServletRequest request =
       BasicUrlConverter.getUserRequest(frameworkParameters.get(Parameter.REQUEST));
   State state = State.getState(request);
   frameworkParameters.set(ACTION, state.getId());
   Url url = urlConverter.getProcessUrl(path, parameters, frameworkParameters, escapeAmps);
   if (url == Url.NOT) {
     log.debug("Fall back url");
     return fallbackConverter
         .getProcessUrl(path, parameters, frameworkParameters, escapeAmps)
         .getUrl();
   } else {
     log.debug("Url converter url " + url);
     return url.getUrl();
   }
 }
Example #13
0
 /**
  * I think in the basic framework this method is never called explicitely, because processing is
  * done implicitly by the render
  */
 @Override
 public void process(
     Processor processor, Parameters blockParameters, Parameters frameworkParameters)
     throws FrameworkException {
   HttpServletRequest request = frameworkParameters.get(Parameter.REQUEST);
   State state = State.getState(request);
   state.startBlock(frameworkParameters, null);
   setBlockParametersForProcess(state, blockParameters);
   processor.process(blockParameters);
 }
Example #14
0
 @Override
 public Block getRenderingBlock(Parameters frameworkParameters) {
   HttpServletRequest request = frameworkParameters.get(Parameter.REQUEST);
   State state = State.getState(request);
   if (state.isRendering()) {
     return state.getBlock();
   } else {
     return null;
   }
 }
Example #15
0
  /**
   * Returns if the classifier of the class subsumes the classifier passed as a parameter.
   *
   * @param cl is the subsumed classifier.
   * @return a boolean indicating if it subsumes
   */
  public boolean doesSubsume(Classifier cl) {

    if (rep.getAction() != cl.rep.getAction()) return false;

    if (parameters.couldSubsume()) {
      if (rep.isMoreGeneral(cl.rep)) {
        return true;
      }
    }
    return false;
  } // end doesSubsume
Example #16
0
  /**
   * Tests if the implementation of the EventAdmin (which is marked as a ProviderType) causes the
   * import of the api package to use the provider version policy.
   */
  public static void testProviderType() throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setPrivatePackage("test.versionpolicy.implemented");
    a.setExportPackage("test.versionpolicy.api");
    a.setImportPackage("test.versionpolicy.api"); // what changed so this is
    // not automatically
    // added?
    a.setProperty("build", "123");
    Jar jar = a.build();
    assertTrue(a.check());
    Manifest m = jar.getManifest();
    m.write(System.err);
    Domain d = Domain.domain(m);

    Parameters parameters = d.getImportPackage();
    Attrs attrs = parameters.get("test.versionpolicy.api");
    assertNotNull(attrs);
    assertEquals("[1.2,1.3)", attrs.get("version"));
  }
Example #17
0
 @SuppressWarnings("unchecked")
 @Override
 public <C> C setSettingValue(Setting<C> setting, Parameters parameters, C value) {
   if (parameters == null)
     throw new SecurityException("You should provide Cloud and request parameters");
   boolean useSession = parameters.get(USE_REQ);
   if (useSession) {
     C ret = getSettingValue(setting, parameters);
     HttpServletRequest req = parameters.get(Parameter.REQUEST);
     req.getSession(true).setAttribute(getKey(setting), value);
     return ret;
   } else {
     Cloud cloud = parameters.get(Parameter.CLOUD);
     if (cloud.getUser().getRank() == org.mmbase.security.Rank.ADMIN) {
       saveSettingValue(setting, value);
       return (C) settingValues.put(setting, value);
     } else {
       throw new SecurityException("Permission denied");
     }
   }
 }
Example #18
0
  /** Check implementation version policy. Uses the package test.versionpolicy.(uses|implemented) */
  static void assertPolicy(String pack, String type) throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setProperty("Export-Package", "test.versionpolicy.api");
    Jar jar = a.build();

    Builder b = new Builder();
    b.addClasspath(jar);
    b.addClasspath(new File("bin"));

    b.setProperty("-versionpolicy-impl", "IMPL");
    b.setProperty("-versionpolicy-uses", "USES");
    b.setProperty("Private-Package", pack);
    b.build();
    Manifest m = b.getJar().getManifest();
    m.write(System.err);
    Map<String, String> map = b.getImports().getByFQN("test.versionpolicy.api");
    assertNotNull(map);
    // String s = map.get(Constants.IMPLEMENTED_DIRECTIVE);
    // assertEquals("true", s);
    Parameters mp = Processor.parseHeader(m.getMainAttributes().getValue("Import-Package"), null);
    assertEquals(type, mp.get("test.versionpolicy.api").get("version"));
  }
Example #19
0
  public void loadModel(String file) throws Exception {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
    params.parameters = (double[]) in.readObject();
    pipe.dataAlphabet = (Alphabet) in.readObject();
    pipe.typeAlphabet = (Alphabet) in.readObject();

    // afm 06-04-08
    if (options.separateLab) {
      classifier = (Classifier) in.readObject();
    }

    in.close();
    pipe.closeAlphabets();
  }
Example #20
0
 @SuppressWarnings("unchecked")
 @Override
 public <C> C getSettingValue(Setting<C> setting, Parameters parameters) {
   boolean useSession = parameters != null && parameters.get(USE_REQ);
   if (useSession) {
     HttpServletRequest req = parameters.get(Parameter.REQUEST);
     if (req != null) {
       Object v = req.getSession(true).getAttribute(getKey(setting));
       if (v != null) {
         return setting.getDataType().cast(v, null, null);
       }
     }
   }
   if (settingValues.containsKey(setting)) {
     return (C) settingValues.get(setting);
   } else {
     C settingValue = loadSettingValue(setting);
     if (settingValue != null) {
       settingValues.put(setting, settingValue);
       return settingValue;
     }
     return setting.getDataType().getDefaultValue();
   }
 }
Example #21
0
  /**
   * Returns if the classifier of the class subsumes the classifier passed as a parameter.
   *
   * @param cl is the subsumed classifier.
   * @return a boolean indicating if it subsumes
   */
  public boolean doesSubsume(Classifier cl) {
    int i;

    // First, check if the condition is the same
    if (action != cl.getAction()) return false;

    // Then, check that is more general
    if (parameters.couldSubsume()) {
      for (i = 0; i < rep.length; i++) {
        if (!rep[i].isMoreGeneral(cl.rep[i])) return false;
      }
      return true;
    }
    return false;
  } // end doesSubsume
Example #22
0
 @Override
 public Node getUserNode(Parameters frameworkParameters) {
   Cloud cloud = frameworkParameters.get(Parameter.CLOUD);
   if (cloud == null) {
     return null;
   } else {
     try {
       int userNode = cloud.getCloudContext().getAuthentication().getNode(cloud.getUser());
       if (cloud.hasNode(userNode)) {
         return cloud.getNode(userNode);
       } else {
         return null;
       }
     } catch (UnsupportedOperationException uoe) {
       // never mind
       return null;
     }
   }
 }
Example #23
0
  /**
   * Prints the desnormalized classifier to the specified file.
   *
   * @param fout is the file output where the classifier has to be printed.
   */
  public void printNotNorm(PrintWriter fout) {
    int i = 0;
    fout.print(" ");
    try {
      for (i = 0; i < rep.length; i++) {
        if (Config.typeOfAttributes[i].equals("ternary")) {
          rep[i].print(fout);
        } else if (Config.typeOfAttributes[i].equals("integer")) {
          rep[i].printNotNorm(fout, Config.attBounds[i][0]);
        } else if (Config.typeOfAttributes[i].equals("real")) {
          rep[i].printNotNorm(fout, Config.attBounds[i][0], Config.attBounds[i][1]);
        }
      }
    } catch (Exception e) {
      System.out.println("Exception when printing the attribute: " + i);
      e.printStackTrace();
    }
    fout.print("\t " + (String) Config.classConv.elementAt(action));

    if (parameters != null) parameters.print(fout);
  }
Example #24
0
 /**
  * Returns if the classifier can subsume. The classifier has to be sufficiently accurate and
  * sufficiently experienced to subsume another classifier.
  *
  * @return a boolean indicating if the classifier can subsume.
  */
 public boolean couldSubsume() {
   return parameters.couldSubsume();
 } // end couldSubsume
  public CommandData parseCommandData(ArtifactData artifact) throws Exception {
    File source = new File(artifact.file);
    if (!source.isFile()) throw new FileNotFoundException();

    CommandData data = new CommandData();
    data.sha = artifact.sha;
    data.jpmRepoDir = repoDir.getCanonicalPath();
    JarFile jar = new JarFile(source);
    try {
      reporter.trace("Parsing %s", source);
      Manifest m = jar.getManifest();
      Attributes main = m.getMainAttributes();
      data.name = data.bsn = main.getValue("Bundle-SymbolicName");
      String version = main.getValue("Bundle-Version");
      if (version == null) data.version = Version.LOWEST;
      else data.version = new Version(version);

      data.main = main.getValue("Main-Class");
      data.description = main.getValue("Bundle-Description");
      data.title = main.getValue("JPM-Name");

      reporter.trace("name " + data.name + " " + data.main + " " + data.title);
      DependencyCollector path = new DependencyCollector(this);
      path.add(artifact);
      DependencyCollector bundles = new DependencyCollector(this);
      if (main.getValue("JPM-Classpath") != null) {
        Parameters requires = OSGiHeader.parseHeader(main.getValue("JPM-Classpath"));

        for (Map.Entry<String, Attrs> e : requires.entrySet()) {
          path.add(e.getKey(), e.getValue().get("name")); // coordinate
        }
      } else if (!artifact.local) { // No JPM-Classpath, falling back to
        // server's revision
        // Iterable<RevisionRef> closure =
        // library.getClosure(artifact.sha,
        // false);
        // System.out.println("getting closure " + artifact.url + " " +
        // Strings.join("\n",closure));

        // if (closure != null) {
        // for (RevisionRef ref : closure) {
        // path.add(Hex.toHexString(ref.revision));
        // }
        // }
      }

      if (main.getValue("JPM-Runbundles") != null) {
        Parameters jpmrunbundles = OSGiHeader.parseHeader(main.getValue("JPM-Runbundles"));

        for (Map.Entry<String, Attrs> e : jpmrunbundles.entrySet()) {
          bundles.add(e.getKey(), e.getValue().get("name"));
        }
      }

      reporter.trace("collect digests runpath");
      data.dependencies.addAll(path.getDigests());
      reporter.trace("collect digests bundles");
      data.runbundles.addAll(bundles.getDigests());

      Parameters command = OSGiHeader.parseHeader(main.getValue("JPM-Command"));
      if (command.size() > 1) reporter.error("Only one command can be specified");

      for (Map.Entry<String, Attrs> e : command.entrySet()) {
        data.name = e.getKey();

        Attrs attrs = e.getValue();

        if (attrs.containsKey("jvmargs")) data.jvmArgs = attrs.get("jvmargs");

        if (attrs.containsKey("title")) data.title = attrs.get("title");

        if (data.title != null) data.title = data.name;
      }
      return data;
    } finally {
      jar.close();
    }
  }
Example #26
0
  private void trainingIter(int[] instanceLengths, String trainfile, File train_forest, int iter)
      throws IOException {

    int numUpd = 0;
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(train_forest));
    boolean evaluateI = true;

    int numInstances = instanceLengths.length;

    for (int i = 0; i < numInstances; i++) {
      if ((i + 1) % 500 == 0) {
        System.out.print((i + 1) + ",");
        // System.out.println("  "+(i+1)+" instances");
      }

      int length = instanceLengths[i];

      // Get production crap.
      FeatureVector[][][] fvs = new FeatureVector[length][length][2];
      double[][][] probs = new double[length][length][2];
      FeatureVector[][][][] nt_fvs = new FeatureVector[length][pipe.types.length][2][2];
      double[][][][] nt_probs = new double[length][pipe.types.length][2][2];
      FeatureVector[][][] fvs_trips = new FeatureVector[length][length][length];
      double[][][] probs_trips = new double[length][length][length];
      FeatureVector[][][] fvs_sibs = new FeatureVector[length][length][2];
      double[][][] probs_sibs = new double[length][length][2];

      DependencyInstance inst;

      if (options.secondOrder) {
        inst =
            ((DependencyPipe2O) pipe)
                .readInstance(
                    in,
                    length,
                    fvs,
                    probs,
                    fvs_trips,
                    probs_trips,
                    fvs_sibs,
                    probs_sibs,
                    nt_fvs,
                    nt_probs,
                    params);
      } else inst = pipe.readInstance(in, length, fvs, probs, nt_fvs, nt_probs, params);

      double upd =
          (double) (options.numIters * numInstances - (numInstances * (iter - 1) + (i + 1)) + 1);
      int K = options.trainK;
      Object[][] d = null;
      if (options.decodeType.equals("proj")) {
        if (options.secondOrder)
          d =
              ((DependencyDecoder2O) decoder)
                  .decodeProjective(
                      inst,
                      fvs,
                      probs,
                      fvs_trips,
                      probs_trips,
                      fvs_sibs,
                      probs_sibs,
                      nt_fvs,
                      nt_probs,
                      K);
        else d = decoder.decodeProjective(inst, fvs, probs, nt_fvs, nt_probs, K);
      }
      if (options.decodeType.equals("non-proj")) {
        if (options.secondOrder)
          d =
              ((DependencyDecoder2O) decoder)
                  .decodeNonProjective(
                      inst,
                      fvs,
                      probs,
                      fvs_trips,
                      probs_trips,
                      fvs_sibs,
                      probs_sibs,
                      nt_fvs,
                      nt_probs,
                      K);
        else d = decoder.decodeNonProjective(inst, fvs, probs, nt_fvs, nt_probs, K);
      }
      params.updateParamsMIRA(inst, d, upd);
    }

    // System.out.println("");
    // System.out.println("  "+numInstances+" instances");

    System.out.print(numInstances);

    in.close();
  }
Example #27
0
 /**
  * Sets the time stamp for this classifier.
  *
  * @param tStamp is the time stamp
  */
 public void setTime(int tStamp) {
   parameters.setTime(tStamp);
 } // end setTime
Example #28
0
 /** Sets the numerosity of the classifier. */
 public void setNumerosity(int num) {
   parameters.setNumerosity(num);
 } // end setNumerosity
Example #29
0
 /**
  * Computes the generality of the classifier and stores it in it corresponding parameter in the
  * class Parameters.
  */
 public void calculateGenerality() {
   parameters.setGenerality(getGenerality());
 } // end calculateGenerality
Example #30
0
 /**
  * Increases the numerosity of the classifier.
  *
  * @param num is the number to increase the numerosity of the classifier.
  */
 public void increaseNumerosity(int num) {
   parameters.increaseNumerosity(num);
 } // end increaseNumerosity