/// <summary> /// Validate the constraint expression. /// </summary> /// <param name="bundle">Model to validate.</param> /// <param name="theContext">Validation context to capture the errors.</param> /// <returns> /// Return true if the constraint is valid, return false if /// the constraint is not valid. /// </returns> public override bool Validate(BundleModel bundle, ModelValidationContext theContext) { if (Expression.Node == null) { return(false); } var variableCaptureVisitor = new ConstraintVariableReferenceCaptureVisitor(); Expression.Node.AcceptVisitor(variableCaptureVisitor); var variableReferences = variableCaptureVisitor.GetReferences(); foreach (var singletonVariableReference in variableReferences.SingletonVariableReferences) { if (bundle.Variables.FirstOrDefault(_ => _.Name.IsEqualTo(singletonVariableReference.VariableName)) == null) { theContext.AddError($"Missing singleton variable {singletonVariableReference.VariableName}"); return(false); } } foreach (var aggregateVariableReference in variableReferences.AggregateVariableReferences) { if (bundle.Aggregates.FirstOrDefault(_ => _.Name.IsEqualTo(aggregateVariableReference.VariableName)) == null) { theContext.AddError($"Missing aggregate variable {aggregateVariableReference.VariableName}"); return(false); } } return(true); }
/// <summary> /// Parse a raw domain expression. /// </summary> /// <param name="rawExpression">Raw domain expression.</param> /// <returns>Parse result.</returns> public ParseResult <SharedDomainExpressionNode> Parse(string rawExpression) { var language = new LanguageData(new SharedDomainGrammar()); var parser = new Parser(language); // 1. first pass: use the Irony grammar var parseTree = parser.Parse(rawExpression); #if false if (!parseTree.HasErrors()) { // 2. second pass: find non grammatical errors var sharedDomainExpressionNode = (SharedDomainExpressionNode)parseTree.Root.AstNode; var variableReferenceCaptureVisitor = new ConstraintVariableReferenceCaptureVisitor(); sharedDomainExpressionNode.AcceptVisitor(variableReferenceCaptureVisitor); } #endif return(CreateResultFrom(parseTree)); }