[ui] update identification logic

feat/vaults
Tomáš Mládek 2021-12-02 23:21:03 +01:00
parent 660d232d9a
commit a202eb720c
2 changed files with 8 additions and 47 deletions

View File

@ -27,7 +27,7 @@
if (resolve) { if (resolve) {
resolving = true; resolving = true;
identify($attributes, $backlinks).then((inferredEntries) => { identify($attributes, $backlinks).then((inferredEntries) => {
inferredIds = inferredEntries.map((eid) => eid.value); inferredIds = inferredEntries;
resolving = false; resolving = false;
}); });
} }

View File

@ -4,6 +4,7 @@ import { derived, Readable, readable, writable } from "svelte/store";
import type { IEntry, ListingResult, OrderedListing } from "upend/types"; import type { IEntry, ListingResult, OrderedListing } from "upend/types";
import { listingAsOrdered } from "upend"; import { listingAsOrdered } from "upend";
import LRU from "lru-cache"; import LRU from "lru-cache";
import { attr } from "svelte/internal";
export function useEntity( export function useEntity(
address: string | (() => string), address: string | (() => string),
@ -80,15 +81,10 @@ export async function queryOnce(query: string): Promise<OrderedListing> {
} }
} }
interface EntityIdentification {
type: string;
value: string;
}
export async function identify( export async function identify(
attributes: OrderedListing, attributes: OrderedListing,
backlinks: OrderedListing backlinks: OrderedListing
): Promise<EntityIdentification[]> { ): Promise<string[]> {
// Get all entries where the object is linked // Get all entries where the object is linked
const hasEntries = backlinks const hasEntries = backlinks
.filter(([_, entry]) => entry.attribute === "HAS") .filter(([_, entry]) => entry.attribute === "HAS")
@ -105,44 +101,9 @@ export async function identify(
return entry.value.c; return entry.value.c;
}); });
// Get all identities of the object // Return all LBLs concatenated with named aliases
const isEntries = attributes return attributes
.filter(([_, entry]) => entry.attribute === "IS") .filter(([_, attr]) => attr.attribute === "LBL")
.map(([_, entry]) => entry.value.c); .map(([_, attr]) => attr.value.c)
.concat(aliasValues);
// Out of those, retrieve their TYPE_ID entries
const typeIdListing = isEntries.length
? await queryOnce(
`(matches (in ${isEntries.map((e) => `"${e}"`).join(" ")}) "TYPE_ID" ?)`
)
: [];
const typeIdAttributes = typeIdListing.map(([_, entry]) => {
return [entry.entity, entry.value.c];
});
// Finally, filter own object's attributes according to TYPE_IDs
// For each identity/TYPE_ID pair
return typeIdAttributes
.map(([type, attrName]) => {
// And each associated TYPE_ID attribute...
// return own matchin attributes
return attributes
.filter(([_, e]) => e.attribute === attrName)
.map(([_, attr]) => {
return {
type,
value: attr.value.c,
};
});
})
.flat()
.concat(
aliasValues.map((value) => {
return {
type: "ALIAS",
value,
};
})
);
} }