1- import { ContentModel , ModelEdge , ModelNode } from '@committed/graph'
1+ import { ContentModel , ModelEdge , ModelItem , ModelNode } from '@committed/graph'
22import {
33 DataFactory ,
44 Literal ,
@@ -69,26 +69,39 @@ export enum LiteralOption {
6969}
7070
7171export enum RdfFormat {
72- /** Suports a permissive superset of Turtle, TriG, N-Triple and N-Quad */
72+ /** Supports a permissive superset of Turtle, TriG, N-Triple and N-Quad */
7373 Turtle = 'turtle' ,
7474 /** Supports N3 format */
7575 N3 = 'N3' ,
7676}
7777
7878export interface RdfOptions {
79- usePrefix : boolean
79+ /** use the prefix in the node ids */
80+ usePrefixId : boolean
81+ /** Define how literals are converted to be used in the graph */
8082 literals : LiteralOption
83+ /** Declare the serialization format of the RDF */
8184 format : RdfFormat
85+ /** Transfer declarations from the http://ont.committed.io/graph/decorator namespace to decoration properties of the graph */
8286 decorate : boolean
87+ /** Declare the attribute to use as the label */
8388 label ?: string
89+ /** Declare the property to use as the node type */
8490 type ?: string
91+ /** Declare the base IRI of the graph */
8592 baseIRI ?: string
93+ /** Declare a blank node prefix to be used */
8694 blankNodePrefix ?: string
95+ /** Add additional prefixes to the used */
8796 additionalPrefixes ?: Record < string , string >
97+ /** Node processor to apply to nodes after conversion */
98+ nodeProcessor ?: ( node : ModelNode ) => ModelNode
99+ /** Edge processor to apply to edges after conversion */
100+ edgeProcessor ?: ( edge : ModelEdge ) => ModelEdge
88101}
89102
90103export const DEFAULT_RDF_OPTIONS : RdfOptions = {
91- usePrefix : false ,
104+ usePrefixId : false ,
92105 literals : LiteralOption . AS_OBJECT ,
93106 format : RdfFormat . Turtle ,
94107 label : rdfsLabel . value ,
@@ -97,7 +110,10 @@ export const DEFAULT_RDF_OPTIONS: RdfOptions = {
97110}
98111
99112interface GraphBuilderOptions
100- extends Pick < RdfOptions , 'usePrefix' | 'literals' | 'decorate' > {
113+ extends Pick <
114+ RdfOptions ,
115+ 'usePrefixId' | 'literals' | 'decorate' | 'nodeProcessor' | 'edgeProcessor'
116+ > {
101117 prefixes : Record < string , string >
102118 label ?: NamedNode
103119 type ?: NamedNode
@@ -120,9 +136,29 @@ class GraphBuilder {
120136 this . triples . forEach ( ( t ) => this . addTriple ( t ) )
121137 this . attributes . forEach ( ( t ) => this . addAttribute ( t ) )
122138
139+ this . processNodes ( )
140+ this . processEdges ( )
123141 return ContentModel . fromRaw ( { nodes : this . nodes , edges : this . edges } )
124142 }
125143
144+ private processNodes ( ) {
145+ const nodeProcessor = this . options . nodeProcessor
146+ if ( nodeProcessor !== undefined ) {
147+ Object . keys ( this . nodes ) . forEach ( ( key ) => {
148+ this . nodes [ key ] = nodeProcessor ( this . nodes [ key ] )
149+ } )
150+ }
151+ }
152+
153+ private processEdges ( ) {
154+ const edgeProcessor = this . options . edgeProcessor
155+ if ( edgeProcessor !== undefined ) {
156+ Object . keys ( this . edges ) . forEach ( ( key ) => {
157+ this . edges [ key ] = edgeProcessor ( this . edges [ key ] )
158+ } )
159+ }
160+ }
161+
126162 private addTriple ( t : Triple ) : void {
127163 if ( t . object . termType === 'Literal' ) {
128164 this . attributes . push ( t )
@@ -213,7 +249,7 @@ class GraphBuilder {
213249 }
214250
215251 toPrefixedId ( id : string ) : string {
216- if ( this . options . usePrefix ) {
252+ if ( this . options . usePrefixId ) {
217253 const match = Object . keys ( this . options . prefixes ) . find ( ( prefix ) =>
218254 id . startsWith ( prefix )
219255 )
@@ -300,7 +336,7 @@ export function buildGraph(
300336 )
301337
302338 const builderOptions = {
303- label : label !== undefined ? DataFactory . namedNode ( label ) : undefined ,
339+ label : typeof label === 'string' ? DataFactory . namedNode ( label ) : undefined ,
304340 type : type !== undefined ? DataFactory . namedNode ( type ) : undefined ,
305341 prefixes,
306342 ...rest ,
0 commit comments