kweaver-dolphin 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- DolphinLanguageSDK/__init__.py +58 -0
- dolphin/__init__.py +62 -0
- dolphin/cli/__init__.py +20 -0
- dolphin/cli/args/__init__.py +9 -0
- dolphin/cli/args/parser.py +567 -0
- dolphin/cli/builtin_agents/__init__.py +22 -0
- dolphin/cli/commands/__init__.py +4 -0
- dolphin/cli/interrupt/__init__.py +8 -0
- dolphin/cli/interrupt/handler.py +205 -0
- dolphin/cli/interrupt/keyboard.py +82 -0
- dolphin/cli/main.py +49 -0
- dolphin/cli/multimodal/__init__.py +34 -0
- dolphin/cli/multimodal/clipboard.py +327 -0
- dolphin/cli/multimodal/handler.py +249 -0
- dolphin/cli/multimodal/image_processor.py +214 -0
- dolphin/cli/multimodal/input_parser.py +149 -0
- dolphin/cli/runner/__init__.py +8 -0
- dolphin/cli/runner/runner.py +989 -0
- dolphin/cli/ui/__init__.py +10 -0
- dolphin/cli/ui/console.py +2795 -0
- dolphin/cli/ui/input.py +340 -0
- dolphin/cli/ui/layout.py +425 -0
- dolphin/cli/ui/stream_renderer.py +302 -0
- dolphin/cli/utils/__init__.py +8 -0
- dolphin/cli/utils/helpers.py +135 -0
- dolphin/cli/utils/version.py +49 -0
- dolphin/core/__init__.py +107 -0
- dolphin/core/agent/__init__.py +10 -0
- dolphin/core/agent/agent_state.py +69 -0
- dolphin/core/agent/base_agent.py +970 -0
- dolphin/core/code_block/__init__.py +0 -0
- dolphin/core/code_block/agent_init_block.py +0 -0
- dolphin/core/code_block/assign_block.py +98 -0
- dolphin/core/code_block/basic_code_block.py +1865 -0
- dolphin/core/code_block/explore_block.py +1327 -0
- dolphin/core/code_block/explore_block_v2.py +712 -0
- dolphin/core/code_block/explore_strategy.py +672 -0
- dolphin/core/code_block/judge_block.py +220 -0
- dolphin/core/code_block/prompt_block.py +32 -0
- dolphin/core/code_block/skill_call_deduplicator.py +291 -0
- dolphin/core/code_block/tool_block.py +129 -0
- dolphin/core/common/__init__.py +17 -0
- dolphin/core/common/constants.py +176 -0
- dolphin/core/common/enums.py +1173 -0
- dolphin/core/common/exceptions.py +133 -0
- dolphin/core/common/multimodal.py +539 -0
- dolphin/core/common/object_type.py +165 -0
- dolphin/core/common/output_format.py +432 -0
- dolphin/core/common/types.py +36 -0
- dolphin/core/config/__init__.py +16 -0
- dolphin/core/config/global_config.py +1289 -0
- dolphin/core/config/ontology_config.py +133 -0
- dolphin/core/context/__init__.py +12 -0
- dolphin/core/context/context.py +1580 -0
- dolphin/core/context/context_manager.py +161 -0
- dolphin/core/context/var_output.py +82 -0
- dolphin/core/context/variable_pool.py +356 -0
- dolphin/core/context_engineer/__init__.py +41 -0
- dolphin/core/context_engineer/config/__init__.py +5 -0
- dolphin/core/context_engineer/config/settings.py +402 -0
- dolphin/core/context_engineer/core/__init__.py +7 -0
- dolphin/core/context_engineer/core/budget_manager.py +327 -0
- dolphin/core/context_engineer/core/context_assembler.py +583 -0
- dolphin/core/context_engineer/core/context_manager.py +637 -0
- dolphin/core/context_engineer/core/tokenizer_service.py +260 -0
- dolphin/core/context_engineer/example/incremental_example.py +267 -0
- dolphin/core/context_engineer/example/traditional_example.py +334 -0
- dolphin/core/context_engineer/services/__init__.py +5 -0
- dolphin/core/context_engineer/services/compressor.py +399 -0
- dolphin/core/context_engineer/utils/__init__.py +6 -0
- dolphin/core/context_engineer/utils/context_utils.py +441 -0
- dolphin/core/context_engineer/utils/message_formatter.py +270 -0
- dolphin/core/context_engineer/utils/token_utils.py +139 -0
- dolphin/core/coroutine/__init__.py +15 -0
- dolphin/core/coroutine/context_snapshot.py +154 -0
- dolphin/core/coroutine/context_snapshot_profile.py +922 -0
- dolphin/core/coroutine/context_snapshot_store.py +268 -0
- dolphin/core/coroutine/execution_frame.py +145 -0
- dolphin/core/coroutine/execution_state_registry.py +161 -0
- dolphin/core/coroutine/resume_handle.py +101 -0
- dolphin/core/coroutine/step_result.py +101 -0
- dolphin/core/executor/__init__.py +18 -0
- dolphin/core/executor/debug_controller.py +630 -0
- dolphin/core/executor/dolphin_executor.py +1063 -0
- dolphin/core/executor/executor.py +624 -0
- dolphin/core/flags/__init__.py +27 -0
- dolphin/core/flags/definitions.py +49 -0
- dolphin/core/flags/manager.py +113 -0
- dolphin/core/hook/__init__.py +95 -0
- dolphin/core/hook/expression_evaluator.py +499 -0
- dolphin/core/hook/hook_dispatcher.py +380 -0
- dolphin/core/hook/hook_types.py +248 -0
- dolphin/core/hook/isolated_variable_pool.py +284 -0
- dolphin/core/interfaces.py +53 -0
- dolphin/core/llm/__init__.py +0 -0
- dolphin/core/llm/llm.py +495 -0
- dolphin/core/llm/llm_call.py +100 -0
- dolphin/core/llm/llm_client.py +1285 -0
- dolphin/core/llm/message_sanitizer.py +120 -0
- dolphin/core/logging/__init__.py +20 -0
- dolphin/core/logging/logger.py +526 -0
- dolphin/core/message/__init__.py +8 -0
- dolphin/core/message/compressor.py +749 -0
- dolphin/core/parser/__init__.py +8 -0
- dolphin/core/parser/parser.py +405 -0
- dolphin/core/runtime/__init__.py +10 -0
- dolphin/core/runtime/runtime_graph.py +926 -0
- dolphin/core/runtime/runtime_instance.py +446 -0
- dolphin/core/skill/__init__.py +14 -0
- dolphin/core/skill/context_retention.py +157 -0
- dolphin/core/skill/skill_function.py +686 -0
- dolphin/core/skill/skill_matcher.py +282 -0
- dolphin/core/skill/skillkit.py +700 -0
- dolphin/core/skill/skillset.py +72 -0
- dolphin/core/trajectory/__init__.py +10 -0
- dolphin/core/trajectory/recorder.py +189 -0
- dolphin/core/trajectory/trajectory.py +522 -0
- dolphin/core/utils/__init__.py +9 -0
- dolphin/core/utils/cache_kv.py +212 -0
- dolphin/core/utils/tools.py +340 -0
- dolphin/lib/__init__.py +93 -0
- dolphin/lib/debug/__init__.py +8 -0
- dolphin/lib/debug/visualizer.py +409 -0
- dolphin/lib/memory/__init__.py +28 -0
- dolphin/lib/memory/async_processor.py +220 -0
- dolphin/lib/memory/llm_calls.py +195 -0
- dolphin/lib/memory/manager.py +78 -0
- dolphin/lib/memory/sandbox.py +46 -0
- dolphin/lib/memory/storage.py +245 -0
- dolphin/lib/memory/utils.py +51 -0
- dolphin/lib/ontology/__init__.py +12 -0
- dolphin/lib/ontology/basic/__init__.py +0 -0
- dolphin/lib/ontology/basic/base.py +102 -0
- dolphin/lib/ontology/basic/concept.py +130 -0
- dolphin/lib/ontology/basic/object.py +11 -0
- dolphin/lib/ontology/basic/relation.py +63 -0
- dolphin/lib/ontology/datasource/__init__.py +27 -0
- dolphin/lib/ontology/datasource/datasource.py +66 -0
- dolphin/lib/ontology/datasource/oracle_datasource.py +338 -0
- dolphin/lib/ontology/datasource/sql.py +845 -0
- dolphin/lib/ontology/mapping.py +177 -0
- dolphin/lib/ontology/ontology.py +733 -0
- dolphin/lib/ontology/ontology_context.py +16 -0
- dolphin/lib/ontology/ontology_manager.py +107 -0
- dolphin/lib/skill_results/__init__.py +31 -0
- dolphin/lib/skill_results/cache_backend.py +559 -0
- dolphin/lib/skill_results/result_processor.py +181 -0
- dolphin/lib/skill_results/result_reference.py +179 -0
- dolphin/lib/skill_results/skillkit_hook.py +324 -0
- dolphin/lib/skill_results/strategies.py +328 -0
- dolphin/lib/skill_results/strategy_registry.py +150 -0
- dolphin/lib/skillkits/__init__.py +44 -0
- dolphin/lib/skillkits/agent_skillkit.py +155 -0
- dolphin/lib/skillkits/cognitive_skillkit.py +82 -0
- dolphin/lib/skillkits/env_skillkit.py +250 -0
- dolphin/lib/skillkits/mcp_adapter.py +616 -0
- dolphin/lib/skillkits/mcp_skillkit.py +771 -0
- dolphin/lib/skillkits/memory_skillkit.py +650 -0
- dolphin/lib/skillkits/noop_skillkit.py +31 -0
- dolphin/lib/skillkits/ontology_skillkit.py +89 -0
- dolphin/lib/skillkits/plan_act_skillkit.py +452 -0
- dolphin/lib/skillkits/resource/__init__.py +52 -0
- dolphin/lib/skillkits/resource/models/__init__.py +6 -0
- dolphin/lib/skillkits/resource/models/skill_config.py +109 -0
- dolphin/lib/skillkits/resource/models/skill_meta.py +127 -0
- dolphin/lib/skillkits/resource/resource_skillkit.py +393 -0
- dolphin/lib/skillkits/resource/skill_cache.py +215 -0
- dolphin/lib/skillkits/resource/skill_loader.py +395 -0
- dolphin/lib/skillkits/resource/skill_validator.py +406 -0
- dolphin/lib/skillkits/resource_skillkit.py +11 -0
- dolphin/lib/skillkits/search_skillkit.py +163 -0
- dolphin/lib/skillkits/sql_skillkit.py +274 -0
- dolphin/lib/skillkits/system_skillkit.py +509 -0
- dolphin/lib/skillkits/vm_skillkit.py +65 -0
- dolphin/lib/utils/__init__.py +9 -0
- dolphin/lib/utils/data_process.py +207 -0
- dolphin/lib/utils/handle_progress.py +178 -0
- dolphin/lib/utils/security.py +139 -0
- dolphin/lib/utils/text_retrieval.py +462 -0
- dolphin/lib/vm/__init__.py +11 -0
- dolphin/lib/vm/env_executor.py +895 -0
- dolphin/lib/vm/python_session_manager.py +453 -0
- dolphin/lib/vm/vm.py +610 -0
- dolphin/sdk/__init__.py +60 -0
- dolphin/sdk/agent/__init__.py +12 -0
- dolphin/sdk/agent/agent_factory.py +236 -0
- dolphin/sdk/agent/dolphin_agent.py +1106 -0
- dolphin/sdk/api/__init__.py +4 -0
- dolphin/sdk/runtime/__init__.py +8 -0
- dolphin/sdk/runtime/env.py +363 -0
- dolphin/sdk/skill/__init__.py +10 -0
- dolphin/sdk/skill/global_skills.py +706 -0
- dolphin/sdk/skill/traditional_toolkit.py +260 -0
- kweaver_dolphin-0.1.0.dist-info/METADATA +521 -0
- kweaver_dolphin-0.1.0.dist-info/RECORD +199 -0
- kweaver_dolphin-0.1.0.dist-info/WHEEL +5 -0
- kweaver_dolphin-0.1.0.dist-info/entry_points.txt +27 -0
- kweaver_dolphin-0.1.0.dist-info/licenses/LICENSE.txt +201 -0
- kweaver_dolphin-0.1.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
from typing import Dict, Any
|
|
3
|
+
|
|
4
|
+
# Avoid circular imports by using type hint strings
|
|
5
|
+
# from .concept import Concept
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ConceptInstance(ABC):
|
|
9
|
+
"""Abstract base class for concept instances (e.g., Object, Relation)
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
concept (Concept): The concept definition this instance belongs to.
|
|
13
|
+
values (Dict[str, Any]): Instance members and their corresponding values.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, concept: "Concept", values: Dict[str, Any]):
|
|
17
|
+
"""Initialize concept instance
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
concept (Concept): The concept this instance belongs to.
|
|
21
|
+
values (Dict[str, Any]): Instance members and their corresponding values.
|
|
22
|
+
|
|
23
|
+
Raises:
|
|
24
|
+
TypeError: If concept is not an instance of Concept.
|
|
25
|
+
ValueError: If the provided values do not match the members or type requirements of Concept.
|
|
26
|
+
"""
|
|
27
|
+
# Delayed import to avoid circular dependencies
|
|
28
|
+
from .concept import Concept
|
|
29
|
+
|
|
30
|
+
if not isinstance(concept, Concept):
|
|
31
|
+
raise TypeError("必须提供一个 Concept 实例")
|
|
32
|
+
|
|
33
|
+
# 1. Verify that the member exists and is complete
|
|
34
|
+
providedMembers = set(values.keys())
|
|
35
|
+
if not concept.validateMembers(providedMembers):
|
|
36
|
+
missing = set(concept.members.keys()) - providedMembers
|
|
37
|
+
extra = providedMembers - set(concept.members.keys())
|
|
38
|
+
errorMsg = f"实例的成员与 Concept '{concept.name}' 不匹配。"
|
|
39
|
+
if missing:
|
|
40
|
+
errorMsg += f" 缺少成员: {missing}."
|
|
41
|
+
if extra:
|
|
42
|
+
errorMsg += f" 多余成员: {extra}."
|
|
43
|
+
raise ValueError(errorMsg)
|
|
44
|
+
|
|
45
|
+
# 2. Validate member value types
|
|
46
|
+
for memberName, value in values.items():
|
|
47
|
+
if not concept.validateMemberValue(memberName, value):
|
|
48
|
+
expectedType = concept.members[memberName].name
|
|
49
|
+
actualType = type(value).__name__
|
|
50
|
+
raise TypeError(
|
|
51
|
+
f"成员 '{memberName}' 的值类型错误。期望类型: {expectedType}, 实际类型: {actualType}"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# Use object.__setattr__ to avoid triggering custom __setattr__
|
|
55
|
+
object.__setattr__(self, "concept", concept)
|
|
56
|
+
object.__setattr__(self, "values", values)
|
|
57
|
+
|
|
58
|
+
def __getattr__(self, name: str) -> Any:
|
|
59
|
+
"""Allow accessing member values through attributes"""
|
|
60
|
+
if name == "concept" or name == "values":
|
|
61
|
+
# Prevent infinite recursion
|
|
62
|
+
raise AttributeError
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
# Try to get the value from the values dictionary
|
|
66
|
+
return self.values[name]
|
|
67
|
+
except KeyError:
|
|
68
|
+
# If the value does not exist, an AttributeError is raised.
|
|
69
|
+
raise AttributeError(
|
|
70
|
+
f"'{type(self).__name__}' 对象没有属性 '{name}' 或该属性未在 values 中定义"
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
def __setattr__(self, name: str, value: Any) -> None:
|
|
74
|
+
"""Allow setting member values through attributes (if the member is defined in the Concept)"""
|
|
75
|
+
if name in ["concept", "values"]:
|
|
76
|
+
# Allow setting internal attributes
|
|
77
|
+
object.__setattr__(self, name, value)
|
|
78
|
+
elif name in self.concept.members:
|
|
79
|
+
# Validate the type of the new value
|
|
80
|
+
if not self.concept.validateMemberValue(name, value):
|
|
81
|
+
expectedType = self.concept.members[name].name
|
|
82
|
+
actualType = type(value).__name__
|
|
83
|
+
raise TypeError(
|
|
84
|
+
f"尝试设置的成员 '{name}' 的值类型错误。期望类型: {expectedType}, 实际类型: {actualType}"
|
|
85
|
+
)
|
|
86
|
+
# Set value
|
|
87
|
+
self.values[name] = value
|
|
88
|
+
else:
|
|
89
|
+
raise AttributeError(
|
|
90
|
+
f"无法设置属性 '{name}',因为它不是 Concept '{self.concept.name}' 定义的成员"
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
def __repr__(self) -> str:
|
|
94
|
+
"""Return the string representation of the instance."""
|
|
95
|
+
return f"{type(self).__name__}(concept='{self.concept.name}', values={self.values})"
|
|
96
|
+
|
|
97
|
+
# Additional generic instance methods can be added
|
|
98
|
+
# For example, get the value of a specific member, check if a member exists, etc.
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# Additional general instance methods can be added
|
|
102
|
+
# For example, retrieving the value of a specific member, checking whether a member exists, etc.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
from typing import Set, Dict, Any
|
|
2
|
+
from enum import Enum, auto
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ConceptMemberType(Enum):
|
|
6
|
+
"""Type of Concept Members"""
|
|
7
|
+
|
|
8
|
+
STRING = auto()
|
|
9
|
+
NUMBER = auto()
|
|
10
|
+
BOOLEAN = auto()
|
|
11
|
+
OBJECT = auto() # Referencing Other Objects
|
|
12
|
+
RELATION = auto() # Referencing Other Relations
|
|
13
|
+
ANY = auto() # Any type
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Concept:
|
|
17
|
+
"""Define the contract (structure) for concepts
|
|
18
|
+
|
|
19
|
+
A Concept defines a set of required member names and their types.
|
|
20
|
+
Each member can have type constraints used to validate values during instantiation.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, name: str, members: Dict[str, ConceptMemberType]):
|
|
24
|
+
"""Initialize Concept
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
name (str): The name of the concept
|
|
28
|
+
members (Dict[str, ConceptMemberType]): A mapping from member names to their types
|
|
29
|
+
|
|
30
|
+
Raises:
|
|
31
|
+
ValueError: If the name is empty or no members are defined
|
|
32
|
+
"""
|
|
33
|
+
if not name:
|
|
34
|
+
raise ValueError("概念名称不能为空")
|
|
35
|
+
if not members:
|
|
36
|
+
raise ValueError("概念必须至少定义一个成员")
|
|
37
|
+
|
|
38
|
+
self.name = name
|
|
39
|
+
self.members = members
|
|
40
|
+
self.mappings = []
|
|
41
|
+
|
|
42
|
+
def __repr__(self) -> str:
|
|
43
|
+
members_str = {name: type.name for name, type in self.members.items()}
|
|
44
|
+
return f"Concept(name='{self.name}', members={members_str})"
|
|
45
|
+
|
|
46
|
+
def addMapping(self, mapping):
|
|
47
|
+
self.mappings.append(mapping)
|
|
48
|
+
|
|
49
|
+
def getDataSourceSchemas(self) -> Dict[str, Any]:
|
|
50
|
+
"""Get the schema of all associated data sources"""
|
|
51
|
+
dataSourceSchemas = {}
|
|
52
|
+
for mapping in self.mappings:
|
|
53
|
+
dataSourceSchemas[mapping.space] = mapping.getDataSourceSchema()
|
|
54
|
+
return dataSourceSchemas
|
|
55
|
+
|
|
56
|
+
def validateMembers(self, provided_members: Set[str]) -> bool:
|
|
57
|
+
"""Check whether the provided member set fully matches the requirements defined by this Concept
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
provided_members (Set[str]): Set of member names to validate
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
bool: True if the provided members exactly match the defined members
|
|
64
|
+
"""
|
|
65
|
+
return set(self.members.keys()) == provided_members
|
|
66
|
+
|
|
67
|
+
def validateMemberValue(self, memberName: str, value: Any) -> bool:
|
|
68
|
+
"""Validate whether the member's value meets the type requirements
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
member_name (str): The name of the member
|
|
72
|
+
value (Any): The value to validate
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
bool: True if the value meets the type requirements
|
|
76
|
+
|
|
77
|
+
Raises:
|
|
78
|
+
KeyError: If the member name does not exist
|
|
79
|
+
"""
|
|
80
|
+
if memberName not in self.members:
|
|
81
|
+
raise KeyError(f"成员 '{memberName}' 未在概念 '{self.name}' 中定义")
|
|
82
|
+
|
|
83
|
+
expectedType = self.members[memberName]
|
|
84
|
+
|
|
85
|
+
# If the type is ANY, return True directly
|
|
86
|
+
if expectedType == ConceptMemberType.ANY:
|
|
87
|
+
return True
|
|
88
|
+
|
|
89
|
+
# Validate according to the expected type
|
|
90
|
+
if expectedType == ConceptMemberType.STRING:
|
|
91
|
+
return isinstance(value, str)
|
|
92
|
+
elif expectedType == ConceptMemberType.BOOLEAN:
|
|
93
|
+
# Note: In Python, bool is a subclass of int; BOOLEAN must be checked first
|
|
94
|
+
return isinstance(value, bool)
|
|
95
|
+
elif expectedType == ConceptMemberType.NUMBER:
|
|
96
|
+
# Exclude bool from NUMBER to avoid misjudging True/False as numbers
|
|
97
|
+
return isinstance(value, (int, float)) and not isinstance(value, bool)
|
|
98
|
+
elif expectedType == ConceptMemberType.OBJECT:
|
|
99
|
+
from .object import Object
|
|
100
|
+
|
|
101
|
+
return isinstance(value, Object)
|
|
102
|
+
elif expectedType == ConceptMemberType.RELATION:
|
|
103
|
+
from .relation import Relation
|
|
104
|
+
|
|
105
|
+
return isinstance(value, Relation)
|
|
106
|
+
|
|
107
|
+
return False # Unknown type
|
|
108
|
+
|
|
109
|
+
def validateValues(self, values: Dict[str, Any]) -> bool:
|
|
110
|
+
"""Validate that all values conform to the concept definition
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
values (Dict[str, Any]): Dictionary of values to validate
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
bool: True if all values meet the requirements
|
|
117
|
+
"""
|
|
118
|
+
if not self.validateMembers(set(values.keys())):
|
|
119
|
+
return False
|
|
120
|
+
|
|
121
|
+
return all(
|
|
122
|
+
self.validateMemberValue(name, value) for name, value in values.items()
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
def toDict(self) -> Dict[str, Any]:
|
|
126
|
+
"""Convert concepts to dictionary"""
|
|
127
|
+
return {
|
|
128
|
+
"name": self.name,
|
|
129
|
+
"members": {name: type_.name for name, type_ in self.members.items()},
|
|
130
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from dolphin.lib.ontology.basic.base import ConceptInstance
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Object(ConceptInstance):
|
|
5
|
+
"""A concrete instance (object) representing a Concept.
|
|
6
|
+
|
|
7
|
+
The Object must adhere to the member contract defined by its associated Concept.
|
|
8
|
+
The values of members can be any Python type, or references to other Object/Relation.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
pass # All basic functions inherit from ConceptInstance
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from typing import Dict, Any
|
|
2
|
+
|
|
3
|
+
from dolphin.lib.ontology.basic.base import ConceptInstance
|
|
4
|
+
from dolphin.lib.ontology.basic.concept import Concept
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Relation(ConceptInstance):
|
|
8
|
+
"""A concrete instance (relation) of a Concept
|
|
9
|
+
|
|
10
|
+
A Relation must adhere to the member contract defined by its associated Concept.
|
|
11
|
+
The values of members are typically used to connect Objects, but may also be any Python type.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
pass # All basic functions inherit from ConceptInstance
|
|
15
|
+
|
|
16
|
+
def __init__(self, concept: Concept, values: Dict[str, Any]):
|
|
17
|
+
"""Initialize Relation
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
concept (Concept): The Concept to which this Relation belongs.
|
|
21
|
+
values (Dict[str, Any]): The members of the Relation and their corresponding values.
|
|
22
|
+
|
|
23
|
+
Raises:
|
|
24
|
+
ValueError: If the provided values do not meet the member requirements of the Concept.
|
|
25
|
+
TypeError: If concept is not an instance of Concept.
|
|
26
|
+
"""
|
|
27
|
+
if not isinstance(concept, Concept):
|
|
28
|
+
raise TypeError("必须提供一个 Concept 实例")
|
|
29
|
+
|
|
30
|
+
provided_members = set(values.keys())
|
|
31
|
+
if not concept.validateMembers(provided_members):
|
|
32
|
+
missing = concept.members - provided_members
|
|
33
|
+
extra = provided_members - concept.members
|
|
34
|
+
error_msg = f"Relation 的成员与 Concept '{concept.name}' 不匹配。"
|
|
35
|
+
if missing:
|
|
36
|
+
error_msg += f" 缺少成员: {missing}."
|
|
37
|
+
if extra:
|
|
38
|
+
error_msg += f" 多余成员: {extra}."
|
|
39
|
+
# Strict mode: missing or extra members are not allowed.
|
|
40
|
+
raise ValueError(error_msg)
|
|
41
|
+
|
|
42
|
+
self.concept = concept
|
|
43
|
+
self.values = values
|
|
44
|
+
|
|
45
|
+
def __getattr__(self, name: str) -> Any:
|
|
46
|
+
"""Allow accessing member values through attributes"""
|
|
47
|
+
if name in self.values:
|
|
48
|
+
return self.values[name]
|
|
49
|
+
raise AttributeError(f"'{type(self).__name__}' 对象没有属性 '{name}'")
|
|
50
|
+
|
|
51
|
+
def __setattr__(self, name: str, value: Any) -> None:
|
|
52
|
+
"""Allow setting member values through attributes (if the member is defined)"""
|
|
53
|
+
if name in ["concept", "values"]:
|
|
54
|
+
super().__setattr__(name, value)
|
|
55
|
+
elif name in self.concept.members:
|
|
56
|
+
self.values[name] = value
|
|
57
|
+
else:
|
|
58
|
+
raise AttributeError(
|
|
59
|
+
f"无法设置属性 '{name}',因为它不是 Concept '{self.concept.name}' 定义的成员"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def __repr__(self) -> str:
|
|
63
|
+
return f"Relation(concept='{self.concept.name}', values={self.values})"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from .datasource import DataSource
|
|
2
|
+
|
|
3
|
+
# 延迟导入 SQL 相关的数据源类(需要 sqlalchemy)
|
|
4
|
+
# 如果 sqlalchemy 未安装,这些类将不可用,但不会阻止模块导入
|
|
5
|
+
try:
|
|
6
|
+
from .sql import DataSourceSql, DataSourceMysql, DataSourceSqlite
|
|
7
|
+
from .oracle_datasource import DataSourceOracle
|
|
8
|
+
_SQL_AVAILABLE = True
|
|
9
|
+
except ImportError:
|
|
10
|
+
# sqlalchemy 未安装,这些类不可用
|
|
11
|
+
_SQL_AVAILABLE = False
|
|
12
|
+
DataSourceSql = None
|
|
13
|
+
DataSourceMysql = None
|
|
14
|
+
DataSourceSqlite = None
|
|
15
|
+
DataSourceOracle = None
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"DataSource",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
if _SQL_AVAILABLE:
|
|
22
|
+
__all__.extend([
|
|
23
|
+
"DataSourceSql",
|
|
24
|
+
"DataSourceMysql",
|
|
25
|
+
"DataSourceSqlite",
|
|
26
|
+
"DataSourceOracle",
|
|
27
|
+
])
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from typing import Any, Dict, List, TYPE_CHECKING
|
|
3
|
+
|
|
4
|
+
# Modify the import, use the correct DataSourceType
|
|
5
|
+
from dolphin.core.common.enums import Messages
|
|
6
|
+
from dolphin.core.config import DataSourceType
|
|
7
|
+
|
|
8
|
+
# Use TYPE_CHECKING to avoid circular imports
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from dolphin.lib.ontology.mapping import Mapping
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DataSource(ABC):
|
|
14
|
+
"""Data source abstract base class"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, name: str, type: DataSourceType, config: Dict[str, Any]):
|
|
17
|
+
self.name = name
|
|
18
|
+
self.config = config
|
|
19
|
+
self.schema = None
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def connect(self) -> Any:
|
|
23
|
+
"""Establish a connection to the data source"""
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
@abstractmethod
|
|
27
|
+
def get_schema(self) -> Dict[str, List[str]]:
|
|
28
|
+
"""Get the schema information of the data source (e.g., tables and columns)"""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def close(self) -> None:
|
|
33
|
+
"""Close data source connection"""
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def test_connection(self) -> bool:
|
|
38
|
+
"""Test whether the data source connection is successful"""
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
@abstractmethod
|
|
42
|
+
def executeQuery(self, query: str, fetchColumns: bool = True) -> Dict[str, Any]:
|
|
43
|
+
"""Execute query query"""
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
@abstractmethod
|
|
47
|
+
def sampleData(self, conceptName: str, count: int = 10) -> Messages:
|
|
48
|
+
"""Get sample data according to concept"""
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
@abstractmethod
|
|
52
|
+
def scan(self) -> List["Mapping"]:
|
|
53
|
+
"""Scan the data source schema to generate Concept objects and their corresponding Mappings.
|
|
54
|
+
|
|
55
|
+
Returns a list of Mappings, where each Mapping represents the mapping relationship
|
|
56
|
+
from a data source entity (e.g., a table) to its corresponding Concept.
|
|
57
|
+
"""
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def type(self) -> DataSourceType:
|
|
63
|
+
"""Return the data source type (using config.DataSourceType)"""
|
|
64
|
+
pass
|
|
65
|
+
|
|
66
|
+
# More general methods, such as execute_query, can be added as needed
|