/**
  * Search anything.
  *
  * @throws ParseError if an error has been encountered while compiling
  */
 @Test
 public void testAny() throws ParseError {
   final SearchCompiler.Match c = SearchCompiler.compile("foo");
   assertTrue(c.match(newPrimitive("foobar", "true")));
   assertTrue(c.match(newPrimitive("name", "hello-foo-xy")));
   assertFalse(c.match(newPrimitive("name", "X")));
 }
 /**
  * Search by equality key=value.
  *
  * @throws ParseError if an error has been encountered while compiling
  */
 @Test
 public void testEquals() throws ParseError {
   final SearchCompiler.Match c = SearchCompiler.compile("foo=bar");
   assertFalse(c.match(newPrimitive("foobar", "true")));
   assertTrue(c.match(newPrimitive("foo", "bar")));
   assertFalse(c.match(newPrimitive("fooX", "bar")));
   assertFalse(c.match(newPrimitive("foo", "barX")));
 }
 private SearchContext(String state) throws ParseError {
   m = SearchCompiler.compile(state);
   n = SearchCompiler.compile('-' + state);
   ds.addPrimitive(n1);
   ds.addPrimitive(n2);
   w1.addNode(n1);
   w1.addNode(n2);
   w2.addNode(n1);
   w2.addNode(n2);
   ds.addPrimitive(w1);
   ds.addPrimitive(w2);
   r1.addMember(new RelationMember("", w1));
   r1.addMember(new RelationMember("", w2));
   r2.addMember(new RelationMember("", w1));
   r2.addMember(new RelationMember("", w2));
   ds.addPrimitive(r1);
   ds.addPrimitive(r2);
 }
Beispiel #4
0
    @Override
    protected void realRun() {
      try {
        foundMatches = 0;
        SearchCompiler.Match matcher = SearchCompiler.compile(setting);

        if (setting.mode == SearchMode.replace) {
          selection.clear();
        } else if (setting.mode == SearchMode.in_selection) {
          foundMatches = selection.size();
        }

        Collection<OsmPrimitive> all;
        if (setting.allElements) {
          all = ds.allPrimitives();
        } else {
          all = ds.getPrimitives(OsmPrimitive::isSelectable);
        }
        final ProgressMonitor subMonitor =
            getProgressMonitor().createSubTaskMonitor(all.size(), false);
        subMonitor.beginTask(
            trn("Searching in {0} object", "Searching in {0} objects", all.size(), all.size()));

        for (OsmPrimitive osm : all) {
          if (canceled) {
            return;
          }
          if (setting.mode == SearchMode.replace) {
            if (matcher.match(osm)) {
              selection.add(osm);
              ++foundMatches;
            }
          } else if (setting.mode == SearchMode.add && !predicate.test(osm) && matcher.match(osm)) {
            selection.add(osm);
            ++foundMatches;
          } else if (setting.mode == SearchMode.remove
              && predicate.test(osm)
              && matcher.match(osm)) {
            selection.remove(osm);
            ++foundMatches;
          } else if (setting.mode == SearchMode.in_selection
              && predicate.test(osm)
              && !matcher.match(osm)) {
            selection.remove(osm);
            --foundMatches;
          }
          subMonitor.worked(1);
        }
        subMonitor.finishTask();
      } catch (ParseError e) {
        Main.debug(e);
        JOptionPane.showMessageDialog(
            Main.parent, e.getMessage(), tr("Error"), JOptionPane.ERROR_MESSAGE);
      }
    }
  /**
   * Search by comparison.
   *
   * @throws ParseError if an error has been encountered while compiling
   */
  @Test
  public void testCompare() throws ParseError {
    final SearchCompiler.Match c1 = SearchCompiler.compile("start_date>1950");
    assertTrue(c1.match(newPrimitive("start_date", "1950-01-01")));
    assertTrue(c1.match(newPrimitive("start_date", "1960")));
    assertFalse(c1.match(newPrimitive("start_date", "1950")));
    assertFalse(c1.match(newPrimitive("start_date", "1000")));
    assertTrue(c1.match(newPrimitive("start_date", "101010")));

    final SearchCompiler.Match c2 = SearchCompiler.compile("start_date<1960");
    assertTrue(c2.match(newPrimitive("start_date", "1950-01-01")));
    assertFalse(c2.match(newPrimitive("start_date", "1960")));
    assertTrue(c2.match(newPrimitive("start_date", "1950")));
    assertTrue(c2.match(newPrimitive("start_date", "1000")));
    assertTrue(c2.match(newPrimitive("start_date", "200")));

    final SearchCompiler.Match c3 = SearchCompiler.compile("name<I");
    assertTrue(c3.match(newPrimitive("name", "Alpha")));
    assertFalse(c3.match(newPrimitive("name", "Sigma")));

    final SearchCompiler.Match c4 = SearchCompiler.compile("\"start_date\"<1960");
    assertTrue(c4.match(newPrimitive("start_date", "1950-01-01")));
    assertFalse(c4.match(newPrimitive("start_date", "2000")));

    final SearchCompiler.Match c5 = SearchCompiler.compile("height>180");
    assertTrue(c5.match(newPrimitive("height", "200")));
    assertTrue(c5.match(newPrimitive("height", "99999")));
    assertFalse(c5.match(newPrimitive("height", "50")));
    assertFalse(c5.match(newPrimitive("height", "-9999")));
    assertFalse(c5.match(newPrimitive("height", "fixme")));

    final SearchCompiler.Match c6 = SearchCompiler.compile("name>C");
    assertTrue(c6.match(newPrimitive("name", "Delta")));
    assertFalse(c6.match(newPrimitive("name", "Alpha")));
  }
 /**
  * Search by nth.
  *
  * @throws ParseError if an error has been encountered while compiling
  */
 @Test
 public void testNth() throws ParseError {
   final DataSet dataSet = new DataSet();
   final Way way = new Way();
   final Node node0 = new Node(new LatLon(1, 1));
   final Node node1 = new Node(new LatLon(2, 2));
   final Node node2 = new Node(new LatLon(3, 3));
   dataSet.addPrimitive(way);
   dataSet.addPrimitive(node0);
   dataSet.addPrimitive(node1);
   dataSet.addPrimitive(node2);
   way.addNode(node0);
   way.addNode(node1);
   way.addNode(node2);
   assertFalse(SearchCompiler.compile("nth:2").match(node1));
   assertTrue(SearchCompiler.compile("nth:1").match(node1));
   assertFalse(SearchCompiler.compile("nth:0").match(node1));
   assertTrue(SearchCompiler.compile("nth:0").match(node0));
   assertTrue(SearchCompiler.compile("nth:2").match(node2));
   assertTrue(SearchCompiler.compile("nth:-1").match(node2));
   assertTrue(SearchCompiler.compile("nth:-2").match(node1));
   assertTrue(SearchCompiler.compile("nth:-3").match(node0));
 }
 /**
  * Search by negative nth.
  *
  * @throws ParseError if an error has been encountered while compiling
  */
 @Test
 public void testNthParseNegative() throws ParseError {
   assertThat(
       SearchCompiler.compile("nth:-1").toString(), CoreMatchers.is("Nth{nth=-1, modulo=false}"));
 }