aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/construction/basiclinear/BasicLinearLayout.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/construction/basiclinear/BasicLinearLayout.java')
-rw-r--r--subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/construction/basiclinear/BasicLinearLayout.java171
1 files changed, 171 insertions, 0 deletions
diff --git a/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/construction/basiclinear/BasicLinearLayout.java b/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/construction/basiclinear/BasicLinearLayout.java
new file mode 100644
index 00000000..cfcc4ee0
--- /dev/null
+++ b/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/construction/basiclinear/BasicLinearLayout.java
@@ -0,0 +1,171 @@
1/*******************************************************************************
2 * Copyright (c) 2004-2010 Gabor Bergmann and Daniel Varro
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v. 2.0 which is available at
5 * http://www.eclipse.org/legal/epl-v20.html.
6 *
7 * SPDX-License-Identifier: EPL-2.0
8 *******************************************************************************/
9
10package tools.refinery.interpreter.rete.construction.basiclinear;
11
12import java.util.Arrays;
13import java.util.Collections;
14import java.util.Set;
15
16import org.apache.log4j.Logger;
17import tools.refinery.interpreter.matchers.backend.IQueryBackendHintProvider;
18import tools.refinery.interpreter.matchers.context.IQueryBackendContext;
19import tools.refinery.interpreter.matchers.context.IQueryMetaContext;
20import tools.refinery.interpreter.matchers.planning.IQueryPlannerStrategy;
21import tools.refinery.interpreter.matchers.planning.SubPlan;
22import tools.refinery.interpreter.matchers.planning.SubPlanFactory;
23import tools.refinery.interpreter.matchers.planning.helpers.BuildHelper;
24import tools.refinery.interpreter.matchers.planning.operations.PApply;
25import tools.refinery.interpreter.matchers.planning.operations.PProject;
26import tools.refinery.interpreter.matchers.planning.operations.PStart;
27import tools.refinery.interpreter.matchers.psystem.DeferredPConstraint;
28import tools.refinery.interpreter.matchers.psystem.PBody;
29import tools.refinery.interpreter.matchers.psystem.PConstraint;
30import tools.refinery.interpreter.matchers.psystem.PVariable;
31import tools.refinery.interpreter.matchers.psystem.VariableDeferredPConstraint;
32import tools.refinery.interpreter.matchers.psystem.basicdeferred.Equality;
33import tools.refinery.interpreter.matchers.psystem.basicdeferred.ExportedParameter;
34import tools.refinery.interpreter.matchers.psystem.basicdeferred.ExpressionEvaluation;
35import tools.refinery.interpreter.matchers.psystem.queries.PQuery;
36import tools.refinery.interpreter.matchers.util.CollectionsFactory;
37import tools.refinery.interpreter.rete.construction.RetePatternBuildException;
38
39/**
40 * Basic layout that builds a linear RETE net based on a heuristic ordering of constraints.
41 *
42 * @author Gabor Bergmann
43 *
44 */
45public class BasicLinearLayout implements IQueryPlannerStrategy {
46
47 //SubPlanProcessor planProcessor = new SubPlanProcessor();
48
49 private IQueryBackendHintProvider hintProvider;
50 private IQueryBackendContext bContext;
51 /**
52 * @param bContext
53 * @since 1.5
54 */
55 public BasicLinearLayout(IQueryBackendContext bContext) {
56 this.bContext = bContext;
57 this.hintProvider = bContext.getHintProvider();
58 }
59
60 @Override
61 public SubPlan plan(final PBody pSystem, Logger logger, IQueryMetaContext context) {
62 SubPlanFactory planFactory = new SubPlanFactory(pSystem);
63 PQuery query = pSystem.getPattern();
64 //planProcessor.setCompiler(compiler);
65 try {
66 logger.debug(String.format(
67 "%s: patternbody build started for %s",
68 getClass().getSimpleName(),
69 query.getFullyQualifiedName()));
70
71 // STARTING THE LINE
72 SubPlan plan = planFactory.createSubPlan(new PStart());
73
74 Set<PConstraint> pQueue = CollectionsFactory.createSet(pSystem.getConstraints());
75
76 // MAIN LOOP
77 while (!pQueue.isEmpty()) {
78 PConstraint pConstraint = Collections.min(pQueue,
79 new OrderingHeuristics(plan, context)); // pQueue.iterator().next();
80 pQueue.remove(pConstraint);
81
82 // if we have no better option than an unready deferred constraint, raise error
83 if (pConstraint instanceof DeferredPConstraint) {
84 final DeferredPConstraint deferred = (DeferredPConstraint) pConstraint;
85 if (!deferred.isReadyAt(plan, context)) {
86 raiseForeverDeferredError(deferred, plan, context);
87 }
88 }
89 // TODO integrate the check above in SubPlan / POperation??
90
91 // replace incumbent plan with its child
92 plan = planFactory.createSubPlan(new PApply(pConstraint), plan);
93 }
94
95 // PROJECT TO PARAMETERS
96 SubPlan finalPlan = planFactory.createSubPlan(new PProject(pSystem.getSymbolicParameterVariables()), plan);
97
98 // FINAL CHECK, whether all exported variables are present + all constraint satisfied
99 BuildHelper.finalCheck(pSystem, finalPlan, context);
100 // TODO integrate the check above in SubPlan / POperation
101
102 logger.debug(String.format(
103 "%s: patternbody query plan concluded for %s as: %s",
104 getClass().getSimpleName(),
105 query.getFullyQualifiedName(),
106 finalPlan.toLongString()));
107
108 return finalPlan;
109
110 } catch (RetePatternBuildException ex) {
111 ex.setPatternDescription(query);
112 throw ex;
113 }
114 }
115
116 /**
117 * Called when the constraint is not ready, but cannot be deferred further.
118 *
119 * @param plan
120 * @throws RetePatternBuildException
121 * to indicate the error in detail.
122 */
123 private void raiseForeverDeferredError(DeferredPConstraint constraint, SubPlan plan, IQueryMetaContext context) {
124 if (constraint instanceof Equality) {
125 raiseForeverDeferredError((Equality)constraint, plan, context);
126 } else if (constraint instanceof ExportedParameter) {
127 raiseForeverDeferredError((ExportedParameter)constraint, plan, context);
128 } else if (constraint instanceof ExpressionEvaluation) {
129 raiseForeverDeferredError((ExpressionEvaluation)constraint, plan, context);
130 } else if (constraint instanceof VariableDeferredPConstraint) {
131 raiseForeverDeferredError(constraint, plan, context);
132 }
133 }
134
135 private void raiseForeverDeferredError(Equality constraint, SubPlan plan, IQueryMetaContext context) {
136 String[] args = { constraint.getWho().toString(), constraint.getWithWhom().toString() };
137 String msg = "Cannot express equality of variables {1} and {2} if neither of them is deducable.";
138 String shortMsg = "Equality between undeducible variables.";
139 throw new RetePatternBuildException(msg, args, shortMsg, null);
140 }
141 private void raiseForeverDeferredError(ExportedParameter constraint, SubPlan plan, IQueryMetaContext context) {
142 String[] args = { constraint.getParameterName() };
143 String msg = "Pattern Graph Search terminated incompletely: "
144 + "exported pattern variable {1} could not be determined based on the pattern constraints. "
145 + "HINT: certain constructs (e.g. negative patterns or check expressions) cannot output symbolic parameters.";
146 String shortMsg = "Could not deduce value of parameter";
147 throw new RetePatternBuildException(msg, args, shortMsg, null);
148 }
149 private void raiseForeverDeferredError(ExpressionEvaluation constraint, SubPlan plan, IQueryMetaContext context) {
150 if (constraint.checkTypeSafety(plan, context) == null) {
151 raiseForeverDeferredError(constraint, plan);
152 } else {
153 String[] args = { toString(), constraint.checkTypeSafety(plan, context).toString() };
154 String msg = "The checking of pattern constraint {1} cannot be deferred further, but variable {2} is still not type safe. "
155 + "HINT: the incremental matcher is not an equation solver, please make sure that all variable values are deducible.";
156 String shortMsg = "Could not check all constraints due to undeducible type restrictions";
157 throw new RetePatternBuildException(msg, args, shortMsg, null);
158 }
159 }
160 private void raiseForeverDeferredError(VariableDeferredPConstraint constraint, SubPlan plan) {
161 Set<PVariable> missing = CollectionsFactory.createSet(constraint.getDeferringVariables());//new HashSet<PVariable>(getDeferringVariables());
162 missing.removeAll(plan.getVisibleVariables());
163 String[] args = { toString(), Arrays.toString(missing.toArray()) };
164 String msg = "The checking of pattern constraint {1} requires the values of variables {2}, but it cannot be deferred further. "
165 + "HINT: the incremental matcher is not an equation solver, please make sure that all variable values are deducible.";
166 String shortMsg = "Could not check all constraints due to undeducible variables";
167 throw new RetePatternBuildException(msg, args, shortMsg, null);
168 }
169
170
171}