public void Handle(KAOSCoreElement element, ParsedElement attribute) { foreach (var builder in attributeBuilders) { var genericArguments = builder.GetType().BaseType.GetGenericArguments(); if (genericArguments[0].IsAssignableFrom(element.GetType()) && genericArguments[1].IsAssignableFrom(attribute.GetType())) { var method = builder.GetType().GetMethod("Handle", new[] { genericArguments[0], genericArguments[1], typeof(KAOSModel) }); if (method == null) { throw new Exception("Cannot find method Handle with generic parameters."); } try { method.Invoke(builder, new object[] { element, attribute, model }); } catch (TargetInvocationException e) { Console.WriteLine(e.InnerException); throw e.InnerException; } } } }
public Formula BuildFormula(ParsedElement value, Dictionary<string, Entity> declaredVariables) { if (value == null) throw new ArgumentNullException ("value"); if (value.GetType() == typeof (ParsedForallExpression)) { var a = new Forall (); var d2 = new Dictionary<string, KAOSTools.MetaModel.Entity> (declaredVariables); foreach (var arg in (value as ParsedForallExpression).arguments) { var name = arg.VariableName; var type = GetOrCreateEntity (arg.Type); if (declaredVariables.ContainsKey(name)) { throw new CompilationException (string.Format ("'{0}' is already defined", name)); } a.Declarations.Add (new KAOSTools.MetaModel.ArgumentDeclaration() { Name = name, Type = type }); d2.Add (name, type); } a.Enclosed = BuildFormula ((value as ParsedForallExpression).Enclosed, d2); return a; } else if (value.GetType() == typeof (ParsedExistsExpression)) { var a = new Exists (); var d2 = new Dictionary<string, KAOSTools.MetaModel.Entity> (declaredVariables); foreach (var arg in (value as ParsedExistsExpression).arguments) { var name = arg.VariableName; var type = GetOrCreateEntity (arg.Type); if (declaredVariables.ContainsKey(name)) { throw new CompilationException (string.Format ("'{0}' is already defined", name)); } a.Declarations.Add (new KAOSTools.MetaModel.ArgumentDeclaration() { Name = name, Type = type }); d2.Add (name, type); } a.Enclosed = BuildFormula ((value as ParsedExistsExpression).Enclosed, d2); return a; } else if (value.GetType() == typeof (ParsedStrongImplyExpression)) { return new StrongImply () { Left = BuildFormula ((value as ParsedStrongImplyExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedStrongImplyExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedImplyExpression)) { return new Imply () { Left = BuildFormula ((value as ParsedImplyExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedImplyExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedEquivalenceExpression)) { return new Equivalence () { Left = BuildFormula ((value as ParsedEquivalenceExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedEquivalenceExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedUntilExpression)) { return new Until () { Left = BuildFormula ((value as ParsedUntilExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedUntilExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedUnlessExpression)) { return new Unless () { Left = BuildFormula ((value as ParsedUnlessExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedUnlessExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedReleaseExpression)) { return new Release () { Left = BuildFormula ((value as ParsedReleaseExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedReleaseExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedAndExpression)) { return new And () { Left = BuildFormula ((value as ParsedAndExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedAndExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedOrExpression)) { return new Or () { Left = BuildFormula ((value as ParsedOrExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedOrExpression).Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedNotExpression)) { return new Not () { Enclosed = BuildFormula ((value as ParsedNotExpression).Enclosed, declaredVariables) }; } else if (value.GetType() == typeof (ParsedNextExpression)) { return new Next () { Enclosed = BuildFormula ((value as ParsedNextExpression).Enclosed, declaredVariables) }; } else if (value.GetType() == typeof (ParsedEventuallyExpression)) { return new Eventually () { Enclosed = BuildFormula ((value as ParsedEventuallyExpression).Enclosed, declaredVariables), TimeBound = BuildTimeBound ((value as ParsedEventuallyExpression).TimeBound) }; } else if (value.GetType() == typeof (ParsedEventuallyBeforeExpression)) { return new EventuallyBefore () { Left = BuildFormula ((value as ParsedEventuallyBeforeExpression).Left, declaredVariables), Right = BuildFormula ((value as ParsedEventuallyBeforeExpression).Right, declaredVariables), TimeBound = BuildTimeBound ((value as ParsedEventuallyBeforeExpression).TimeBound) }; } else if (value.GetType() == typeof (ParsedGloballyExpression)) { return new Globally () { Enclosed = BuildFormula ((value as ParsedGloballyExpression).Enclosed, declaredVariables), TimeBound = BuildTimeBound ((value as ParsedGloballyExpression).TimeBound) }; } else if (value.GetType() == typeof (ParsedPredicateReferenceExpression)) { var prel = value as ParsedPredicateReferenceExpression; // Check if arguments are all defined foreach (var arg in prel.ActualArguments) { if (!declaredVariables.ContainsKey (arg)) { throw new CompilationException (string.Format("'{0}' is not declared ({1}:{2},{3})", arg, prel.Filename, prel.Line, prel.Col)); } } return new PredicateReference () { Predicate = GetOrCreatePredicate (prel, declaredVariables), ActualArguments = prel.ActualArguments }; } else if (value.GetType() == typeof (ParsedInRelationExpression)) { var prel = value as ParsedInRelationExpression; foreach (var arg in prel.Variables) { if (!declaredVariables.ContainsKey (arg)) { throw new CompilationException (string.Format("'{0}' is not declared", arg)); } } return new RelationReference () { Relation = GetOrCreateRelation (value as ParsedInRelationExpression, declaredVariables), ActualArguments = prel.Variables }; } else if (value.GetType() == typeof (ParsedAttributeReferenceExpression)) { var pref = value as ParsedAttributeReferenceExpression; if (declaredVariables.ContainsKey(pref.Variable)) { return new AttributeReference () { Variable = pref.Variable, Entity = declaredVariables[pref.Variable], Attribute = GetOrCreateAttribute (value as ParsedAttributeReferenceExpression, declaredVariables[pref.Variable]) }; } else { throw new CompilationException (string.Format ("Variable '{0}' is not declared", pref.Variable)); } } else if (value.GetType() == typeof (ParsedComparisonExpression)) { var pref = value as ParsedComparisonExpression; ComparisonCriteria criteria; if (pref.criteria == ParsedComparisonCriteria.Equals) { criteria = ComparisonCriteria.Equals; } else if (pref.criteria == ParsedComparisonCriteria.NotEquals) { criteria = ComparisonCriteria.NotEquals; } else if (pref.criteria == ParsedComparisonCriteria.BiggerThan) { criteria = ComparisonCriteria.BiggerThan; } else if (pref.criteria == ParsedComparisonCriteria.BiggerThanOrEquals) { criteria = ComparisonCriteria.BiggerThanOrEquals; } else if (pref.criteria == ParsedComparisonCriteria.LessThan) { criteria = ComparisonCriteria.LessThan; } else if (pref.criteria == ParsedComparisonCriteria.LessThanOrEquals) { criteria = ComparisonCriteria.LessThanOrEquals; } else { throw new NotImplementedException (); } return new ComparisonPredicate () { Criteria = criteria, Left = BuildFormula (pref.Left, declaredVariables), Right = BuildFormula (pref.Right, declaredVariables) }; } else if (value.GetType() == typeof (ParsedStringConstantExpression)) { return new StringConstant { Value = Sanitize((value as ParsedStringConstantExpression).Value) }; } else if (value.GetType() == typeof (ParsedNumericConstantExpression)) { return new NumericConstant { Value = (value as ParsedNumericConstantExpression).Value }; } else if (value.GetType() == typeof (ParsedBoolConstantExpression)) { return new BoolConstant { Value = (value as ParsedBoolConstantExpression).Value }; } else if (value.GetType() == typeof (ParsedVariableReference)) { if (!declaredVariables.ContainsKey((value as ParsedVariableReference).Value)) { throw new CompilationException (string.Format ("Variable '{0}' is not declared", (value as ParsedVariableReference).Value)); } return new VariableReference { Name = (value as ParsedVariableReference).Value }; } throw new NotImplementedException (string.Format ("{0} is not yet supported", value.GetType ().Name)); }
public Formula BuildFormula(ParsedElement value, Dictionary <string, Entity> declaredVariables) { if (value == null) { throw new ArgumentNullException("value"); } if (value.GetType() == typeof(ParsedForallExpression)) { var a = new Forall(); var d2 = new Dictionary <string, KAOSTools.Core.Entity> (declaredVariables); foreach (var arg in (value as ParsedForallExpression).arguments) { var name = arg.VariableName; var type = GetOrCreateEntity(arg.Type); if (declaredVariables.ContainsKey(name)) { throw new BuilderException(string.Format("'{0}' is already defined", name), value.Filename, value.Line, value.Col); } a.Declarations.Add(new KAOSTools.Core.ArgumentDeclaration() { Name = name, Type = type.Identifier }); d2.Add(name, type); } a.Enclosed = BuildFormula((value as ParsedForallExpression).Enclosed, d2); return(a); } else if (value.GetType() == typeof(ParsedExistsExpression)) { var a = new Exists(); var d2 = new Dictionary <string, KAOSTools.Core.Entity> (declaredVariables); foreach (var arg in (value as ParsedExistsExpression).arguments) { var name = arg.VariableName; var type = GetOrCreateEntity(arg.Type); if (declaredVariables.ContainsKey(name)) { throw new BuilderException(string.Format("'{0}' is already defined", name), value.Filename, value.Line, value.Col); } a.Declarations.Add(new KAOSTools.Core.ArgumentDeclaration() { Name = name, Type = type.Identifier }); d2.Add(name, type); } a.Enclosed = BuildFormula((value as ParsedExistsExpression).Enclosed, d2); return(a); } else if (value.GetType() == typeof(ParsedStrongImplyExpression)) { return(new StrongImply() { Left = BuildFormula((value as ParsedStrongImplyExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedStrongImplyExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedImplyExpression)) { return(new Imply() { Left = BuildFormula((value as ParsedImplyExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedImplyExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedEquivalenceExpression)) { return(new Equivalence() { Left = BuildFormula((value as ParsedEquivalenceExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedEquivalenceExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedUntilExpression)) { return(new Until() { Left = BuildFormula((value as ParsedUntilExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedUntilExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedUnlessExpression)) { return(new Unless() { Left = BuildFormula((value as ParsedUnlessExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedUnlessExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedReleaseExpression)) { return(new Release() { Left = BuildFormula((value as ParsedReleaseExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedReleaseExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedAndExpression)) { return(new And() { Left = BuildFormula((value as ParsedAndExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedAndExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedOrExpression)) { return(new Or() { Left = BuildFormula((value as ParsedOrExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedOrExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedNotExpression)) { return(new Not() { Enclosed = BuildFormula((value as ParsedNotExpression).Enclosed, declaredVariables) }); } else if (value.GetType() == typeof(ParsedNextExpression)) { return(new Next() { Enclosed = BuildFormula((value as ParsedNextExpression).Enclosed, declaredVariables) }); } else if (value.GetType() == typeof(ParsedEventuallyExpression)) { return(new Eventually() { Enclosed = BuildFormula((value as ParsedEventuallyExpression).Enclosed, declaredVariables), TimeBound = BuildTimeBound((value as ParsedEventuallyExpression).TimeBound) }); } else if (value.GetType() == typeof(ParsedGloballyExpression)) { return(new Globally() { Enclosed = BuildFormula((value as ParsedGloballyExpression).Enclosed, declaredVariables), TimeBound = BuildTimeBound((value as ParsedGloballyExpression).TimeBound) }); } else if (value.GetType() == typeof(ParsedPredicateReferenceExpression)) { var prel = value as ParsedPredicateReferenceExpression; // Check if arguments are all defined foreach (var arg in prel.ActualArguments) { if (!declaredVariables.ContainsKey(arg)) { throw new BuilderException(string.Format("'{0}' is not declared"), value.Filename, value.Line, value.Col); } } return(new PredicateReference() { PredicateIdentifier = GetOrCreatePredicate(prel, declaredVariables).Identifier, ActualArguments = prel.ActualArguments }); } else if (value.GetType() == typeof(ParsedInRelationExpression)) { var prel = value as ParsedInRelationExpression; foreach (var arg in prel.Variables) { if (!declaredVariables.ContainsKey(arg)) { throw new BuilderException(string.Format("'{0}' is not declared", arg), value.Filename, value.Line, value.Col); } } return(new RelationReference() { Relation = GetOrCreateRelation(value as ParsedInRelationExpression, declaredVariables).Identifier, ActualArguments = prel.Variables }); } else if (value.GetType() == typeof(ParsedAttributeReferenceExpression)) { var pref = value as ParsedAttributeReferenceExpression; if (declaredVariables.ContainsKey(pref.Variable)) { var boolType = GetOrCreateGivenType("boolean"); return(new AttributeReference() { Variable = pref.Variable, Entity = declaredVariables [pref.Variable].Identifier, Attribute = GetOrCreateAttribute(value as ParsedAttributeReferenceExpression, declaredVariables [pref.Variable], boolType).Identifier }); } else { throw new BuilderException(string.Format("Variable '{0}' is not declared", pref.Variable), value.Filename, value.Line, value.Col); } } else if (value.GetType() == typeof(ParsedComparisonExpression)) { var pref = value as ParsedComparisonExpression; ComparisonCriteria criteria; if (pref.criteria == ParsedComparisonCriteria.Equals) { criteria = ComparisonCriteria.Equals; } else if (pref.criteria == ParsedComparisonCriteria.NotEquals) { criteria = ComparisonCriteria.NotEquals; } else if (pref.criteria == ParsedComparisonCriteria.BiggerThan) { criteria = ComparisonCriteria.BiggerThan; } else if (pref.criteria == ParsedComparisonCriteria.BiggerThanOrEquals) { criteria = ComparisonCriteria.BiggerThanOrEquals; } else if (pref.criteria == ParsedComparisonCriteria.LessThan) { criteria = ComparisonCriteria.LessThan; } else if (pref.criteria == ParsedComparisonCriteria.LessThanOrEquals) { criteria = ComparisonCriteria.LessThanOrEquals; } else { throw new NotImplementedException(); } return(new ComparisonPredicate() { Criteria = criteria, Left = BuildFormula(pref.Left, declaredVariables), Right = BuildFormula(pref.Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedStringConstantExpression)) { return(new StringConstant { Value = Sanitize((value as ParsedStringConstantExpression).Value) }); } else if (value.GetType() == typeof(ParsedNumericConstantExpression)) { return(new NumericConstant { Value = (value as ParsedNumericConstantExpression).Value }); } else if (value.GetType() == typeof(ParsedBoolConstantExpression)) { return(new BoolConstant { Value = (value as ParsedBoolConstantExpression).Value }); } else if (value.GetType() == typeof(ParsedVariableReference)) { if (!declaredVariables.ContainsKey((value as ParsedVariableReference).Value)) { throw new BuilderException(string.Format("Variable '{0}' is not declared", (value as ParsedVariableReference).Value), value.Filename, value.Line, value.Col); } return(new VariableReference { Name = (value as ParsedVariableReference).Value }); } throw new NotImplementedException(string.Format("{0} is not yet supported", value.GetType().Name)); }