Skip to content

Instantly share code, notes, and snippets.

@cullophid
Last active September 16, 2019 20:16
Show Gist options
  • Save cullophid/bba2ee38e72ba768d6084daaf520b605 to your computer and use it in GitHub Desktop.
Save cullophid/bba2ee38e72ba768d6084daaf520b605 to your computer and use it in GitHub Desktop.
Feature Type resolution
const resolveFeatureTypes = (userSelection, data) => {
const resolved = {}
const visited = {}
const resolveFeatureType = (id) => {
// If we have already resolved this feature type we return the result
if (resolved[id]) {
return resolved[id]
}
// If we have visited the feature types, but not resolved it we have ciscular dependencies
if (visited[id]) {
throw new Error("Circular dependencies")
}
visited[id] = true
const ft = data.d.ft.find(ft => ft.id === id); // find the feature type
// if the feature does not exist we cannot resolve feature types.
if (!ft) {
throw new Error("No Feature type Data for feature with id:", id)
}
for (const feature of ft.f) {
// if the feature type is part of the user selection, ignore non selected features
if (userSelection[ft.t] && userSelection[ft.t] !== feature.c) {
continue;
}
// Sort the variations based on number of dependencies
const variations = feature.va.sort((a, b) => b.d.length - a.d.length)
for (const variation of variations) {
// recursively call resolveFeatureType to find all dependencies
const isMatch = variation.d.every(dependency => resolveFeatureType(dependency.ft).feature === dependency.f)
if (isMatch) {
resolved[id] = { feature: feature.id, content: variation.k }
return resolved[id]
}
}
}
throw new Error("Could not Resolve feature type")
}
// loop through each feature type to find the resolved values.
data.d.ft.forEach(ft => resolveFeatureType(ft.id));
return resolved;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment