Ejemplo n.º 1
0
  private static boolean assignableAndPrimitivableWithVarargs(
      ParameterTypes paramTypes, IRubyObject... args) {
    // bail out if this is not a varargs method
    if (!paramTypes.isVarArgs()) return false;

    Class[] types = paramTypes.getParameterTypes();

    Class varArgArrayType = types[types.length - 1];
    Class varArgType = varArgArrayType.getComponentType();

    // if there's no args, we only match when there's just varargs
    if (args.length == 0) {
      return types.length <= 1;
    }

    // dig out as many trailing args as will fit, ensuring they match varargs type
    int nonVarargs = types.length - 1;
    for (int i = args.length - 1; i >= nonVarargs; i--) {
      if (!(ASSIGNABLE.match(varArgType, args[i]) || PRIMITIVABLE.match(varArgType, args[i]))) {
        return false;
      }
    }

    // check remaining args
    for (int i = 0; i < nonVarargs; i++) {
      if (!(ASSIGNABLE.match(types[i], args[i]) || PRIMITIVABLE.match(types[i], args[i]))) {
        return false;
      }
    }
    return true;
  }
Ejemplo n.º 2
0
  private static boolean assignableOrDuckable(ParameterTypes paramTypes, IRubyObject... args) {
    Class[] types = paramTypes.getParameterTypes();

    if (args.length != types.length) return false;

    for (int i = 0; i < types.length; i++) {
      if (!(ASSIGNABLE.match(types[i], args[i]) || DUCKABLE.match(types[i], args[i]))) {
        return false;
      }
    }
    return true;
  }
 /**
  * Looks for a list item that matches the given value and selects the one that is found.
  *
  * @param value the value to look for
  * @param matcher the object used to match the given value with the list item values
  */
 protected void setSelectedValue(String value, Matcher<String> matcher) {
   // First, see if the given value is already selected.
   if (getSelectedIndex() >= 0 && matcher.match(getValue(getSelectedIndex()), value)) {
     return;
   }
   // Look for a matching value.
   for (int i = getItemCount() - 1; i >= 0; i--) {
     if (matcher.match(getValue(i), value)) {
       setSelectedIndex(i);
       return;
     }
   }
   // No matching value found.
   setSelectedIndex(-1);
 }
Ejemplo n.º 4
0
  private static List<ParameterTypes> findCallable(
      ParameterTypes[] callables, IRubyObject... args) {
    List<ParameterTypes> retainedCallables = new ArrayList<ParameterTypes>(callables.length);
    List<ParameterTypes> incomingCallables =
        new ArrayList<ParameterTypes>(Arrays.asList(callables));

    for (int currentArg = 0; currentArg < args.length; currentArg++) {
      retainedCallables.clear();
      for (Matcher matcher : MATCH_SEQUENCE) {
        for (Iterator<ParameterTypes> callableIter = incomingCallables.iterator();
            callableIter.hasNext(); ) {
          ParameterTypes callable = callableIter.next();
          Class[] types = callable.getParameterTypes();

          if (matcher.match(types[currentArg], args[currentArg])) {
            callableIter.remove();
            retainedCallables.add(callable);
          }
        }
      }
      incomingCallables.clear();
      incomingCallables.addAll(retainedCallables);
    }

    return retainedCallables;
  }
Ejemplo n.º 5
0
  public boolean isMatchAt(
      Playfield<E> haystack,
      Playfield<E> needle,
      Matcher<E, E> m,
      IntegerElement x,
      IntegerElement y) {
    IntegerElement width = needle.getMaxX().subtract(needle.getMinX()).succ();
    IntegerElement height = needle.getMaxY().subtract(needle.getMinY()).succ();

    if (x.pred().add(width).compareTo(haystack.getMaxX()) > 0
        || y.pred().add(height).compareTo(haystack.getMaxY()) > 0) {
      // exceeds the right or bottom edge, so, no
      return false;
    }
    IntegerElement cx, cy, dx, dy;
    for (cx = x, dx = needle.getMinX();
        dx.compareTo(needle.getMaxX()) <= 0;
        cx = cx.succ(), dx = dx.succ()) {
      for (cy = y, dy = needle.getMinY();
          dy.compareTo(needle.getMinY()) <= 0;
          cy = cy.succ(), dy = dy.succ()) {
        E soughtElem = needle.get(dx, dy);
        E foundElem = haystack.get(cx, cy);
        // System.out.printf("sought (%s,%s): '%s', found (%s,%s): '%s'\n",
        //     dx, dy, soughtElem.getName(),
        //     cx, cy, foundElem.getName());
        if (m.match(soughtElem, foundElem)) {
          // add something to the result-list
        } else {
          return false;
        }
      }
    }
    return true;
  }
