Skip to content

Commit

Permalink
Remove try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
surilindur committed Jan 9, 2024
1 parent 6a082ae commit 2aeed01
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 155 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ docs
*.js
*.js.map
*.d.ts
*.html
*.png

componentsjs-error-state.json

test*

catalogue*
client*
server*
queries*
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"componentsjs-generator": "^3.0.0",
"eslint": "8.0.0",
"lerna": "^7.0.0",
"rdf-js": "^4.0.0",
"typescript": "^5.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,41 @@ export class ActorRdfMetadataAccumulateVoIDDescription extends ActorRdfMetadataA
}

if (action.accumulatedMetadata.voidDescriptions || action.appendingMetadata.voidDescriptions) {
try {
let cardinality: QueryResultCardinality | undefined;

const descriptions: IVoIDDescription[] = [
...action.accumulatedMetadata.voidDescriptions ? action.accumulatedMetadata.voidDescriptions : [],
...action.appendingMetadata.voidDescriptions ? action.appendingMetadata.voidDescriptions : [],
];

const sources = this.getContextSourceUrls(action.context);

if (sources) {
let bestEstimate = 0;
let bestDataset: string | undefined;
const pattern: Algebra.Pattern = action.context.getSafe(KeysQueryOperation.operation);

for (const source of sources) {
const matchingDescription = this.getDescriptionWithLongestMatch(source, descriptions);
if (matchingDescription) {
const estimatedCardinality = this.triplePatternCardinalityEstimator
.estimate(matchingDescription, pattern);
if (estimatedCardinality && (!bestDataset || matchingDescription.dataset.length > bestDataset.length)) {
bestEstimate = estimatedCardinality;
bestDataset = matchingDescription.dataset;
}
let cardinality: QueryResultCardinality | undefined;

const descriptions: IVoIDDescription[] = [
...action.accumulatedMetadata.voidDescriptions ? action.accumulatedMetadata.voidDescriptions : [],
...action.appendingMetadata.voidDescriptions ? action.appendingMetadata.voidDescriptions : [],
];

const sources = this.getContextSourceUrls(action.context);

if (sources) {
let bestEstimate = 0;
let bestDataset: string | undefined;
const pattern: Algebra.Pattern = action.context.getSafe(KeysQueryOperation.operation);

for (const source of sources) {
const matchingDescription = this.getDescriptionWithLongestMatch(source, descriptions);
if (matchingDescription) {
const estimatedCardinality = this.triplePatternCardinalityEstimator
.estimate(matchingDescription, pattern);
if (estimatedCardinality && (!bestDataset || matchingDescription.dataset.length > bestDataset.length)) {
bestEstimate = estimatedCardinality;
bestDataset = matchingDescription.dataset;
}
}

if (bestDataset) {
cardinality = { type: 'estimate', value: bestEstimate, dataset: bestDataset };
}
}

return { metadata: {
voidDescriptions: descriptions,
...cardinality ? { cardinality } : {},
}};
} catch (error: unknown) {
// eslint-disable-next-line no-console
console.log(`Error occurred in <${this.name}>`, error);
if (bestDataset) {
cardinality = { type: 'estimate', value: bestEstimate, dataset: bestDataset };
}
}

return { metadata: {
voidDescriptions: descriptions,
...cardinality ? { cardinality } : {},
}};
}

return { metadata: {}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ export abstract class TriplePatternCardinalityEstimator implements ITriplePatter
*/
export class TriplePatternCardinalityEstimatorVoIDDescriptionPredicateCount extends TriplePatternCardinalityEstimator {
public estimate(description: IVoIDDescription, pattern: Algebra.Pattern): number | undefined {
try {
if (pattern.predicate.termType === 'NamedNode') {
return description.propertyPartitions.get(pattern.predicate.value)?.triples;
}
} catch (error: unknown) {
// eslint-disable-next-line no-console
console.log('Error in TriplePatternCardinalityEstimatorVoIDDescriptionPredicateCount', error);
if (pattern.predicate.termType === 'NamedNode') {
return description.propertyPartitions.get(pattern.predicate.value)?.triples;
}
}
}
Expand All @@ -37,79 +32,74 @@ export class TriplePatternCardinalityEstimatorVoIDDescriptionPredicateCount exte
*/
export class TriplePatternCardinalityEstimatorVoIDDescription extends TriplePatternCardinalityEstimator {
public estimate(description: IVoIDDescription, pattern: Algebra.Pattern): number | undefined {
try {
if (pattern.predicate.termType === 'NamedNode') {
const pp = description.propertyPartitions.get(pattern.predicate.value);
if (pp !== undefined) {
if (pattern.subject.termType === 'Variable') {
if (pattern.object.termType === 'Variable') {
// ?s <p> ?o
// ?s rdf:type ?o
return pp.triples;
}
if (pattern.object.termType === 'NamedNode') {
if (pattern.predicate.value === RDF_TYPE) {
// ?s rdf:type <o>
const cp = description.classPartitions.get(pattern.object.value);
return cp?.entities;
}
// ?s <p> <o>
return pp.triples !== undefined && pp.distinctObjects ?
pp.triples / pp.distinctObjects :
undefined;
}
} else if (pattern.subject.termType === 'NamedNode') {
if (pattern.object.termType === 'Variable') {
// <s> <p> ?o
// <s> rdf:type ?o
return pp.triples !== undefined && pp.distinctSubjects ?
pp.triples / pp.distinctSubjects :
undefined;
}
if (pattern.object.termType === 'NamedNode') {
// <s> rdf:type <o>
if (pattern.predicate.value === RDF_TYPE && !description.classPartitions.get(pattern.object.value)) {
// The paper says that for rdf:type here,
// if there is no class partition for <o> then 0 should be returned
return 0;
}
// <s> <p> <o>
return pp.triples !== undefined && pp.distinctSubjects && pp.distinctObjects ?
pp.triples / (pp.distinctSubjects * pp.distinctObjects) :
undefined;
}
}
}
} else if (pattern.predicate.termType === 'Variable') {
if (pattern.predicate.termType === 'NamedNode') {
const pp = description.propertyPartitions.get(pattern.predicate.value);
if (pp !== undefined) {
if (pattern.subject.termType === 'Variable') {
if (pattern.object.termType === 'Variable') {
// ?s ?p ?o
return description.triples;
// ?s <p> ?o
// ?s rdf:type ?o
return pp.triples;
}
if (pattern.object.termType === 'NamedNode') {
// ?s ?p <o>
return description.triples !== undefined && description.distinctObjects ?
description.triples / description.distinctObjects :
if (pattern.predicate.value === RDF_TYPE) {
// ?s rdf:type <o>
const cp = description.classPartitions.get(pattern.object.value);
return cp?.entities;
}
// ?s <p> <o>
return pp.triples !== undefined && pp.distinctObjects ?
pp.triples / pp.distinctObjects :
undefined;
}
} else if (pattern.subject.termType === 'NamedNode') {
if (pattern.object.termType === 'Variable') {
// <s> ?p ?o
return description.triples !== undefined && description.distinctSubjects ?
description.triples / description.distinctSubjects :
// <s> <p> ?o
// <s> rdf:type ?o
return pp.triples !== undefined && pp.distinctSubjects ?
pp.triples / pp.distinctSubjects :
undefined;
}
if (pattern.object.termType === 'NamedNode') {
// <s> ?p <o>
return description.triples !== undefined && description.distinctSubjects && description.distinctObjects ?
description.triples / (description.distinctSubjects * description.distinctObjects) :
// <s> rdf:type <o>
if (pattern.predicate.value === RDF_TYPE && !description.classPartitions.get(pattern.object.value)) {
// The paper says that for rdf:type here,
// if there is no class partition for <o> then 0 should be returned
return 0;
}
// <s> <p> <o>
return pp.triples !== undefined && pp.distinctSubjects && pp.distinctObjects ?
pp.triples / (pp.distinctSubjects * pp.distinctObjects) :
undefined;
}
}
}
} catch (error: unknown) {
// eslint-disable-next-line no-console
console.log('Error in TriplePatternCardinalityEstimatorVoIDDescription', error);
} else if (pattern.predicate.termType === 'Variable') {
if (pattern.subject.termType === 'Variable') {
if (pattern.object.termType === 'Variable') {
// ?s ?p ?o
return description.triples;
}
if (pattern.object.termType === 'NamedNode') {
// ?s ?p <o>
return description.triples !== undefined && description.distinctObjects ?
description.triples / description.distinctObjects :
undefined;
}
} else if (pattern.subject.termType === 'NamedNode') {
if (pattern.object.termType === 'Variable') {
// <s> ?p ?o
return description.triples !== undefined && description.distinctSubjects ?
description.triples / description.distinctSubjects :
undefined;
}
if (pattern.object.termType === 'NamedNode') {
// <s> ?p <o>
return description.triples !== undefined && description.distinctSubjects && description.distinctObjects ?
description.triples / (description.distinctSubjects * description.distinctObjects) :
undefined;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,19 @@ export class ActorRdfMetadataExtractVoIDDescription extends ActorRdfMetadataExtr

public async run(action: IActionRdfMetadataExtract): Promise<IActorRdfMetadataExtractOutput> {
const descriptions: IVoIDDescription[] = [];

try {
const preliminaryDescriptions = await this.extractDescriptions(action.metadata);

for (const description of preliminaryDescriptions) {
if (this.descriptionIsComplete(description)) {
descriptions.push(description);
} else if (this.shouldCompletePartialDescriptions) {
const data = await this.mediatorDereferenceRdf.mediate({ url: description.dataset, context: action.context });
const newDescriptions = await this.extractDescriptions(data.data);
const filledDescription = newDescriptions.find(vd => vd.dataset === description.dataset);
if (filledDescription && filledDescription.propertyPartitions.size > 0) {
descriptions.push(filledDescription);
}
const preliminaryDescriptions = await this.extractDescriptions(action.metadata);
for (const description of preliminaryDescriptions) {
if (this.descriptionIsComplete(description)) {
descriptions.push(description);
} else if (this.shouldCompletePartialDescriptions) {
const data = await this.mediatorDereferenceRdf.mediate({ url: description.dataset, context: action.context });
const newDescriptions = await this.extractDescriptions(data.data);
const filledDescription = newDescriptions.find(vd => vd.dataset === description.dataset);
if (filledDescription && filledDescription.propertyPartitions.size > 0) {
descriptions.push(filledDescription);
}
}
} catch (error: unknown) {
// eslint-disable-next-line no-console
console.log(`Error occurred in <${this.name}>`, error);
}

return { metadata: descriptions.length > 0 ? { voidDescriptions: descriptions } : {}};
}

Expand Down
Loading

0 comments on commit 2aeed01

Please sign in to comment.