diff options
author | Kristóf Marussy <kristof@marussy.com> | 2024-06-30 14:12:45 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2024-06-30 20:29:21 +0200 |
commit | d77406349cf792823bd093332c1ca4d6e7a1091a (patch) | |
tree | f4ed54e857b30d7ff2dfb9a636f86c5b598c2120 /subprojects/language-semantics | |
parent | feat: access computed values of relations (diff) | |
download | refinery-d77406349cf792823bd093332c1ca4d6e7a1091a.tar.gz refinery-d77406349cf792823bd093332c1ca4d6e7a1091a.tar.zst refinery-d77406349cf792823bd093332c1ca4d6e7a1091a.zip |
refactor(language): move computed to Atom
Diffstat (limited to 'subprojects/language-semantics')
-rw-r--r-- | subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/ModelInitializer.java | 55 |
1 files changed, 22 insertions, 33 deletions
diff --git a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/ModelInitializer.java b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/ModelInitializer.java index 8f58583b..3400d9f4 100644 --- a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/ModelInitializer.java +++ b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/ModelInitializer.java | |||
@@ -710,8 +710,7 @@ public class ModelInitializer { | |||
710 | } | 710 | } |
711 | } | 711 | } |
712 | case Atom atom -> { | 712 | case Atom atom -> { |
713 | var target = extractedOuter.modality.wrapPartialRelation(getPartialRelation(atom.getRelation())); | 713 | var constraint = getConstraint(atom); |
714 | var constraint = atom.isTransitiveClosure() ? getTransitiveWrapper(target) : target; | ||
715 | var argumentList = toArgumentList(atom.getArguments(), localScope, literals); | 714 | var argumentList = toArgumentList(atom.getArguments(), localScope, literals); |
716 | literals.add(extractedOuter.modality.wrapConstraint(constraint).call(CallPolarity.POSITIVE, argumentList)); | 715 | literals.add(extractedOuter.modality.wrapConstraint(constraint).call(CallPolarity.POSITIVE, argumentList)); |
717 | } | 716 | } |
@@ -724,8 +723,7 @@ public class ModelInitializer { | |||
724 | var negatedScope = extendScope(localScope, negationExpr.getImplicitVariables()); | 723 | var negatedScope = extendScope(localScope, negationExpr.getImplicitVariables()); |
725 | List<Variable> argumentList = toArgumentList(atom.getArguments(), negatedScope, literals); | 724 | List<Variable> argumentList = toArgumentList(atom.getArguments(), negatedScope, literals); |
726 | var innerModality = extractedInner.modality().merge(outerModality.negate()); | 725 | var innerModality = extractedInner.modality().merge(outerModality.negate()); |
727 | var target = innerModality.wrapPartialRelation(getPartialRelation(atom.getRelation())); | 726 | var constraint = getConstraint(atom); |
728 | Constraint constraint = atom.isTransitiveClosure() ? getTransitiveWrapper(target) : target; | ||
729 | literals.add(createNegationLiteral(innerModality, constraint, argumentList, localScope)); | 727 | literals.add(createNegationLiteral(innerModality, constraint, argumentList, localScope)); |
730 | } | 728 | } |
731 | case ComparisonExpr comparisonExpr -> { | 729 | case ComparisonExpr comparisonExpr -> { |
@@ -744,6 +742,12 @@ public class ModelInitializer { | |||
744 | } | 742 | } |
745 | } | 743 | } |
746 | 744 | ||
745 | private Constraint getConstraint(Atom atom) { | ||
746 | var target = getPartialRelation(atom.getRelation()); | ||
747 | var computedTarget = atom.isComputed() ? new ComputedConstraint(target) : target; | ||
748 | return atom.isTransitiveClosure() ? getTransitiveWrapper(computedTarget) : computedTarget; | ||
749 | } | ||
750 | |||
747 | private Constraint getTransitiveWrapper(Constraint target) { | 751 | private Constraint getTransitiveWrapper(Constraint target) { |
748 | return Query.of(target.name() + "#transitive", (builder, p1, p2) -> builder.clause( | 752 | return Query.of(target.name() + "#transitive", (builder, p1, p2) -> builder.clause( |
749 | target.callTransitive(p1, p2) | 753 | target.callTransitive(p1, p2) |
@@ -753,7 +757,7 @@ public class ModelInitializer { | |||
753 | private static Literal createNegationLiteral( | 757 | private static Literal createNegationLiteral( |
754 | ConcreteModality innerModality, Constraint constraint, List<Variable> argumentList, | 758 | ConcreteModality innerModality, Constraint constraint, List<Variable> argumentList, |
755 | Map<tools.refinery.language.model.problem.Variable, ? extends Variable> localScope) { | 759 | Map<tools.refinery.language.model.problem.Variable, ? extends Variable> localScope) { |
756 | if (innerModality.isModal()) { | 760 | if (innerModality.isSet()) { |
757 | boolean needsQuantification = false; | 761 | boolean needsQuantification = false; |
758 | var filteredArguments = new LinkedHashSet<Variable>(); | 762 | var filteredArguments = new LinkedHashSet<Variable>(); |
759 | for (var argument : argumentList) { | 763 | for (var argument : argumentList) { |
@@ -783,7 +787,7 @@ public class ModelInitializer { | |||
783 | private Literal createEquivalenceLiteral( | 787 | private Literal createEquivalenceLiteral( |
784 | ConcreteModality outerModality, boolean positive, Variable left, Variable right, | 788 | ConcreteModality outerModality, boolean positive, Variable left, Variable right, |
785 | Map<tools.refinery.language.model.problem.Variable, ? extends Variable> localScope) { | 789 | Map<tools.refinery.language.model.problem.Variable, ? extends Variable> localScope) { |
786 | if (!outerModality.isModal()) { | 790 | if (!outerModality.isSet()) { |
787 | return new EquivalenceLiteral(positive, left, right); | 791 | return new EquivalenceLiteral(positive, left, right); |
788 | } | 792 | } |
789 | if (positive) { | 793 | if (positive) { |
@@ -794,15 +798,12 @@ public class ModelInitializer { | |||
794 | localScope); | 798 | localScope); |
795 | } | 799 | } |
796 | 800 | ||
797 | private record ConcreteModality(@Nullable Derivation derivation, @Nullable Concreteness concreteness, | 801 | private record ConcreteModality(@Nullable Concreteness concreteness, @Nullable Modality modality) { |
798 | @Nullable Modality modality) { | 802 | public static final ConcreteModality NULL = new ConcreteModality((Concreteness) null, null); |
799 | public static final ConcreteModality NULL = new ConcreteModality(null, (Concreteness) null, null); | ||
800 | 803 | ||
801 | public ConcreteModality(Derivation derivation, | 804 | public ConcreteModality(tools.refinery.language.model.problem.Concreteness concreteness, |
802 | tools.refinery.language.model.problem.Concreteness concreteness, | ||
803 | tools.refinery.language.model.problem.Modality modality) { | 805 | tools.refinery.language.model.problem.Modality modality) { |
804 | this( | 806 | this( |
805 | derivation, | ||
806 | switch (concreteness) { | 807 | switch (concreteness) { |
807 | case PARTIAL -> Concreteness.PARTIAL; | 808 | case PARTIAL -> Concreteness.PARTIAL; |
808 | case CANDIDATE -> Concreteness.CANDIDATE; | 809 | case CANDIDATE -> Concreteness.CANDIDATE; |
@@ -817,37 +818,25 @@ public class ModelInitializer { | |||
817 | 818 | ||
818 | public ConcreteModality negate() { | 819 | public ConcreteModality negate() { |
819 | var negatedModality = modality == null ? null : modality.negate(); | 820 | var negatedModality = modality == null ? null : modality.negate(); |
820 | return new ConcreteModality(derivation, concreteness, negatedModality); | 821 | return new ConcreteModality(concreteness, negatedModality); |
821 | } | 822 | } |
822 | 823 | ||
823 | public ConcreteModality merge(ConcreteModality outer) { | 824 | public ConcreteModality merge(ConcreteModality outer) { |
824 | var mergedDerivation = derivation == null ? outer.derivation() : derivation; | ||
825 | var mergedConcreteness = concreteness == null ? outer.concreteness() : concreteness; | 825 | var mergedConcreteness = concreteness == null ? outer.concreteness() : concreteness; |
826 | var mergedModality = modality == null ? outer.modality() : modality; | 826 | var mergedModality = modality == null ? outer.modality() : modality; |
827 | return new ConcreteModality(mergedDerivation, mergedConcreteness, mergedModality); | 827 | return new ConcreteModality(mergedConcreteness, mergedModality); |
828 | } | 828 | } |
829 | 829 | ||
830 | public Constraint wrapConstraint(Constraint inner) { | 830 | public Constraint wrapConstraint(Constraint inner) { |
831 | if (modality != null) { | 831 | if (isSet()) { |
832 | if (concreteness == null) { | ||
833 | throw new IllegalStateException("Missing concreteness"); | ||
834 | } | ||
835 | return new ModalConstraint(modality, concreteness, inner); | 832 | return new ModalConstraint(modality, concreteness, inner); |
836 | } | 833 | } |
837 | return inner; | 834 | return inner; |
838 | } | 835 | } |
839 | 836 | ||
840 | public boolean isModal() { | 837 | public boolean isSet() { |
841 | return concreteness != null || modality != null; | 838 | return concreteness != null || modality != null; |
842 | } | 839 | } |
843 | |||
844 | public Constraint wrapPartialRelation(PartialRelation partialRelation) { | ||
845 | return isComputed() ? new ComputedConstraint(partialRelation) : partialRelation; | ||
846 | } | ||
847 | |||
848 | public boolean isComputed() { | ||
849 | return derivation == Derivation.COMPUTED; | ||
850 | } | ||
851 | } | 840 | } |
852 | 841 | ||
853 | private record ExtractedModalExpr(ConcreteModality modality, Expr body) { | 842 | private record ExtractedModalExpr(ConcreteModality modality, Expr body) { |
@@ -855,8 +844,8 @@ public class ModelInitializer { | |||
855 | 844 | ||
856 | private ExtractedModalExpr extractModalExpr(Expr expr) { | 845 | private ExtractedModalExpr extractModalExpr(Expr expr) { |
857 | if (expr instanceof ModalExpr modalExpr) { | 846 | if (expr instanceof ModalExpr modalExpr) { |
858 | return new ExtractedModalExpr(new ConcreteModality(modalExpr.getDerivation(), modalExpr.getConcreteness(), | 847 | return new ExtractedModalExpr(new ConcreteModality(modalExpr.getConcreteness(), modalExpr.getModality()), |
859 | modalExpr.getModality()), modalExpr.getBody()); | 848 | modalExpr.getBody()); |
860 | } | 849 | } |
861 | return new ExtractedModalExpr(ConcreteModality.NULL, expr); | 850 | return new ExtractedModalExpr(ConcreteModality.NULL, expr); |
862 | } | 851 | } |
@@ -1018,9 +1007,9 @@ public class ModelInitializer { | |||
1018 | parameterMap.put(problemParameter, parameter); | 1007 | parameterMap.put(problemParameter, parameter); |
1019 | var parameterType = problemParameter.getParameterType(); | 1008 | var parameterType = problemParameter.getParameterType(); |
1020 | if (parameterType != null) { | 1009 | if (parameterType != null) { |
1021 | var modality = new ConcreteModality(problemParameter.getDerivation(), | 1010 | var partialType = getPartialRelation(parameterType); |
1022 | problemParameter.getConcreteness(), problemParameter.getModality()); | 1011 | var modality = new ConcreteModality(problemParameter.getConcreteness(), |
1023 | var partialType = modality.wrapPartialRelation(getPartialRelation(parameterType)); | 1012 | problemParameter.getModality()); |
1024 | commonLiterals.add(modality.wrapConstraint(partialType).call(parameter)); | 1013 | commonLiterals.add(modality.wrapConstraint(partialType).call(parameter)); |
1025 | } | 1014 | } |
1026 | if (ruleDefinition.getKind() == RuleKind.DECISION && | 1015 | if (ruleDefinition.getKind() == RuleKind.DECISION && |