Ejemplo n.º 6
0
 public void match(
     final String expression, final Handler<Triple> handler, final TweetContext context)
     throws MatcherException {
   for (Matcher m : componentMatchers) {
     m.match(expression, handler, context);
   }
 }
  public void testMatch() {
    Matcher matcher = new Matcher();

    int[] expected = new int[] {10, 50, 30, 98};
    int clipLimit = 100;
    int delta = 5;

    int[] actual = new int[] {12, 55, 25, 110};

    assertTrue(matcher.match(expected, actual, clipLimit, delta));

    actual = new int[] {10, 60, 30, 98};
    assertTrue(!matcher.match(expected, actual, clipLimit, delta));

    actual = new int[] {10, 50, 30};
    assertTrue(!matcher.match(expected, actual, clipLimit, delta));
  }
Ejemplo n.º 8
0
  public String readLine() throws IOException {
    StringBuffer sb = new StringBuffer();
    boolean lineIsOnlyWhiteSpace = true;
    boolean done = false;
    int i = read();
    while ((i != -1) && !done) {
      switch (i) {
        case 13: // '\r'
          i = read();
          if (i != 10) {
            throw new IOException("Bad line terminator");
          }
          // fall through
        case 10: // '\n'
          if (lineIsOnlyWhiteSpace) {
            sb.setLength(0);
          }
          mark(1);
          i = read();
          reset();
          switch (i) {
            case 9: // '\t'
            case 32: // ' '
              skip(1L);

              break;
            default:
              if (!lineIsOnlyWhiteSpace) {
                done = true;

                continue;
              }
              skip(1L);
          }

          break;
        case 9: // '\t'
        case 32: // ' '
          sb.append((char) i);
          i = read();

          break;
        default:
          sb.append((char) i);
          if (matcher.match(sb)) {
            return sb.toString().trim();
          }
          i = read();
          lineIsOnlyWhiteSpace = false;
      }
      if (lineIsOnlyWhiteSpace) {
        return null;
      }
    }

    return sb.toString().trim();
  }
 @Override
 public StorageResults fetch(Expression userQuery) {
   Expression expression = userQuery.normalize();
   Matcher matcher = expression.accept(new MatcherCreator());
   List<DataRecord> matchRecords = new LinkedList<DataRecord>();
   for (DataRecord dataRecord : storage) {
     if (matcher.match(dataRecord)) {
       matchRecords.add(dataRecord);
     }
   }
   List<DataRecord> filteredRecords = expression.accept(new Filter(matchRecords));
   return new InMemoryStorageResults(filteredRecords);
 }
Ejemplo n.º 10
0
 @Test
 public void basicTest() throws GrammarException, IOException {
   String[] rules = {
     //
     "<ROOT> = <a> <s> <b>", //
     "<a> = 'a'", //
     "<b> = 'b'", //
     "<s> = /\\s++/", //
   };
   Grammar g = new Grammar(rules);
   String s = "a\nb";
   Matcher m = g.find(s);
   assertNotNull("found joe", m.match());
 }
