Skip to content

Commit

Permalink
fix: class constraint in node shape
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Aug 17, 2023
1 parent 054516c commit d1a8688
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-peaches-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/shape-to-query": patch
---

When used in node shape, class constraint would produce an incorrect pattern
5 changes: 5 additions & 0 deletions .changeset/red-plums-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/shape-to-query": minor
---

Change the abstract methods of constraint components to separate property shape and node shape constraints
7 changes: 7 additions & 0 deletions .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
'watch-files': [
'./**/*.ts'
],
require: require.resolve('./mocha-setup.cjs'),
loader: 'ts-node/esm'
}
9 changes: 0 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@
"workspaces": [
"packages/*"
],
"mocha": {
"watch-files": [
"./**/*.ts"
],
"require": [
"mocha-setup.cjs"
],
"loader": "ts-node/esm"
},
"lint-staged": {
"*.{js,ts}": [
"eslint --fix --quiet"
Expand Down
2 changes: 0 additions & 2 deletions packages/demo/public/docs/example/in.ttl.rq
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ WHERE {
{ ?resource1 schema:familyName ?resource3. }
UNION
{ ?resource1 schema:knows ?resource4. }
?resource1 schema:knows ?resource4.
FILTER(?resource4 IN(<https://tbbt.tv/sheldon-cooper>, <https://tbbt.tv/stuart-bloom>))
}
14 changes: 13 additions & 1 deletion packages/shape-to-query/model/constraint/ConstraintComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ export abstract class ConstraintComponent {
protected constructor(public readonly type: NamedNode) {
}

abstract buildPatterns(arg: Parameters): string | SparqlTemplateResult | SparqlTemplateResult[]
buildPatterns(arg: Parameters): string | SparqlTemplateResult | SparqlTemplateResult[] {
if (arg.propertyPath) {
return this.buildPropertyShapePatterns(arg)
}

return this.buildNodeShapePatterns(arg)
}

buildNodeShapePatterns(arg: Parameters): string | SparqlTemplateResult | SparqlTemplateResult[] {
return this.buildPropertyShapePatterns(arg)
}

abstract buildPropertyShapePatterns(arg: Parameters): string | SparqlTemplateResult | SparqlTemplateResult[]
}

export function assertList(arg: ListOrPointer): asserts arg is List {
Expand Down
2 changes: 1 addition & 1 deletion packages/shape-to-query/model/constraint/and.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class AndConstraintComponent extends ConstraintComponent {
}
}

buildPatterns(arg: Parameters): SparqlTemplateResult {
buildPropertyShapePatterns(arg: Parameters): SparqlTemplateResult {
return sparql`${this.inner.map(inner => inner.buildConstraints(arg))}`
}
}
6 changes: 5 additions & 1 deletion packages/shape-to-query/model/constraint/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export class ClassConstraintComponent extends ConstraintComponent {
}
}

buildPatterns({ valueNode }: Parameters) {
buildPropertyShapePatterns({ valueNode }: Parameters) {
return sparql`${valueNode} a ${this.clas} .`
}

buildNodeShapePatterns({ focusNode }: Parameters) {
return sparql`${focusNode} a ${this.clas} .`
}
}
2 changes: 1 addition & 1 deletion packages/shape-to-query/model/constraint/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ExpressionConstraintComponent extends ConstraintComponent {
super(sh.ExpressionConstraintComponent)
}

buildPatterns({ focusNode: subject, valueNode: object, rootPatterns, variable }: Parameters) {
buildPropertyShapePatterns({ focusNode: subject, valueNode: object, rootPatterns, variable }: Parameters) {
let patterns: Select | SparqlTemplateResult
let filter: SparqlValue

Expand Down
8 changes: 4 additions & 4 deletions packages/shape-to-query/model/constraint/hasValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export class HasValueConstraintComponent extends ConstraintComponent {
}
}

buildPatterns({ focusNode, propertyPath, valueNode }: Omit<Parameters, 'rootPatterns'>): string | SparqlTemplateResult {
if (!propertyPath) {
return ''
}
buildNodeShapePatterns(): string | SparqlTemplateResult | SparqlTemplateResult[] {
return ''
}

buildPropertyShapePatterns({ focusNode, propertyPath, valueNode }: Omit<Parameters, 'rootPatterns'>): string | SparqlTemplateResult {
if (this.terms.length === 1) {
return sparql`FILTER( ${valueNode} = ${this.terms[0]} )`
}
Expand Down
6 changes: 5 additions & 1 deletion packages/shape-to-query/model/constraint/in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export class InConstraintComponent extends ConstraintComponent {
super(sh.InConstraintComponent)
}

buildPatterns({ valueNode, propertyPath }: Omit<Parameters, 'rootPatterns'>): string | SparqlTemplateResult {
buildPropertyShapePatterns(): string | SparqlTemplateResult | SparqlTemplateResult[] {
return ''
}

buildNodeShapePatterns({ valueNode, propertyPath }: Omit<Parameters, 'rootPatterns'>): string | SparqlTemplateResult {
if (!propertyPath) {
return ''
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shape-to-query/model/constraint/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class NodeConstraintComponent extends ConstraintComponent {
}
}

buildPatterns({ valueNode, variable, ...arg }: Parameters) {
buildPropertyShapePatterns({ valueNode, variable, ...arg }: Parameters) {
return this.shape.buildConstraints({
...arg,
variable,
Expand Down
2 changes: 1 addition & 1 deletion packages/shape-to-query/model/constraint/nodeKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class NodeKindConstraintComponent extends ConstraintComponent {
}
}

buildPatterns({ valueNode }: Pick<Parameters, 'valueNode'>) {
buildPropertyShapePatterns({ valueNode }: Pick<Parameters, 'valueNode'>) {
return sparql`FILTER( ${this.__createFilterExpression(valueNode)} )`
}
}
2 changes: 1 addition & 1 deletion packages/shape-to-query/model/constraint/or.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class OrConstraintComponent extends ConstraintComponent {
}
}

buildPatterns(arg: Parameters): SparqlTemplateResult {
buildPropertyShapePatterns(arg: Parameters): SparqlTemplateResult {
const propExpr = arg.propertyPath ? sparql`${arg.focusNode} ${arg.propertyPath} ${arg.valueNode} .` : ''

const inner = this.inner
Expand Down
2 changes: 1 addition & 1 deletion packages/shape-to-query/model/constraint/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class PatternConstraintComponent extends ConstraintComponent {
super(sh.PatternConstraintComponent)
}

buildPatterns({ valueNode }: Parameters) {
buildPropertyShapePatterns({ valueNode }: Parameters) {
const flags = this.flags ? `, "${this.flags}"` : ''

return sparql`FILTER(REGEX(${valueNode}, "${this.pattern}"${flags}))`
Expand Down
2 changes: 1 addition & 1 deletion packages/shape-to-query/model/constraint/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class PropertyConstraintComponent extends ConstraintComponent {
}
}

buildPatterns(arg: Parameters) {
buildPropertyShapePatterns(arg: Parameters) {
return this.shape.buildConstraints(arg)
}
}
3 changes: 3 additions & 0 deletions packages/shape-to-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@
"string-to-stream": "^3.0.1",
"tbbt-ld": "^1.1.0",
"wait-on": "^7.0.1"
},
"mocha": {
"config": "../../.mocharc.cjs"
}
}
49 changes: 49 additions & 0 deletions packages/shape-to-query/test/model/constraint/class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { schema } from '@tpluscode/rdf-ns-builders'
import { expect } from 'chai'
import $rdf from 'rdf-ext'
import { sparql } from '@tpluscode/rdf-string'
import { variable } from '../../variable.js'
import { ClassConstraintComponent } from '../../../model/constraint/class.js'

describe('model/constraint/class', () => {
before(() => import('../../sparql.js'))

describe('buildPatterns', () => {
context('node shape', () => {
it('creates correct pattern', () => {
// given
const constraint = new ClassConstraintComponent(schema.DefinedTerm)

// when
const whereClause = constraint.buildPatterns({
focusNode: $rdf.namedNode('this'),
valueNode: variable(),
variable,
rootPatterns: undefined,
})

// then
expect(whereClause).to.equalPatterns('<this> a schema:DefinedTerm .')
})
})

context('property shape', () => {
it('creates correct pattern', () => {
// given
const constraint = new ClassConstraintComponent(schema.DefinedTerm)

// when
const whereClause = constraint.buildPatterns({
focusNode: $rdf.namedNode('this'),
valueNode: variable(),
variable,
rootPatterns: undefined,
propertyPath: sparql`prop`,
})

// then
expect(whereClause).to.equalPatterns('?valueNode a schema:DefinedTerm .')
})
})
})
})

0 comments on commit d1a8688

Please sign in to comment.