Skip to content

Commit

Permalink
chore: use matchPatternMappings from rdf-term
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Sep 16, 2022
1 parent b0d0840 commit 87bdc1c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ IPremiseConclusionRule[] {
return allRules;
}

// TODO: Use an existing function from rdf-terms or move this logic into that package
// TODO: See why this needs to be different from matchPatternMappings in rdf-term package
// this may indicate a bug in other modules that use that export
/**
* Check if the base quad matches against all terms in the pattern.
*
Expand Down
26 changes: 2 additions & 24 deletions packages/bus-rdf-reason/lib/ActorRdfReasonMediated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Rule, IReasonStatus, IReasonGroup } from '@comunica/reasoning-type
import type { IActionContext } from '@comunica/types';
import type * as RDF from '@rdfjs/types';
import { wrap, type AsyncIterator } from 'asynciterator';
import { everyTerms } from 'rdf-terms';
import { matchPatternMappings } from 'rdf-terms';
import type { Algebra } from 'sparqlalgebrajs';
import type { IActionRdfReason, IActorRdfReasonOutput } from './ActorRdfReason';
import {
Expand Down Expand Up @@ -87,33 +87,11 @@ export abstract class ActorRdfReasonMediated extends ActorRdfReason {
return status.done;
}

// TODO: Import from rdf-terms.js once https://github.com/rubensworks/rdf-terms.js/pull/42 is merged
/* istanbul ignore next */
function matchBaseQuadPattern(__pattern: RDF.BaseQuad, quad: RDF.BaseQuad): boolean {
const mapping: Record<string, RDF.Term> = {};
function match(_pattern: RDF.BaseQuad, _quad: RDF.BaseQuad): boolean {
return everyTerms(_pattern, (term, key) => {
switch (term.termType) {
case 'Quad':
return _quad[key].termType === 'Quad' && match(term, <RDF.BaseQuad> _quad[key]);
case 'Variable':
// eslint-disable-next-line no-return-assign
return term.value in mapping ?
mapping[term.value].equals(_quad[key]) :
(mapping[term.value] = _quad[key]) && true;
default:
return term.equals(_quad[key]);
}
});
}
return match(__pattern, quad);
}

// If we have already done partial reasoning and are only interested in a certain
// pattern then maybe we can use that
if (status.type === 'partial' && pattern) {
for (const [ key, value ] of status.patterns) {
if (value.reasoned && matchBaseQuadPattern(key, pattern)) {
if (value.reasoned && matchPatternMappings(pattern, key)) {
return value.done;
}
}
Expand Down

0 comments on commit 87bdc1c

Please sign in to comment.