From 2d3ead6f70d7453c257bbe9f00a37e2968f74f3a Mon Sep 17 00:00:00 2001 From: Jules Neny Date: Tue, 27 Oct 2026 12:50:36 +0100 Subject: [PATCH] =?UTF-8?q?feat(connaissances):=20B19=20M1=20=E2=80=94=20a?= =?UTF-8?q?daptateur=20toMiniConnaissance=20(nature,=20=C3=A9chelle,=20por?= =?UTF-8?q?teur,=20couche)=20et=20n=C5=93uds=20du=20graphe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0179wsewuntMrKBA2s9y7ZeX --- composables/useFicheAdapter.ts | 125 ++++++++++++++++++++++++++++++++- scripts/test-connaissances.mjs | 29 ++++++++ 2 files changed, 152 insertions(+), 2 deletions(-) diff --git a/composables/useFicheAdapter.ts b/composables/useFicheAdapter.ts index 995adc1..a09f630 100644 --- a/composables/useFicheAdapter.ts +++ b/composables/useFicheAdapter.ts @@ -27,7 +27,7 @@ export interface MiniFiche { chipsDimension?: string echelle?: string geoloc: boolean - kind: 'org' | 'structure' | 'mot' + kind: 'org' | 'structure' | 'mot' | 'concept' | 'texte' | 'organisation' | 'outil' /** Étiquettes complètes par dimension (valeurs telles qu'écrites dans l'URL). */ etiquettes?: Record coords?: [number, number] @@ -193,6 +193,127 @@ export function toMiniMot(fiche: FicheMot, ressources: RessourceMot[]): MiniFich } } +// ── Connaissances partagées (B19-M1) : un objet de la couche = une fiche ────── + +/** + * Un objet de `data/connaissances/` (schéma B18). Type local, volontairement lâche : + * le contrat fait foi dans `data/connaissances/schema.json`, le contrôle dans + * `utils/connaissances/valider.ts`. + */ +export interface ObjetCouche { + id: string + nature: 'texte' | 'organisation' | 'outil' | 'concept' + nom_canonique: string + etat: string + definition_courte?: string + libelle_source?: string + variantes?: string[] + porteur_ids?: string[] + echelles?: string[] + couche?: string + date_original?: string | null + credit?: string | null + [champ: string]: unknown +} +export interface JeuCouche { + objets: ObjetCouche[] + relations: { type: string; de: string; vers: string; provenance: string; etat: string; note?: string }[] + [champ: string]: unknown +} + +export const LIBELLES_NATURE: Record = { + concept: 'Concept', + texte: 'Texte', + organisation: 'Organisation', + outil: 'Outil', +} + +/** + * Ce que la page rend : les objets publiés. Un objet écarté reste en base et n'est jamais + * rendu ; `en_file`, `a_trier` et `a_creuser` non plus, tant qu'un humain n'a pas tranché + * (interdit n° 2 de la CONCEPTION §5). + */ +export const estRendu = (o: { etat: string }) => o.etat === 'publie' + +/** `org:holosapiens` → `holosapiens` : la forme d'un porteur dans l'URL (`porteur=holosapiens`). */ +export const valeurPorteur = (id: string) => id.replace(/^[a-z]+:/, '') + +/** Porteurs d'un objet : ses `porteur_ids` qui sont des organisations rendues ; une organisation se porte elle-même. */ +function porteursDe(o: ObjetCouche, rendus: Map): ObjetCouche[] { + if (o.nature === 'organisation') return [o] + return (o.porteur_ids ?? []).map((id) => rendus.get(id)).filter((p): p is ObjetCouche => !!p && p.nature === 'organisation') +} + +/** Échelles d'un objet : les siennes ; pour une source, celles des concepts publiés qui en sont extraits (projection, rien en base). */ +function echellesDe(o: ObjetCouche, jeu: JeuCouche, rendus: Map): string[] { + if (o.echelles?.length) return [...o.echelles] + const out = new Set() + for (const r of jeu.relations) { + if (r.type !== 'extrait_de' || r.vers !== o.id) continue + for (const e of rendus.get(r.de)?.echelles ?? []) out.add(e) + } + return ['S', 'M', 'XL'].filter((e) => out.has(e)) +} + +/** + * Un objet de la couche → `MiniFiche`. `id` = identifiant préfixé (`cpt:holoptisme`), + * `kind` = nature, résumé = définition dans nos mots (concept) ou titre et porteur (objet). + * Pas de géographie : `geoloc: true`, comme `toMiniMot` (sinon la vue annoncerait « N sans + * adresse » et n'offrirait pas « Voir dans le graphe »). + */ +export function toMiniConnaissance(objet: ObjetCouche, jeu: JeuCouche): MiniFiche { + const rendus = new Map(jeu.objets.filter(estRendu).map((o) => [o.id, o])) + const porteurs = porteursDe(objet, rendus) + const echelles = echellesDe(objet, jeu, rendus) + const nomsPorteurs = porteurs.filter((p) => p.id !== objet.id).map((p) => p.nom_canonique) + const resume = objet.nature === 'concept' + ? (objet.definition_courte ?? '') + : [objet.nom_canonique, nomsPorteurs.join(', ')].filter(Boolean).join(' — ') + return { + id: objet.id, + kind: objet.nature, + nom: objet.nom_canonique, + resume: truncateOnWord(resume, 140), + chips: echelles, + chipsDimension: 'echelle', + /** Badge non cliquable de la carte-fiche : la nature. */ + echelle: LIBELLES_NATURE[objet.nature], + geoloc: true, + etiquettes: { + nature: [objet.nature], + echelle: echelles, + porteur: porteurs.map((p) => valeurPorteur(p.id)), + couche: objet.couche ? [objet.couche] : [], + }, + date: objet.date_original ?? undefined, + texte: [objet.libelle_source, objet.credit, ...(objet.variantes ?? []), ...nomsPorteurs].filter(Boolean).join(' '), + } +} + +/** Les fiches de la page : objets publiés, dans l'ordre du jeu. */ +export function fichesConnaissances(jeu: JeuCouche): MiniFiche[] { + return jeu.objets.filter(estRendu).map((o) => toMiniConnaissance(o, jeu)) +} + +/** + * Nœuds et liens du graphe, depuis les seuls objets publiés et les relations publiées + * entre eux (`renvoie_a` et les porteurs du registre restent hors du graphe : ils visent + * une autre base). Aucune position : `GrapheConnaissances` la calcule. + */ +export function grapheConnaissances(jeu: JeuCouche): { + noeuds: { id: string; libelle: string; kind: string; poids: number }[] + liens: { de: string; vers: string; type: string; provenance: string }[] +} { + const rendus = new Map(jeu.objets.filter(estRendu).map((o) => [o.id, o])) + const liens = jeu.relations + .filter((r) => r.etat === 'publie' && r.type !== 'renvoie_a' && rendus.has(r.de) && rendus.has(r.vers)) + .map((r) => ({ de: r.de, vers: r.vers, type: r.type, provenance: r.provenance })) + const degre = new Map() + for (const l of liens) for (const id of [l.de, l.vers]) degre.set(id, (degre.get(id) ?? 0) + 1) + const noeuds = [...rendus.values()].map((o) => ({ id: o.id, libelle: o.nom_canonique, kind: o.nature, poids: degre.get(o.id) ?? 0 })) + return { noeuds, liens } +} + export function useFicheAdapter() { - return { toMini, toMiniMot } + return { toMini, toMiniMot, toMiniConnaissance } } diff --git a/scripts/test-connaissances.mjs b/scripts/test-connaissances.mjs index 32d376e..810b7c4 100644 --- a/scripts/test-connaissances.mjs +++ b/scripts/test-connaissances.mjs @@ -172,5 +172,34 @@ ok(V.valider(joint).ok, 'le jeu joint passe toujours le validateur') } ok(J.idRegistre('registre:P2-18') === 'P2-18' && J.idRegistre('cpt:x') === null, 'idRegistre : préfixe registre: seulement') +// ── B19 M1 : adaptateur toMiniConnaissance et graphe ──────────── +const A = await jiti.import(join(racine, 'composables', 'useFicheAdapter.ts')) +{ + const fiches = A.fichesConnaissances(fixture) + const ids = fiches.map((f) => f.id) + ok(fiches.length === fixture.objets.filter((o) => o.etat === 'publie').length, `adaptateur : une fiche par objet publié (${fiches.length})`) + ok(!ids.includes('cpt:signaux-faibles-ansoff') && !ids.includes('txt:noubel-economie-du-don'), 'adaptateur : a_trier et a_creuser ne sont pas rendus') + { + const j = copie(fixture) + objet(j, 'cpt:matrice-aep').etat = 'ecarte' + ok(!A.fichesConnaissances(j).some((f) => f.id === 'cpt:matrice-aep'), 'adaptateur : un objet écarté n’est pas rendu') + } + const h = fiches.find((f) => f.id === 'cpt:holoptisme') + ok(h.kind === 'concept' && h.nom === 'Holoptisme' && h.resume === objet(fixture, 'cpt:holoptisme').definition_courte, 'adaptateur : id préfixé, kind = nature, nom canonique, résumé = définition') + ok(JSON.stringify(h.etiquettes) === JSON.stringify({ nature: ['concept'], echelle: ['S', 'M'], porteur: ['holosapiens'], couche: ['socle'] }), 'adaptateur : étiquettes nature, échelle, porteur, couche', JSON.stringify(h.etiquettes)) + ok(h.date === '2015-08-08' && h.geoloc === true, 'adaptateur : date = date_original ; pas de géographie') + const t = fiches.find((f) => f.id === 'txt:hs-signaux-faibles') + ok(t.kind === 'texte' && t.resume.includes('Les signaux faibles') && t.resume.includes('Holosapiens'), 'adaptateur : résumé d’un objet = titre et porteur', t.resume) + ok(JSON.stringify(t.etiquettes.echelle) === JSON.stringify(['XL']), 'adaptateur : échelles d’une source = celles de ses concepts (projection)', JSON.stringify(t.etiquettes.echelle)) + ok(fiches.find((f) => f.id === 'org:holosapiens').etiquettes.porteur[0] === 'holosapiens', 'adaptateur : une organisation se porte elle-même') + + const g = A.grapheConnaissances(fixture) + ok(g.noeuds.length === fiches.length, 'graphe : un nœud par fiche') + ok(g.liens.every((l) => ids.includes(l.de) && ids.includes(l.vers)), 'graphe : aucun lien vers un objet non rendu') + ok(!g.liens.some((l) => l.type === 'renvoie_a' || l.type === 'homonyme_de'), 'graphe : ni renvoie_a ni homonyme vers un non-publié') + ok(g.noeuds.every((x) => !('x' in x) && !('y' in x)), 'graphe : aucune position dans les données') + ok(g.liens.some((l) => l.type === 'recoupe' && l.de === 'cpt:architectures-invisibles' && l.vers === 'cpt:matrice-aep'), 'graphe : le croisement architectures invisibles ↔ matrice est un lien') +} + console.log(`\n${n - echecs}/${n} OK${echecs ? ` · ${echecs} ÉCHEC(S)` : ''}`) process.exit(echecs ? 1 : 0)