Skip to content

Search

Search sub-package for MaRDMO.

Provides search functionality across mathematical models, algorithms, and interdisciplinary workflows stored in the MaRDI Portal:

  • :mod:~MaRDMO.search.providers — RDMO option provider (:class:MaRDISearch) for searching the MaRDI Portal from any questionnaire field.
  • :mod:~MaRDMO.search.worker — Background worker that executes structured SPARQL queries against the MaRDI Portal and returns ranked matches for models, workflows, and algorithms.
  • :mod:~MaRDMO.search.sparql — Parameterised SPARQL query templates and filter fragments assembled at runtime for the portal search.

SPARQL

SPARQL query templates and filter fragments used by the MaRDMO portal search.

Each query_base_* string is a parameterised SPARQL SELECT template that is assembled at runtime by inserting placeholder fragments (*_sparql). The placeholder strings bind Wikibase item/property QIDs at format-time via :meth:str.format.

Providers

RDMO optionset provider for the cross-catalog MaRDI search questionnaire.

Implements a single provider that searches the MaRDI Portal for any entity type (models, workflows, algorithms) to let users discover and reference existing entries from any of the three documentation catalogs.

Provides:

  • :class:MaRDISearch — unified search across MaRDI Portal; no refresh upon selection; no user creation

MaRDISearch

Bases: Provider

General Provider (MaRDI Portal), No User Creation, No Refresh Upon Selection

Source code in MaRDMO/search/providers.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class MaRDISearch(Provider):
    '''General Provider (MaRDI Portal),
       No User Creation, No Refresh Upon Selection
    '''

    search = True

    def get_options(self, project, search=None, user=None, site=None):
        '''Query the MaRDI Portal and return matching options.

        Args:
            project: RDMO project instance (unused).
            search:  Search string entered by the user; returns empty list when
                     fewer than 3 characters.
            user:    Requesting user (unused).
            site:    Current site (unused).

        Returns:
            List of ``{"id": …, "text": …}`` option dicts sorted by relevance.
        '''
        if not search or len(search) < 3:
            return []

        # Define the query sources
        sources = ['mardi']

        return query_sources(search, sources)

get_options(project, search=None, user=None, site=None)

Query the MaRDI Portal and return matching options.

Parameters:

Name Type Description Default
project

RDMO project instance (unused).

required
search

Search string entered by the user; returns empty list when fewer than 3 characters.

None
user

Requesting user (unused).

None
site

Current site (unused).

None

Returns:

Type Description

List of {"id": …, "text": …} option dicts sorted by relevance.

Source code in MaRDMO/search/providers.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def get_options(self, project, search=None, user=None, site=None):
    '''Query the MaRDI Portal and return matching options.

    Args:
        project: RDMO project instance (unused).
        search:  Search string entered by the user; returns empty list when
                 fewer than 3 characters.
        user:    Requesting user (unused).
        site:    Current site (unused).

    Returns:
        List of ``{"id": …, "text": …}`` option dicts sorted by relevance.
    '''
    if not search or len(search) < 3:
        return []

    # Define the query sources
    sources = ['mardi']

    return query_sources(search, sources)

Workers

Background worker for the cross-catalog MaRDI search questionnaire.

Runs a SPARQL search against the MaRDI Portal to find mathematical models, workflows, and algorithms that match user-supplied criteria. Results are rendered as an HTML table.

Provides:

  • search — entry point that executes the portal query, formats the result table, and stores it for the view layer to retrieve

search(answers, options)

Build SPARQL queries from user search criteria, execute them, and store results.

Dispatches on answers["search"]["options"] to one of three search modes: Interdisciplinary Workflow, Mathematical Model, or Algorithm. For each mode, constructs a SPARQL query from the selected filter criteria, executes it against the appropriate endpoint (MaRDI Portal or MathAlgoDB), and writes the escaped query string, result count, and link list back into answers.

Parameters:

Name Type Description Default
answers

Top-level answers dict (mutated in place with "query", "no_results", and "links" keys).

required
options

Global RDMO options dict used for option-value comparisons.

required

Returns:

Type Description

The mutated answers dict.

