internal ModelMethodEvent(ModelInstance instance, ModelMethod method, object[] args, string[] include) { this.Instance = instance; this.method = method; this.Include = include; this.args = args; }
/// <summary> /// Returns the interface methods this method implements. /// </summary> public IEnumerable<MethodInfo> GetImplementedInterfaceMethods(ModelMethod byMethod) { if ((this.interfaceMapping == null) && (byMethod.DeclaringType.IsInterface == false)) { lock (this) { if (this.interfaceMapping == null) { this.interfaceMapping = new Dictionary<MethodBase, List<MethodInfo>>(); foreach (Type itype in this.runtimeType.GetInterfaces()) { InterfaceMapping map = this.runtimeType.GetInterfaceMap(itype); for (int i = 0; i < map.InterfaceMethods.Length; i++) { List<MethodInfo> list; if (!this.interfaceMapping.TryGetValue(map.TargetMethods[i], out list)) list = this.interfaceMapping[map.TargetMethods[i]] = new List<MethodInfo>(); list.Add(map.InterfaceMethods[i]); } } } } } List<MethodInfo> result; if (byMethod.DeclaringType.IsInterface) return EmptyMethodInfoSet; else if (this.interfaceMapping.TryGetValue(byMethod.MethodBase, out result)) return result; else return EmptyMethodInfoSet; }
public bool Matches(ModelMethod method) { foreach (BaseMatchingRule rule in this.rules) if (!rule.Matches(method)) return false; return true; }
private static void CollectMethodAttributes(ModelMethod method, HashSet <string> attributes) { foreach (Attribute attr in method.MethodBase.GetCustomAttributes(true)) { attributes.Add(attr.GetType().FullName); } }
public override bool Matches(ModelMethod method) { Type returnType = null; if (method.MethodBase is MethodInfo) returnType = ((MethodInfo)method.MethodBase).ReturnType; return this.Matches(returnType ?? typeof(void)); }
protected ModelMethodParameter(ModelMethod method, string name, Type parameterType, ModelType referenceType, bool isList) { this.Method = method; this.Name = name; this.ParameterType = parameterType; this.ReferenceType = referenceType; this.IsList = isList; }
public override bool Matches(ModelMethod method) { foreach (MethodInfo ifaceMethod in method.ImplementedInterfaceMethods) { if (ifaceMethod.DeclaringType.FullName == this.interfaceName) return true; } return false; }
public bool Matches(ModelMethod method) { foreach (BaseMatchingRule rule in this.rules) { if (!rule.Matches(method)) { return(false); } } return(true); }
public override bool Matches(ModelMethod method) { Type returnType = null; if (method.MethodBase is MethodInfo) { returnType = ((MethodInfo)method.MethodBase).ReturnType; } return(this.Matches(returnType ?? typeof(void))); }
/// <summary> /// Helper method for generating the string representation of a given ModelMethod. /// </summary> /// <param name="method">The ModelMethod for which to generate a string representation.</param> /// <returns>The string representation of the ModelMethod.</returns> public static string GetString(ModelMethod method, string[] args = null, bool withEnding = false) { var methodName = method.ToString().Replace("__", "."); var concatArgs = args == null ? string.Empty : string.Join(",", args); var outString = methodName + "(" + concatArgs + ")"; if (withEnding) { outString += ";\n"; } return(outString); }
public override bool Matches(ModelMethod method) { switch(this.target) { case RuleTarget.Method: return this.MatchesOnMethod(method); case RuleTarget.Type: return this.MatchesOnType(method.DeclaringType); default: throw new InvalidOperationException(String.Format("Invalid target '{0}' for {1}.", this.target, this.GetType().Name)); } }
public override bool Matches(ModelMethod method) { foreach (MethodInfo ifaceMethod in method.ImplementedInterfaceMethods) { if (ifaceMethod.DeclaringType.FullName == this.interfaceName) { return(true); } } return(false); }
public override bool Matches(ModelMethod method) { switch (this.target) { case RuleTarget.Method: return(this.MatchesOnMethod(method)); case RuleTarget.Type: return(this.MatchesOnType(method.DeclaringType)); default: throw new InvalidOperationException(String.Format("Invalid target '{0}' for {1}.", this.target, this.GetType().Name)); } }
public void Process(CodeModel codeModel) { foreach (ModelMethod method in codeModel.Methods.WhereTagsContains(DefaultImplementationTag)) { foreach (MethodInfo imethodInfo in method.ImplementedInterfaceMethods) { ModelMethod imethod = codeModel.Methods.ForMethodBase(imethodInfo); if (imethod != null) { imethod.CallsMethods.Add(method); } } } }
public void Process(CodeModel codeModel) { foreach (ModelMethod method in codeModel.Methods) { if (method.IsOverride) { ModelMethod baseMethod = codeModel.Methods.ForMethodBase(method.BaseMethodDefinition); if (baseMethod != null && baseMethod.HasAnyOfTags(BaseImplementationTag)) { baseMethod.CallsMethods.Add(method); } } } }
private bool MatchesOnMethod(ModelMethod method) { // Retrieve attributes list in cache for given method: HashSet<String> methodAttributes; if (!this.methodAttributesCache.TryGetValue(method, out methodAttributes)) { methodAttributes = new HashSet<String>(); CollectMethodAttributes(method, methodAttributes); lock (this.methodAttributesCache) { this.methodAttributesCache[method] = methodAttributes; } } // Test match: return methodAttributes.Contains(this.typeName); }
private bool MatchesOnMethod(ModelMethod method) { // Retrieve attributes list in cache for given method: HashSet <String> methodAttributes; if (!this.methodAttributesCache.TryGetValue(method, out methodAttributes)) { methodAttributes = new HashSet <String>(); CollectMethodAttributes(method, methodAttributes); lock (this.methodAttributesCache) { this.methodAttributesCache[method] = methodAttributes; } } // Test match: return(methodAttributes.Contains(this.typeName)); }
/// <summary> /// Whether the given method positively matches this rule. /// </summary> public abstract bool Matches(ModelMethod method);
public override bool Matches(ModelMethod method) { return this.reverse ^ method.Tags.Contains(this.tagName); }
static void Main(string[] args) { if (args.Count() != 2 && args.Count() != 3) { Console.WriteLine("Incorrec parameters: <data>.json <rules>.fis [<step_size>]"); Environment.ExitCode = 1; return; } string argData = args[0]; string argRules = args[1]; double argStepSize = 0.1; if (args.Count() == 3) { argStepSize = double.Parse(args[2]); } // Load from files var rules = new AntlrFileStream(argRules); dynamic data = JsonConvert.DeserializeObject <dynamic>(new StreamReader(argData).ReadToEnd()); // Parse rules file var lexer = new FuzzyLexer(rules); var errors = new List <string>(); lexer.RemoveErrorListeners(); lexer.AddErrorListener(new ErrorListener(errors)); var tokens = new CommonTokenStream(lexer); var parser = new FuzzyParser(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(new ParserErrorListener(errors)); IParseTree tree = parser.compileUnit(); if (errors.Any()) { Console.WriteLine(); foreach (var item in errors) { Console.WriteLine(item); } Environment.ExitCode = 1; return; } // Retieve dictionaries from data JSON file var values = data["variables"]; var funcs = new Dictionary <string, FunctionBase>(); foreach (JProperty f in data["functions"]) { funcs.Add(f.Name, GetFunction(f.Values().First(), f.Values().Skip(1))); } // Evaluate rules expressions var evaluator = new Evaluator(values, funcs); dynamic result = evaluator.Visit(tree); (string model, string defuzzy, IEnumerable <dynamic> output) = (result.Item1, result.Item2, result.Item3); model = model.ToLower(); var rulesOutput = new List <(double, string, string)>(); foreach (var item in output) { double tmp = double.Parse(item.Item1.ToString()); rulesOutput.Add((tmp, item.Item2, item.Item3)); } if (model == "mamdani") { Print(ModelMethod.Mamdani(rulesOutput, GetDefuzzifier(defuzzy.ToLower()), funcs, argStepSize)); } else if (model == "sugeno") { var rulesOutputSugeno = new List <(double, string, double)>(); foreach (var item in output) { double tmp = double.Parse(item.Item3.ToString()); rulesOutputSugeno.Add((item.Item1, item.Item2, tmp)); } Print(ModelMethod.Sugeno(rulesOutputSugeno)); } else if (model == "tsukamoto") { Print(ModelMethod.Tsukamoto(rulesOutput, funcs, argStepSize)); } else { throw new Exception($"The model {model} is not exists"); } }
public override bool Matches(ModelMethod method) { return(this.reverse ^ method.Tags.Contains(this.tagName)); }
public override bool Matches(ModelMethod method) { return(this.Matches(method.DeclaringType)); }
private bool MatchesOnMethod(ModelMethod method) { return(this.reverse ^ ((method.MethodBase.Attributes & this.methodAttributes) == this.methodAttributes)); }
public bool MatchesOnMethod(ModelMethod method) { return(this.match.IsMatch(method.Name)); }
public override bool Matches(ModelMethod method) { return this.Matches(method.DeclaringType); }
private static void CollectMethodAttributes(ModelMethod method, HashSet<string> attributes) { foreach (Attribute attr in method.MethodBase.GetCustomAttributes(true)) attributes.Add(attr.GetType().FullName); }
object IJsonSerializable.Deserialize(JsonReader reader) { ModelInstance instance = null; string eventName = null; string property; Type eventType = null; DomainEvent domainEvent = null; ModelMethod method = null; object[] methodArgs = null; while (reader.ReadProperty(out property)) { switch (property) { // Get the event target case "instance": instance = reader.ReadValue <ModelInstance>(); break; // Get the property paths to include case "include": Include = reader.ReadValue <string[]>(); break; // Get the type of event case "type": eventName = reader.ReadValue <string>(); break; // Get the event data case "event": if (eventName != null && instance != null) { eventType = instance.Type.GetEventType(eventName); } // Deserialize the strongly-typed event if (eventType != null) { // Create a generic event instance domainEvent = (DomainEvent)typeof(DomainEvent <>) .MakeGenericType(eventType) .GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { eventType }, null) .Invoke(new object[] { reader.ReadValue(eventType) }); // Set the event target and inclusion paths domainEvent.Instance = instance; domainEvent.Include = Include; } // Otherwise, see if it is a model method invocation else { var separator = eventName.LastIndexOf("."); if (separator > 0) { var type = ModelContext.Current.GetModelType(eventName.Substring(0, separator)); var methodName = eventName.Substring(separator + 1); while (type != null && method == null) { method = type.Methods[methodName]; type = type.BaseType; } if (method != null && reader.TokenType == Newtonsoft.Json.JsonToken.StartObject && reader.Read()) { methodArgs = new object[method.Parameters.Count]; string parameter; while (reader.ReadProperty(out parameter)) { // Get the parameter with the specified name var p = method.Parameters.FirstOrDefault(pm => pm.Name == parameter); if (p == null) { throw new ArgumentException("The parameter '" + property + "' is not valid for method '" + method.DeclaringType.Name + "." + method.Name + "'."); } // Value type if (p.ReferenceType == null) { methodArgs[p.Index] = reader.ReadValue(p.ParameterType); } // List type else if (p.IsList) { methodArgs[p.Index] = reader.ReadValue <ModelInstance[]>(); } // Reference type else { methodArgs[p.Index] = reader.ReadValue <ModelInstance>(); } } reader.Read(); } } } break; default: throw new ArgumentException("The specified property could not be deserialized.", property); } } // Custom domain event if (domainEvent != null) { return(domainEvent); } // Method invocation if (method != null) { return(new ModelMethodEvent(instance, method, methodArgs, Include)); } if (eventName == "Save") { return new DomainEvent <SaveEvent>(new SaveEvent()) { Instance = instance, Include = Include } } ; // Indicate that the event could not be resolved throw new ArgumentException(eventName + " is not a valid event for " + instance.Type.Name); } #endregion }
private bool MatchesOnMethod(ModelMethod method) { return this.reverse ^ ((method.MethodBase.Attributes & this.methodAttributes) == this.methodAttributes); }
public bool MatchesOnMethod(ModelMethod method) { return (this.match.IsMatch(method.Name)); }
/// <summary> /// Process the given session and return a codemodel containing /// the in-memory method call network. /// </summary> public CodeModel Process(StaticCodeAnalyzerSession session) { List<ModelAssembly> massemblies = new List<ModelAssembly>(); List<ModelType> mtypes = new List<ModelType>(); Dictionary<string, ModelMethod> mmethods = new Dictionary<string, ModelMethod>(); ILanguageInfo languageInfo = session.LanguageInfo; IAnalyzerFilter analyzerFilter = session.AnalyzerFilter; // Retrieve all methods and constructors: foreach (Assembly asm in session.Assemblies) { try { if ((analyzerFilter != null) && (!analyzerFilter.ProcessAssembly(asm))) continue; ModelAssembly masm = new ModelAssembly(asm, languageInfo); massemblies.Add(masm); foreach (Type type in asm.GetTypes()) { try { if ((analyzerFilter != null) && (!analyzerFilter.ProcessType(type))) continue; ModelType mtype = new ModelType(masm, type, languageInfo); mtypes.Add(mtype); foreach (MethodBase mb in type.GetConstructors(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { if ((analyzerFilter != null) && (!analyzerFilter.ProcessMethod(mb))) continue; mmethods[GetMethodKey(mb)] = new ModelMethod(mtype, mb, languageInfo); } foreach (MethodBase mb in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) { if ((analyzerFilter != null) && (!analyzerFilter.ProcessMethod(mb))) continue; mmethods[GetMethodKey(mb)] = new ModelMethod(mtype, mb, languageInfo); } } catch (Exception ex) { // Rethrow with mode info: throw new TargetInvocationException( String.Format("Error reading type {0}: {1} (See innerException.)", type.FullName, ex.Message), ex ); } } } catch (ReflectionTypeLoadException ex) { // Rethrow with mode info: throw new TargetInvocationException( String.Format("Error reading assembly {0}: {1} (See innerException.)", asm.FullName, ex.Message), ex.LoaderExceptions.FirstOrDefault() ?? ex ); } catch (Exception ex) { // Rethrow with mode info: throw new TargetInvocationException( String.Format("Error reading assembly {0}: {1} (See innerException.)", asm.FullName, ex.Message), ex ); } } // Build network of method calls: foreach (ModelMethod m in mmethods.Values) { try { MethodBodyReader reader = new MethodBodyReader(m.MethodBase); foreach (MethodBase calledmb in reader.GetCalledMethods(true, true)) { ModelMethod calledm = FindMethod(mmethods, calledmb); if (calledm != null) { m.CallsMethods.Add(calledm); } } } catch (InvalidOperationException ex) { Debug.WriteLine(ex.Message); } } // Construct a code model: CodeModel codeModel = new CodeModel( massemblies, mtypes, mmethods.Values); // Apply processors: foreach (IProcessor processor in session.Processors) { processor.Process(codeModel); } // Construct & return a code model: return codeModel; }