From 0fe212036509e0255dfd747c1060ca5a76826bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20Marussy?= Date: Fri, 2 Aug 2024 22:36:46 +0200 Subject: [PATCH] feat(semantics): base predicate serialization --- .../semantics/SolutionSerializer.java | 23 ++++++-- .../semantics/SolutionSerializerTest.java | 59 +++++++++++++++++++ .../refinery/language/utils/ProblemUtil.java | 4 ++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java index ed4841c4..16a41824 100644 --- a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java +++ b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SolutionSerializer.java @@ -24,6 +24,7 @@ import tools.refinery.language.scoping.imports.ImportAdapterProvider; import tools.refinery.language.typesystem.SignatureProvider; import tools.refinery.language.utils.ProblemUtil; +import tools.refinery.logic.term.truthvalue.TruthValue; import tools.refinery.store.model.Model; import tools.refinery.store.reasoning.ReasoningAdapter; import tools.refinery.store.reasoning.interpretation.PartialInterpretation; @@ -31,13 +32,15 @@ import tools.refinery.store.reasoning.representation.PartialRelation; import tools.refinery.store.reasoning.translator.typehierarchy.InferredType; import tools.refinery.store.reasoning.translator.typehierarchy.TypeHierarchyTranslator; -import tools.refinery.logic.term.truthvalue.TruthValue; import tools.refinery.store.tuple.Tuple; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.*; +import java.util.Map; +import java.util.Objects; +import java.util.TreeMap; +import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; @@ -107,6 +110,7 @@ public Problem serializeSolution(ProblemTrace trace, Model model, URI uri) { addExistsAssertions(); addClassAssertions(); addReferenceAssertions(); + addBasePredicateAssertions(); if (nodeDeclaration.getNodes().isEmpty()) { problem.getStatements().remove(nodeDeclaration); } @@ -249,8 +253,8 @@ private void addExistsAssertions() { } private void addClassAssertions() { - var types = - trace.getMetamodel().typeHierarchy().getPreservedTypes().keySet().stream().collect(Collectors.toMap(Function.identity(), this::findPartialRelation)); + var types = trace.getMetamodel().typeHierarchy().getPreservedTypes().keySet().stream() + .collect(Collectors.toMap(Function.identity(), this::findPartialRelation)); var cursor = model.getInterpretation(TypeHierarchyTranslator.TYPE_SYMBOL).getAll(); while (cursor.move()) { var key = cursor.getKey(); @@ -306,6 +310,17 @@ private void addReferenceAssertions() { } } + private void addBasePredicateAssertions() { + for (var entry : trace.getRelationTrace().entrySet()) { + if (entry.getKey() instanceof PredicateDefinition predicateDefinition && + ProblemUtil.isBasePredicate(predicateDefinition)) { + var partialRelation = entry.getValue(); + addDefaultAssertion(partialRelation); + addAssertions(partialRelation); + } + } + } + private void addAssertions(PartialRelation partialRelation) { var relation = findPartialRelation(partialRelation); var cursor = reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, partialRelation).getAll(); diff --git a/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java b/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java index 69b5f1c2..fded04bf 100644 --- a/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java +++ b/subprojects/language-semantics/src/test/java/tools/refinery/language/semantics/SolutionSerializerTest.java @@ -234,6 +234,65 @@ class Foo. !exists(a). !exists(Foo::new). Foo(foo1). + """), Arguments.of(""" + class Foo { + partial Bar[] bar + } + + class Bar. + """, """ + bar(a, b). + scope Foo = 2, Bar = 2. + """, """ + declare a, b, foo1, bar1. + !exists(Foo::new). + !exists(Bar::new). + Foo(foo1). + Bar(bar1). + Foo(a). + Bar(b). + default !bar(*, *). + ?bar(foo1, bar1). + ?bar(foo1, b). + ?bar(a, bar1). + bar(a, b). + """), Arguments.of(""" + class Foo. + class Bar. + pred bar(Foo x, Bar y). + """, """ + bar(a, b). + scope Foo = 2, Bar = 2. + """, """ + declare a, b, foo1, bar1. + !exists(Foo::new). + !exists(Bar::new). + Foo(foo1). + Bar(bar1). + Foo(a). + Bar(b). + default !bar(*, *). + bar(a, b). + """), Arguments.of(""" + class Foo. + class Bar. + partial pred bar(Foo x, Bar y). + """, """ + bar(a, b). + scope Foo = 2, Bar = 2. + """, """ + declare a, b, foo1, bar1. + !exists(Foo::new). + !exists(Bar::new). + Foo(foo1). + Bar(bar1). + Foo(a). + Bar(b). + default !bar(*, *). + ?bar(foo1, bar1). + ?bar(foo1, b). + ?bar(a, bar1). + bar(a, b). """)); } } diff --git a/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java b/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java index 55b0ca60..75e2ded0 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java +++ b/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java @@ -121,6 +121,10 @@ public static boolean isDerivedStatePredicate(PredicateDefinition predicateDefin } public static boolean isBasePredicate(PredicateDefinition predicateDefinition) { + if (isBuiltIn(predicateDefinition) || predicateDefinition == null) { + // Built-in predicates have no clauses, but are not base. + return false; + } return switch (predicateDefinition.getKind()) { case DEFAULT -> predicateDefinition.getBodies().isEmpty(); case PARTIAL -> true;