Skip to content

Commit

Permalink
feat(semantics): base predicate serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kris7t committed Aug 2, 2024
1 parent 17ab3a8 commit 0fe2120
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,23 @@
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;
import tools.refinery.store.reasoning.literal.Concreteness;
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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
"""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0fe2120

Please sign in to comment.