Skip to content

Removed pin concept, encode connections as 4-tuples #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 37 additions & 162 deletions ObjectAlgebraExecutionGraphs/Algebras/CSharpTranslatableGraphAlgebra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,26 @@
using ObjectAlgebraExecutionGraphs.Variants;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ObjectAlgebraExecutionGraphs.Algebras
{
public class CSharpTranslatableGraphAlgebra : IExecutionGraphAlgebra<Type, ICSharpTranslatableNode<ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>>, ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>>
public class CSharpTranslatableGraphAlgebra : IDataGraphAlgebra<Type, ICSharpTranslatableNode>, IExecutionGraphAlgebra<Type, ICSharpTranslatableNode>
{
public ICSharpTranslatableNode<ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>> CreateLiteralNode(Type type, object value)
=> new LiteralNode(type, value);

public ICSharpTranslatableNode<ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>> CreateConcatenateNode(ICSharpTranslatableOutputDataPin<Type> aFrom, ICSharpTranslatableOutputDataPin<Type> bFrom, ICSharpTranslatableInputExecPin execTo)
=> new ConcatenateNode(aFrom, bFrom, execTo);

public ICSharpTranslatableNode<ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>> CreateReverseStringNode(ICSharpTranslatableOutputDataPin<Type> aFrom)
=> new ReverseStringNode(aFrom);
public ICSharpTranslatableNode CreateConcatenateNode() => new ConcatenateNode();
public ICSharpTranslatableNode CreateLiteralNode(Type type, object value) => new LiteralNode(type, value);
public ICSharpTranslatableNode CreateReverseStringNode() => new ReverseStringNode();

public Type TypeFromString(string typeString) => Type.GetType(typeString);

private class InputExecPin : ICSharpTranslatableInputExecPin
{
public string Label { get; } = RandomGenerator.GetRandomLowerLetters(16);
}

private class OutputExecPin : ICSharpTranslatableOutputExecPin
{

}

private class InputDataPin : ICSharpTranslatableInputDataPin<Type>
{
public string VariableName { get; } = RandomGenerator.GetRandomLowerLetters(16);
public Type Type { get; }

private readonly ICSharpTranslatableOutputDataPin<Type> incomingPin;

public InputDataPin(Type type, ICSharpTranslatableOutputDataPin<Type> incomingPin)
{
Type = type;
this.incomingPin = incomingPin;
}

public IEnumerable<string> TranslateCallPureFunction()
{
if (incomingPin?.IsPure == true)
{
return incomingPin.TranslateCallPureFunction();
}

return new string[0];
}
}

private class OutputDataPin : ICSharpTranslatableOutputDataPin<Type>
{
public string VariableName { get; } = RandomGenerator.GetRandomLowerLetters(16);
public bool IsPure => node.IsPure;
public Type Type { get; }

private readonly ICSharpTranslatableNode<ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>> node;

public OutputDataPin(ICSharpTranslatableNode<ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>> node, Type type)
{
this.node = node;
Type = type;
}

public IEnumerable<string> TranslateCallPureFunction()
{
if (!IsPure)
{
throw new Exception();
}

return node.TranslateCallPureFunction();
}
}

private abstract class BaseCSharpTranslatableNode : ICSharpTranslatableNode<ICSharpTranslatableInputExecPin, ICSharpTranslatableOutputExecPin, ICSharpTranslatableInputDataPin<Type>, ICSharpTranslatableOutputDataPin<Type>>
private abstract class BaseCSharpTranslatableNode : ICSharpTranslatableNode
{
public IEnumerable<ICSharpTranslatableInputExecPin> InputExecPins => ixps;
public IEnumerable<ICSharpTranslatableOutputExecPin> OutputExecPins => oxps;
public IEnumerable<ICSharpTranslatableInputDataPin<Type>> InputDataPins => idps;
public IEnumerable<ICSharpTranslatableOutputDataPin<Type>> OutputDataPins => odps;

protected readonly IList<ICSharpTranslatableInputDataPin<Type>> idps = new List<ICSharpTranslatableInputDataPin<Type>>();
protected readonly IList<ICSharpTranslatableOutputDataPin<Type>> odps = new List<ICSharpTranslatableOutputDataPin<Type>>();
protected readonly IList<ICSharpTranslatableInputExecPin> ixps = new List<ICSharpTranslatableInputExecPin>();
protected readonly IList<ICSharpTranslatableOutputExecPin> oxps = new List<ICSharpTranslatableOutputExecPin>();
public IImmutableList<(Type type, string variableName)> Inputs { get; private set; } = ImmutableList<(Type, string)>.Empty;
public IImmutableList<(Type type, string variableName)> Outputs { get; private set; } = ImmutableList<(Type, string)>.Empty;
public IImmutableList<string> ExecInputs { get; private set; } = ImmutableList<string>.Empty;
public int ExecOutputCount { get; private set; }

public virtual bool IsPure { get; }
public string PureFunctionName { get; }
Expand All @@ -102,72 +33,41 @@ public BaseCSharpTranslatableNode()
PureFunctionName = $"{GetType().Name}_{nodeCounter++}";
}

public virtual string TranslateVariables()
{
return "";
}

public virtual string TranslatePureFunctions()
{
return "";
}
public virtual string TranslateVariables() => string.Concat(Inputs.Concat(Outputs).Select(x => $"{x.type} {x.variableName} = default({x.type});\n"));

public virtual string TranslateStates()
{
return "";
}

public IEnumerable<string> TranslateCallPureFunction()
{
var pureCalls = idps.SelectMany(idp => idp.TranslateCallPureFunction());
public virtual string TranslatePureFunctions() => "";

if (IsPure)
{
pureCalls = pureCalls.Concat(new[] { $"{PureFunctionName}();" });
}
public virtual string TranslateStates(IImmutableList<string> outputExecLabels, string pureCalls) => "";

return pureCalls.Distinct();
}
protected void AddInput(Type type) => Inputs = Inputs.Add((type, RandomGenerator.GetRandomLowerLetters(8)));
protected void AddOutput(Type type) => Outputs = Outputs.Add((type, RandomGenerator.GetRandomLowerLetters(8)));
protected void AddExecInput() => ExecInputs = ExecInputs.Add(RandomGenerator.GetRandomLowerLetters(8));
protected void AddExecOutput() => ExecOutputCount++;
}

private class ConcatenateNode : BaseCSharpTranslatableNode
{
private readonly ICSharpTranslatableInputExecPin execTo;

public ConcatenateNode(ICSharpTranslatableOutputDataPin<Type> aFrom, ICSharpTranslatableOutputDataPin<Type> bFrom, ICSharpTranslatableInputExecPin execTo)
public ConcatenateNode()
{
ixps.Add(new InputExecPin());
idps.Add(new InputDataPin(typeof(string), aFrom));
idps.Add(new InputDataPin(typeof(string), bFrom));
odps.Add(new OutputDataPin(this, typeof(string)));
oxps.Add(new OutputExecPin());

this.execTo = execTo;
AddInput(typeof(string));
AddInput(typeof(string));
AddOutput(typeof(string));
AddExecInput();
AddExecOutput();
}

public override string TranslateVariables()
{
return $"var {odps.Single().VariableName} = default(string);\n";
}

public override string TranslateStates()
public override string TranslateStates(IImmutableList<string> outputExecLabels, string pureCalls)
{
StringBuilder builder = new StringBuilder();

// Translate label
builder.Append($"{ixps[0].Label}:\n");

// Translate pure calls
builder.Append(string.Join("\n", TranslateCallPureFunction()));
builder.Append("\n");
builder.Append($"{ExecInputs[0]}:\n");
builder.Append(pureCalls);
builder.Append($"{Outputs.Single().variableName} = {Inputs[0].variableName} + \" \" + {Inputs[1].variableName};\n");

// Translate actual logic and result assignment
builder.Append($"{odps.Single().VariableName} = {idps[0].VariableName} + \" \" + {idps[1].VariableName};\n");

// Translate goto next
if (execTo != null)
var outputLabel = outputExecLabels.Single();
if (outputLabel != null)
{
builder.Append($"goto {execTo.Label};\n");
builder.Append($"goto {outputLabel};\n");
}
else
{
Expand All @@ -187,54 +87,29 @@ private class LiteralNode : BaseCSharpTranslatableNode
public LiteralNode(Type type, object value)
{
this.value = value;
odps.Add(new OutputDataPin(this, type));
AddOutput(type);
}

public override string TranslateVariables()
{
var odp = OutputDataPins.Single();

return $"const {odp.Type.FullName} {odp.VariableName} = {value};\n";
}

public override string TranslatePureFunctions()
{
StringBuilder builder = new StringBuilder();

builder.Append($"void {PureFunctionName}()\n");
builder.Append("{\n");
//builder.Append($"{OutputDataPins.Single().VariableName} = {value};\n");
builder.Append("}\n");

return builder.ToString();
var output = Outputs.Single();
return $"const {output.type.FullName} {output.variableName} = {value};\n";
}
}

private class ReverseStringNode : BaseCSharpTranslatableNode
{
public override bool IsPure => true;

public ReverseStringNode(ICSharpTranslatableOutputDataPin<Type> aFrom)
{
idps.Add(new InputDataPin(typeof(string), aFrom));
odps.Add(new OutputDataPin(this, typeof(string)));
}

public override string TranslateVariables()
public ReverseStringNode()
{
return $"var {OutputDataPins.Single().VariableName} = default(string);\n";
AddInput(typeof(string));
AddOutput(typeof(string));
}

public override string TranslatePureFunctions()
{
StringBuilder builder = new StringBuilder();

builder.Append($"void {PureFunctionName}()\n");
builder.Append("{\n");
builder.Append($"{OutputDataPins.Single().VariableName} = string.Concat({idps.Single().VariableName}.Reverse());\n");
builder.Append("}\n");

return builder.ToString();
return $"{Outputs.Single().variableName} = string.Concat({Inputs.Single().variableName}.Reverse());\n";
}
}
}
Expand Down
Loading