What's really happening when you run exists(Pattern, 0)?

Asked by Paul Chambre on 2018-09-20

I wrote a wrapper method to iterate through a set of Patterns and check exists(Pattern, 0) on them to see whether any of them exists.

I'm using this to check for slightly different, but all valid, versions of a graph. For one graph I have only two valid pngs; for another I have only one. For these graphs, exists() does not match the correct chart the first time through the loop, but it does match the second time (the loop runs until a specified timeout).

I'm wondering why the first try of exists() for the correct png didn't match, but the second one did.

Here's a code snippet and some log results:

public static boolean multiPatternExists(@NotNull final Region region, @NotNull final Map<String, Pattern> patterns, final long timeoutMs, final float similarity, final String... patternsToMatch) {
    if (timeoutMs < 0) {
        throw new IllegalArgumentException("timeoutMs cannot be negative.");
    final long start = System.currentTimeMillis();
    boolean found = false;
    while (System.currentTimeMillis() < start + timeoutMs && !found) {
        for (final String patternName : patternsToMatch) {
            final Pattern testPattern = patterns.get(patternName);
            if (testPattern != null) {
                Debug.action("Trying " + patternName);
                if (region.exists(testPattern.similar(similarity), 0) != null) {
                    found = true;
                    Debug.action("Match found with " + patternName);
            } else {
                Debug.action("Pattern: " + patternName + " not found in patterns map.");
    return found;

[log] CLICK on L(433,600)@S(0)[0,0 1920x1080] (625 msec)
[log] Trying ohlcPlotByChart.png
[log] Trying ohlcPlotByChart2.png
[log] Trying ohlcPlotByChart.png
[log] Trying ohlcPlotByChart2.png
[log] Match found with ohlcPlotByChart2.png


Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

surely a timing problem:
it looks like before calling multiPatternExists() there is a click, that might have the function, to show up the content, where the images/patterns should be found.

Since you use exists(whatever, 0), there is no search-repetition until found, but only one trial. So at the time of the first exists, the content of the region is not yet ready, but is at the time of the second exists some ten to hundred millisecs later.

Timing is very important with SikuliX, so if you do some tests like this, you should report the timing too, to understand the behavior

Can you help with this problem?

Provide an answer of your own, or ask Paul Chambre for more information if necessary.

To post a message you must log in.