Skip to content

Commit

Permalink
Sparql template cleanup (#448)
Browse files Browse the repository at this point in the history
* Add dedent

* Convert to function and use dedent

* rename variables for simplicity

* Add dedent to third party license file
  • Loading branch information
kmcginnes committed Jun 20, 2024
1 parent abc48b4 commit 5811777
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 95 deletions.
24 changes: 24 additions & 0 deletions THIRD_PARTY_LICENSES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5316,3 +5316,27 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

------

** dedent; version 1.5.3 -- https://github.com/dmnd/dedent
Copyright Desmond Brand

MIT License

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions packages/graph-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"cytoscape-klay": "^3.1.4",
"date-fns": "^2.30.0",
"dayjs": "^1.11.11",
"dedent": "^1.5.3",
"file-saver": "^2.0.5",
"flat": "^5.0.2",
"framer-motion": "^11.1.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ type RawNeighborsPredicatesResponse = {
};
};

const fetchBlankNodeNeighborsPredicates = async (
async function fetchBlankNodeNeighborsPredicates(
sparqlFetch: SparqlFetch,
subQuery: string,
resourceURI: string,
resourceClass: string,
subjectURIs: string[]
) => {
) {
const template = blankNodeSubjectPredicatesTemplate({
subQuery,
subjectURIs,
Expand All @@ -59,9 +59,9 @@ const fetchBlankNodeNeighborsPredicates = async (

return mapOutgoingToEdge(resourceURI, resourceClass, result);
});
};
}

export const mapOneHop = (data: RawBlankNodeNeighborsResponse) => {
export function mapOneHop(data: RawBlankNodeNeighborsResponse) {
const groupBySubject = groupBy(
data.results.bindings,
result => result.subject.value
Expand All @@ -85,12 +85,12 @@ export const mapOneHop = (data: RawBlankNodeNeighborsResponse) => {
return Object.values(mappedResults).map(result => {
return mapRawResultToVertex(result);
});
};
}

const fetchBlankNodeNeighbors = async (
export default async function fetchBlankNodeNeighbors(
sparqlFetch: SparqlFetch,
req: SPARQLBlankNodeNeighborsRequest
): Promise<SPARQLBlankNodeNeighborsResponse> => {
): Promise<SPARQLBlankNodeNeighborsResponse> {
const neighborsTemplate = blankNodeOneHopNeighborsTemplate(req.subQuery);
const neighbors = await sparqlFetch<
RawBlankNodeNeighborsResponse | ErrorResponse
Expand Down Expand Up @@ -132,6 +132,4 @@ const fetchBlankNodeNeighbors = async (
edges,
},
};
};

export default fetchBlankNodeNeighbors;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const fetchNeighborsCount = async (
let totalCount = 0;
const counts: Record<string, number> = {};

const incomingTemplate = neighborsCountTemplate(req);
const incomingData = await sparqlFetch<RawNeighborCount>(incomingTemplate);
incomingData.results.bindings.forEach(result => {
const template = neighborsCountTemplate(req);
const response = await sparqlFetch<RawNeighborCount>(template);
response.results.bindings.forEach(result => {
const count = Number(result.count.value);
counts[result.class.value] = count;
totalCount += count;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import dedent from "dedent";

// It returns the number of instances of the given class
const classWithCountsTemplates = (className: string) => {
return `
export default function classWithCountsTemplates(className: string) {
return dedent`
SELECT (COUNT(?start) AS ?instancesCount) {
?start a <${className}>
}
`;
};

export default classWithCountsTemplates;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import dedent from "dedent";

// It returns a list of classes with the number of instances for each class
const classesWithCountsTemplates = () => {
return `
export default function classesWithCountsTemplates() {
return dedent`
SELECT ?class (COUNT(?start) AS ?instancesCount) {
?start a ?class
}
GROUP BY ?class
`;
};

export default classesWithCountsTemplates;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
*
* @see keywordSearchTemplate
*/
const keywordSearchBlankNodesIdsTemplate = ({
export default function keywordSearchBlankNodesIdsTemplate({
searchTerm,
subjectClasses = [],
predicates = [],
limit = 10,
offset = 0,
exactMatch = true,
}: SPARQLKeywordSearchRequest): string => {
}: SPARQLKeywordSearchRequest): string {
return `
SELECT DISTINCT ?bNode {
?bNode ?pred ?value {
Expand All @@ -35,6 +35,4 @@ const keywordSearchBlankNodesIdsTemplate = ({
FILTER(isBlank(?bNode))
}
`;
};

export default keywordSearchBlankNodesIdsTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from "dedent";
import { SPARQLKeywordSearchRequest } from "../../types";
import {
getFilterObject,
Expand Down Expand Up @@ -36,15 +37,15 @@ import {
* FILTER(isLiteral(?value))
* }
*/
const keywordSearchTemplate = ({
export default function keywordSearchTemplate({
searchTerm,
subjectClasses = [],
predicates = [],
limit = 10,
offset = 0,
exactMatch = false,
}: SPARQLKeywordSearchRequest): string => {
return `
}: SPARQLKeywordSearchRequest): string {
return dedent`
SELECT ?subject ?pred ?value ?class {
?subject ?pred ?value {
SELECT DISTINCT ?subject ?class {
Expand All @@ -59,6 +60,4 @@ const keywordSearchTemplate = ({
FILTER(isLiteral(?value))
}
`;
};

export default keywordSearchTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SPARQLBlankNodeNeighborsCountRequest } from "../../types";
import dedent from "dedent";

/**
* Count neighbors by class which are related with the given subject URI.
Expand All @@ -19,11 +20,11 @@ import { SPARQLBlankNodeNeighborsCountRequest } from "../../types";
* }
* GROUP BY ?bNode ?class
*/
const blankNodeNeighborsCountTemplate = ({
export default function blankNodeNeighborsCountTemplate({
subQuery,
limit = 0,
}: SPARQLBlankNodeNeighborsCountRequest) => {
return `
}: SPARQLBlankNodeNeighborsCountRequest) {
return dedent`
SELECT ?bNode ?class (COUNT(?class) AS ?count) {
?subject a ?class {
SELECT DISTINCT ?bNode ?subject ?class {
Expand All @@ -38,6 +39,4 @@ const blankNodeNeighborsCountTemplate = ({
}
GROUP BY ?bNode ?class
`;
};

export default blankNodeNeighborsCountTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from "dedent";
import { SPARQLNeighborsCountRequest } from "../../types";

/**
Expand All @@ -18,11 +19,11 @@ import { SPARQLNeighborsCountRequest } from "../../types";
* }
* GROUP BY ?class
*/
const neighborsCountTemplate = ({
export default function neighborsCountTemplate({
resourceURI,
limit = 0,
}: SPARQLNeighborsCountRequest) => {
return `
}: SPARQLNeighborsCountRequest) {
return dedent`
SELECT ?class (COUNT(?class) AS ?count) {
?subject a ?class {
SELECT DISTINCT ?subject ?class {
Expand All @@ -36,6 +37,4 @@ const neighborsCountTemplate = ({
}
GROUP BY ?class
`;
};

export default neighborsCountTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from "dedent";
import { SPARQLNeighborsCountRequest } from "../types";

/**
Expand All @@ -18,11 +19,11 @@ import { SPARQLNeighborsCountRequest } from "../types";
* }
* GROUP BY ?class
*/
const neighborsCountTemplate = ({
export default function neighborsCountTemplate({
resourceURI,
limit = 0,
}: SPARQLNeighborsCountRequest) => {
return `
}: SPARQLNeighborsCountRequest) {
return dedent`
SELECT ?class (COUNT(?class) AS ?count) {
?subject a ?class {
SELECT DISTINCT ?subject ?class {
Expand All @@ -36,6 +37,4 @@ const neighborsCountTemplate = ({
}
GROUP BY ?class
`;
};

export default neighborsCountTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import dedent from "dedent";

/**
* Fetch all neighbors and their predicates, values, and classes
* given a blank node sub-query.
*
* @see oneHopNeighborsTemplate
*/
const blankNodeOneHopNeighborsTemplate = (subQuery: string) => {
return `
export default function blankNodeOneHopNeighborsTemplate(subQuery: string) {
return dedent`
SELECT ?bNode ?subject ?pred ?value ?subjectClass ?pToSubject ?pFromSubject {
?subject a ?subjectClass ;
?pred ?value .
Expand All @@ -20,6 +22,4 @@ const blankNodeOneHopNeighborsTemplate = (subQuery: string) => {
FILTER(isLiteral(?value))
}
`;
};

export default blankNodeOneHopNeighborsTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from "dedent";
import { SPARQLNeighborsRequest } from "../../types";
import { getFilters, getLimit, getSubjectClasses } from "./helpers";

Expand All @@ -7,14 +8,14 @@ import { getFilters, getLimit, getSubjectClasses } from "./helpers";
*
* @see oneHopNeighborsTemplate
*/
const oneHopNeighborsBlankNodesIdsTemplate = ({
export default function oneHopNeighborsBlankNodesIdsTemplate({
resourceURI,
subjectClasses = [],
filterCriteria = [],
limit = 0,
offset = 0,
}: SPARQLNeighborsRequest) => {
return `
}: SPARQLNeighborsRequest) {
return dedent`
SELECT DISTINCT (?subject AS ?bNode) {
BIND(<${resourceURI}> AS ?argument)
${getSubjectClasses(subjectClasses)}
Expand All @@ -35,6 +36,4 @@ const oneHopNeighborsBlankNodesIdsTemplate = ({
}
${getLimit(limit, offset)}
`;
};

export default oneHopNeighborsBlankNodesIdsTemplate;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SPARQLNeighborsRequest } from "../../types";
import { getFilters, getLimit, getSubjectClasses } from "./helpers";
import dedent from "dedent";

/**
* Fetch all neighbors and their predicates, values, and classes.
Expand Down Expand Up @@ -50,14 +51,14 @@ import { getFilters, getLimit, getSubjectClasses } from "./helpers";
* FILTER(isLiteral(?value))
* }
*/
const oneHopNeighborsTemplate = ({
export default function oneHopNeighborsTemplate({
resourceURI,
subjectClasses = [],
filterCriteria = [],
limit = 0,
offset = 0,
}: SPARQLNeighborsRequest): string => {
return `
}: SPARQLNeighborsRequest): string {
return dedent`
SELECT ?subject ?pred ?value ?subjectClass ?pToSubject ?pFromSubject {
?subject a ?subjectClass;
?pred ?value {
Expand All @@ -83,6 +84,4 @@ const oneHopNeighborsTemplate = ({
FILTER(isLiteral(?value))
}
`;
};

export default oneHopNeighborsTemplate;
}
Loading

0 comments on commit 5811777

Please sign in to comment.