import java.util.ArrayList;
import org.cojen.classfile.TypeDesc;
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
import org.simantics.scl.compiler.compilation.CompilationContext;
import org.simantics.scl.compiler.constants.BooleanConstant;
import org.simantics.scl.compiler.constants.Constant;
public CHRRuleset() {
initConstraint = new CHRConstraint(Locations.NO_LOCATION, INIT_CONSTRAINT, Type.EMPTY_ARRAY);
- constraints.add(initConstraint);
+ addConstraint(initConstraint);
+ }
+
+ public void addConstraint(CHRConstraint constraint) {
+ constraints.add(constraint);
+ constraint.setParent(this);
}
public void resolve(TranslationContext context) {
}
}
- public void compile(SimplificationContext context) {
- initializeCodeGeneration(context.getCompilationContext());
- if(extensible)
- applyExtensibleDefaults();
- UsageAnalysis.analyzeUsage(this);
- for(CHRRule rule : rules) {
- rule.compile(context.getCompilationContext(), initConstraint);
- for(CHRSearchPlan plan : rule.plans) {
- CHRConstraint constraint = plan.constraint;
- if(!activeConstraintGGInfo.containsKey(constraint)) {
- activeConstraintGGInfo.put(constraint, new CHRConstraintGGInfo(rule.priority));
- IncludeStatement include = constraintSourceMap.get(constraint);
- if(include != null) {
- ArrayList<CHRConstraint> list = inverseActiveConstraintSourceMap.get(include);
- if(list == null) {
- list = new ArrayList<CHRConstraint>(4);
- inverseActiveConstraintSourceMap.put(include, list);
+ private void compile(SimplificationContext context) {
+ try {
+ initializeCodeGeneration(context.getCompilationContext());
+ if(extensible)
+ applyExtensibleDefaults();
+ UsageAnalysis.analyzeUsage(this);
+ for(CHRRule rule : rules) {
+ rule.compile(context.getCompilationContext(), initConstraint);
+ for(CHRSearchPlan plan : rule.plans) {
+ CHRConstraint constraint = plan.constraint;
+ if(!activeConstraintGGInfo.containsKey(constraint)) {
+ activeConstraintGGInfo.put(constraint, new CHRConstraintGGInfo(rule.priority));
+ IncludeStatement include = constraintSourceMap.get(constraint);
+ if(include != null) {
+ ArrayList<CHRConstraint> list = inverseActiveConstraintSourceMap.get(include);
+ if(list == null) {
+ list = new ArrayList<CHRConstraint>(4);
+ inverseActiveConstraintSourceMap.put(include, list);
+ }
+ list.add(constraint);
}
- list.add(constraint);
}
}
}
- }
- // remove init constraint if it is not useful
- if(getMinimumPriority(initConstraint) == Integer.MAX_VALUE) {
- constraints.remove(0);
- initConstraint = null;
+ // remove init constraint if it is not useful
+ if(getMinimumPriority(initConstraint) == Integer.MAX_VALUE) {
+ constraints.remove(0);
+ initConstraint = null;
+ }
+ } catch(Exception e) {
+ throw InternalCompilerError.injectLocation(location, e);
}
}
}
}
- public void simplify(SimplificationContext context) {
- for(IncludeStatement include : includes)
+ private void simplify(SimplificationContext context) {
+ for(IncludeStatement include : includes) {
+ //include.ruleset.simplifyAndCompileIfNeeded(context);
include.value = include.value.simplify(context);
+ }
for(CHRRule rule : rules)
rule.simplify(context);
}
+ public void simplifyAndCompileIfNeeded(SimplificationContext context) {
+ if(runtimeRulesetTypeDesc == null) {
+ simplify(context);
+ compile(context);
+ }
+ }
+
public void setRulesetType(TCon type, String className) {
this.runtimeRulesetType = type;
this.runtimeRulesetClassName = className;