Files
langchain_KG/project copy.py
T
2025-06-03 13:45:48 +08:00

614 lines
19 KiB
Python

"""
软件知识图谱类定义
根据Ontology_Layer.txt文件中的知识图谱信息创建
"""
class ProjectTookiIt:
"""
项目类
描述: 代表整个项目结构的顶层容器,包含项目划分集
"""
def __init__(self):
self.project_division_set = ProjectDivisionSet() # 项目划分集对象
def get_division_item_by_path(self, path):
"""
通过路径获取项目划分对象
Args:
path (str): 以'/'分隔的多级项目划分名称路径
Returns:
ProjectDivisionItem|None: 对应的项目划分对象,如果路径不存在返回None,成功找到返回项目划分对象
Note:
当路径为空字符串时,会返回None
"""
names = path.split("/")
if not names:
raise ValueError("路径不能为空")
try:
current = self.project_division_set.get_division_item(names[0])
except KeyError:
return None
for name in names[1:]:
found = False
for child in current.children:
if child.项目名称 == name:
current = child
found = True
break
if not found:
return None
return current
def get_quantities_by_paths(self, paths_str, quantity_type=None, keyword=None):
"""
获取多个项目路径下的工程量对象
Args:
paths_str (str): 以'@@'分隔的多个项目路径字符串,每个路径以'/'分隔层级
quantity_type (str): 工程量类型('定额'、'主材'、'设备'或None表示所有类型)
keyword (str): 工程量代码或名称中的关键字(可选)
Returns:
list: 包含所有匹配的工程量对象的列表
"""
paths = paths_str.split("@@") if paths_str else []
results = []
for path in paths:
item = self.get_division_item_by_path(path.strip())
if item is None:
continue
def collect_quantities(node):
for quantity in node.get_quantities():
# 检查类型匹配
type_matched = True
if quantity_type:
if quantity_type == "定额" and not isinstance(quantity, Ration):
type_matched = False
elif quantity_type == "主材" and not isinstance(quantity, Material):
type_matched = False
elif quantity_type == "设备" and not isinstance(quantity, Equipment):
type_matched = False
# 检查关键字匹配
keyword_matched = True
if keyword:
keyword_matched = (keyword in (quantity.编码 or "")) or (keyword in (quantity.name or ""))
if type_matched and keyword_matched:
results.append(quantity)
for child in node.children:
collect_quantities(child)
collect_quantities(item)
return results
class ProjectDivisionSet:
"""
项目划分集
描述: 代表projectDivision下的第一层键和第二层建,例如"安装工程"、"安装"
JSON对应: projectDivision下的直接键名,如"安装工程": {...}、"安装": [...]
"""
def __init__(self):
self.category_name = None # xsd:string
self.division_items = {} # 存储项目划分项的字典,键为项目名称
def get_division_item(self, item_name):
"""
Get project division item by name
Args:
item_name (str): The name of item to find
Returns:
ProjectDivisionItem: The corresponding division item object
Raises:
KeyError: When item name does not exist
"""
if item_name not in self.division_items:
raise KeyError(f'Item name "{item_name}" does not exist')
return self.division_items[item_name]
# 工程属性查询方法
@abstractmethod
def get_project_property_by_name(self, name):
"""
通过名称获取工程属性
Args:
name (str): 工程属性名称
Returns:
str|None: 对应的工程属性值,如果不存在返回None
"""
pass
@abstractmethod
def get_project_property_by_parent_and_name(self, parent_path, partial_name):
"""
通过父节点路径和模糊名称获取工程属性
Args:
parent_path (str): 父节点的路径,以'/'分隔的多级节点路径
partial_name (str): 目标节点的模糊或不完整名称
Returns:
dict: 包含所有匹配的工程属性的字典,键为属性名,值为属性值
"""
pass
class ProjectDivisionItem:
"""
项目划分项
描述: 代表项目结构中的层级条目,具有自身的属性,并且可以包含子项目划分项或详细工作项
JSON对应: ProjectDivisionSet数组中的对象,或ProjectDivisionItem的children数组中type为"项目划分"的对象
"""
def __init__(self):
self.GUID = None # xsd:string
self.id = None # xsd:string
self.name = None # xsd:string
self.代码 = None # xsd:string (可选)
self.费率 = None # xsd:string
self.单位 = None # xsd:string (可选)
self.取费表id = None # xsd:string
self.颜色标记 = None # xsd:string
self.取费表 = None # xsd:string
self.合价含税 = None # xsd:string (可选)
self.type = None # xsd:string
self.专业类型 = None # xsd:string
self.资源库列表 = None # xsd:list (可选)
self.notCheck = None # xsd:string (可选)
self.children = [] # 存储子项目划分项的数组
self.quantities = [] # 存储工程量对象的数组
self.cost_set = None # xsd:CostSet
def get_cost_set(self):
"""
获取费用集对象
Returns:
CostSet: 关联的费用集对象
"""
return self.cost_set
def add_child(self, child_item):
"""
添加子项目划分项
Args:
child_item (ProjectDivisionItem): 子项目对象
Raises:
ValueError: 当child_item不是ProjectDivisionItem类型时
"""
if not isinstance(child_item, ProjectDivisionItem):
raise ValueError("child_item必须是ProjectDivisionItem类型")
self.children.append(child_item)
def get_child(self, index):
"""
根据索引获取子项目划分项
Args:
index (int): 子项目索引
Returns:
ProjectDivisionItem: 对应的子项目对象
Raises:
IndexError: 当索引超出范围时
"""
if index < 0 or index >= len(self.children):
raise IndexError(f'索引"{index}"超出范围')
return self.children[index]
def traverse(self, callback, depth=0):
"""
遍历树形结构
Args:
callback (function): 回调函数,接收当前节点和深度作为参数
depth (int): 当前深度,默认为0
"""
callback(self, depth)
for child in self.children:
child.traverse(callback, depth + 1)
def get_quantities(self):
"""
获取当前节点的ProjectQuantity及其子类(Ration, Material, Equipment)的实例
Returns:
list: 包含当前节点ProjectQuantity实例的列表
"""
return self.quantities
class ProjectQuantity:
"""
工程量
描述: 代表项目划分项(ProjectDivisionItem)下的具体工作单元或物料项,是定额、主材、设备的父类型
"""
def __init__(self):
self.id = None # xsd:string
self.类型 = None # xsd:string ("0"为定额,"1"为主材,"5"为设备)
self.name = None # xsd:string
self.编码 = None # xsd:string
self.单位 = None # xsd:string
self.数量 = None # xsd:string
self.资源库名称 = None # xsd:string
self.投标数量 = None # xsd:string (可选)
self.投标单价 = None # xsd:string (可选)
self.特征段 = None # xsd:string (可选)
self.关联父级量 = None # xsd:string (可选)
self.颜色标记 = None # xsd:string (可选)
self.单价不含税 = None
self.cost_set = None # xsd:CostSet
def get_cost_set(self):
"""
获取费用集对象
Returns:
CostSet: 关联的费用集对象
"""
return self.cost_set # xsd:string (可选)
class Ration(ProjectQuantity):
"""
定额
描述: 代表一种标准的工程量条目,通常包含详细的工、料、机消耗标准
"""
def __init__(self):
super().__init__()
self.计算式 = None # xsd:string
self.中标计算式 = None # xsd:string
self.人工费 = None # xsd:string
self.机械费 = None # xsd:string
self.甲供材料费不含税 = None # xsd:string
self.材料费 = None # xsd:string
self.定额系数 = None # xsd:string
self.人工系数 = None # xsd:string
self.材料系数 = None # xsd:string
self.机械系数 = None # xsd:string
self.定额范围 = None # xsd:string
self.定额章节名称 = None # xsd:string
self.费用类型 = None # xsd:string
self.甲供材料费含税 = None # xsd:string
self.投标合价 = None # xsd:string
self.其中甲供材料费 = None # xsd:string
self.合价不含税 = None # xsd:string
self.基价 = None # xsd:string
self.所属定额库 = None # xsd:string
class Material(ProjectQuantity):
"""
主材
描述: 代表工程中使用的主要材料
"""
def __init__(self):
super().__init__()
self.规格型号 = None # xsd:string
self.损耗率 = None # xsd:string
self.供货方 = None # xsd:string
self.集中配送 = None # xsd:string (可选)
self.单重 = None # xsd:string (可选)
self.市场价不含税 = None # xsd:string
self.市场价含税 = None # xsd:string
self.单价含税 = None # xsd:string
self.结算市场价不含税 = None # xsd:string (可选)
self.结算市场价含税 = None # xsd:string (可选)
self.基准价不含税 = None # xsd:string (可选)
self.基准价含税 = None # xsd:string (可选)
self.费用类型 = None # xsd:string
self.增值税率 = None # xsd:string (可选)
self.合价含税 = None # xsd:string
self.合价不含税 = None # xsd:string
self.线重 = None # xsd:string (可选)
self.制造长度 = None # xsd:string (可选)
self.截面积 = None # xsd:string (可选)
class Equipment(ProjectQuantity):
"""
设备
描述: 代表工程中安装或使用的设备
"""
def __init__(self):
super().__init__()
self.规格型号 = None # xsd:string
self.供货方 = None # xsd:string
self.运杂费率 = None # xsd:string (可选)
self.单价含税 = None # xsd:string
self.设备类型 = None # xsd:string (可选)
self.增值税率 = None # xsd:string (可选)
self.合价含税 = None # xsd:string
self.合价不含税 = None # xsd:string
class MaterialOrEquipment:
"""
材机
描述: 代表DetailedWorkItem中所列出的具体材料、人工或机械设备及其详细信息
"""
def __init__(self):
self.id = None # xsd:string
self.编码 = None # xsd:string
self.名称 = None # xsd:string
self.单位 = None # xsd:string
self.类型 = None # xsd:string
self.供货方 = None # xsd:string
self.预算价不含税 = None # xsd:string
self.市场价不含税 = None # xsd:string
self.预算价含税 = None # xsd:string
self.市场价含税 = None # xsd:string
self.结算预算价不含税 = None # xsd:string
self.结算市场价不含税 = None # xsd:string
self.结算预算价含税 = None # xsd:string
self.结算市场价含税 = None # xsd:string
self.暂估价 = None # xsd:string
self.拆分 = None # xsd:string
self.全口径市场价不含税 = None # xsd:string
self.全口径市场价含税 = None # xsd:string
self.商品砼 = None # xsd:string
self.数量 = None # xsd:string
self.是否未计价 = None # xsd:string
class CostSet:
"""
费用集
描述: 代表费用预览层级结构中的一个节点
"""
def __init__(self):
self.GUID = None # xsd:string
self.cost_items = {} # 存储费用项的字典,键为项目名称
def get_cost_item(self, item_name):
"""
根据名称获取费用项
Args:
item_name (str): 要查找的费用项名称
Returns:
CostItem: 对应的费用项对象
Raises:
KeyError: 当费用项名称不存在时
"""
if item_name not in self.cost_items:
raise KeyError(f'费用项名称"{item_name}"不存在')
return self.cost_items[item_name]
class CostItem:
"""
费用项
描述: 代表预览层级结构中的叶子节点,显示具体的费用项及其金额
"""
def __init__(self):
self.id = None # xsd:string
self.cost = None # xsd:string
self.sub_items = {} # 存储子费用项的字典,键为项目名称
def get_sub_item(self, item_name):
"""
根据名称获取子费用项
Args:
item_name (str): 要查找的子费用项名称
Returns:
CostItem: 对应的子费用项对象
Raises:
KeyError: 当子费用项名称不存在时
"""
if item_name not in self.sub_items:
raise KeyError(f'子费用项名称"{item_name}"不存在')
return self.sub_items[item_name]
class FeeTableTemplateSet:
"""
取费表模板集
描述:
"""
def __init__(self):
self.name = None # xsd:string
self.feetable_template = {} # 存储费用项的字典,键为项目名称
def get_feetable_template_item(self, item_name):
"""
根据名称获取取费表模板项
Args:
item_name (str): 要查找的取费表模板项名称
Returns:
FeeTableTemplateItem: 对应的取费表模板项对象
Raises:
KeyError: 当取费表模板项名称不存在时
"""
if item_name not in self.feetable_template:
raise KeyError(f'取费表模板项名称"{item_name}"不存在')
return self.feetable_template[item_name]
class FeeTableTemplateItem:
"""
取费表模板项
描述:
"""
def __init__(self):
self.name = None # xsd:string
self.OutlayID = None # xsd:string
self.type = None # xsd:string
self.profession = None # xsd:string
self.sub_feecollections = {} # 存储子费用项的字典,键为项目名称
def get_sub_fee(self, item_name):
"""
根据名称获取子取费
Args:
item_name (str): 要查找的子取费名称
Returns:
feecollections: 对应的子费用项对象
Raises:
KeyError: 当子费用项名称不存在时
"""
if item_name not in self.sub_feecollections:
raise KeyError(f'取费名称"{item_name}"不存在')
return self.sub_feecollections[item_name]
class FeeCollection:
"""
取费
描述:
"""
def __init__(self):
self.name = None # xsd:string
self.serialNumber = None # xsd:string
self.code = None # xsd:string
self.base = None # xsd:string
self.rate = None # xsd:string
self.sub_feecollections = {} # 存储子费用项的字典,键为项目名称
def get_sub_feecollections(self, item_name):
"""
根据名称获取子取费
Args:
item_name (str): 要查找的子费用名称
Returns:
feecollections: 对应的费用对象
Raises:
KeyError: 当子费用名称不存在时
"""
if item_name not in self.sub_feecollections:
raise KeyError(f'取费名称"{item_name}"不存在')
return self.sub_feecollections[item_name]
class FeeScheduleSet:
"""
费用表集
描述:
"""
def __init__(self):
self.name = None # xsd:string
self.feeschedule_template = {} # 存储费用项的字典,键为项目名称
def get_feetable_template_item(self, item_name):
"""
根据名称获取费用表项
Args:
item_name (str): 要查找的费用表项名称
Returns:
FeeScheduleItem: 对应的费用表项对象
Raises:
KeyError: 当费用表项名称不存在时
"""
if item_name not in self.feeschedule_template:
raise KeyError(f'取费表模板项名称"{item_name}"不存在')
return self.feeschedule_template[item_name]
class FeeScheduleItem:
"""
费用表项
描述:
"""
def __init__(self):
self.name = None # xsd:string
self.sub_fees = {} # 存储子费用项的字典,键为项目名称
def get_sub_fee(self, item_name):
"""
根据名称获取子费用
Args:
item_name (str): 要查找的子费用名称
Returns:
fees: 对应的子费用项对象
Raises:
KeyError: 当子费用项名称不存在时
"""
if item_name not in self.sub_fees:
raise KeyError(f'费用名称"{item_name}"不存在')
return self.sub_fees[item_name]
class Fee:
"""
取费
描述:
"""
def __init__(self):
self.name = None # xsd:string
self.serialNumber = None # xsd:string
self.code = None # xsd:string
self.rate = None # xsd:string
self.amount = None # xsd:string
self.sub_feecollections = {} # 存储子费用项的字典,键为项目名称
def get_sub_feecollections(self, item_name):
"""
根据名称获取子取费
Args:
item_name (str): 要查找的子费用名称
Returns:
feecollections: 对应的费用对象
Raises:
KeyError: 当子费用名称不存在时
"""
if item_name not in self.sub_feecollections:
raise KeyError(f'费用名称"{item_name}"不存在')
return self.sub_feecollections[item_name]