MetadataSelectorMatcher { public boolean matches(Metadata metadata) { if (null == metadata || null == metadata.getName()) { return false; } boolean classnameMatch = (null == this.classnamePattern) || (this.classnamePattern.reset(metadata.getName()).matches()); if (!classnameMatch) { return false; } Map<String, String> inputLabels = metadata.getLabels(); Map<String, String> inputAttributes = metadata.getAttributes(); boolean labelAndAttributeMatch = true; if (null != this.attributesPatterns) { if (null != this.labelsPatterns && null != inputLabels) { for (String label: this.missingLabels) { if (null != inputLabels.get(label)) { labelAndAttributeMatch = false; break; } } for (Entry<String, Matcher> ls: this.labelsPatterns.entrySet()) { if (!labelAndAttributeMatch) { break; } String inputLabel = inputLabels.get(ls.getKey()); labelAndAttributeMatch = (null != inputLabel) && ls.getValue().reset(inputLabel).matches(); if (!labelAndAttributeMatch) { break; } } } if (null != inputAttributes) { for (String attr: this.missingAttributes) { if (null != inputAttributes.get(attr)) { labelAndAttributeMatch = false; break; } } for (Entry<String, Matcher> ls: this.attributesPatterns.entrySet()) { if (!labelAndAttributeMatch) { break; } String inputAttribute = inputAttributes.get(ls.getKey()); labelAndAttributeMatch = (null != inputAttribute) && ls.getValue().reset(inputAttribute).matches(); } } } else { if (null != this.labelsPatterns) { for (String label: this.missingLabels) { if ((null != inputLabels && null != inputLabels.get(label)) || (null != inputAttributes && null != inputAttributes.get(label))) { labelAndAttributeMatch = false; break; } } for (Entry<String, Matcher> ls: this.labelsPatterns.entrySet()) { if (!labelAndAttributeMatch) { break; } if (null != inputLabels) { String inputLabel = inputLabels.get(ls.getKey()); if (null != inputLabel) { labelAndAttributeMatch = ls.getValue().reset(inputLabel).matches(); } else if (null != inputAttributes) { String inputAttribute = inputAttributes.get(ls.getKey()); labelAndAttributeMatch = (null != inputAttribute) && ls.getValue().reset(inputAttribute).matches(); } else { labelAndAttributeMatch = false; } } } } } return labelAndAttributeMatch; } MetadataSelectorMatcher(String selector); boolean matches(Metadata metadata);  }
@Test public void testMetaDataMatch() throws WarpScriptException { Metadata test = new Metadata(); test.setName("temperature"); Map<String, String> labels = new HashMap<String, String>(); labels.put("sensor", "23"); test.setLabels(labels); Map<String, String> attributes = new HashMap<String, String>(); attributes.put("room", "A"); test.setAttributes(attributes); MetadataSelectorMatcher x; x = new MetadataSelectorMatcher("~.*{toto~tata.*}{attr=yes,attr2~.false.*}"); Assert.assertFalse(x.matches(test)); x = new MetadataSelectorMatcher("~temp.*{sensor~(23|22),room=B}{}"); Assert.assertFalse(x.matches(test)); x = new MetadataSelectorMatcher("~temp.*{sensor~(23|22),room=B}"); Assert.assertFalse(x.matches(test)); Metadata testemptyclassname = new Metadata(); testemptyclassname.setName(""); labels = new HashMap<String, String>(); labels.put("sensor", "23"); testemptyclassname.setLabels(labels); attributes = new HashMap<String, String>(); attributes.put("attr", "yes"); attributes.put("attr2", "afalse detection failure"); testemptyclassname.setAttributes(attributes); GeoTimeSerie g = new GeoTimeSerie(); g.setName(""); x = new MetadataSelectorMatcher("={attr=yes,attr2~.false.*}"); Assert.assertTrue(x.matches(testemptyclassname)); Assert.assertFalse(x.matches(test)); Assert.assertFalse(x.matches(g.getMetadata())); x = new MetadataSelectorMatcher("~{attr=yes,attr2~.false.*}"); Assert.assertTrue(x.matches(testemptyclassname)); Assert.assertFalse(x.matches(test)); x = new MetadataSelectorMatcher("={}"); Assert.assertTrue(x.matches(g.getMetadata())); }