Skip to content
Snippets Groups Projects
Commit 858e6640 authored by Alexander Rose's avatar Alexander Rose
Browse files

get enumeration values for ucode type fields from mmcif dic

parent a7119590
No related branches found
No related tags found
No related merge requests found
// import dic from './dic' // import dic from './dic'
import { Field, Category } from '../schema' import { Field/*, Category*/ } from '../schema'
import * as Data from '../data-model' import * as Data from '../data-model'
const pooledStr = Field.pooledStr() const pooledStr = Field.pooledStr()
...@@ -8,7 +8,7 @@ const str = Field.str() ...@@ -8,7 +8,7 @@ const str = Field.str()
const int = Field.int() const int = Field.int()
const float = Field.float() const float = Field.float()
function getFieldType (type: string) { export function getFieldType (type: string) {
switch (type) { switch (type) {
case 'code': case 'code':
case 'ucode': case 'ucode':
...@@ -71,28 +71,53 @@ interface SafeFrameData { ...@@ -71,28 +71,53 @@ interface SafeFrameData {
links: SafeFrameLinks links: SafeFrameLinks
} }
function getCode (d: Data.SafeFrame, ctx: SafeFrameData): string|undefined { // get field from given or linked category
function getField ( category: string, field: string, d: Data.SafeFrame, ctx: SafeFrameData): Data.Field|undefined {
const { categories, links } = ctx const { categories, links } = ctx
const item_type = d.categories['_item_type'] const cat = d.categories[category]
if (item_type) { if (cat) {
const code = item_type.getField('code') return cat.getField(field)
if (code) {
return code.str(0)
} else {
console.log(`item_type.code not found for '${d.header}'`)
}
} else { } else {
if (d.header in links) { if (d.header in links) {
return getCode(categories[links[d.header]], ctx) return getField(category, field, categories[links[d.header]], ctx)
} else { } else {
console.log(`no links found for '${d.header}'`) // console.log(`no links found for '${d.header}'`)
}
}
}
function getEnums (d: Data.SafeFrame, ctx: SafeFrameData): string[]|undefined {
const value = getField('_item_enumeration', 'value', d, ctx)
if (value) {
const enums: string[] = []
for (let i = 0; i < value.rowCount; ++i) {
enums.push(value.str(i))
// console.log(value.str(i))
} }
return enums
} else {
// console.log(`item_enumeration.value not found for '${d.header}'`)
}
}
function getCode (d: Data.SafeFrame, ctx: SafeFrameData): string|undefined {
const code = getField('_item_type', 'code', d, ctx)
if (code) {
let c = code.str(0)
if (c === 'ucode') {
const enums = getEnums(d, ctx)
if (enums) c += `: ${enums.join('|')}`
}
return c
} else {
console.log(`item_type.code not found for '${d.header}'`)
} }
} }
export function getSchema (dic: Data.Block) { // todo Block needs to be specialized with safe frames as well export function getSchema (dic: Data.Block) { // todo Block needs to be specialized with safe frames as well
const schema: { [category: string]: Category.Schema } = {} // const schema: { [category: string]: Category.Schema } = {}
const schema: { [category: string]: { [k: string]: string } } = {}
const categories: SafeFrameCategories = {} const categories: SafeFrameCategories = {}
const links: SafeFrameLinks = {} const links: SafeFrameLinks = {}
...@@ -116,8 +141,9 @@ export function getSchema (dic: Data.Block) { // todo Block needs to be special ...@@ -116,8 +141,9 @@ export function getSchema (dic: Data.Block) { // todo Block needs to be special
} }
}) })
Object.keys(categories).forEach(categoryName => { Object.keys(categories).forEach(fullName => {
const d = categories[categoryName] const d = categories[fullName]
const categoryName = d.header.substring(0, d.header.indexOf('.'))
const itemName = d.header.substring(d.header.indexOf('.') + 1) const itemName = d.header.substring(d.header.indexOf('.') + 1)
let fields let fields
if (categoryName in schema) { if (categoryName in schema) {
...@@ -129,7 +155,7 @@ export function getSchema (dic: Data.Block) { // todo Block needs to be special ...@@ -129,7 +155,7 @@ export function getSchema (dic: Data.Block) { // todo Block needs to be special
const code = getCode(d, { categories, links }) const code = getCode(d, { categories, links })
if (code) { if (code) {
fields[itemName] = getFieldType(code) fields[itemName] = code // getFieldType(code)
} else { } else {
console.log(`could not determine code for '${d.header}'`) console.log(`could not determine code for '${d.header}'`)
} }
...@@ -137,13 +163,3 @@ export function getSchema (dic: Data.Block) { // todo Block needs to be special ...@@ -137,13 +163,3 @@ export function getSchema (dic: Data.Block) { // todo Block needs to be special
return schema return schema
} }
// TODO
// support controlled vocabulary as a specialization string type field
// in the example below the string type would be `Y|N`
// _item_type.code ucode
// loop_
// _item_enumeration.value
// _item_enumeration.detail
// Y 'Yes'
// N 'No'
...@@ -151,8 +151,8 @@ async function runDic(input: string | Uint8Array) { ...@@ -151,8 +151,8 @@ async function runDic(input: string | Uint8Array) {
} }
const schema = getSchema(parsed.result.blocks[0]) const schema = getSchema(parsed.result.blocks[0])
// console.log(util.inspect(schema, {showHidden: false, depth: 1})) console.log(util.inspect(schema, {showHidden: false, depth: 3}))
console.log(util.inspect(Object.keys(schema).length, {showHidden: false, depth: 1})) // console.log(util.inspect(Object.keys(schema).length, {showHidden: false, depth: 1}))
} }
export function _dic() { export function _dic() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment