diff options
Diffstat (limited to 'subprojects/generator')
6 files changed, 103 insertions, 9 deletions
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefinery.java b/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefinery.java index 81bf712a..56c7a23b 100644 --- a/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefinery.java +++ b/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefinery.java | |||
@@ -5,22 +5,31 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.generator; | 6 | package tools.refinery.generator; |
7 | 7 | ||
8 | import tools.refinery.generator.impl.ProblemTraceImpl; | ||
9 | import tools.refinery.language.semantics.metadata.NodeMetadata; | ||
8 | import tools.refinery.store.model.Model; | 10 | import tools.refinery.store.model.Model; |
9 | import tools.refinery.store.model.ModelStore; | 11 | import tools.refinery.store.model.ModelStore; |
10 | import tools.refinery.store.reasoning.ReasoningAdapter; | 12 | import tools.refinery.store.reasoning.ReasoningAdapter; |
11 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | 13 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; |
12 | import tools.refinery.store.reasoning.seed.ModelSeed; | 14 | import tools.refinery.store.reasoning.seed.ModelSeed; |
15 | import tools.refinery.store.reasoning.translator.TranslationException; | ||
13 | 16 | ||
14 | public class AbstractRefinery { | 17 | import java.util.List; |
18 | |||
19 | public abstract class AbstractRefinery { | ||
15 | protected final ProblemTrace problemTrace; | 20 | protected final ProblemTrace problemTrace; |
16 | protected final ModelStore store; | 21 | protected final ModelStore store; |
17 | protected final Model model; | 22 | protected final Model model; |
18 | protected final ReasoningAdapter reasoningAdapter; | 23 | protected final ReasoningAdapter reasoningAdapter; |
19 | 24 | ||
20 | public AbstractRefinery(ProblemTrace problemTrace, ModelStore store, ModelSeed modelSeed) { | 25 | protected AbstractRefinery(ProblemTrace problemTrace, ModelStore store, ModelSeed modelSeed) { |
21 | this.problemTrace = problemTrace; | 26 | this.problemTrace = problemTrace; |
22 | this.store = store; | 27 | this.store = store; |
23 | model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed); | 28 | try { |
29 | model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed); | ||
30 | } catch (TranslationException e) { | ||
31 | throw ProblemTraceImpl.wrapException(problemTrace, e); | ||
32 | } | ||
24 | reasoningAdapter = model.getAdapter(ReasoningAdapter.class); | 33 | reasoningAdapter = model.getAdapter(ReasoningAdapter.class); |
25 | } | 34 | } |
26 | 35 | ||
@@ -35,4 +44,11 @@ public class AbstractRefinery { | |||
35 | public Model getModel() { | 44 | public Model getModel() { |
36 | return model; | 45 | return model; |
37 | } | 46 | } |
47 | |||
48 | public List<NodeMetadata> getNodesMetadata() { | ||
49 | int nodeCount = reasoningAdapter.getNodeCount(); | ||
50 | return problemTrace.getNodesMetadata(nodeCount, isPreserveNewNodes()); | ||
51 | } | ||
52 | |||
53 | protected abstract boolean isPreserveNewNodes(); | ||
38 | } | 54 | } |
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefineryBuilder.java b/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefineryBuilder.java index b60a3890..d4649051 100644 --- a/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefineryBuilder.java +++ b/subprojects/generator/src/main/java/tools/refinery/generator/AbstractRefineryBuilder.java | |||
@@ -21,6 +21,7 @@ import tools.refinery.language.semantics.model.ModelInitializer; | |||
21 | import tools.refinery.store.query.ModelQueryBuilder; | 21 | import tools.refinery.store.query.ModelQueryBuilder; |
22 | import tools.refinery.store.query.interpreter.QueryInterpreterAdapter; | 22 | import tools.refinery.store.query.interpreter.QueryInterpreterAdapter; |
23 | import tools.refinery.store.reasoning.literal.Concreteness; | 23 | import tools.refinery.store.reasoning.literal.Concreteness; |
24 | import tools.refinery.store.reasoning.translator.TranslationException; | ||
24 | import tools.refinery.store.util.CancellationToken; | 25 | import tools.refinery.store.util.CancellationToken; |
25 | 26 | ||
26 | import java.io.File; | 27 | import java.io.File; |
@@ -29,6 +30,8 @@ import java.io.InputStream; | |||
29 | import java.util.Collection; | 30 | import java.util.Collection; |
30 | import java.util.Map; | 31 | import java.util.Map; |
31 | 32 | ||
33 | // This class is used as a fluent builder. | ||
34 | @SuppressWarnings("UnusedReturnValue") | ||
32 | public abstract class AbstractRefineryBuilder<T> { | 35 | public abstract class AbstractRefineryBuilder<T> { |
33 | @Inject | 36 | @Inject |
34 | private Provider<XtextResourceSet> resourceSetProvider; | 37 | private Provider<XtextResourceSet> resourceSetProvider; |
@@ -113,8 +116,12 @@ public abstract class AbstractRefineryBuilder<T> { | |||
113 | if (problemLoaded) { | 116 | if (problemLoaded) { |
114 | throw new IllegalStateException("Problem was already set"); | 117 | throw new IllegalStateException("Problem was already set"); |
115 | } | 118 | } |
116 | initializer.readProblem(problem); | ||
117 | problemTrace.setInitializer(initializer); | 119 | problemTrace.setInitializer(initializer); |
120 | try { | ||
121 | initializer.readProblem(problem); | ||
122 | } catch (TranslationException e) { | ||
123 | throw ProblemTraceImpl.wrapException(problemTrace, e); | ||
124 | } | ||
118 | problemLoaded = true; | 125 | problemLoaded = true; |
119 | return self(); | 126 | return self(); |
120 | } | 127 | } |
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/ModelGenerator.java b/subprojects/generator/src/main/java/tools/refinery/generator/ModelGenerator.java index e5115352..78c807d1 100644 --- a/subprojects/generator/src/main/java/tools/refinery/generator/ModelGenerator.java +++ b/subprojects/generator/src/main/java/tools/refinery/generator/ModelGenerator.java | |||
@@ -29,6 +29,11 @@ public class ModelGenerator extends AbstractRefinery { | |||
29 | initialVersion = model.commit(); | 29 | initialVersion = model.commit(); |
30 | } | 30 | } |
31 | 31 | ||
32 | @Override | ||
33 | protected boolean isPreserveNewNodes() { | ||
34 | return false; | ||
35 | } | ||
36 | |||
32 | public int getRandomSeed() { | 37 | public int getRandomSeed() { |
33 | return randomSeed; | 38 | return randomSeed; |
34 | } | 39 | } |
@@ -50,6 +55,8 @@ public class ModelGenerator extends AbstractRefinery { | |||
50 | .toList(); | 55 | .toList(); |
51 | } | 56 | } |
52 | 57 | ||
58 | // This method only makes sense if it returns {@code true} on success. | ||
59 | @SuppressWarnings("BooleanMethodIsAlwaysInverted") | ||
53 | public boolean tryRun() { | 60 | public boolean tryRun() { |
54 | var iterator = run(1).iterator(); | 61 | var iterator = run(1).iterator(); |
55 | if (!iterator.hasNext()) { | 62 | if (!iterator.hasNext()) { |
@@ -59,6 +66,12 @@ public class ModelGenerator extends AbstractRefinery { | |||
59 | return true; | 66 | return true; |
60 | } | 67 | } |
61 | 68 | ||
69 | public void run() { | ||
70 | if (!tryRun()) { | ||
71 | throw new UnsatisfiableProblemException(); | ||
72 | } | ||
73 | } | ||
74 | |||
62 | public <A, C> PartialInterpretation<A, C> getCandidateInterpretation(PartialSymbol<A, C> partialSymbol) { | 75 | public <A, C> PartialInterpretation<A, C> getCandidateInterpretation(PartialSymbol<A, C> partialSymbol) { |
63 | return reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, partialSymbol); | 76 | return reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, partialSymbol); |
64 | } | 77 | } |
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/ModelSemantics.java b/subprojects/generator/src/main/java/tools/refinery/generator/ModelSemantics.java index 8bac1910..661bc97c 100644 --- a/subprojects/generator/src/main/java/tools/refinery/generator/ModelSemantics.java +++ b/subprojects/generator/src/main/java/tools/refinery/generator/ModelSemantics.java | |||
@@ -19,6 +19,11 @@ public class ModelSemantics extends AbstractRefinery { | |||
19 | super(problemTrace, store, modelSeed); | 19 | super(problemTrace, store, modelSeed); |
20 | } | 20 | } |
21 | 21 | ||
22 | @Override | ||
23 | protected boolean isPreserveNewNodes() { | ||
24 | return true; | ||
25 | } | ||
26 | |||
22 | public <A, C> PartialInterpretation<A, C> getPartialInterpretation(PartialSymbol<A, C> partialSymbol) { | 27 | public <A, C> PartialInterpretation<A, C> getPartialInterpretation(PartialSymbol<A, C> partialSymbol) { |
23 | return reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, partialSymbol); | 28 | return reasoningAdapter.getPartialInterpretation(Concreteness.PARTIAL, partialSymbol); |
24 | } | 29 | } |
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/ProblemTrace.java b/subprojects/generator/src/main/java/tools/refinery/generator/ProblemTrace.java index 7be2dc00..f7963c58 100644 --- a/subprojects/generator/src/main/java/tools/refinery/generator/ProblemTrace.java +++ b/subprojects/generator/src/main/java/tools/refinery/generator/ProblemTrace.java | |||
@@ -8,8 +8,12 @@ package tools.refinery.generator; | |||
8 | import org.eclipse.xtext.naming.QualifiedName; | 8 | import org.eclipse.xtext.naming.QualifiedName; |
9 | import tools.refinery.language.model.problem.Problem; | 9 | import tools.refinery.language.model.problem.Problem; |
10 | import tools.refinery.language.model.problem.Relation; | 10 | import tools.refinery.language.model.problem.Relation; |
11 | import tools.refinery.language.semantics.metadata.NodeMetadata; | ||
12 | import tools.refinery.language.semantics.metadata.RelationMetadata; | ||
13 | import tools.refinery.store.reasoning.representation.AnyPartialSymbol; | ||
11 | import tools.refinery.store.reasoning.representation.PartialRelation; | 14 | import tools.refinery.store.reasoning.representation.PartialRelation; |
12 | 15 | ||
16 | import java.util.List; | ||
13 | import java.util.Map; | 17 | import java.util.Map; |
14 | 18 | ||
15 | public interface ProblemTrace { | 19 | public interface ProblemTrace { |
@@ -17,9 +21,15 @@ public interface ProblemTrace { | |||
17 | 21 | ||
18 | Map<Relation, PartialRelation> getRelationTrace(); | 22 | Map<Relation, PartialRelation> getRelationTrace(); |
19 | 23 | ||
24 | Relation getInverseTrace(AnyPartialSymbol partialSymbol); | ||
25 | |||
20 | PartialRelation getPartialRelation(Relation relation); | 26 | PartialRelation getPartialRelation(Relation relation); |
21 | 27 | ||
22 | PartialRelation getPartialRelation(QualifiedName qualifiedName); | 28 | PartialRelation getPartialRelation(QualifiedName qualifiedName); |
23 | 29 | ||
24 | PartialRelation getPartialRelation(String qualifiedName); | 30 | PartialRelation getPartialRelation(String qualifiedName); |
31 | |||
32 | List<RelationMetadata> getRelationsMetadata(); | ||
33 | |||
34 | List<NodeMetadata> getNodesMetadata(int nodeCount, boolean preserveNewNodes); | ||
25 | } | 35 | } |
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/impl/ProblemTraceImpl.java b/subprojects/generator/src/main/java/tools/refinery/generator/impl/ProblemTraceImpl.java index 109e34af..5671d3f8 100644 --- a/subprojects/generator/src/main/java/tools/refinery/generator/impl/ProblemTraceImpl.java +++ b/subprojects/generator/src/main/java/tools/refinery/generator/impl/ProblemTraceImpl.java | |||
@@ -6,6 +6,7 @@ | |||
6 | package tools.refinery.generator.impl; | 6 | package tools.refinery.generator.impl; |
7 | 7 | ||
8 | import com.google.inject.Inject; | 8 | import com.google.inject.Inject; |
9 | import com.google.inject.Provider; | ||
9 | import org.eclipse.emf.ecore.util.EcoreUtil; | 10 | import org.eclipse.emf.ecore.util.EcoreUtil; |
10 | import org.eclipse.xtext.naming.IQualifiedNameConverter; | 11 | import org.eclipse.xtext.naming.IQualifiedNameConverter; |
11 | import org.eclipse.xtext.naming.IQualifiedNameProvider; | 12 | import org.eclipse.xtext.naming.IQualifiedNameProvider; |
@@ -15,10 +16,17 @@ import tools.refinery.generator.ProblemTrace; | |||
15 | import tools.refinery.language.model.problem.Problem; | 16 | import tools.refinery.language.model.problem.Problem; |
16 | import tools.refinery.language.model.problem.ProblemPackage; | 17 | import tools.refinery.language.model.problem.ProblemPackage; |
17 | import tools.refinery.language.model.problem.Relation; | 18 | import tools.refinery.language.model.problem.Relation; |
19 | import tools.refinery.language.semantics.metadata.MetadataCreator; | ||
20 | import tools.refinery.language.semantics.metadata.NodeMetadata; | ||
21 | import tools.refinery.language.semantics.metadata.RelationMetadata; | ||
18 | import tools.refinery.language.semantics.model.ModelInitializer; | 22 | import tools.refinery.language.semantics.model.ModelInitializer; |
23 | import tools.refinery.language.semantics.model.TracedException; | ||
24 | import tools.refinery.store.reasoning.representation.AnyPartialSymbol; | ||
19 | import tools.refinery.store.reasoning.representation.PartialRelation; | 25 | import tools.refinery.store.reasoning.representation.PartialRelation; |
26 | import tools.refinery.store.reasoning.translator.TranslationException; | ||
20 | 27 | ||
21 | import java.util.Collections; | 28 | import java.util.Collections; |
29 | import java.util.List; | ||
22 | import java.util.Map; | 30 | import java.util.Map; |
23 | 31 | ||
24 | public final class ProblemTraceImpl implements ProblemTrace { | 32 | public final class ProblemTraceImpl implements ProblemTrace { |
@@ -31,22 +39,31 @@ public final class ProblemTraceImpl implements ProblemTrace { | |||
31 | @Inject | 39 | @Inject |
32 | private IScopeProvider scopeProvider; | 40 | private IScopeProvider scopeProvider; |
33 | 41 | ||
34 | private Problem problem; | 42 | @Inject |
43 | private Provider<MetadataCreator> metadataCreatorProvider; | ||
44 | |||
45 | private ModelInitializer initializer; | ||
35 | private Map<Relation, PartialRelation> relationTrace; | 46 | private Map<Relation, PartialRelation> relationTrace; |
47 | private MetadataCreator metadataCreator; | ||
36 | 48 | ||
37 | public void setInitializer(ModelInitializer initializer) { | 49 | public void setInitializer(ModelInitializer initializer) { |
38 | problem = initializer.getProblem(); | 50 | this.initializer = initializer; |
39 | relationTrace = Collections.unmodifiableMap(initializer.getRelationTrace()); | 51 | relationTrace = Collections.unmodifiableMap(initializer.getRelationTrace()); |
40 | } | 52 | } |
41 | 53 | ||
42 | public Problem getProblem() { | 54 | public Problem getProblem() { |
43 | return problem; | 55 | return initializer.getProblem(); |
44 | } | 56 | } |
45 | 57 | ||
46 | public Map<Relation, PartialRelation> getRelationTrace() { | 58 | public Map<Relation, PartialRelation> getRelationTrace() { |
47 | return relationTrace; | 59 | return relationTrace; |
48 | } | 60 | } |
49 | 61 | ||
62 | @Override | ||
63 | public Relation getInverseTrace(AnyPartialSymbol partialSymbol) { | ||
64 | return initializer.getInverseTrace(partialSymbol); | ||
65 | } | ||
66 | |||
50 | public PartialRelation getPartialRelation(Relation relation) { | 67 | public PartialRelation getPartialRelation(Relation relation) { |
51 | var partialRelation = relationTrace.get(relation); | 68 | var partialRelation = relationTrace.get(relation); |
52 | if (partialRelation == null) { | 69 | if (partialRelation == null) { |
@@ -58,7 +75,7 @@ public final class ProblemTraceImpl implements ProblemTrace { | |||
58 | } | 75 | } |
59 | 76 | ||
60 | public PartialRelation getPartialRelation(QualifiedName qualifiedName) { | 77 | public PartialRelation getPartialRelation(QualifiedName qualifiedName) { |
61 | var scope = scopeProvider.getScope(problem, ProblemPackage.Literals.ASSERTION__RELATION); | 78 | var scope = scopeProvider.getScope(getProblem(), ProblemPackage.Literals.ASSERTION__RELATION); |
62 | var iterator = scope.getElements(qualifiedName).iterator(); | 79 | var iterator = scope.getElements(qualifiedName).iterator(); |
63 | if (!iterator.hasNext()) { | 80 | if (!iterator.hasNext()) { |
64 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); | 81 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); |
@@ -69,7 +86,7 @@ public final class ProblemTraceImpl implements ProblemTrace { | |||
69 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); | 86 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); |
70 | throw new IllegalArgumentException("Ambiguous relation: " + qualifiedNameString); | 87 | throw new IllegalArgumentException("Ambiguous relation: " + qualifiedNameString); |
71 | } | 88 | } |
72 | var eObject = EcoreUtil.resolve(eObjectDescription.getEObjectOrProxy(), problem); | 89 | var eObject = EcoreUtil.resolve(eObjectDescription.getEObjectOrProxy(), getProblem()); |
73 | if (!(eObject instanceof Relation relation)) { | 90 | if (!(eObject instanceof Relation relation)) { |
74 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); | 91 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); |
75 | throw new IllegalArgumentException("Not a relation: " + qualifiedNameString); | 92 | throw new IllegalArgumentException("Not a relation: " + qualifiedNameString); |
@@ -81,4 +98,30 @@ public final class ProblemTraceImpl implements ProblemTrace { | |||
81 | var convertedName = qualifiedNameConverter.toQualifiedName(qualifiedName); | 98 | var convertedName = qualifiedNameConverter.toQualifiedName(qualifiedName); |
82 | return getPartialRelation(convertedName); | 99 | return getPartialRelation(convertedName); |
83 | } | 100 | } |
101 | |||
102 | @Override | ||
103 | public List<RelationMetadata> getRelationsMetadata() { | ||
104 | return getMetadataCreator().getRelationsMetadata(); | ||
105 | } | ||
106 | |||
107 | @Override | ||
108 | public List<NodeMetadata> getNodesMetadata(int nodeCount, boolean preserveNewNodes) { | ||
109 | return getMetadataCreator().getNodesMetadata(nodeCount, preserveNewNodes); | ||
110 | } | ||
111 | |||
112 | private MetadataCreator getMetadataCreator() { | ||
113 | if (metadataCreator == null) { | ||
114 | metadataCreator = metadataCreatorProvider.get(); | ||
115 | metadataCreator.setInitializer(initializer); | ||
116 | } | ||
117 | return metadataCreator; | ||
118 | } | ||
119 | |||
120 | public static RuntimeException wrapException(ProblemTrace trace, TranslationException translationException) { | ||
121 | var source = trace.getInverseTrace(translationException.getPartialSymbol()); | ||
122 | if (source == null) { | ||
123 | return translationException; | ||
124 | } | ||
125 | return new TracedException(source, translationException); | ||
126 | } | ||
84 | } | 127 | } |