def create_class_extends_relation(old_class_id, parent_class_name): if parent_class_name is None or parent_class_name == "null" or parent_class_name.strip() == "": logger.error("no old_extends_parent_class_id for %d", old_class_id) return None parent_class_name = parent_class_name.strip() parent_class_entity = APIEntity.find_by_qualifier(session=session, qualified_name=parent_class_name) if parent_class_entity is None: logger.error("no parent_class_entity for %s", parent_class_name) return None new_class_api_entity_id = KnowledgeTableRowMapRecord.get_end_row_id(session=session, start_knowledge_table=android_class_knowledge_table, end_knowledge_table=api_knowledge_table, start_row_id=old_class_id) if new_class_api_entity_id is None: logger.error("no new_class_api_entity_id for %d", old_class_id) return None new_extended_class_api_entity_id = parent_class_entity.id if new_extended_class_api_entity_id is None: logger.error("no new_extended_class_api_entity_id ") return None relation = APIRelation(new_class_api_entity_id, new_extended_class_api_entity_id, APIRelation.RELATION_TYPE_EXTENDS) logger.info("%d extends to %d", new_class_api_entity_id, new_extended_class_api_entity_id) return relation
def import_jdk_parameter_relation(cur, session): parameter_data = read_parameter_data(cur) for each in parameter_data: if each is not None: name = each[0] method_id = each[1] type_string = each[2] description = each[3] type_class = each[4] full_declaration = type_string + " " + name description = clean_html_text(description) parameter_entity = APIEntity.find_by_full_declaration_and_description(session, full_declaration, description) jdk_method_knowledge_table = KnowledgeTableFactory.get_jdk_method_table(session) api_knowledge_table = KnowledgeTableFactory.get_api_entity_table(session) method_entity_id = KnowledgeTableRowMapRecord.get_end_row_id(session, jdk_method_knowledge_table, api_knowledge_table, method_id) if parameter_entity is not None and method_entity_id is not None: end_api_id = parameter_entity.id api_relation_has_parameter = APIRelation(method_entity_id, end_api_id, APIRelation.RELATION_TYPE_HAS_PARAMETER) print("------------------------") print(api_relation_has_parameter) api_relation_has_parameter.find_or_create(session, autocommit=False) if type_class == 0: qualified_class_name = type_string else: qualified_class_name = get_qualified_class_name(type_class, cur) if qualified_class_name is not None and parameter_entity is not None: class_entity = APIEntity.find_by_qualifier(session, qualified_class_name) if class_entity is not None: api_relation_has_type = APIRelation(parameter_entity.id, class_entity.id, APIRelation.RELATION_TYPE_PARAMETER_HAS_TYPE) print("=============================") print(api_relation_has_type) api_relation_has_type.find_or_create(session, autocommit=False) session.commit()
def import_parameter_has_type_relation(session): parameter_data_from_codehub = read_parameter_data_from_codehub(session) for each in parameter_data_from_codehub: # print(each.qualified_name) qualified_type = each.qualified_name.split(" ")[0] print(qualified_type) type_entity = APIEntity.find_by_qualifier(session, qualified_type) if type_entity is not None: # print(type_entity.id) api_relation_has_type = APIRelation( each.id, type_entity.id, APIRelation.RELATION_TYPE_PARAMETER_HAS_TYPE) api_relation_has_type.find_or_create(session, autocommit=False) session.commit()
def import_jdk_return_value_relation(cur, session): return_value_data = read_return_value_data(cur) total = 0 type1 = 0 type2 = 0 for each in return_value_data: total += 1 method_id = each[0] full_declaration = each[1] return_type = each[2] return_string = each[3] return_type = process_return_type_string(return_type) qualified_name = construct_qualified_name_by_full_declaration(full_declaration, return_type) print("****************") print(method_id) print(return_type) print(full_declaration) print(qualified_name) if qualified_name is None: qualified_name = return_type return_string = clean_html_text(return_string) parameter_entity = APIEntity.find_by_full_declaration_and_description(session, return_type, return_string) jdk_method_knowledge_table = KnowledgeTableFactory.get_jdk_method_table(session) api_knowledge_table = KnowledgeTableFactory.get_api_entity_table(session) method_entity_id = KnowledgeTableRowMapRecord.get_end_row_id(session, jdk_method_knowledge_table, api_knowledge_table, method_id) if parameter_entity is not None and method_entity_id is not None: end_api_id = parameter_entity.id api_relation_has_return_value = APIRelation(method_entity_id, end_api_id, APIRelation.RELATION_TYPE_HAS_RETURN_VALUE) api_relation_has_return_value.find_or_create(session, autocommit=False) print("------------------------") print(api_relation_has_return_value) type1 += 1 if qualified_name is not None and parameter_entity is not None: print("+++++++++++++++++++++") print(qualified_name) class_entity = APIEntity.find_by_qualifier(session, qualified_name) if class_entity is not None: api_relation_has_type = APIRelation(parameter_entity.id, class_entity.id, APIRelation.RELATION_TYPE_RETURN_VALUE_HAS_TYPE) api_relation_has_type.find_or_create(session, autocommit=False) print("=============================") print(api_relation_has_type) type2 += 1 session.commit() print("total: " + str(total) + ", type1: " + str(type1) + ", type2: " + str(type2))
def import_jdk_relation_value_relation_for_method(): return_value_data = get_return_value_data() for each in return_value_data: original_method_id = each[0] full_declaration = each[1] return_type = each[2] start_api_id = KnowledgeTableRowMapRecord.get_end_row_id(session, jdk_method_knowledge_table, api_knowledge_table, original_method_id) qualified_name = construct_qualified_name_by_full_declaration(full_declaration, return_type) if start_api_id is not None and qualified_name is not None: print "-------------------------------------" print "start_api_id: ", start_api_id, ", qualified_name: ", qualified_name api_entity = APIEntity.find_by_qualifier(session, qualified_name) if api_entity is not None: end_api_id = api_entity.id print api_entity, " ", end_api_id api_relation = APIRelation(start_api_id, end_api_id, APIRelation.RELATION_TYPE_RETURN_VALUE) print api_relation api_relation.find_or_create(session, autocommit=False) session.commit()
def test_get_api_relation(self): session = EngineFactory.create_session() api = APIEntity.find_by_id(session, 462) api=APIEntity.find_by_qualifier(session,"java.lang.String") print "api =", api print api.document_websites print "out_relation" for r in api.out_relation: print r print "start_api=", r.start_api print "end_api=", r.end_api print "------------" print "in_relation" for r in api.in_relation: print r print "start_api=", r.start_api print "end_api=", r.end_api print "------------"
def api_is_valid(self, name, general_concept_searcher, session): api_entity = APIEntity.find_by_qualifier(session, name) all_aliases = api_entity.all_aliases useful_alias_list = set([]) for alias_entity in all_aliases: if alias_entity.type in self.useless_alias_type_list: continue if api_entity.api_type == APIEntity.API_TYPE_METHOD or api_entity.api_type == APIEntity.API_TYPE_CONSTRUCTOR: if alias_entity.type == APIAlias.ALIAS_TYPE_QUALIFIER_NAME: method_alias_need_to_add = alias_entity.alias.split("(")[0] useful_alias_list.add(method_alias_need_to_add) continue useful_alias_list.add(alias_entity.alias) if len(useful_alias_list) == 0: return False valid_count = 0 for name in useful_alias_list: if self.is_valid(name, general_concept_searcher): valid_count += 1 if valid_count * 1.0 / len(useful_alias_list) < 0.2: return False return True