[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) {
resolving = true;
identify($attributes, $backlinks).then((inferredEntries) => {
inferredIds = inferredEntries.map((eid) => eid.value);
inferredIds = inferredEntries;
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 { listingAsOrdered } from "upend";
import LRU from "lru-cache";
import { attr } from "svelte/internal";
export function useEntity(
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(
attributes: OrderedListing,
backlinks: OrderedListing
): Promise<EntityIdentification[]> {
): Promise<string[]> {
// Get all entries where the object is linked
const hasEntries = backlinks
.filter(([_, entry]) => entry.attribute === "HAS")
@ -105,44 +101,9 @@ export async function identify(
return entry.value.c;
});
// Get all identities of the object
const isEntries = attributes
.filter(([_, entry]) => entry.attribute === "IS")
.map(([_, entry]) => entry.value.c);
// 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,
};
})
);
// Return all LBLs concatenated with named aliases
return attributes
.filter(([_, attr]) => attr.attribute === "LBL")
.map(([_, attr]) => attr.value.c)
.concat(aliasValues);
}