Ejemplo n.º 11
0
  public static void main(String[] args) {
    String newSnapFile, oldSnapFile, targetClass;
    boolean use_statics = false;

    if (args.length < 5) {
      System.out.println(
          "Usage: main old_snapshot_dir1 old_snapshot_dir2 new_snapshot_dir classname fieldname");
      System.exit(-1);
    }

    File old_dir1 = new File(args[0]);
    File old_dir2 = new File(args[1]);
    File new_dir = new File(args[2]);
    FieldPath synthField = new FieldPath(args[4]);
    if (args.length == 6)
      if (args[5].equals("-static")) {
        use_statics = true;
        System.out.println("Using static fields.");
      }
    System.out.println("synthField = " + synthField);
    targetClass = args[3];
    File[] old_snapshots1 = old_dir1.listFiles();
    File[] old_snapshots2 = old_dir2.listFiles();
    File[] new_snapshots = new_dir.listFiles();
    if (old_snapshots1.length != old_snapshots2.length
        || old_snapshots1.length != new_snapshots.length) {
      System.out.println("Directories contain unequal numbers of snapshot files.");
      System.exit(-1);
    }
    int num_snapshots = old_snapshots1.length;
    assert (num_snapshots == new_snapshots.length);

    System.out.println("Loading heap snapshots...");
    LinkedList<HeapSnapshot> oldSnaps1 = new LinkedList<HeapSnapshot>();
    LinkedList<HeapSnapshot> oldSnaps2 = new LinkedList<HeapSnapshot>();
    LinkedList<HeapSnapshot> newSnaps = new LinkedList<HeapSnapshot>();
    for (int i = 0; i < num_snapshots; i++) {
      System.out.println("Loading old snapshot 1...");
      HeapSnapshot snap = new HeapSnapshot(old_snapshots1[i]);
      oldSnaps1.add(snap);
      System.out.println("Loading old snapshot 2...");
      snap = new HeapSnapshot(old_snapshots2[i]);
      oldSnaps2.add(snap);
      System.out.println("Loading new snapshot...");
      snap = new HeapSnapshot(new_snapshots[i]);
      newSnaps.add(snap);
    }

    Stats stats = new Stats(oldSnaps1, oldSnaps2, newSnaps, targetClass);
    stats.printStats();

    /* TODO: resurrect this code at some point
    if(newSets.getFirst().getFields().contains(new FieldPath(synthField))) {
    	System.out.println("Field is present in examples.");
    }
    else {
    	System.out.println("Field is not present in examples.");
    	System.out.println("Field list:");
    	System.out.println(newSets.getFirst().getFields().toString());
    	System.exit(-1);
    }
    */

    long start = System.currentTimeMillis();
    Matcher matcher = new Matcher(oldSnaps1, oldSnaps2, newSnaps, use_statics);
    List<Example> exList = matcher.match(targetClass, true, synthField);
    long match_time = System.currentTimeMillis() - start;

    System.out.println("Synthesizing update for field: " + synthField);
    SynthSwitch part = null;
    System.out.println("Number of example pairs: " + exList.size());

    Example.examplesToHTML(exList, "examples.html");

    // Remove example pairs not containing the field.
    System.out.println("Removing examples not containing field " + synthField);
    Iterator<Example> iter = exList.iterator();
    while (iter.hasNext()) {
      Example ex = iter.next();
      if (ex.getOld() == null
          || ex.getNew() == null
          ||
          /* !ex.getOld().containsField(synthField) ||*/
          !ex.getNew().containsField(synthField)) iter.remove();
    }
    System.out.println("Number of example pairs: " + exList.size());

    start = System.currentTimeMillis();
    int exNum = 0;
    for (Example ex : exList) {
      System.out.println("Example " + exNum++ + "\n");
      if (part == null) part = SynthSwitch.getSynthFun(targetClass, synthField, ex);
      else part.expand(ex);
    }
    System.out.print("Number of partitions: ");
    if (part != null) {
      System.out.println(part.numPartitions());
      System.out.println("Synthesized function:");
      System.out.println("type = " + part.getClass());
      System.out.println(part);
    } else System.out.println("Synthesis failed.");
    long synth_time = System.currentTimeMillis() - start;
    System.out.println("Match time: " + ((double) match_time) / 1000);
    System.out.println("Synth time: " + ((double) synth_time) / 1000);
  }