Example #1
0
    def update_kb(self):
        kus = KnowledgeUpdateServiceArrayRequest()
        # Get info from KB functions and propositions (to know type of variable)
        # Fill update
        self.dump_cache_mutex.acquire(True)
        self.mutex.acquire(True)
        sensed_predicates = self.sensed_topics.copy()
        self.mutex.release()
        self.srv_mutex.acquire(True)
        sensed_predicates.update(self.sensed_services.copy())
        self.srv_mutex.release()

        for predicate, (val, changed,
                        updated) in sensed_predicates.iteritems():
            if updated:
                continue

            if predicate in self.sensed_topics:
                self.mutex.acquire(True)
                self.sensed_topics[predicate] = (
                    self.sensed_topics[predicate][0], False, True
                )  # Set to not changed and updated KB
                self.mutex.release()
            else:
                self.srv_mutex.acquire(True)
                self.sensed_services[predicate] = (
                    self.sensed_services[predicate][0], False, True
                )  # Set to not changed and updated KB
                self.srv_mutex.release()
            predicate_info = predicate.split(':')
            ki = KnowledgeItem()
            ki.attribute_name = predicate_info.pop(0)
            if type(val) is bool:
                ki.knowledge_type = ki.FACT
                ki.is_negative = not val
            else:
                ki.knowledge_type = ki.FUNCTION
                ki.function_value = val

            # TODO what to do if no parameters specified? iterate over all the instantiated parameters and add them?
            for i, param in enumerate(predicate_info):
                kv = KeyValue()
                kv.key = 'p' + str(i)
                kv.value = param
                ki.values.append(kv)

            kus.update_type += np.array(kus.ADD_KNOWLEDGE).tostring()
            kus.knowledge.append(ki)
        self.dump_cache_mutex.release()

        # Update the KB with the full array
        if len(kus.update_type) > 0:
            try:
                self.update_kb_srv.call(kus)
            except Exception as e:
                rospy.logerr(
                    "KCL (SensingInterface) Failed to update knowledge base: %s"
                    % e.message)
Example #2
0
def _make_predicate(type_name, parameters):

    new_type_name, is_negative = _is_predicate_negative(type_name)
    kb_item = KnowledgeItem()
    kb_item.knowledge_type = KB_ITEM_FACT
    kb_item.is_negative = is_negative
    kb_item.attribute_name = new_type_name
    kb_item.values = dict_to_keyval(parameters)
    return kb_item
 def publish_predicates(self, preds):
     ka = []
     for p in preds:
         k = KnowledgeItem()
         k.knowledge_type = k.FACT
         k.attribute_name = p.head.lower()
         for i in xrange(len(p.args)):
             kv = KeyValue()
             kv.key = self.predicate_key_name[(k.attribute_name, i)]
             kv.value = p.args[i].lower()
             k.values.append(kv)
         ka.append(k)
         k.is_negative = not p.isTrue
     self.publish(ka)
	def publish_predicates(self, preds):
		ka = []	
		for p in preds:
			k = KnowledgeItem()
			k.knowledge_type = k.FACT
			k.attribute_name = p.head.lower()
			for i in xrange(len(p.args)):
				kv = KeyValue()
				kv.key = self.predicate_key_name[(k.attribute_name, i)]
				kv.value = p.args[i].lower()
				k.values.append(kv)
			ka.append(k)
			k.is_negative = not p.isTrue
		self.publish(ka)
Example #5
0
def predicate_maker(attr_name, attr_key, attr_value, is_negative=None):
    ki = KnowledgeItem()
    ki.knowledge_type = 1
    ki.attribute_name = attr_name
    ki_values = []
    if type(attr_key) == list:
        for key, value in zip(attr_key, attr_value):
            kv = KeyValue()
            kv.key = key
            kv.value = value
            ki_values.append(kv)
    else:
        kv = KeyValue()
        kv.key = attr_key
        kv.value = attr_value
        ki_values.append(kv)
    ki.values = ki_values
    if is_negative:
        ki.is_negative = is_negative
    return ki