/**
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import { Log } from '../logging/index.js' ; import { ObjectPredicate , Topic , TypedTopic } from '../triples/triple.js' ; import { UrlNode } from '../triples/types.js' ; import { IsNamedClass , IsDataType , ClassIsDataType , } from '../triples/wellKnown.js' ; import { AliasBuiltin , Class , ClassMap , DataTypeUnion , RoleBuiltin , } from '../ts/class.js' ; import { assert } from '../util/assert.js' ; function toClass ( cls , topic , map ) { const rest = [ ] ; for ( const value of topic . values ) { const added = cls . add ( value , map ) ; if ( ! added ) rest . push ( value ) ; } if ( rest . length > 0 ) { Log ( `Class ${ cls . subject . name }: Did not add [${ rest . map ( r => `(${ r . Predicate . name } ${ r . Object . toString ( ) })` ) . join ( ',' ) }]` ) ; } return cls ; } const wellKnownTypes = [ new AliasBuiltin ( 'http://schema.org/Text' , AliasBuiltin . Alias ( 'string' ) ) , new AliasBuiltin ( 'http://schema.org/Number' , AliasBuiltin . Alias ( 'number' ) , AliasBuiltin . NumberStringLiteral ( ) ) , new AliasBuiltin ( 'http://schema.org/Time' , AliasBuiltin . Alias ( 'string' ) ) , new AliasBuiltin ( 'http://schema.org/Date' , AliasBuiltin . Alias ( 'string' ) ) , new AliasBuiltin ( 'http://schema.org/DateTime' , AliasBuiltin . Alias ( 'string' ) ) , new AliasBuiltin ( 'http://schema.org/Boolean' , AliasBuiltin . Alias ( 'boolean' ) ) , new RoleBuiltin ( UrlNode . Parse ( 'http://schema.org/Role' ) ) , new RoleBuiltin ( UrlNode . Parse ( 'http://schema.org/OrganizationRole' ) ) , new RoleBuiltin ( UrlNode . Parse ( 'http://schema.org/EmployeeRole' ) ) , new RoleBuiltin ( UrlNode . Parse ( 'http://schema.org/LinkRole' ) ) , new RoleBuiltin ( UrlNode . Parse ( 'http://schema.org/PerformanceRole' ) ) , ] ; const wellKnownStrings = [ UrlNode . Parse ( 'http://schema.org/Quantity' ) , UrlNode . Parse ( 'http://schema.org/EntryPoint' ) , UrlNode . Parse ( 'http://schema.org/Organization' ) , UrlNode . Parse ( 'http://schema.org/Person' ) , UrlNode . Parse ( 'http://schema.org/Place' ) , UrlNode . Parse ( 'https://schema.org/Quantity' ) , UrlNode . Parse ( 'https://schema.org/EntryPoint' ) , UrlNode . Parse ( 'https://schema.org/Organization' ) , UrlNode . Parse ( 'https://schema.org/Person' ) , UrlNode . Parse ( 'https://schema.org/Place' ) , ] ; function ForwardDeclareClasses ( topics ) { const classes = new Map < string , Class > ( ) ; const dataType = new DataTypeUnion ( 'http://schema.org/DataType' , [ ] ) ; for ( const topic of topics ) { if ( IsDataType ( topic . Subject ) ) { classes . set ( topic . Subject . toString ( ) , dataType ) ; continue ; } else if ( ! IsNamedClass ( topic ) ) continue ; const wk = wellKnownTypes . find ( wk => wk . subject . equivTo ( topic . Subject ) ) ; if ( ClassIsDataType ( topic ) ) { assert ( wk , `${ topic . Subject . toString ( ) } must have corresponding well-known type.` ) ; dataType . wk . push ( wk ) ; wk [ '_isDataType' ] = true ; } const cls = wk || new Class ( topic . Subject ) ; const allowString = wellKnownStrings . some ( wks => wks . equivTo ( topic . Subject ) ) ; if ( allowString ) cls . addTypedef ( AliasBuiltin . Alias ( 'string' ) ) ; classes . set ( topic . Subject . toString ( ) , cls ) ; } return classes ; } function BuildClasses ( topics , classes ) { for ( const topic of topics ) { if ( ! IsNamedClass ( topic ) ) continue ; const cls = classes . get ( topic . Subject . toString ( ) ) ; assert ( cls ) ; toClass ( cls , topic , classes ) ; } } param topics returns export function ProcessClasses ( topics ) { const classes = ForwardDeclareClasses ( topics ) ; BuildClasses ( topics , classes ) ; return classes ; }