bt builder

This commit is contained in:
IDONTSUDO 2024-05-29 15:38:36 +03:00
parent 5162612a77
commit c5ae89d18a
52 changed files with 1013 additions and 441 deletions

View file

@ -1,9 +1,21 @@
import { IsArray, IsString, ValidateNested } from "class-validator";
import { IsArray, IsOptional, IsString, ValidateNested } from "class-validator";
import { Type } from "class-transformer";
import { ISkillView } from "../../features/behavior_tree_builder/presentation/ui/skill_tree/skill_tree";
import { v4 } from "uuid";
import { Result } from "../helper/result";
import clone from "just-clone";
export interface ISkillPoseEstimation {
export interface IDependency {
skills: ISkillDependency[];
}
export interface ISkillDependency {
sid: string;
dependency: Object;
}
export interface ISkill {
sid?: string;
SkillPackage: ISkillPackage;
Module: IModule;
Launch: ILaunch;
@ -14,17 +26,21 @@ export interface ISkillPoseEstimation {
xxx: IXxx;
}
export interface IWeightsDependency {
objectName: string;
weightsPath: string;
weights_name:string;
object_name: string;
weights_file: string;
dimensions: number[];
}
export interface IParam {
type: string;
dependency: IWeightsDependency;
dependency: Object;
}
export interface IBTAction {
name: string;
format: string;
// TODO: Нужно выпилить его отсюда
// sid?: string;
type: string;
param: IParam[];
result: string[];
@ -91,6 +107,7 @@ export class BTAction implements IBTAction {
format: string;
@IsString()
type: string;
sid?: string;
@IsArray()
param: IParam[];
@IsArray()
@ -127,7 +144,10 @@ export class Xxx implements IXxx {
topicImage: string;
topicCameraInfo: string;
}
export class SkillModelPoseEstimation implements ISkillPoseEstimation {
export class SkillModel implements ISkill {
@IsOptional()
@IsString()
sid?: string;
@ValidateNested()
@Type(() => SkillPackage)
SkillPackage: ISkillPackage;
@ -154,12 +174,71 @@ export class SkillModelPoseEstimation implements ISkillPoseEstimation {
@ValidateNested()
@Type(() => Xxx)
xxx: IXxx;
static empty() {
const skillModel = new SkillModel();
skillModel.BTAction = [];
return skillModel;
}
public static isEmpty(skill: SkillModel): Result<void, SkillModel> {
if (skill.BTAction.isEmpty()) {
return Result.error(undefined);
}
return Result.ok(Object.assign(skill, {}));
}
public getSid = () => this.sid;
public setSid = (sid: string) => {
const result = clone(this);
result.sid = sid;
return result;
};
}
export class SkillDependency implements IDependency {
constructor(public skills: ISkillDependency[]) {}
static empty() {
return new SkillDependency([]);
}
static isEmpty = (skill: SkillDependency) => {
if (skill.skills.isEmpty()) {
return Result.error(undefined);
}
return Result.ok(skill);
};
}
export class Skills {
@IsArray()
@Type(() => SkillModelPoseEstimation)
skills: SkillModelPoseEstimation[];
@Type(() => SkillModel)
skills: SkillModel[];
validation = (): Result<string[], void> => {
const errors: string[] = [];
this.skills.forEach((skill) => {
skill.BTAction.forEach((action) => {
if (action.param.isNotEmpty()) {
action.param.forEach((param) => {
if (Object.keys(param.dependency).isEmpty()) {
errors.push(param.type);
}
});
}
});
});
if (errors.isNotEmpty()) {
return Result.error(errors);
}
return Result.ok(undefined);
};
skillBySid = (sid: string) =>
SkillModel.isEmpty(
this.skills.reduce<SkillModel>((acc, el) => {
if (el.sid?.isEqual(sid)) {
acc = el;
}
return acc;
}, SkillModel.empty())
);
toSkillView = (): ISkillView[] =>
this.skills.map((el) => {
return {
@ -169,6 +248,20 @@ export class Skills {
}),
};
});
getSkill = (name: string) =>
SkillModel.isEmpty(
this.skills.reduce<SkillModel>((acc, el) => {
if (el.BTAction.find((el) => el.name.isEqual(name))) {
el.BTAction.map((action) => {
action.param.map((param) => {
return param;
});
});
acc = el;
}
return acc;
}, SkillModel.empty())
);
getSkilsOut = (name: string) =>
this.skills
@ -207,7 +300,7 @@ export class Skills {
getForms = (skillLabel: string) =>
this.skills
.reduce<SkillModelPoseEstimation[]>((acc, el) => {
.reduce<SkillModel[]>((acc, el) => {
if (el.BTAction.find((el) => el.name.isEqual(skillLabel))) {
acc.push(el);
}
@ -217,17 +310,83 @@ export class Skills {
.flat(1)
.flat(1)
.filter((el) => el !== "");
getDependencyBySkillLabelAndType = <T>(skillLabel: string, skillType: string) =>
getDependencyBySkillLabelAndType = <T>(skillType: string, sid: string) =>
this.skills
.reduce<SkillModelPoseEstimation[]>((acc, el) => {
if (el.BTAction.find((el) => el.name.isEqual(skillLabel))) {
acc.push(el);
.reduce<Object[]>((acc, skill) => {
if (skill.sid?.isEqual(sid)) {
skill.BTAction.map((action) => {
action.param.map((param) => {
if (param.type.isEqual(skillType)) {
acc.push(param.dependency);
}
return param;
});
return action;
});
}
return acc;
}, [])
.map((el) => el.BTAction.map((act) => act.param.filter((el) => el.type.isEqual(skillType))))
.flat(1)
.flat(1)
.map((el) => el.dependency)
.at(0) as T;
static isEmpty(model: Skills): Result<void, void> {
if (model.skills.isEmpty()) {
return Result.error(undefined);
}
return Result.ok(undefined);
}
static empty() {
const skills = new Skills();
skills.skills = [];
return skills;
}
public dependencyIsFilled = (skillType: string, sid: string) =>
this.skills.reduce((acc, skill) => {
if (skill.sid?.isEqual(sid)) {
skill.BTAction.forEach((action) => {
action.param.forEach((param) => {
if (param.type.isEqual(skillType)) {
// console.log('SKILL TYPE')
// console.log(skillType);
// console.log("SID")
// console.log(sid)
// console.log("DEPENDENCY")
// console.log(param.dependency)
acc = Object.keys(param.dependency).isNotEmpty();
}
});
});
}
return acc;
}, false);
getAllSids = () =>
this.skills.reduce((acc, skill) => {
skill.BTAction.forEach((action) =>
action.param.forEach((param) => {
// acc.incrementValue(param.sid ?? "empty");
return param;
})
);
return acc;
}, new Map<string, number>());
deleteSid(sid: string): SkillModel[] {
return this.skills.filter((skill) => !skill.sid?.isEqual(sid));
}
updateSkill = (skill: SkillModel) => {
console.log(skill);
this.skills = this.skills.map((el) => {
if (el.sid?.isEqual(skill.sid ?? "")) {
el = skill;
}
return el;
});
};
skillHasForm = (label: string): boolean => {
// TODO:NEED IMPLEMENTS
return true;
};
}