Skip to content

Commit

Permalink
Match resources with partial rdfsClasses declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Feb 17, 2025
1 parent 51304d3 commit 57ff3d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- `SolidTypeRegistration.forClass` is now declared as an array.
- Default `rdfContext` resolution changed to prioritize the vocab used in `rdfsClass` if present.
- Matching `rdfsClasses` is not exhaustive anymore. In previous versions, models with multiple classes defined would only be found when all classes were present.

Also, check [soukai's release notes](https://github.com/NoelDeMartin/soukai/blob/main/CHANGELOG.md) for further changes.

Expand Down
23 changes: 22 additions & 1 deletion src/models/SolidModel.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-len */
import { after, arrayWithout, range, stringToSlug, tap, toString, tt, urlParentDirectory, urlResolve, urlResolveDirectory, urlRoute, uuid } from '@noeldemartin/utils';
import { expandIRI as defaultExpandIRI } from '@noeldemartin/solid-utils';
import { expandIRI as defaultExpandIRI, turtleToQuadsSync } from '@noeldemartin/solid-utils';
import { fakeContainerUrl, fakeDocumentUrl, fakeResourceUrl } from '@noeldemartin/testing';
import { FieldType, InMemoryEngine, ModelKey, TimestampField, bootModels, setEngine } from 'soukai';
import dayjs from 'dayjs';
Expand Down Expand Up @@ -442,6 +442,27 @@ describe('SolidModel', () => {
expect(actions[0].object).toEqual(movie.url);
});

it('finds resource ids with partial class matches', () => {
// Arrange
class StubModel extends SolidModel {

public static rdfsClasses = ['https://schema.org/Action', 'http://www.w3.org/2002/12/cal/ical#Vtodo'];

}
const documentUrl = fakeDocumentUrl();
const quads = turtleToQuadsSync('<#it> a <https://schema.org/Action> .', {
baseIRI: documentUrl,
});

bootModels({ StubModel });

// Act
const ids = StubModel.findMatchingResourceIds(quads);

// Assert
expect(ids).toEqual([`${documentUrl}#it`]);
});

it('sends JSON-LD with related model updates using parent engine', async () => {
// Arrange
const containerUrl = urlResolveDirectory(faker.internet.url());
Expand Down
6 changes: 3 additions & 3 deletions src/models/SolidModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export class SolidModel extends SolidModelBase {

return Object
.entries(resourcesTypes)
.filter(([_, types]) => !this.rdfsClasses.some(rdfsClass => !types.includes(rdfsClass)))
.filter(([_, types]) => types.some(type => this.rdfsClasses.includes(type)))
.map(([resourceId]) => baseUrl ? urlResolve(baseUrl, resourceId) : resourceId);
}

Expand Down Expand Up @@ -1347,7 +1347,7 @@ export class SolidModel extends SolidModelBase {
}

protected async createManyFromEngineDocuments(documents: Record<string, EngineDocument>): Promise<this[]> {
const rdfsClasses = [this.static('rdfsClasses'), ...this.static('rdfsClassesAliases')];
const rdfsClasses = arrayUnique([this.static('rdfsClasses'), ...this.static('rdfsClassesAliases')].flat());
const models = await Promise.all(Object.entries(documents).map(async ([documentUrl, engineDocument]) => {
const rdfDocument = await RDFDocument.fromJsonLD(engineDocument);

Expand All @@ -1357,7 +1357,7 @@ export class SolidModel extends SolidModelBase {
.filter(
(resource): resource is RDFResource & { url: string } =>
!!resource.url &&
rdfsClasses.some(types => !types.some(type => !resource.isType(type))),
rdfsClasses.some(type => resource.isType(type)),
)
.map(async resource => this.createFromEngineDocument(
documentUrl,
Expand Down

0 comments on commit 57ff3d0

Please sign in to comment.