Skip to content

Commit

Permalink
Add datafactory type to formula linkeddata#355
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Oct 23, 2019
1 parent 389e1ae commit 9b484d8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 58 deletions.
26 changes: 14 additions & 12 deletions src/formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import Namespace from './namespace'
import Node from './node-internal'
import Serializer from './serialize'
import Statement from './statement'
import { appliedFactoryMethods, arrayToStatements, isStatement } from './util'
import { appliedFactoryMethods, arrayToStatements, isStatement } from './utils'
import {
DataFactory,
GraphType,
ObjectType,
PredicateType,
SubjectType,
TFTerm,
ValueType,
TFPredicateType,
TFDataFactory,
} from './types'
import Variable from './variable'
import Literal from './literal'
Expand All @@ -26,7 +26,7 @@ export function isFormula<T>(value: T | TFTerm): value is Formula {
}

interface FormulaOpts {
rdfFactory?: DataFactory
rdfFactory?: TFDataFactory
}

/**
Expand Down Expand Up @@ -56,7 +56,7 @@ export default class Formula extends Node {

optional: ReadonlyArray<any>

rdfFactory: DataFactory
rdfFactory: TFDataFactory | any

/**
* Initializes this formula
Expand All @@ -83,6 +83,7 @@ export default class Formula extends Node {
this.initBindings = initBindings || []
this.optional = optional || []

// TODO: Make CanonicalDataFactory comply with
this.rdfFactory = (opts && opts.rdfFactory) || CanonicalDataFactory
// Enable default factory methods on this while preserving factory context.
for(const factoryMethod of appliedFactoryMethods) {
Expand All @@ -104,11 +105,12 @@ export default class Formula extends Node {
*/
add (
subject: SubjectType,
predicate: PredicateType,
predicate: TFPredicateType,
object: ObjectType,
graph?: GraphType
) {
return (this.statements as Statement[]).push(this.rdfFactory.quad(subject, predicate, object, graph))
return (this.statements as Statement[])
.push(this.rdfFactory.quad(subject, predicate, object, graph))
}

