def console_input(ctx: ContextWrapper): @receptor(ctx_wrap=ctx, write="rawio:in") def write_console_input(ctx_input, value: str): ctx_input["rawio:in"] = value @receptor(ctx_wrap=ctx, write="interloc:all") def push_console_interloc(ctx: ContextWrapper, console_node: Node): if ctx.push(parentpath="interloc:all", child=PropertyBase(name=DEFAULT_INTERLOC_ID, default_value=console_node)): logger.debug(f"Pushed {console_node} to interloc:all") @receptor(ctx_wrap=ctx, write="interloc:all") def pop_console_interloc(ctx: ContextWrapper): if ctx.pop(f"interloc:all:{DEFAULT_INTERLOC_ID}"): logger.debug(f"Popped interloc:all:{DEFAULT_INTERLOC_ID}") while not ctx.shutting_down(): input_value = input("> ") write_console_input(input_value) console_interloc_exists = f"interloc:all:{DEFAULT_INTERLOC_ID}" in ctx.enum( "interloc:all") # push Node if you got a greeting if input_value.strip() in get_phrase_list( "greeting") and not console_interloc_exists: # set up scientio sess: Session = ravestate_ontology.get_session() onto: Ontology = ravestate_ontology.get_ontology() # create scientio Node of type Person query = Node(metatype=onto.get_type("Person")) query.set_name("x") console_node_list = sess.retrieve(query) if not console_node_list: console_node = sess.create(query) logger.info( f"Created new Node in scientio session: {console_node}" ) elif len(console_node_list) == 1: console_node = console_node_list[0] else: logger.error( f'Found multiple Persons with name {DEFAULT_INTERLOC_ID} in scientio session. Cannot push node to interloc:all!' ) continue # push interloc-Node push_console_interloc(console_node) # pop Node if you got a farewell elif input_value.strip() in get_phrase_list( "farewells") and console_interloc_exists: pop_console_interloc()
def push_interlocutor(ctx): interloc_exists = ANON_INTERLOC_PATH in ctx.enum(prop_all) if not interloc_exists: mem.initialized.wait() onto: Ontology = mem.get_ontology() new_interloc = Node(metatype=onto.get_type("Person")) new_interloc.set_name(ANON_INTERLOC_ID) if ctx.push(parent_property_or_path=prop_all, child=rs.Property(name=ANON_INTERLOC_ID, default_value=new_interloc)): logger.debug(f"Pushed {new_interloc} to interloc:all")
def react(ctx: rs.ContextWrapper): """ Retrieves memory node with the name, or creates a new one outputs a polite response. """ onto: Ontology = mem.get_ontology() sess: Session = mem.get_session() inferred_answer = ctx[prop_answer] pred = ctx[prop_predicate] subject_path: str = ctx[prop_subject] if not subject_path: return rs.Resign() subject_node: Node = ctx[subject_path] assert inferred_answer if pred == "NAME": # If name was asked, it must be because the node is not yet persistent assert subject_node.get_id() < 0 subject_node.set_name(inferred_answer) persistent_subject_node = retrieve_or_create_node(subject_node) # TODO: Workaround - see #83 - if this state would write to interloc:all, # it would promise an interloc:all:pushed signal. This would be # picked up by persqa:new_interloc. subject_node.set_node(persistent_subject_node) sess.update(subject_node) ctx[interloc.prop_persisted] = subject_node elif pred in PREDICATE_SET: relationship_type = onto.get_type(ONTOLOGY_TYPE_FOR_PRED[pred]) relationship_node = Node(metatype=relationship_type) relationship_node.set_name(inferred_answer) relationship_node = retrieve_or_create_node(relationship_node) if relationship_node is not None: subject_node.add_relationships( {pred: {relationship_node.get_id()}}) sess.update(subject_node) ctx[rawio.prop_out] = verbaliser.get_random_successful_answer( pred).format(name=subject_node.get_name(), obj=inferred_answer) ctx[prop_predicate] = None
def test_init(self): """ Integration test with a default neo4j instance. Should be executed by Travis after setting up neo4j in docker. """ # Default values are neo4j defaults, EXCEPT the password. address = os.environ.get('NEO4J_ADDRESS', 'bolt://localhost:7687') user = os.environ.get('NEO4J_USERNAME', 'neo4j') passw = os.environ.get('NEO4J_PASSWORD', 'neo42j') ontpath = "scientio/examples/example_ontology.yaml" o = Ontology(path_to_yaml=str(ontpath)) s = Session(ontology=o, neo4j_address=address, neo4j_username=user, neo4j_password=passw) n = Node(metatype=o.get_type('Person')) n.set_name('Test') n_response = s.create(n) # TODO make smarter assert (n_response is not None)
def handle_single_interlocutor_input(ctx: ContextWrapper, input_value: str, id="anonymous_interlocutor") -> None: """ Forwards input to `rawio:in` and manages creation/deletion of a singleton interlocutor. A new interlocutor node is pushed, when the input is a greeting, and there is no interlocutor present. The interlocutor is popped, if the input is a farewell, and there is an interlocutor present. * `ctx`: Context Wrapper of the calling state. Must have read permissions for `interloc:all`. * `input_value`: The string input value that should be written to `rawio:in`. * `id`: Name of the interlocutor context property, and initial name for the interlocutor's Neo4j node (until a proper name is set by persqa). """ @receptor(ctx_wrap=ctx, write="rawio:in") def write_input(ctx_input, value: str): ctx_input["rawio:in"] = value @receptor(ctx_wrap=ctx, write="interloc:all") def push_interloc(ctx: ContextWrapper, interlocutor_node: Node): if ctx.push(parentpath="interloc:all", child=PropertyBase(name=id, default_value=interlocutor_node)): logger.debug(f"Pushed {interlocutor_node} to interloc:all") @receptor(ctx_wrap=ctx, write="interloc:all") def pop_interloc(ctx: ContextWrapper): if ctx.pop(f"interloc:all:{id}"): logger.debug(f"Popped interloc:all:{id}") write_input(input_value) interloc_exists = f"interloc:all:{id}" in ctx.enum("interloc:all") # push Node if you got a greeting if input_value.strip() in get_phrase_list( "greeting") and not interloc_exists: # set up scientio sess: Session = ravestate_ontology.get_session() onto: Ontology = ravestate_ontology.get_ontology() # create scientio Node of type Person query = Node(metatype=onto.get_type("Person")) query.set_name(id) interlocutor_node_list = sess.retrieve(query) if not interlocutor_node_list: interlocutor_node = sess.create(query) logger.info( f"Created new Node in scientio session: {interlocutor_node}") else: interlocutor_node = interlocutor_node_list[0] # push interloc-node push_interloc(interlocutor_node) # pop Node if you got a farewell elif input_value.strip() in get_phrase_list( "farewells") and interloc_exists: pop_interloc()