How to use the platform/api/sparql.SparqlUtil.parseQuery function in platform

To help you get started, we’ve selected a few platform examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github researchspace / researchspace / researchspace / web / src / main / components / search / query-builder / QueryBuilder.tsx View on Github external
private setClauseBindingsParsed(
    searchState: RelationTermSelection, query: string
  ): SparqlJs.SelectQuery {
    const parsedQuery = SparqlUtil.parseQuery(query);

    // try to extract pattern from relation or category range
    const resourcePatternConfig = tryGetRelationPatterns(
      this.props.context.baseConfig, searchState.relation,
    ).find(p => p.kind === 'resource') as PatternResource;

    // parametrize query with found pattern or fallback to default one,
    // which just uses pattern IRI
    let patterns: SparqlJs.Pattern[];
    if (resourcePatternConfig) {
      patterns = SparqlUtil.parsePatterns(
        resourcePatternConfig.queryPattern, parsedQuery.prefixes);
      // transform pattern to resource suggestion
      const renamer = new VariableRenameBinder(
        SEMANTIC_SEARCH_VARIABLES.RESOURCE_VAR,
        RESOURCE_SEGGESTIONS_VARIABLES.SUGGESTION_IRI);
github researchspace / researchspace / metaphacts-platform / web / src / main / components / 3-rd-party / ontodia / data / OntodiaDataProvider.ts View on Github external
function fieldToPropertyConfig(field: FieldDefinition): PropertyConfiguration {
  const parsedQuery = SparqlUtil.parseQuery(field.selectPattern);
  if (parsedQuery.type === 'query' && parsedQuery.queryType === 'SELECT') {
    const domain = field.domain ? field.domain.map(iri => iri.value) : undefined;

    const directPredicate = parseDirectPredicate(parsedQuery.where);
    if (directPredicate) {
      return {id: field.iri, domain, path: directPredicate};
    }

    new VariableRenameBinder('subject', 'inst').query(parsedQuery);
    /* keep the same name for 'value' variable */

    return {
      id: field.iri,
      domain,
      path: serializePatterns(parsedQuery.where),
    };
github researchspace / researchspace / metaphacts-platform / web / src / main / components / forms / FieldMapping.ts View on Github external
function parseQuery(query: string): SparqlJs.SparqlQuery | ValidationError {
  try {
    return SparqlUtil.parseQuery(query);
  } catch (err) {
    return new ValidationError(err.message);
  }
}
github researchspace / researchspace / researchspace / web / src / main / components / alignment / ToolController.ts View on Github external
function createQueries(rawPatterns: AlignmentQueries, graph: Rdf.Iri): {
  queries: ParsedQueries;
  nodeQuery: SparqlJs.SelectQuery;
  patterns: ComplexTreePatterns;
} {
  const rootsQuery = SparqlUtil.parseQuery(rawPatterns.rootsQuery) as SparqlJs.SelectQuery;
  const childrenQuery = SparqlUtil.parseQuery(rawPatterns.childrenQuery) as SparqlJs.SelectQuery;
  const parentsQuery = SparqlUtil.parseQuery(rawPatterns.parentsQuery) as SparqlJs.SelectQuery;
  const searchQuery = SparqlUtil.parseQuery(rawPatterns.searchQuery) as SparqlJs.SelectQuery;
  const nodeQuery = SparqlUtil.parseQuery(rawPatterns.nodeQuery) as SparqlJs.SelectQuery;

  const binder = new IriBinder({
    'alignment:__graph__': graph,
  });

  [rootsQuery, childrenQuery, parentsQuery, searchQuery, nodeQuery]
    .forEach(q => binder.sparqlQuery(q));

  const patterns: ComplexTreePatterns = {
    rootsQuery: SparqlUtil.serializeQuery(rootsQuery),
    childrenQuery: SparqlUtil.serializeQuery(childrenQuery),
    parentsQuery: SparqlUtil.serializeQuery(parentsQuery),
github researchspace / researchspace / researchspace / web / src / main / components / alignment / ToolController.ts View on Github external
function createQueries(rawPatterns: AlignmentQueries, graph: Rdf.Iri): {
  queries: ParsedQueries;
  nodeQuery: SparqlJs.SelectQuery;
  patterns: ComplexTreePatterns;
} {
  const rootsQuery = SparqlUtil.parseQuery(rawPatterns.rootsQuery) as SparqlJs.SelectQuery;
  const childrenQuery = SparqlUtil.parseQuery(rawPatterns.childrenQuery) as SparqlJs.SelectQuery;
  const parentsQuery = SparqlUtil.parseQuery(rawPatterns.parentsQuery) as SparqlJs.SelectQuery;
  const searchQuery = SparqlUtil.parseQuery(rawPatterns.searchQuery) as SparqlJs.SelectQuery;
  const nodeQuery = SparqlUtil.parseQuery(rawPatterns.nodeQuery) as SparqlJs.SelectQuery;

  const binder = new IriBinder({
    'alignment:__graph__': graph,
  });

  [rootsQuery, childrenQuery, parentsQuery, searchQuery, nodeQuery]
    .forEach(q => binder.sparqlQuery(q));

  const patterns: ComplexTreePatterns = {
    rootsQuery: SparqlUtil.serializeQuery(rootsQuery),
    childrenQuery: SparqlUtil.serializeQuery(childrenQuery),
    parentsQuery: SparqlUtil.serializeQuery(parentsQuery),
    searchQuery: SparqlUtil.serializeQuery(searchQuery),
  };
github researchspace / researchspace / metaphacts-platform / web / src / main / components / forms / field-editor / Validation.ts View on Github external
function validateQuery(params: ValidateQueryParams): Value {
  const {query, patterns, type, variables = [], projection = [], error} = params;

  if (typeof query !== 'string' && typeof patterns !== 'string') {
    return {value: '', error: new Error(error)};
  }

  let value: string;
  let queryInfo: QueryInfo;
  try {
    if (query) {
      value = query;
      const parsedQuery = SparqlUtil.parseQuery(query);
      queryInfo = collectQueryInfo({query: parsedQuery});
    } else if (patterns) {
      value = patterns;
      const parsedPatterns = SparqlUtil.parsePatterns(patterns);
      queryInfo = collectQueryInfo({patterns: parsedPatterns});
    }
  } catch (e) {
    return {value, error: e};
  }

  const {queryType, allVariables, projectionVariables} = queryInfo;
  const hasCorrectType = !type || queryType === type;
  const projectionSet = new Set(projectionVariables);
  const hasEveryVariable = variables.every(v => allVariables.has(v));
  const hasEveryProjection = projection.every(v => projectionSet.has(v));
github researchspace / researchspace / researchspace / web / src / main / components / search / facet / FacetStore.ts View on Github external
private fetchRelations(
    baseQuery: SparqlJs.SelectQuery, conjuntcs: F.Conjuncts,
    maybeCategory: Data.Maybe
  ): Kefir.Property {
    const relations = this.config.searchProfileStore.relationsFor({
      domain: maybe.Just(this.config.domain),
      range: maybeCategory,
    }).map(this.buildFacetRelationBinding) as Relations;

    const facetEnabledQuery =
      SparqlUtil.parseQuery(
        'ASK { FILTER(?__relationPattern__) }'
      );
    facetEnabledQuery.where.unshift(...baseQuery.where);
    facetEnabledQuery.where = facetEnabledQuery.where.concat(
      this.generateQueryClause(baseQuery, conjuntcs)
    );

    const query = generateQueryForMultipleDatasets(
      facetEnabledQuery, this.context.selectedDatasets, this.context.baseConfig.datasetsConfig
    ) as SparqlJs.AskQuery;
    const enabledFacets = relations.valueSeq()
      .sortBy(relation => relation.label)
      .map(relation => this.fetchRelation(query, relation))
      .toArray();

    return Kefir.merge(enabledFacets).toProperty().scan(
github researchspace / researchspace / researchspace / web / src / main / components / panel-system / IIIFViewerPanel.tsx View on Github external
private queryImages() {
    const {semanticContext} = this.context;
    const {query, iris, repositories = [semanticContext.repository]} = this.props;

    const parsedQuery = SparqlUtil.parseQuery(query);
    const querying = iris.map(iri => {
      const sparql = SparqlClient.setBindings(parsedQuery, {[BINDING_VARIABLE]: Rdf.iri(iri)});
      const requests = repositories.map(repository =>
        SparqlClient.select(sparql, {context: {repository}}).map(
          ({results}) => ({iri, images: results.bindings.map(({image}) => image.value)})
        )
      );
      return Kefir.zip(requests);
    });
    this.queryingCancellation = this.cancellation.deriveAndCancel(this.queryingCancellation);
    this.queryingCancellation.map(
      Kefir.combine(querying)
    ).onValue(result => {
      const imageOrRegion: { [iri: string]: Array } = {};
      flatten(result).forEach(({iri, images}) => {
        if (!(imageOrRegion[iri] && imageOrRegion[iri].length)) {
github researchspace / researchspace / researchspace / web / src / main / widgets / image-upload-widget.ts View on Github external
function parametrizeQueryWithFormValues(
  createResourceQuery: string, fieldValues: CompositeValue
) {
  const initialQuery = SparqlUtil.parseQuery(createResourceQuery);
  if (initialQuery.type !== 'query') {
    throw new Error('createResourceQuery must be a SELECT or CONSTRUCT query');
  }
  const resultQuery = fieldValues.fields.reduce(
    (query, {values}, fieldId) => {
      const bindings = values
        .filter(FieldValue.isAtomic)
        .map(FieldValue.asRdfNode)
        .map(value => ({[fieldId]: value}) as _.Dictionary)
        .toArray();
      return SparqlClient.prepareParsedQuery(bindings)(query);
    },
    initialQuery
  );
  return resultQuery;
}