/** Add a statment object
Expand Down Expand Up @@ -387,7 +389,7 @@ export default class Formula extends Node {
var len4: number
var m: number
var members
var pred
var pred: TFTerm
var q: number
var ref
var ref1: Statement[]
Expand Down Expand Up @@ -448,7 +450,7 @@ export default class Formula extends Node {
* @param subject A named node
*/
findMemberURIs(
subject: TF
subject: Node
): {
[uri: string]: Statement;
} {
Expand Down Expand Up @@ -505,7 +507,7 @@ export default class Formula extends Node {
* We use NT representations in this version because they handle blank nodes.
*/
findTypesNT(
subject: Node
subject: TFTerm
): {
[uri: string]: boolean;
} {
Expand Down Expand Up @@ -564,7 +566,7 @@ export default class Formula extends Node {
* @param subject A subject node
*/
findTypeURIs(
subject: Node
subject: TFTerm
): {
[uri: string]: boolean;
} {
Expand Down Expand Up @@ -621,7 +623,7 @@ export default class Formula extends Node {
/**
* Creates a new empty formula - features not applicable, but necessary for typing to pass
*/
formula(features?: ReadonlyArray<string>): Formula {
formula(_features?: ReadonlyArray<string>): Formula {
return new Formula()
}

Expand Down
6 changes: 4 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function parse (
str: string,
kb: Formula,
base: string,
contentType?: string,
contentType: string,
callback: (error: any, kb: Formula | null) => void
) {
contentType = contentType || 'text/turtle'
Expand Down Expand Up @@ -67,6 +67,8 @@ export default function parse (
contentType === 'application/n-quads') {
var n3Parser = new N3jsParser({ factory: DataFactory })
nquadCallback(null, str)
} else if (contentType === undefined) {
throw new Error("contentType is undefined")
} else {
throw new Error("Don't know how to parse " + contentType + ' yet')
}
Expand Down Expand Up @@ -105,7 +107,7 @@ export default function parse (
} else {
let e2 = new Error('' + e + ' while trying to parse <' + base + '> as ' + contentType)
e2.cause = e
throw e2.
throw e2
}
}
}
Expand Down
80 changes: 36 additions & 44 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BlankNode from './blank-node';
import Collection from './collection';
import Literal from './literal';
import NamedNode from './named-node';
import { TFNamedNode } from './types';

/**
* Types that support both Enums (for typescript) and regular strings
Expand Down Expand Up @@ -125,6 +126,40 @@ export interface TFDefaultGraph extends TFTerm {
/** Any TFTerm that is suitable for the Object value */
export type TFObject = TFNamedNode | TFBlankNode | TFLiteral

/**
* RDF.js taskforce DataFactory
* @link https://rdf.js.org/data-model-spec/#datafactory-interface
*/
export interface TFDataFactory {
/** Returns a new instance of NamedNode. */
namedNode: (value: string) => TFNamedNode,
/**
* Returns a new instance of BlankNode.
* If the value parameter is undefined a new identifier for the
* blank node is generated for each call.
*/
blankNode: (value?: string) => TFBlankNode,
/**
* Returns a new instance of Literal.
* If languageOrDatatype is a NamedNode, then it is used for the value of datatype.
* Otherwise languageOrDatatype is used for the value of language. */
literal: (value: string, languageOrDatatype: string | TFNamedNode) => TFLiteral,
/** Returns a new instance of Variable. This method is optional. */
variable?: (value: string) => TFVariable,
/** Returns an instance of DefaultGraph. */
defaultGraph: () => TFDefaultGraph,
/**
* Returns a new instance of the specific Term subclass given by original.termType
* (e.g., NamedNode, BlankNode, Literal, etc.),
* such that newObject.equals(original) returns true.
*/
fromTerm: (original: TFTerm) => TFTerm
/**
* Returns a new instance of Quad, such that newObject.equals(original) returns true.
*/
fromQuad: (original: TFQuad) => TFQuad
}

/**
* A type for values that serves as inputs
*/
Expand All @@ -136,6 +171,7 @@ export interface Bindings {

export type TFSomeNode = TFBlankNode | TFNamedNode
export type SubjectType = TFBlankNode | NamedNode | TFNamedNode | Variable
export type TFPredicateType = TFNamedNode
export type PredicateType = TFNamedNode | NamedNode | Variable
export type ObjectType = TFObject | NamedNode | Literal | Collection | BlankNode | Variable
export type GraphType = TFDefaultGraph | TFNamedNode | NamedNode | Variable
Expand Down Expand Up @@ -163,47 +199,3 @@ export enum Feature {
}

export type SupportTable = Record<Feature, boolean>

/**
* Defines a strict subset of the DataFactory as defined in the RDF/JS: Data model specification
*
* Non RDF-native features have been removed (e.g. no Variable, no Literal as predicate, etc.).
*
* bnIndex is optional but useful.
*/
export interface DataFactory {
bnIndex?: number

supports: SupportTable

namedNode(value: string): TFNamedNode

blankNode(value?: string): TFBlankNode

literal(value: string, languageOrDatatype?: string | TFNamedNode): TFLiteral

literal(value: unknown): TFLiteral

defaultGraph(): TFNamedNode

quad(
subject: TFNamedNode | TFBlankNode | TFVariable,
predicate: TFNamedNode,
object: TFTerm,
graph?: TFNamedNode
): TFQuad

isQuad(obj: any): obj is TFQuad

fromTerm(original: Literal | TFTerm): TFTerm

fromQuad(original: TFQuad): TFQuad

equals(a: Comparable, b: Comparable): boolean

id(obj: TFObject | TFQuad): Indexable | unknown

find?(id: Indexable | unknown): TFTerm

toNQ(term: TFTerm | TFQuad): string
}

0 comments on commit 9b484d8

Please sign in to comment.