UNPKG

sparnatural

Version:

Visual client-side SPARQL query builder and knowledge graph exploration tool

222 lines 8.21 kB
import { Config } from "../../ontologies/SparnaturalConfig"; import { DataFactory } from 'rdf-data-factory'; import { SH } from '../../../rdf/vocabularies/SH'; import { SKOS } from '../../../rdf/vocabularies/SKOS'; import { XSD } from '../../../rdf/vocabularies/XSD'; import { GEOSPARQL } from "../../components/widgets/MapWidget"; import { StatisticsReader } from "../StatisticsReader"; import { RDF } from "../BaseRDFReader"; const factory = new DataFactory(); export class SparnaturalSearchWidgetsScorerRegistry { constructor() { this.searchWidgets = new Array(); } static getInstance() { if (!SparnaturalSearchWidgetsScorerRegistry.instance) { SparnaturalSearchWidgetsScorerRegistry.instance = new SparnaturalSearchWidgetsScorerRegistry(); } return SparnaturalSearchWidgetsScorerRegistry.instance; } getSearchWidgets() { return this.searchWidgets; } } SparnaturalSearchWidgetsScorerRegistry.instance = new SparnaturalSearchWidgetsScorerRegistry(); export class AutocompleteWidgetScorer { getUri() { return Config.AUTOCOMPLETE_PROPERTY; } score(propertyShape, range, store, config) { return 10; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new AutocompleteWidgetScorer()); export class ListWidgetScorer { getUri() { return Config.LIST_PROPERTY; } score(propertyShape, range, store, config) { // if there is a provided list of values, score higher if (store.hasTriple(factory.namedNode(propertyShape), SH.IN, null)) { return 100; } // if there is a distinctObjectsCount and the distinctObjectsCount is < 500, then this will score higher let count = new StatisticsReader(store).getDistinctObjectsCountForShape(factory.namedNode(propertyShape)); if (count && (count < 500)) { return 20; } // if the shape points to skos:Concept in sh:class, score higher if (store.hasTriple(factory.namedNode(propertyShape), SH.CLASS, SKOS.CONCEPT)) { return 20; } if (config.getEntity(range).couldBeSkosConcept()) { return 20; } return -1; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new ListWidgetScorer()); export class TreeWidgetScorer { getUri() { return Config.TREE_PROPERTY; } score(propertyShape, range, store, config) { return -1; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new TreeWidgetScorer()); export class DatePickerWidgetScorer { getUri() { return Config.TIME_PROPERTY_DATE; } score(propertyShape, range, store, config) { let hasDateOrDateTimePredicate = function (rdfNode) { if (store.hasTriple(rdfNode, SH.DATATYPE, XSD.DATE) || store.hasTriple(rdfNode, SH.DATATYPE, XSD.DATE_TIME)) { return true; } else { return false; } }; // if the datatype is xsd:date or xsd:dateTime if (hasDateOrDateTimePredicate(factory.namedNode(propertyShape))) { return 50; } else { return -1; } ; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new DatePickerWidgetScorer()); export class YearPickerWidgetScorer { getUri() { return Config.TIME_PROPERTY_YEAR; } score(propertyShape, range, store, config) { // if the datatype is xsd:gYear if (store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.GYEAR)) { return 50; } else { return -1; } ; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new YearPickerWidgetScorer()); export class MapWidgetScorer { getUri() { return Config.MAP_PROPERTY; } score(propertyShape, range, store, config) { // if the datatype is geo:wktLiteral if (store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, GEOSPARQL.WKT_LITERAL)) { return 50; } else { return -1; } ; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new MapWidgetScorer()); export class BooleanWidgetScorer { getUri() { return Config.BOOLEAN_PROPERTY; } score(propertyShape, range, store, config) { // if the datatype is xsd:boolean if (store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.BOOLEAN)) { return 50; } else { return -1; } ; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new BooleanWidgetScorer()); export class NumberWidgetScorer { getUri() { return Config.NUMBER_PROPERTY; } score(propertyShape, range, store, config) { // if the datatype is xsd:boolean if (store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.BYTE) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.DECIMAL) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.DOUBLE) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.FLOAT) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.INT) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.INTEGER) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.LONG) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.NONNEGATIVE_INTEGER) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.SHORT) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.UNSIGNED_BYTE) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.UNSIGNED_INT) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.UNSIGNED_LONG) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.UNSIGNED_SHORT)) { return 50; } else { return -1; } ; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new NumberWidgetScorer()); export class NoWidgetScorer { getUri() { return Config.NON_SELECTABLE_PROPERTY; } score(propertyShape, range, store, config) { return -1; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new NoWidgetScorer()); export class SearchRegexWidgetScorer { getUri() { return Config.SEARCH_PROPERTY; } score(propertyShape, range, store, config) { let count = new StatisticsReader(store).getDistinctObjectsCountForShape(factory.namedNode(propertyShape)); // if the datatype is xsd:string or rdf:langString, and there is a large number, otherwise we stick with list widget if ((store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, XSD.STRING) || store.hasTriple(factory.namedNode(propertyShape), SH.DATATYPE, RDF.LANG_STRING)) && // if there is no count, this will always score high (!count || (count > 500))) { return 50; } else { return -1; } ; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new SearchRegexWidgetScorer()); export class SearchEqualWidgetScorer { getUri() { return Config.STRING_EQUALS_PROPERTY; } score(propertyShape, range, store, config) { return -1; } } SparnaturalSearchWidgetsScorerRegistry.getInstance().getSearchWidgets().push(new SearchEqualWidgetScorer()); //# sourceMappingURL=SHACLSearchWidgets.js.map