public ActionResult Submit(string json) { StringBuilder spec = new StringBuilder(); //String json = new StreamReader(this.Request.InputStream).ReadToEnd(); if (json.Contains("ClientServer")) { ClientServerJSONTranslator CnsJSONTranslator = new ClientServerJSONTranslator(); ClientServerSpecCreator CnsSpecCreator = new ClientServerSpecCreator(); ClientServerSystemModel system = CnsJSONTranslator.CreateModel(json); spec = CnsSpecCreator.CreateSpec(system); } else if (json.Contains("PipeAndFilter")) { PipeFilterJSONTranslator PnfJSONTranslator = new PipeFilterJSONTranslator(); PipeFilterSpecCreator PnfSpecCreator = new PipeFilterSpecCreator(); PipeFilterSystemModel system = PnfJSONTranslator.CreateModel(json); spec = PnfSpecCreator.CreateSpec(system); } saveFileClientSide(spec); return(new EmptyResult()); }
public StringBuilder CreateSpec(PipeFilterSystemModel system) { StringBuilder pf_spec = new StringBuilder(); // Header: User can rename it later. pf_spec.Append("module PnfExample\n\n"); // Includes: User must have the Pnf_simplified spec in a library folder where this spec is saved. pf_spec.Append("open library/Pnf_simplified as Pnf\n\n"); // System Name: pf_spec.Append("one sig "); pf_spec.Append(system.Name); pf_spec.Append(" extends System{}\n\n"); // For each Filter: foreach (PnfFilter f in system.Filters) { pf_spec.Append("one sig "); pf_spec.Append(f.Name); pf_spec.Append(" extends Filter{}{\n"); int count_ports = 0; foreach (PnfInput i in f.Inputs) { if (count_ports == 0) { pf_spec.Append("\tports = "); pf_spec.Append(i.Name); count_ports = 1; } else { pf_spec.Append(" + "); pf_spec.Append(i.Name); } } foreach (PnfOutput o in f.Outputs) { if (count_ports == 0) { pf_spec.Append("\tports = "); pf_spec.Append(o.Name); count_ports = 1; } else { pf_spec.Append(" + "); pf_spec.Append(o.Name); } } pf_spec.Append("\n}\n\n"); } // For each Pipe: foreach (PnfPipe p in system.Pipes) { pf_spec.Append("one sig "); pf_spec.Append(p.Name); pf_spec.Append(" extends Pipe{}{\n"); int count_roles = 0; foreach (PnfSink s in p.Sinks) { if (count_roles == 0) { pf_spec.Append("\troles = "); pf_spec.Append(s.Name); count_roles = 1; } else { pf_spec.Append(" + "); pf_spec.Append(s.Name); } } foreach (PnfSource s in p.Sources) { if (count_roles == 0) { pf_spec.Append("\troles = "); pf_spec.Append(s.Name); count_roles = 1; } else { pf_spec.Append(" + "); pf_spec.Append(s.Name); } } pf_spec.Append("\n}\n\n"); } // For each Source: foreach (PnfDataSource s in system.DataSources) { pf_spec.Append("one sig "); pf_spec.Append(s.Name); pf_spec.Append(" extends DataSource{}{\n"); int count_ports = 0; foreach (PnfOutput o in s.Outputs) { if (count_ports == 0) { pf_spec.Append("\tports = "); pf_spec.Append(o.Name); count_ports = 1; } else { pf_spec.Append(" + "); pf_spec.Append(o.Name); } } pf_spec.Append("\n}\n\n"); } // For each Sink: foreach (PnfDataSink s in system.DataSinks) { pf_spec.Append("one sig "); pf_spec.Append(s.Name); pf_spec.Append(" extends DataSink{}{\n"); int count_ports = 0; foreach (PnfInput i in s.Inputs) { if (count_ports == 0) { pf_spec.Append("\tports = "); pf_spec.Append(i.Name); count_ports = 1; } else { pf_spec.Append(" + "); pf_spec.Append(i.Name); } } pf_spec.Append("\n}\n\n"); } // For each Output from DataSources: foreach (PnfDataSource s in system.DataSources) { foreach (PnfOutput o in s.Outputs) { pf_spec.Append("one sig "); pf_spec.Append(o.Name); pf_spec.Append(" extends Output{}\n\n"); } } // For each Input from DataSinks: foreach (PnfDataSink s in system.DataSinks) { foreach (PnfInput i in s.Inputs) { pf_spec.Append("one sig "); pf_spec.Append(i.Name); pf_spec.Append(" extends Input{}\n\n"); } } // For each Input + Output in Filters: foreach (PnfFilter f in system.Filters) { // Inputs: foreach (PnfInput i in f.Inputs) { pf_spec.Append("one sig "); pf_spec.Append(i.Name); pf_spec.Append(" extends Input{}\n\n"); } // Outputs: foreach (PnfOutput o in f.Outputs) { pf_spec.Append("one sig "); pf_spec.Append(o.Name); pf_spec.Append(" extends Output{}\n\n"); } } // For each Source + Sink: foreach (PnfPipe p in system.Pipes) { foreach (PnfSource s in p.Sources) { pf_spec.Append("one sig "); pf_spec.Append(s.Name); pf_spec.Append(" extends Source{}\n\n"); } foreach (PnfSink s in p.Sinks) { pf_spec.Append("one sig "); pf_spec.Append(s.Name); pf_spec.Append(" extends Sink{}\n\n"); } } // Fact for attachments: if (system.Attachments.Count > 1) { pf_spec.Append("fact Correct_Attachments {\n"); pf_spec.Append(system.Name); pf_spec.Append(".attachments =\n"); int attachment_count = 0; foreach (KeyValuePair <PnfRole, PnfPort> a in system.Attachments) { if (attachment_count == 0) { pf_spec.Append(a.Key.Name); pf_spec.Append("->"); pf_spec.Append(a.Value.Name); pf_spec.Append("\n"); attachment_count = 1; } else { pf_spec.Append("+ "); pf_spec.Append(a.Key.Name); pf_spec.Append("->"); pf_spec.Append(a.Value.Name); pf_spec.Append("\n"); } } pf_spec.Append("}\n\n"); } // Footer: pred show and run show pf_spec.Append("pred show {\n"); pf_spec.Append("\tone System\n"); pf_spec.Append("}\n\n"); pf_spec.Append("run show\n\n"); return(pf_spec); }
public PipeFilterSystemModel CreateModel(String json) { var details = JsonConvert.DeserializeObject <JsonDetails>(json); PipeFilterSystemModel system = new PipeFilterSystemModel(); system.Name = "PipeFilterSystemExample"; // For each Component foreach (Component c in details.components) { if (c.name.Contains("DataSink")) { PnfDataSink tempDataSink = new PnfDataSink(); tempDataSink.Name = c.name; // Find inputs foreach (Port p in details.ports) { if (p.component == c.id) { if (p.name.Contains("Input")) { PnfInput tempInput = new PnfInput(); tempInput.Name = p.name; tempDataSink.Inputs.Add(tempInput); } else { //THROW ERROR: name for port should contain "Input" for DataSink } } } if (tempDataSink.Inputs == null || tempDataSink.Inputs.Count < 1) { //THROW ERROR: no inputs found for DataSink } else { system.DataSinks.Add(tempDataSink); } } else if (c.name.Contains("DataSource")) { PnfDataSource tempDataSource = new PnfDataSource(); tempDataSource.Name = c.name; // Find outputs foreach (Port p in details.ports) { if (p.component == c.id) { if (p.name.Contains("Output")) { PnfOutput tempOutput = new PnfOutput(); tempOutput.Name = p.name; tempDataSource.Outputs.Add(tempOutput); } else { //THROW ERROR: name for port should contain "Output" for DataSource } } } if (tempDataSource.Outputs == null || tempDataSource.Outputs.Count < 1) { //THROW ERROR: no outputs found for DataSource } else { system.DataSources.Add(tempDataSource); } } else if (c.name.Contains("Filter")) { PnfFilter tempFilter = new PnfFilter(); tempFilter.Name = c.name; // Find inputs and outputs foreach (Port p in details.ports) { if (p.component == c.id) { if (p.name.Contains("Input")) { PnfInput tempInput = new PnfInput(); tempInput.Name = p.name; tempFilter.Inputs.Add(tempInput); } else if (p.name.Contains("Output")) { PnfOutput tempOutput = new PnfOutput(); tempOutput.Name = p.name; tempFilter.Outputs.Add(tempOutput); } else { //THROW ERROR: name for port should contain "Input" or "Output" for Filter } } } if ((tempFilter.Outputs == null || tempFilter.Outputs.Count < 1) || (tempFilter.Inputs == null || tempFilter.Inputs.Count < 1)) { //THROW ERROR: no inputs or outputs found for Filter } else { system.Filters.Add(tempFilter); } } else { //THROW ERROR: Wrong name for component. Should be "DataSink", "DataSource", or "Filter". } } // For each Connector foreach (Connector c in details.connectors) { // All connectors are Pipes PnfPipe newPipe = new PnfPipe(); newPipe.Name = c.name; // Find Sinks and Sources foreach (Role r in details.roles) { if (r.connector == c.id) { if (r.name.Contains("Sink")) { PnfSink tempSink = new PnfSink(); tempSink.Name = r.name; newPipe.Sinks.Add(tempSink); } else if (r.name.Contains("Source")) { PnfSource tempSource = new PnfSource(); tempSource.Name = r.name; newPipe.Sources.Add(tempSource); } else { //THROW ERROR: role name should contain either "Sink" or "Source" for Pipes. } } } if ((newPipe.Sinks == null || newPipe.Sinks.Count < 1) || (newPipe.Sources == null || newPipe.Sources.Count < 1)) { //THROW ERROR: no Sinks or Sources found for Pipe. } else { system.Pipes.Add(newPipe); } } // For each attachment foreach (Interaction i in details.interactions) { PnfRole tempRole = new PnfRole(); PnfPort tempPort = new PnfPort(); // Find name of each role foreach (Role r in details.roles) { if (i.role == r.id) { tempRole.Name = r.name; } } // Find name of each port foreach (Port p in details.ports) { if (i.port == p.id) { tempPort.Name = p.name; } } // if both are not null add to attachments if (string.IsNullOrEmpty(tempPort.Name) || string.IsNullOrEmpty(tempRole.Name)) { //THROW ERROR: Broken, ids are wrong for interactions } else { system.Attachments.Add(tempRole, tempPort); } } return(system); }