106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
from apimock import *
|
|
from monitor_stanu_dialogowego import add_fact
|
|
|
|
gramar_slots = {
|
|
'pullrequest': (('action', 'optional'),
|
|
('repo', 'required')
|
|
),
|
|
'brefing': (('action', 'optional'),
|
|
('repo', 'required')
|
|
),
|
|
'notifications': (('liczba', 'optional'),
|
|
('repo', 'required')
|
|
),
|
|
'tests': (('test', 'optional'),
|
|
('repo', 'required')
|
|
),
|
|
'time': (('time_when', 'required'),
|
|
('timeunit', 'required'),
|
|
('liczba','optional')
|
|
),
|
|
'issues': (('time_when','optional'),
|
|
('liczba','optional'),
|
|
('timeunit','optional'),
|
|
('repo', 'required')
|
|
),
|
|
'number': (('liczba', 'required')
|
|
),
|
|
'help': (('functions', 'optional'),
|
|
('help', 'optional')
|
|
),
|
|
'repo': (('repo', 'required')
|
|
),
|
|
'hello': (),
|
|
'bye': (),
|
|
}
|
|
|
|
|
|
def taktyka_dialogu(state, frame):
|
|
if frame['act'] == 'null':
|
|
return 'null'
|
|
# if state['current_context'] in ['hello', 'pomoc', 'bye']:
|
|
# return short_thread(state)
|
|
# else:
|
|
return long_thread(state)
|
|
|
|
# def short_thread(state):
|
|
# if state['current_context'] == 'pomoc':
|
|
# return state['current_context']
|
|
# else:
|
|
# return state['current_context']
|
|
|
|
def validate_repo(repo):
|
|
if repo not in listRepositories():
|
|
return 'repo not in listRepositories'
|
|
if repo not in listPublicRepositories():
|
|
return 'repo not in listPublicRepositories'
|
|
|
|
def long_thread(state):
|
|
act = state['current_context']
|
|
if act == None:
|
|
return 'null'
|
|
for topic in state['topics']:
|
|
if act == topic['act']:
|
|
slots = topic['slots']
|
|
required = gramar_slots[act]
|
|
req_args, opt_args = agregate(required, slots)
|
|
facts = state['facts']
|
|
req_args = add_facts(req_args, facts)
|
|
opt_args = add_facts(opt_args, facts)
|
|
required_empty = check_req(req_args)
|
|
if required_empty:
|
|
return False, required_empty[0], act, req_args, opt_args #slot do uzupełnienia
|
|
return True, act, req_args, opt_args #wyświetl użytkownikowi
|
|
|
|
def check_req(req_args):
|
|
req = []
|
|
for item in req_args:
|
|
if item[1] == None:
|
|
req.append(item)
|
|
return req
|
|
|
|
def add_facts(args, facts):
|
|
for key, value in facts.items():
|
|
for idx, item in enumerate(args):
|
|
if key == item[0]:
|
|
if value != None and item[1] == None:
|
|
args[idx] = value
|
|
if key == 'repo':
|
|
validate_repo(value)
|
|
if value == None and item[1] != None:
|
|
add_fact(key, item[1])
|
|
return args
|
|
|
|
def agregate(grammar, slots):
|
|
req_args, opt_args = [], []
|
|
for item in grammar:
|
|
value = None
|
|
for slot in slots:
|
|
if slot[0] == item[0]:
|
|
value = None if slot[1] == '' else slot[1]
|
|
if item[1] == 'required':
|
|
req_args.append([item[0], value])
|
|
elif item[1] == 'optional':
|
|
opt_args.append([item[0], value])
|
|
return req_args, opt_args
|