def match(self, tokens, rules): for rule in rules: @timed def f(): # find the pattern to be updated matcher = pybo.CQLMatcher(rule.cql) slices = matcher.match([t.pyboToken for t in tokens]) return slices slices = f() for slice in slices: # find index for the token to be updated matcher = pybo.CQLMatcher(rule.actionCql) pattern = tokens[slice[0]:slice[1] + 1] sliceInPattern = matcher.match([t.pyboToken for t in pattern])[0] # FIXME: get first match for now # FIXME: bug, dont match last one index = sliceInPattern[0] + slice[0] # update token's attrs actionToken = tokens[index] for key, val in json.loads(rule.action).items(): if hasattr(actionToken, key): setattr(actionToken, key, val) tokens[index] = actionToken
def featureCheckCall(self, data, is_fact=True): matcher = pybo.CQLMatcher(self.expected.value) tokens = data[0] slices = matcher.match([t.pyboToken for t in tokens]) if len(slices) > 0: return True else: return False
def actionFunc(self): matcher = pybo.CQLMatcher(rule.cql) slices = matcher.match([t.pyboToken for t in tokens]) newTokens = self.facts[1][0] for slice in slices: # find index for the token to be updated matcher = pybo.CQLMatcher(rule.actionCql) pattern = tokens[slice[0]:slice[1] + 1] sliceInPattern = matcher.match( [t.pyboToken for t in pattern])[0] index = sliceInPattern[0] + slice[0] for k, v in json.loads(rule.action).items(): setattr(newTokens[index], k, v) self.declare(CQL(newTokens))
def findCqlTokens(self): query = self.findInput.text() matcher = pybo.CQLMatcher(query) tokens = self.editor.tokens slices = matcher.match([t.pyboToken for t in tokens]) for slice in slices: item = QtWidgets.QListWidgetItem() item.slice = list(slice) item.setText(' '.join( [w.text for w in tokens[slice[0]:slice[1] + 1]])) self.resultList.addItem(item) self.resultLabel.setText(str(len(slices)) + " Matches")
def f(): # find the pattern to be updated matcher = pybo.CQLMatcher(rule.cql) slices = matcher.match([t.pyboToken for t in tokens]) return slices