Skip to content

Commit

Permalink
fix: refine codes
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Nov 6, 2024
1 parent b346ef9 commit a6d68d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
24 changes: 10 additions & 14 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ do you want to continue?",

Ok(())
} else {
// The last model always represents the latest version.
// The last model always represents the optimal version.
//
// The first error messages (i.e., non_usable_models.first()) might be
// changed when we run `tman install` in an app folder, if the app
Expand Down Expand Up @@ -904,19 +904,15 @@ do you want to continue?",
// depends_on_declared("app", "ten_agent", "0.4.0", "extension", "agora_rtm", "0.1.0").
// depends_on_declared("app", "ten_agent", "0.4.0", "extension", "agora_rtm", "0.1.1").
// ```
//
// When clingo analyzes the input.lp, it generates the answer models
// based on the order of the `depends_on_declared` facts. If there is no
// answer for the version declared in the first `depends_on_declared`
// fact, it will ignore the versions smaller than it. Ex: in case 1,
// there will be answer models for both 0.1.0, 0.1.1 and 0.1.2, and the
// first model is for 0.1.0, and the last is for 0.1.2. While in case 2,
// there only be answer models for 0.1.2. If we take the first error
// message (i.e., non_usable_models.first()) from the answer models, the
// error messages for those two cases are as above.
//
// That's also why we need to take the last model as the final error
// message.
// Due to the different order of input data, clingo may start searching
// from different points. However, clingo will only output increasingly
// better models. Therefore, if we select the first `non_usable_models`,
// it is often inconsistent. But if we select the last model, it is
// usually consistent, representing the optimal error model that clingo
// can find. This phenomenon is similar to the gradient descent process
// in neural networks that seeks local optima. Thus, we should select
// the last model to obtain the optimal error model and achieve stable
// results.
if let Some(non_usable_model) = non_usable_models.last() {
// Extract introducer relations, and parse the error message.
if let Ok(conflict_info) = parse_error_statement(non_usable_model) {
Expand Down
20 changes: 13 additions & 7 deletions core/src/ten_manager/src/solver/introducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ pub fn extract_introducer_relations_from_raw_solver_results(

pub fn get_dependency_chain(
introducer: &PkgInfo,
conflict_pkg: &PkgInfo,
conflict_pkg_identity: &PkgIdentity,
introducer_relations: &HashMap<PkgInfo, (String, Option<PkgInfo>)>,
) -> Vec<(String, PkgInfo)> {
) -> Vec<(String, PkgIdentity)> {
let mut chain = Vec::new();
let mut current_pkg = introducer.clone();
let mut visited = HashSet::new();
Expand All @@ -99,8 +99,8 @@ pub fn get_dependency_chain(
// dependency chain first.
let requested_dep_in_introducer = introducer
.get_dependency_by_type_and_name(
&conflict_pkg.pkg_identity.pkg_type.to_string(),
&conflict_pkg.pkg_identity.name,
&conflict_pkg_identity.pkg_type.to_string(),
&conflict_pkg_identity.name,
)
.unwrap();
chain.push((
Expand All @@ -109,7 +109,7 @@ pub fn get_dependency_chain(
.as_ref()
.unwrap()
.clone(),
conflict_pkg.clone(),
conflict_pkg_identity.clone(),
));

loop {
Expand All @@ -124,12 +124,18 @@ pub fn get_dependency_chain(
// `current_pkg`@`requested_version`, i.e., the
// `requested_version` is used to declared the `current_pkg` in
// the manifest.json of `introducer_pkg`.
chain.push((requested_version.clone(), current_pkg.clone()));
chain.push((
requested_version.clone(),
current_pkg.pkg_identity.clone(),
));
current_pkg = introducer_pkg.clone();
}
Some((requested_version, None)) => {
// Reached the root.
chain.push((requested_version.clone(), current_pkg.clone()));
chain.push((
requested_version.clone(),
current_pkg.pkg_identity.clone(),
));
break;
}
None => {
Expand Down
28 changes: 11 additions & 17 deletions core/src/ten_manager/src/solver/solver_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn parse_error_statement(clingo_output: &[String]) -> Result<ConflictInfo> {
}

fn print_dependency_chain(
chain: &[(String, PkgInfo)],
chain: &[(String, PkgIdentity)],
pkg_type: &str,
pkg_name: &str,
version: &str,
Expand All @@ -133,8 +133,8 @@ fn print_dependency_chain(
println!(
"{:indent$}└─ [{}]{}@{}",
"",
pkg.1.pkg_identity.pkg_type,
pkg.1.pkg_identity.name,
pkg.1.pkg_type,
pkg.1.name,
pkg.0,
indent = i * 3
);
Expand Down Expand Up @@ -169,27 +169,21 @@ pub fn print_conflict_info(
)?;

// The introducer_pkg_info_1/introducer_pkg_info_2 (i.e., the introducer)
// depends on the pkg identified by `conflict_pkg_type` &
// `conflict_pkg_name` (i.e., the leaf node). The conflict_pkg_version_x
// used to get the pkg_info does not matter, as it will be replaced by
// the `version_req_str` of the introducer, refer
// to `get_dependency_chain()`.
let leaf_node_pkg = get_pkg_info_from_candidates(
&conflict_info.conflict_pkg_type,
&conflict_info.conflict_pkg_name,
&conflict_info.conflict_pkg_version_1.pkg_version,
all_candidates,
)?;
// depends on the pkg identified by `conflict_pkg_identity`.
let conflict_pkg_identity = PkgIdentity {
pkg_type: conflict_info.conflict_pkg_type.parse()?,
name: conflict_info.conflict_pkg_name.clone(),
};

// Get dependency chains
// Get dependency chains.
let chain1 = get_dependency_chain(
&introducer_pkg_info_1,
&leaf_node_pkg,
&conflict_pkg_identity,
introducer_relations,
);
let chain2 = get_dependency_chain(
&introducer_pkg_info_2,
&leaf_node_pkg,
&conflict_pkg_identity,
introducer_relations,
);

Expand Down

0 comments on commit a6d68d0

Please sign in to comment.