diff options
author | 2023-09-14 19:29:36 +0200 | |
---|---|---|
committer | 2023-09-14 19:29:36 +0200 | |
commit | 98ed3b6db5f4e51961a161050cc31c66015116e8 (patch) | |
tree | 8bfd6d9bc8d6ed23b9eb0f889dd40b6c24fe8f92 /subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/PartialInterpretation2Json.java | |
parent | Merge pull request #38 from nagilooh/design-space-exploration (diff) | |
parent | Merge remote-tracking branch 'upstream/main' into partial-interpretation (diff) | |
download | refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.tar.gz refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.tar.zst refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.zip |
Merge pull request #39 from kris7t/partial-interpretation
Implement partial interpretation based model generation
Diffstat (limited to 'subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/PartialInterpretation2Json.java')
-rw-r--r-- | subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/PartialInterpretation2Json.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/PartialInterpretation2Json.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/PartialInterpretation2Json.java new file mode 100644 index 00000000..5d5da8fe --- /dev/null +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/semantics/PartialInterpretation2Json.java | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.language.web.semantics; | ||
7 | |||
8 | import com.google.gson.JsonArray; | ||
9 | import com.google.gson.JsonObject; | ||
10 | import com.google.inject.Inject; | ||
11 | import com.google.inject.Singleton; | ||
12 | import tools.refinery.language.semantics.model.ModelInitializer; | ||
13 | import tools.refinery.language.semantics.model.SemanticsUtils; | ||
14 | import tools.refinery.store.map.Cursor; | ||
15 | import tools.refinery.store.model.Model; | ||
16 | import tools.refinery.store.reasoning.ReasoningAdapter; | ||
17 | import tools.refinery.store.reasoning.literal.Concreteness; | ||
18 | import tools.refinery.store.reasoning.representation.PartialRelation; | ||
19 | import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator; | ||
20 | import tools.refinery.store.tuple.Tuple; | ||
21 | import tools.refinery.store.util.CancellationToken; | ||
22 | |||
23 | import java.util.TreeMap; | ||
24 | |||
25 | @Singleton | ||
26 | public class PartialInterpretation2Json { | ||
27 | @Inject | ||
28 | private SemanticsUtils semanticsUtils; | ||
29 | |||
30 | public JsonObject getPartialInterpretation(ModelInitializer initializer, Model model, Concreteness concreteness, | ||
31 | CancellationToken cancellationToken) { | ||
32 | var adapter = model.getAdapter(ReasoningAdapter.class); | ||
33 | var json = new JsonObject(); | ||
34 | for (var entry : initializer.getRelationTrace().entrySet()) { | ||
35 | var relation = entry.getKey(); | ||
36 | var partialSymbol = entry.getValue(); | ||
37 | var tuples = getTuplesJson(adapter, concreteness, partialSymbol); | ||
38 | var name = semanticsUtils.getName(relation).orElse(partialSymbol.name()); | ||
39 | json.add(name, tuples); | ||
40 | cancellationToken.checkCancelled(); | ||
41 | } | ||
42 | json.add("builtin::count", getCountJson(model)); | ||
43 | return json; | ||
44 | } | ||
45 | |||
46 | private static JsonArray getTuplesJson(ReasoningAdapter adapter, Concreteness concreteness, | ||
47 | PartialRelation partialSymbol) { | ||
48 | var interpretation = adapter.getPartialInterpretation(concreteness, partialSymbol); | ||
49 | var cursor = interpretation.getAll(); | ||
50 | return getTuplesJson(cursor); | ||
51 | } | ||
52 | |||
53 | private static JsonArray getTuplesJson(Cursor<Tuple, ?> cursor) { | ||
54 | var map = new TreeMap<Tuple, Object>(); | ||
55 | while (cursor.move()) { | ||
56 | map.put(cursor.getKey(), cursor.getValue()); | ||
57 | } | ||
58 | var tuples = new JsonArray(); | ||
59 | for (var entry : map.entrySet()) { | ||
60 | tuples.add(toArray(entry.getKey(), entry.getValue())); | ||
61 | } | ||
62 | return tuples; | ||
63 | } | ||
64 | |||
65 | private static JsonArray toArray(Tuple tuple, Object value) { | ||
66 | int arity = tuple.getSize(); | ||
67 | var json = new JsonArray(arity + 1); | ||
68 | for (int i = 0; i < arity; i++) { | ||
69 | json.add(tuple.get(i)); | ||
70 | } | ||
71 | json.add(value.toString()); | ||
72 | return json; | ||
73 | } | ||
74 | |||
75 | private static JsonArray getCountJson(Model model) { | ||
76 | var interpretation = model.getInterpretation(MultiObjectTranslator.COUNT_STORAGE); | ||
77 | var cursor = interpretation.getAll(); | ||
78 | return getTuplesJson(cursor); | ||
79 | |||
80 | } | ||
81 | } | ||