Source code in MaRDMO/search/worker.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def search(answers, options):
    '''Build SPARQL queries from user search criteria, execute them, and store results.

    Dispatches on ``answers["search"]["options"]`` to one of three search
    modes: Interdisciplinary Workflow, Mathematical Model, or Algorithm.
    For each mode, constructs a SPARQL query from the selected filter criteria,
    executes it against the appropriate endpoint (MaRDI Portal or MathAlgoDB),
    and writes the escaped query string, result count, and link list back into
    *answers*.

    Args:
        answers: Top-level answers dict (mutated in place with ``"query"``,
                 ``"no_results"``, and ``"links"`` keys).
        options: Global RDMO options dict used for option-value comparisons.

    Returns:
        The mutated *answers* dict.
    '''
    if answers['search'].get('options') == options['InterdisciplinaryWorkflow']:

        # SPARQL via Research Objectives

        quote_str = ''
        res_obj_strs = ''

        # If SPARQL query via research objective desired
        if answers['search'].get('via_research_objective') == options['Yes']:
            quote_str = quote_sparql
            # Separate key words for SPARQL query vie research objective
            if answers['search'].get('research_objective'):
                for res_obj in answers['search']['research_objective'].values():
                    # Define Filters for SPARQL queries
                    res_obj_strs += res_obj_sparql.format(res_obj.lower())

        # SPARQL via Research Disciplines

        res_disc_str = ''

        # If SPARQL query via research discipline desired
        if answers['search'].get('via_research_discipline') == options['Yes']:
            # Separate disciplines for SPARQL query via research discipline
            if answers['search'].get('research_discipline'):
                for key in answers['search']['research_discipline'].keys():
                    # Get ID and Name of Research Discipline
                    identifier = answers['search']['research_discipline'][key]['ID'].split(':')[1]
                    name = answers['search']['research_discipline'][key]['Name']
                    answers['search']['research_discipline'][key].update({'ID': identifier})
                    answers['search']['research_discipline'][key].update({'Name': name})
                    # Define Filters for SPARQL queries
                    res_disc_str += res_disc_sparql.format(
                        identifier.split(':')[-1],
                        **get_items(),
                        **get_properties()
                    )

        # SPARQL via Mathematical Models, Methods, Softwares, Input or Output Data Sets

        mmsios_str = ''

        # If SPARQL query via Mathematical Models, Methods, Softwares, Input or Output Data Sets
        if answers['search'].get('via_workflow_entity') == options['Yes']:
            # Separate Mathematical Model, Methods, Software, Input or Output Data Sets
            if answers['search'].get('workflow_entity'):
                for key in answers['search']['workflow_entity'].keys():
                    # Get ID and Name of Research Discipline
                    identifier = answers['search']['workflow_entity'][key]['ID'].split(':')[1]
                    name = answers['search']['workflow_entity'][key]['Name']
                    answers['search']['workflow_entity'][key].update({'ID': identifier})
                    answers['search']['workflow_entity'][key].update({'Name': name})
                    # Define Filters for SPARQL queries
                    mmsios_str += mmsio_sparql.format(
                        identifier.split(':')[-1],
                        **get_items(),
                        **get_properties()
                    )

        # Set up entire SPARQL query
        query = "\n".join(
            line
            for line in query_base_workflow.format(
                res_disc_str,
                mmsios_str,
                quote_str,
                res_obj_strs,
                **get_items(),
                **get_properties(),
            ).splitlines()
            if line.strip()
        )

        # Add Query to answer dictionary
        answers['query'] = html.escape(query).replace('\n', '<br>')

        # Query MaRDI Portal
        results = query_sparql(query, get_url('mardi', 'sparql'))

        # Number of Results
        answers['no_results'] = str(len(results))

        # Generate Links to Wikipage and Knowledge Graoh Entry of Results
        links=[]
        for result in results:
            links.append(
                [
                    result["label"]["value"],
                    f"{get_url('mardi', 'uri')}/wiki/workflow:{result['qid']['value'][1:]}",
                    f"{get_item_url('mardi')}{result['qid']['value']}"
                ]
            )

        answers['links'] = links

    elif answers['search'].get('options') == options['MathematicalModel']:

        # SPARQL via Research Problems

        pro_str = ''
        pro_fil_strs = ''

        # If SPARQL query via research objective desired
        if answers['search'].get('via_research_problem') == options['Yes']:
            pro_str = problem_sparql.format(**get_items(), **get_properties())
            # Separate key words for SPARQL query vie research objective
            if answers['search'].get('research_problem'):
                for res_pro in answers['search']['research_problem'].values():
                    # Define Filters for SPARQL queries
                    pro_fil_strs += problem_filter_sparql.format(res_pro.lower())

        # SPARQL via Research Fields

        fie_str = ''

        # If SPARQL query via research field desired
        if answers['search'].get('via_research_field') == options['Yes']:
            #fie_str = field_sparql
            # Separate key words for SPARQL query vie research objective
            if answers['search'].get('research_field'):
                for key in answers['search']['research_field'].keys():
                    # Get ID and Name of Research Field
                    identifier = answers['search']['research_field'][key]['ID'].split(':')[1]
                    name = answers['search']['research_field'][key]['Name']
                    answers['search']['research_field'][key].update({'ID': identifier})
                    answers['search']['research_field'][key].update({'Name': name})
                    # Define Filters for SPARQL queries
                    fie_str += field_sparql.format(
                        identifier,
                        **get_items(),
                        **get_properties()
                    )

        # SPARQL via Mathematical Formulations, Computational Task and Quantities

        for_str = ''
        ta_str = ''
        qu_str = quantity_sparql.format(**get_items(), **get_properties())

        # If SPARQL query via model entity desired
        if answers['search'].get('via_model_entity') == options['Yes']:
            # Via Formulations
            if answers['search'].get('model_formulation'):
                for key in answers['search']['model_formulation'].keys():
                    # Get ID and Name of Formulation
                    identifier = answers['search']['model_formulation'][key]['ID'].split(':')[1]
                    name = answers['search']['model_formulation'][key]['Name']
                    answers['search']['model_formulation'][key].update({'ID': identifier})
                    answers['search']['model_formulation'][key].update({'Name': name})
                    # Define Filters for SPARQL queries
                    for_str += formulation_sparql.format(
                        identifier,
                        **get_items(),
                        **get_properties()
                    )
            # Via Computational Tasls
            if answers['search'].get('model_task'):
                for key in answers['search']['model_task'].keys():
                    # Get ID and Name of Computational Task
                    identifier = answers['search']['model_task'][key]['ID'].split(':')[1]
                    name = answers['search']['model_task'][key]['Name']
                    answers['search']['model_task'][key].update({'ID': identifier})
                    answers['search']['model_task'][key].update({'Name': name})
                    # Define Filters for SPARQL queries
                    ta_str += task_sparql.format(
                        identifier,
                        **get_items(),
                        **get_properties()
                    )
            # Via Computational Tasls
            if answers['search'].get('model_quantity'):
                for idx, key in enumerate(answers['search']['model_quantity'].keys()):
                    # Get ID and Name of Computational Task
                    identifier = answers['search']['model_quantity'][key]['ID'].split(':')[1]
                    name = answers['search']['model_quantity'][key]['Name']
                    answers['search']['model_quantity'][key].update({'ID': identifier})
                    answers['search']['model_quantity'][key].update({'Name': name})
                    # Define Filters for SPARQL queries
                    if idx == 0:
                        qu_str += quantity_filter_sparql.format(
                            identifier,
                            **get_items(),
                            **get_properties()
                        )
                    else:
                        qu_str += """\n UNION""" + quantity_filter_sparql.format(
                            identifier,
                            **get_items(),
                            **get_properties()
                        )

        # Set up entire SPARQL query
        query = "\n".join(
            line
            for line in query_base_model.format(
                pro_str,
                pro_fil_strs,
                fie_str,
                for_str,
                ta_str,
                qu_str,
                **get_items(),
                **get_properties()
            ).splitlines()
            if line.strip()
        )

        # Add Query to answer dictionary
        answers['query'] = html.escape(query).replace('\n', '<br>')

        # Query MathModDB
        results = query_sparql(query, get_url('mardi', 'sparql'))

        # Number of Results
        answers['no_results'] = str(len(results))

        # Generate Links to Entry
        links=[]
        for result in results:
            links.append(
                [
                    result["label"]["value"],
                    f"{get_url('mardi', 'uri')}/wiki/model:{result['qid']['value'][1:]}",
                    f"{get_item_url('mardi')}{result['qid']['value']}"
                ]
            )

        answers['links'] = links

    elif answers['search'].get('options') == options['Algorithm']:

        # SPARQL via Algorithmic Problems

        apr_str = ''
        apr_fil_strs = ''

        # If SPARQL query via research objective desired
        if answers['search'].get('via_algorithmic_problem') == options['Yes']:
            apr_str = algorithmic_problem_sparql
            # Separate key words for SPARQL query vie research objective
            if answers['search'].get('algorithmic_problem'):
                for alg_pro in answers['search']['algorithmic_problem'].values():
                    # Define Filters for SPARQL queries
                    apr_fil_strs += algorithmic_problem_filter_sparql.format(alg_pro.lower())

        # SPARQL via Softwares

        sof_str = ''

        # If SPARQL query via software desired
        if answers['search'].get('via_software') == options['Yes']:
            # Separate key words for SPARQL query vie research objective
            if answers['search'].get('software'):
                for key in answers['search']['software'].keys():
                    # Get ID and Name of Software
                    identifier = answers['search']['software'][key]['ID'].split(':')[1]
                    name = answers['search']['software'][key]['Name']
                    answers['search']['software'][key].update({'ID': identifier})
                    answers['search']['software'][key].update({'Name': name})
                    # Define Filters for SPARQL queries
                    sof_str += software_sparql.format(f"software:{identifier}")

        # Set up entire SPARQL query
        query = "\n".join(
            line
            for line in query_base_algorithm.format(
                apr_str,
                apr_fil_strs,
                sof_str
            ).splitlines()
            if line.strip()
        )

        # Add Query to answer dictionary
        answers['query'] = html.escape(query).replace('\n', '<br>')

        # Query MathAlgoDB
        results = query_sparql(query, get_url('mathalgodb', 'sparql'))

        # Number of Results
        answers['no_results'] = str(len(results))

        # Generate Links to Entry
        links=[]
        for result in results:
            links.append(
                [
                    result["label"]["value"],
                    get_url('mathalgodb', 'uri') + 'object/al:' + result["qid"]["value"],
                    ''
                ]
            )

        answers['links'] = links

    return answers