2 Replies Latest reply: Sep 12, 2011 3:53 AM by cash1981 RSS

    Typesafe Simple PTQL for ProcessFinder

    cash1981 Hot Shot

      I have created a typesafe simple PTQL for ProcessFinder.
      I found that it was error prone to use the string based query, and thus I created a type safe way of creating simple PTQL queries.

      Currently Env.* and Modules.*  are not supported.

      The SimplePTQL class looks like this: http://code.google.com/p/jodconverter/source/browse/branches/3.0-sigar/jodconverter-core/src/main/java/org/artofsolving/jodconverter/sigar/SimplePTQL.java

      You use it like this:

      SimplePTQL ptql = new SimplePTQL.Builder(SimplePTQL.STATE_NAME(), SimplePTQL.EQ(), "java").createQuery();

      Assert.assertEquals(ptql.getQuery(), "State.Name.eq=java");

      ptql = new SimplePTQL.Builder(SimplePTQL.STATE_NAME(), SimplePTQL.EQ(), "Hopefully There is no Process called this").createQuery();
      List<Long> find2 = spm.find(ptql);
      Assert.assertTrue(find2.size() == 0);
      ptql = new SimplePTQL.Builder(SimplePTQL.PID_PID(), SimplePTQL.GT(), "1").createQuery();
      List<Long> find3 = spm.find(ptql);
      Assert.assertTrue(find3.size() > 1);

      And this is how you use the Args.

      public void realArguments() throws Exception {
      if (PlatformUtils.isWindows()) {
           throw new SkipException("Sleep only works on unix");
      Process process = new ProcessBuilder("sleep", "60s").start();
      SimplePTQL ptql = new SimplePTQL.Builder(SimplePTQL.STATE_NAME(), SimplePTQL.EQ(), "sleep")
      .addArgs(1, SimplePTQL.EQ(), "60s", Strategy.NOT_ESCAPE).createQuery();

      long[] pids = processFinder.find(ptql.getQuery());
      Assert.assertEquals(pids,pids.length == 1);

      //Proving arguments work
      public void args() throws Exception {
      SimplePTQL ptql = new SimplePTQL.Builder(SimplePTQL.STATE_NAME(), SimplePTQL.RE(), "office.*")
      .addArgs(1, SimplePTQL.RE(), "\\Qpipe,name,office1\\E", Strategy.ESCAPE)

      Assert.assertEquals(ptql.getQuery(), "State.Name.re=office.*,Args.1.re=pipe.name.office1");
      ptql = new SimplePTQL.Builder(SimplePTQL.STATE_NAME(), SimplePTQL.EQ(), "office.*")
      .addArgs(1, SimplePTQL.RE(), "\\Qpipe,name,office1\\E", Strategy.ESCAPE)
      .addArgs(2, SimplePTQL.EQ(), "\\Qpipe,name,office2\\E", Strategy.ESCAPE)
      Assert.assertEquals(ptql.getQuery(), "State.Name.eq=office.*,Args.1.re=pipe.name.office1,Args.2.eq=pipe.name.office2");

      Any comments/feedback appreciated.
      I am sure there might be errors in the code, and I also want to include Env and Modules, however, I don't know how they work since I couldn't find any documentation on them. Would love to include support for that as well.

      PS: Feel free to include this in the distribution, or work on it if you feel to do so

      I wrote a blog post about it here: http://shervinasgari.blogspot.com/2011/03/api-helper-wrapper-for-processfinder-in.html

      Regards Shervin

      Added link to blog post