public void SerializeOverlapped(Stream stream, Epa e1, Epa e2) { Contract.Requires(stream != null && stream.CanWrite); Contract.Requires(e1 != null && e2 != null); Contract.Requires(Contract.ForAll(e2.Transitions, t => e1.Transitions.Any(t2 => t2.Equals(t))), "e2 is subgraph of e1"); var graph = new Graph { Attr = { OptimizeLabelPositions = true, LayerDirection = LayerDirection.LR } }; epa = e1; foreach (var s in e1.States) { AddState(s, graph); } foreach (var t in e1.Transitions) { var overlapped = e2.Transitions.Any(t2 => t2.Equals(t)); AddTransitionOverlapped(t, graph, overlapped); } RenderGraph(stream, graph); }
public IStatement GenerateIf(FieldDefinition field, List <int> toStateIds, Epa epa, Dictionary <State, int> stateNumberMap) { var toStates = new List <State>(); foreach (var kvp in stateNumberMap) { var id = kvp.Value; var state = kvp.Key; if (toStateIds.Contains(id)) { toStates.Add(state); } } var conditions = GenerateStatesConditions(toStates); var stmt = new AssignmentGenerator().GenerateAssign(field, toStateIds[0]); for (var i = 1; i < toStateIds.Count; ++i) { stmt = new ConditionalStatement { Condition = conditions[i], TrueBranch = new AssignmentGenerator().GenerateAssign(field, toStateIds[i]), FalseBranch = stmt }; } return(stmt); }
protected void SerializeStates(Epa epa, XmlWriter writer) { Contract.Requires(epa != null && epa.Type != null); Contract.Requires(writer != null); var states = new List <State>(from s in epa.States orderby s.Name select s); foreach (var s in states) { writer.WriteStartElement("state"); writer.WriteAttributeString("name", s.Name); var enabledActions = new List <Action>(from a in s.EnabledActions orderby a.Name select a); foreach (var a in enabledActions) { writer.WriteStartElement("enabled_label"); writer.WriteAttributeString("name", a.Name); writer.WriteEndElement(); } SerializeTransitions(epa, writer, s); writer.WriteEndElement(); // State } }
protected void OnInitialStateAdded(object sender, IEpaBuilder epaBuilder) { generatedEpa = epaBuilder.Build(); var stateAddedEventArg = new StateAddedEventArgs(epaBuilder.Type, epaBuilder, generatedEpa.Initial); StateAdded(sender, stateAddedEventArg); }
protected static void SaveEpasAsImages(Epa epa, DirectoryInfo outputDir) { Contract.Requires(epa != null); Contract.Requires(outputDir != null); SaveEpaAs <EpaBinarySerializer>(epa, outputDir, "png"); }
protected static void SaveEpasAsXml(Epa epa, DirectoryInfo outputDir) { Contract.Requires(epa != null); Contract.Requires(outputDir.Exists); SaveEpaAs <EpaXmlSerializer>(epa, outputDir, "xml"); }
public void Serialize(Stream stream, Epa epa) { Contract.Requires(stream.CanWrite); Contract.Requires(epa != null); throw new NotImplementedException(); }
public void Serialize(Stream stream, Epa epa) { this.epa = epa; var graph = new Graph { Attr = { OptimizeLabelPositions = true, LayerDirection = LayerDirection.LR } }; foreach (var s in epa.States) { AddState(s, graph); } foreach (var t in epa.Transitions) { AddTransition(t, graph); } RenderGraph(stream, graph); }
protected static IAssembly GenerateStrengthenedAssembly(Epa epa, CciAssembly assembly) { Contract.Requires(epa != null); Contract.Requires(assembly != null); System.Console.WriteLine("Generating strengthened output assembly"); var instrumenter = new Instrumenter.Instrumenter(); return(instrumenter.InstrumentType(assembly, epa)); }
protected static void SaveEpaAs <T>(Epa epa, DirectoryInfo outputDir, string fileNameExtension) where T : ISerializer, new() { var typeName = epa.Type.ToString().Replace('.', '_'); var safeFileName = GetSafeFilename(string.Format("{0}\\{1}.{2}", outputDir.FullName, typeName, fileNameExtension)); using (var stream = File.Create(safeFileName)) { new T().Serialize(stream, epa); } }
public TypeAnalysisResult(Epa epa, TimeSpan totalTime, int generatedQueriesCount, int unprovenQueriesCount) { Contract.Requires(epa != null); Contract.Requires(totalTime != null); Contract.Requires(generatedQueriesCount >= 0); Contract.Requires(unprovenQueriesCount >= 0); Contract.Requires(unprovenQueriesCount <= generatedQueriesCount); this.epa = epa; this.totalTime = totalTime; this.generatedQueriesCount = generatedQueriesCount; this.unprovenQueriesCount = unprovenQueriesCount; }
protected void SerializeActions(Epa epa, XmlWriter writer) { Contract.Requires(epa != null); Contract.Requires(writer != null); var actions = new HashSet <Action>(from t in epa.Transitions orderby t.Action.Name select t.Action); foreach (var a in actions) { writer.WriteStartElement("label"); writer.WriteAttributeString("name", a.Name); writer.WriteEndElement(); } }
protected void GenerateAssembly(FileInfo outputFile, Epa epa) { UpdateStatus("Generating strengthened output assembly {0}...", outputFile); try { var instrumenter = new Instrumenter.Instrumenter(); var strengthenedAssembly = instrumenter.InstrumentType(model.InputAssembly as CciAssembly, epa) as CciAssembly; new CciAssemblyPersister().Save(strengthenedAssembly, outputFile.FullName); } catch (Exception ex) { HandleException(ex); } UpdateStatus("Done!", outputFile); }
protected void SerializeTransitions(Epa epa, XmlWriter writer, State s) { Contract.Requires(epa != null && epa.Type != null); Contract.Requires(writer != null); Contract.Requires(s != null); var transitionsFromState = epa.Transitions.Where(t => t.SourceState.Equals(s)); var transitions = (from t in transitionsFromState select t).OrderBy(x => x.TargetState.Name).ThenByDescending(x => x.Action.Name); foreach (var t in transitions) { writer.WriteStartElement("transition"); writer.WriteAttributeString("destination", t.TargetState.Name); writer.WriteAttributeString("label", t.Action.Name); writer.WriteAttributeString("uncertain", t.IsUnproven.ToString().ToLower()); writer.WriteAttributeString("violates_invariant", "false"); //Contractor.NET does not support this attribute writer.WriteEndElement(); } }
protected void ExportGraph(FileInfo outputFile, Epa epa, ISerializer serializer) { Contract.Requires(outputFile != null); Contract.Requires(epa != null); Contract.Requires(serializer != null); UpdateStatus("Exporting graph to {0}...", outputFile); try { using (var fileStream = File.Create(outputFile.FullName)) { serializer.Serialize(fileStream, epa); } } catch (Exception ex) { HandleException(ex); } UpdateStatus("Done!", outputFile); }
public void Serialize(Stream stream, Epa epa) { var settings = new XmlWriterSettings { CloseOutput = false, Indent = true }; using (var writer = XmlWriter.Create(stream, settings)) { writer.WriteStartDocument(); writer.WriteStartElement("abstraction"); writer.WriteAttributeString("initial_state", epa.Initial.Name); writer.WriteAttributeString("input_format", "code-with-pre"); writer.WriteAttributeString("name", epa.Type.Name); SerializeActions(epa, writer); SerializeStates(epa, writer); writer.WriteEndDocument(); } }
public SwitchGenerator(Epa epa, Dictionary <State, int> stateNumberMap) { this.epa = epa; this.stateNumberMap = stateNumberMap; }
protected void OnTransitionAdded(object sender, TransitionAddedEventArgs e) { generatedEpa = e.EpaBuilder.Build(); TransitionAdded(sender, e); }
public IAssembly InstrumentType(CciAssembly assembly, Epa epa) { Contract.Requires(assembly != null); Contract.Requires(epa != null); Contract.Requires(assembly.Types().Contains(epa.Type)); // Clone assembly var host = CciHostEnvironment.GetInstance(); var module = new CodeAndContractDeepCopier(host).Copy(assembly.Module); var contractExtractor = host.GetContractExtractor(module.UnitIdentity); var contractProvider = new AggregatingContractProvider(contractExtractor); var preconditions = new Dictionary <string, List <IPrecondition> >(); var actions = new List <Action>(from transition in epa.Transitions.GroupBy(t => t.Action) select transition.Key); Contract.Assert(actions.Any()); foreach (var action in actions) { // TODO (lleraromero): Es necesario? NullObject Pattern? var mc = action.Contract as MethodContract; if (mc == null) { continue; } preconditions.Add(action.Name, action.Contract.Preconditions.ToList()); } var typeDefinition = epa.Type; var cciTypeDefinition = module.AllTypes.First( t => TypeHelper.GetTypeName(t, NameFormattingOptions.UseGenericTypeNameSuffix).Equals(typeDefinition.Name)) as NamedTypeDefinition; Contract.Assert(cciTypeDefinition != null); var typeContract = RemoveInvariantMethods(typeDefinition, cciTypeDefinition); // Add a field to represent the state var field = CreateStateField(cciTypeDefinition, typeContract); cciTypeDefinition.Fields.Add(field); // Associate type contract to the contract provider contractProvider.AssociateTypeWithContract(cciTypeDefinition, typeContract); // Create Ids var stateNumberMap = new Dictionary <State, int>(); foreach (var state in epa.States) { stateNumberMap.Add(state, stateNumberMap.Keys.Count); } foreach (var action in actions) { // voy a agrupar las transiciones que usan esta accion por sourceState.Id // transitions = Dicc<int, List<int>> o sea: "Dicc<from, List<to>>" var transitionsUsingAction = new List <Transition>(from t in epa.Transitions where t.Action.Equals(action) select t); var transitionsSourceIds = new HashSet <int>(from t in transitionsUsingAction select stateNumberMap[t.SourceState]).Distinct(); var transitions = new Dictionary <int, List <int> >(); foreach (var t in transitionsUsingAction) { var sourceStateId = stateNumberMap[t.SourceState]; if (!transitions.ContainsKey(sourceStateId)) { transitions.Add(sourceStateId, new List <int>()); } var targetStateId = stateNumberMap[t.TargetState]; transitions[sourceStateId].Add(targetStateId); } var methodContract = action.Contract as MethodContract ?? new MethodContract(); var methodDefinition = cciTypeDefinition.Methods.First(m => m.GetUniqueName().Equals(action.Method.GetUniqueName())); BlockStatement actionBodyBlock = null; if (methodDefinition.Body is SourceMethodBody) { var actionBody = (SourceMethodBody)methodDefinition.Body; actionBodyBlock = actionBody.Block as BlockStatement; } else if (methodDefinition.Body is Microsoft.Cci.MutableCodeModel.SourceMethodBody) { var actionBody = (Microsoft.Cci.MutableCodeModel.SourceMethodBody)methodDefinition.Body; actionBodyBlock = actionBody.Block as BlockStatement; } Contract.Assert(actionBodyBlock != null); //Por tratarse de un constructor insertamos en 1 porque en 0 esta base..ctor() var insertAtIndex = methodDefinition.IsConstructor ? 1 : 0; // CodeContracts no permite utilizar 'this' en los requires de los constructores if (!methodDefinition.IsConstructor) { var pre = new PreconditionGenerator().GeneratePrecondition(field, transitions.Keys.ToList()); methodContract.Preconditions.Add(pre); } var posts = new PostconditionGenerator(stateNumberMap[epa.Initial]).GeneratePostconditions(field, transitions); methodContract.Postconditions.AddRange(posts); // Associate contract contractProvider.AssociateMethodWithContract(methodDefinition, methodContract); var stmt = new SwitchGenerator(epa, stateNumberMap).GenerateSwitch(field, transitions); // Se actualiza el $state en un finally porque los if de adentro del switch tienen que ser ejecutados despues del cuerpo de este metodo var stmtsCount = actionBodyBlock.Statements.Count - insertAtIndex; var tryBlock = new BlockStatement(); var bodyStmts = new List <IStatement>(actionBodyBlock.Statements.GetRange(insertAtIndex, stmtsCount)); tryBlock.Statements.AddRange(bodyStmts); var finallyBlock = new BlockStatement(); finallyBlock.Statements.Add(stmt); var tryStmt = new TryCatchFinallyStatement { TryBody = tryBlock, FinallyBody = finallyBlock }; actionBodyBlock.Statements.RemoveRange(insertAtIndex, stmtsCount); actionBodyBlock.Statements.Insert(insertAtIndex, tryStmt); } return(new CciAssembly(module, contractProvider)); }