-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGuanQueryDispatcher.cs
50 lines (43 loc) · 2.02 KB
/
GuanQueryDispatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
45
46
47
48
49
50
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Guan.Logic;
namespace GuanExamples
{
public class GuanQueryDispatcher
{
private readonly Module module_;
public GuanQueryDispatcher(Module module)
{
module_ = module;
}
public async Task RunQueryAsync(string queryExpression, int maxResults = 1)
{
// Required ModuleProvider instance. You created the module used in its construction in Program.cs.
ModuleProvider moduleProvider = new ModuleProvider();
moduleProvider.Add(module_);
// Required QueryContext instance. You must supply moduleProvider (it implements IFunctorProvider).
QueryContext queryContext = new QueryContext(moduleProvider);
// The Query instance that will be used to execute the supplied query expression over the related rules.
Query query = Query.Create(queryExpression, queryContext);
// Execute the query.
// result will be () if there is no answer/result for supplied query (see the simple external predicate rules, for example).
if (maxResults == 1)
{
// Gets one result.
Term result = await query.GetNextAsync();
Console.WriteLine($"answer: {result}"); // () if there is no answer.
}
else
{
// Gets multiple results, if possible, up to supplied maxResults value.
List<Term> results = await query.GetResultsAsync(maxResults);
Console.WriteLine($"answer: {string.Join(',', results)}"); // convert the List<Term> object into a comma-delimited string.
}
}
}
}