Esempio n. 1
0
        public void BuildReturnParameters(ProcedureInvocation invocation, SequenceVariable[] ReturnVars, InheritanceType ownerType, out String returnParameterDeclarations, out String returnArguments, out String returnAssignments)
        {
            // can't use the normal xgrs variables for return value receiving as the type of an out-parameter must be invariant
            // this is bullshit, as it is perfectly safe to assign a subtype to a variable of a supertype
            // so we create temporary variables of exact type, which are used to receive the return values,
            // and finally we assign these temporary variables to the real xgrs variables

            returnParameterDeclarations = "";
            returnArguments             = "";
            returnAssignments           = "";
            for (int i = 0; i < ownerType.GetProcedureMethod(invocation.Name).Outputs.Length; ++i)
            {
                String varName;
                if (ReturnVars.Length != 0)
                {
                    varName = GetUniqueId() + ReturnVars[i].PureName;
                }
                else
                {
                    varName = GetUniqueId();
                }
                String typeName   = TypesHelper.DotNetTypeToXgrsType(ownerType.GetProcedureMethod(invocation.Name).Outputs[i]);
                String tmpvarName = "tmpvar_" + varName;
                returnParameterDeclarations += TypesHelper.XgrsTypeToCSharpType(typeName, model) + " " + tmpvarName + "; ";
                returnArguments             += ", out " + tmpvarName;
                if (ReturnVars.Length != 0)
                {
                    returnAssignments += SetVar(ReturnVars[i], tmpvarName);
                }
            }
        }
Esempio n. 2
0
        public String BuildParameters(Invocation invocation, SequenceExpression[] ArgumentExpressions, IProcedureDefinition procedureMethod, SourceBuilder source)
        {
            String parameters = "";

            for (int i = 0; i < ArgumentExpressions.Length; i++)
            {
                String typeName = TypesHelper.DotNetTypeToXgrsType(procedureMethod.Inputs[i]);
                String cast     = "(" + TypesHelper.XgrsTypeToCSharpType(typeName, model) + ")";
                parameters += ", " + cast + exprGen.GetSequenceExpression(ArgumentExpressions[i], source);
            }
            return(parameters);
        }
Esempio n. 3
0
        private void GenerateGenericExternalDefinedSequenceApplicationMethod(SourceBuilder source, DefinedSequenceInfo sequence)
        {
            source.AppendFront("public override bool Apply(GRGEN_LIBGR.IGraphProcessingEnvironment procEnv, object[] arguments, out object[] returnValues)");
            source.AppendFront("{\n");
            source.Indent();
            source.AppendFront("GRGEN_LGSP.LGSPGraph graph = ((GRGEN_LGSP.LGSPActionExecutionEnvironment)procEnv).graph;\n");

            for (int i = 0; i < sequence.Parameters.Length; ++i)
            {
                string typeName = TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.ParameterTypes[i]), model);
                source.AppendFront(typeName + " var_" + sequence.Parameters[i]);
                source.Append(" = (" + typeName + ")arguments[" + i + "];\n");
            }
            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                string typeName = TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.OutParameterTypes[i]), model);
                source.AppendFront(typeName + " var_" + sequence.OutParameters[i]);
                source.Append(" = " + TypesHelper.DefaultValueString(typeName, model) + ";\n");
            }


            source.AppendFront("bool result = ApplyXGRS_" + sequence.Name + "((GRGEN_LGSP.LGSPGraphProcessingEnvironment)procEnv");
            for (int i = 0; i < sequence.Parameters.Length; ++i)
            {
                source.Append(", var_" + sequence.Parameters[i]);
            }
            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                source.Append(", ref var_" + sequence.OutParameters[i]);
            }
            source.Append(");\n");


            source.AppendFront("returnValues = ReturnValues;\n");

            if (sequence.OutParameters.Length > 0)
            {
                source.AppendFront("if(result) {\n");
                source.Indent();
                for (int i = 0; i < sequence.OutParameters.Length; ++i)
                {
                    source.AppendFront("returnValues[" + i + "] = var_" + sequence.OutParameters[i] + ";\n");
                }
                source.Unindent();
                source.AppendFront("}\n");
            }

            source.AppendFront("return result;\n");
            source.Unindent();
            source.AppendFront("}\n");
        }
Esempio n. 4
0
        private void GenerateExactExternalDefinedSequenceApplicationMethod(SourceBuilder source, DefinedSequenceInfo sequence)
        {
            source.AppendFront("public static bool Apply_" + sequence.Name + "(GRGEN_LIBGR.IGraphProcessingEnvironment procEnv");
            for (int i = 0; i < sequence.Parameters.Length; ++i)
            {
                source.Append(", " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.ParameterTypes[i]), model) + " var_");
                source.Append(sequence.Parameters[i]);
            }
            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                source.Append(", ref " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.OutParameterTypes[i]), model) + " var_");
                source.Append(sequence.OutParameters[i]);
            }
            source.Append(")\n");
            source.AppendFront("{\n");
            source.Indent();

            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                string typeName = TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.OutParameterTypes[i]), model);
                source.AppendFront(typeName + " vari_" + sequence.OutParameters[i]);
                source.Append(" = " + TypesHelper.DefaultValueString(typeName, model) + ";\n");
            }
            source.AppendFront("bool result = ApplyXGRS_" + sequence.Name + "((GRGEN_LGSP.LGSPGraphProcessingEnvironment)procEnv");
            for (int i = 0; i < sequence.Parameters.Length; ++i)
            {
                source.Append(", var_" + sequence.Parameters[i]);
            }
            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                source.Append(", ref var_" + sequence.OutParameters[i]);
            }
            source.Append(");\n");
            if (sequence.OutParameters.Length > 0)
            {
                source.AppendFront("if(result) {\n");
                source.Indent();
                for (int i = 0; i < sequence.OutParameters.Length; ++i)
                {
                    source.AppendFront("var_" + sequence.OutParameters[i]);
                    source.Append(" = vari_" + sequence.OutParameters[i] + ";\n");
                }
                source.Unindent();
                source.AppendFront("}\n");
            }

            source.AppendFront("return result;\n");
            source.Unindent();
            source.AppendFront("}\n");
        }
Esempio n. 5
0
        private void GenerateInternalDefinedSequenceApplicationMethodStub(SourceBuilder source, DefinedSequenceInfo sequence, String externalActionsExtensionFilename)
        {
            source.AppendFrontFormat("// You must implement the following function in the same partial class in ./{0}\n", externalActionsExtensionFilename);

            source.AppendFront("//public static bool ApplyXGRS_" + sequence.Name + "(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv");
            for (int i = 0; i < sequence.Parameters.Length; ++i)
            {
                source.Append(", " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.ParameterTypes[i]), model) + " var_");
                source.Append(sequence.Parameters[i]);
            }
            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                source.Append(", ref " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.OutParameterTypes[i]), model) + " var_");
                source.Append(sequence.OutParameters[i]);
            }
            source.Append(")\n");
        }
Esempio n. 6
0
        public String BuildParameters(Invocation invocation, SequenceExpression[] ArgumentExpressions, IFunctionDefinition functionMethod, SourceBuilder source)
        {
            String parameters = "";

            for (int i = 0; i < ArgumentExpressions.Length; i++)
            {
                if (ArgumentExpressions[i] != null)
                {
                    String typeName = TypesHelper.DotNetTypeToXgrsType(functionMethod.Inputs[i]);
                    String cast     = "(" + TypesHelper.XgrsTypeToCSharpType(typeName, model) + ")";
                    parameters += ", " + cast + exprGen.GetSequenceExpression(ArgumentExpressions[i], source);
                }
                else
                {
                    // the sequence parser always emits all argument expressions, for interpreted and compiled
                    throw new Exception("Internal error: missing argument expressions");
                }
            }
            return(parameters);
        }
Esempio n. 7
0
        public bool GenerateXGRSCode(string xgrsName, String package, String xgrsStr,
                                     String[] paramNames, GrGenType[] paramTypes,
                                     String[] defToBeYieldedToNames, GrGenType[] defToBeYieldedToTypes,
                                     SourceBuilder source, int lineNr)
        {
            Dictionary <String, String> varDecls = new Dictionary <String, String>();

            for (int i = 0; i < paramNames.Length; ++i)
            {
                varDecls.Add(paramNames[i], TypesHelper.DotNetTypeToXgrsType(paramTypes[i]));
            }
            for (int i = 0; i < defToBeYieldedToNames.Length; ++i)
            {
                varDecls.Add(defToBeYieldedToNames[i], TypesHelper.DotNetTypeToXgrsType(defToBeYieldedToTypes[i]));
            }

            Sequence seq;

            try
            {
                SequenceParserEnvironmentCompiled parserEnv = new SequenceParserEnvironmentCompiled(package, actionNames, model);
                List <string> warnings = new List <string>();
                seq = SequenceParser.ParseSequence(xgrsStr, parserEnv, varDecls, warnings);
                foreach (string warning in warnings)
                {
                    Console.Error.WriteLine("The exec statement \"" + xgrsStr
                                            + "\" given on line " + lineNr + " reported back:\n" + warning);
                }
                seq.Check(env);
                seq.SetNeedForProfilingRecursive(emitProfiling);
            }
            catch (ParseException ex)
            {
                Console.Error.WriteLine("The exec statement \"" + xgrsStr
                                        + "\" given on line " + lineNr + " caused the following error:\n" + ex.Message);
                return(false);
            }
            catch (SequenceParserException ex)
            {
                Console.Error.WriteLine("The exec statement \"" + xgrsStr
                                        + "\" given on line " + lineNr + " caused the following error:\n");
                HandleSequenceParserException(ex);
                return(false);
            }

            source.Append("\n");
            source.AppendFront("public static bool ApplyXGRS_" + xgrsName + "(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv");
            for (int i = 0; i < paramNames.Length; ++i)
            {
                source.Append(", " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(paramTypes[i]), model) + " var_");
                source.Append(paramNames[i]);
            }
            for (int i = 0; i < defToBeYieldedToTypes.Length; ++i)
            {
                source.Append(", ref " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(defToBeYieldedToTypes[i]), model) + " var_");
                source.Append(defToBeYieldedToNames[i]);
            }
            source.Append(")\n");
            source.AppendFront("{\n");
            source.Indent();

            source.AppendFront("GRGEN_LGSP.LGSPGraph graph = procEnv.graph;\n");
            source.AppendFront("GRGEN_LGSP.LGSPActions actions = procEnv.curActions;\n");

            neededEntitiesEmitter.Reset();

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugEntering(\"{0}\", \"{1}\");\n",
                                         InjectExec(xgrsName), xgrsStr.Replace("\\", "\\\\").Replace("\"", "\\\""));
            }

            neededEntitiesEmitter.EmitNeededVarAndRuleEntities(seq, source);

            seqGen.EmitSequence(seq, source);

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugExiting(\"{0}\");\n",
                                         InjectExec(xgrsName));
            }

            source.AppendFront("return " + seqGen.GetSequenceResult(seq) + ";\n");
            source.Unindent();
            source.AppendFront("}\n");

            List <SequenceExpressionContainerConstructor> containerConstructors = new List <SequenceExpressionContainerConstructor>();
            Dictionary <SequenceVariable, SetValueType>   variables             = new Dictionary <SequenceVariable, SetValueType>();

            seq.GetLocalVariables(variables, containerConstructors, null);
            foreach (SequenceExpressionContainerConstructor cc in containerConstructors)
            {
                SequenceContainerConstructorEmitter.GenerateContainerConstructor(model, cc, source);
            }

            return(true);
        }
Esempio n. 8
0
        private void GenerateInternalDefinedSequenceApplicationMethod(SourceBuilder source, DefinedSequenceInfo sequence, Sequence seq)
        {
            source.AppendFront("public static bool ApplyXGRS_" + sequence.Name + "(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv");
            for (int i = 0; i < sequence.Parameters.Length; ++i)
            {
                source.Append(", " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.ParameterTypes[i]), model) + " var_");
                source.Append(sequence.Parameters[i]);
            }
            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                source.Append(", ref " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.OutParameterTypes[i]), model) + " var_");
                source.Append(sequence.OutParameters[i]);
            }
            source.Append(")\n");
            source.AppendFront("{\n");
            source.Indent();

            source.AppendFront("GRGEN_LGSP.LGSPGraph graph = procEnv.graph;\n");
            source.AppendFront("GRGEN_LGSP.LGSPActions actions = procEnv.curActions;\n");

            neededEntitiesEmitter.Reset();

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugEntering(\"{0}\"", sequence.Name);
                for (int i = 0; i < sequence.Parameters.Length; ++i)
                {
                    source.Append(", var_");
                    source.Append(sequence.Parameters[i]);
                }
                source.Append(");\n");
            }

            neededEntitiesEmitter.EmitNeededVarAndRuleEntities(seq, source);

            seqGen.EmitSequence(seq, source);

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugExiting(\"{0}\"", sequence.Name);
                for (int i = 0; i < sequence.OutParameters.Length; ++i)
                {
                    source.Append(", var_");
                    source.Append(sequence.OutParameters[i]);
                }
                source.Append(");\n");
            }

            source.AppendFront("return " + seqGen.GetSequenceResult(seq) + ";\n");
            source.Unindent();
            source.AppendFront("}\n");

            List <SequenceExpressionContainerConstructor> containerConstructors = new List <SequenceExpressionContainerConstructor>();
            Dictionary <SequenceVariable, SetValueType>   variables             = new Dictionary <SequenceVariable, SetValueType>();

            seq.GetLocalVariables(variables, containerConstructors, null);
            foreach (SequenceExpressionContainerConstructor cc in containerConstructors)
            {
                SequenceContainerConstructorEmitter.GenerateContainerConstructor(model, cc, source);
            }
        }
Esempio n. 9
0
        public bool GenerateDefinedSequence(SourceBuilder source, DefinedSequenceInfo sequence)
        {
            Dictionary <String, String> varDecls = new Dictionary <String, String>();

            for (int i = 0; i < sequence.Parameters.Length; i++)
            {
                varDecls.Add(sequence.Parameters[i], TypesHelper.DotNetTypeToXgrsType(sequence.ParameterTypes[i]));
            }
            for (int i = 0; i < sequence.OutParameters.Length; i++)
            {
                varDecls.Add(sequence.OutParameters[i], TypesHelper.DotNetTypeToXgrsType(sequence.OutParameterTypes[i]));
            }

            Sequence seq;

            try
            {
                SequenceParserEnvironmentCompiled parserEnv = new SequenceParserEnvironmentCompiled(sequence.Package, actionNames, model);
                List <string> warnings = new List <string>();
                seq = SequenceParser.ParseSequence(sequence.XGRS, parserEnv, varDecls, warnings);
                foreach (string warning in warnings)
                {
                    Console.Error.WriteLine("In the defined sequence " + sequence.Name
                                            + " the exec part \"" + sequence.XGRS
                                            + "\" reported back:\n" + warning);
                }
                seq.Check(env);
                seq.SetNeedForProfilingRecursive(emitProfiling);
            }
            catch (ParseException ex)
            {
                Console.Error.WriteLine("In the defined sequence " + sequence.Name
                                        + " the exec part \"" + sequence.XGRS
                                        + "\" caused the following error:\n" + ex.Message);
                return(false);
            }
            catch (SequenceParserException ex)
            {
                Console.Error.WriteLine("In the defined sequence " + sequence.Name
                                        + " the exec part \"" + sequence.XGRS
                                        + "\" caused the following error:\n");
                HandleSequenceParserException(ex);
                return(false);
            }

            // exact sequence definition compiled class
            source.Append("\n");

            if (sequence.Package != null)
            {
                source.AppendFrontFormat("namespace {0}\n", sequence.Package);
                source.AppendFront("{\n");
                source.Indent();
            }

            source.AppendFront("public class Sequence_" + sequence.Name + " : GRGEN_LIBGR.SequenceDefinitionCompiled\n");
            source.AppendFront("{\n");
            source.Indent();

            GenerateSequenceDefinedSingleton(source, sequence);

            source.Append("\n");
            GenerateGenericMethodReturnValues(source, sequence);

            source.Append("\n");
            GenerateInternalDefinedSequenceApplicationMethod(source, sequence, seq);

            source.Append("\n");
            GenerateExactExternalDefinedSequenceApplicationMethod(source, sequence);

            source.Append("\n");
            GenerateGenericExternalDefinedSequenceApplicationMethod(source, sequence);

            // end of exact sequence definition compiled class
            source.Unindent();
            source.AppendFront("}\n");

            if (sequence.Package != null)
            {
                source.Unindent();
                source.AppendFront("}\n");
            }

            return(true);
        }
        public int ApplyRewrite(RuleInvocationParameterBindings paramBindings, int which, int localMaxMatches, bool special, bool test, List <FilterCall> filters)
        {
            int curMaxMatches = (localMaxMatches > 0) ? localMaxMatches : MaxMatches;

            object[] parameters;
            if (paramBindings.ArgumentExpressions.Length > 0)
            {
                parameters = paramBindings.Arguments;
                for (int i = 0; i < paramBindings.ArgumentExpressions.Length; i++)
                {
                    if (paramBindings.ArgumentExpressions[i] != null)
                    {
                        parameters[i] = paramBindings.ArgumentExpressions[i].Evaluate(this);
                    }
                }
            }
            else
            {
                parameters = null;
            }

            if (paramBindings.Subgraph != null)
            {
                SwitchToSubgraph((IGraph)paramBindings.Subgraph.GetVariableValue(this));
            }

#if DEBUGACTIONS || MATCHREWRITEDETAIL
            PerformanceInfo.StartLocal();
#endif
            IMatches matches = paramBindings.Action.Match(this, curMaxMatches, parameters);
#if DEBUGACTIONS || MATCHREWRITEDETAIL
            PerformanceInfo.StopMatch();
#endif
            for (int i = 0; i < filters.Count; ++i)
            {
                paramBindings.Action.Filter(this, matches, filters[i]);
            }

            Matched(matches, null, special);
            if (matches.Count == 0)
            {
                if (paramBindings.Subgraph != null)
                {
                    ReturnFromSubgraph();
                }
                return(0);
            }

            PerformanceInfo.MatchesFound += matches.Count;

            if (test)
            {
                if (paramBindings.Subgraph != null)
                {
                    ReturnFromSubgraph();
                }
                return(matches.Count);
            }

            Finishing(matches, special);

#if DEBUGACTIONS || MATCHREWRITEDETAIL
            PerformanceInfo.StartLocal();
#endif
            List <object[]> retElemsList = Replace(matches, which);
            if (which == -1)
            {
                IList[] returnVars = null;
                if (paramBindings.ReturnVars.Length > 0)
                {
                    returnVars = new IList[paramBindings.ReturnVars.Length];
                    for (int i = 0; i < paramBindings.ReturnVars.Length; i++)
                    {
                        returnVars[i] = (IList)paramBindings.ReturnVars[i].GetVariableValue(this);
                        if (returnVars[i] == null)
                        {
                            string returnType = TypesHelper.DotNetTypeToXgrsType(paramBindings.Action.RulePattern.Outputs[i]);
                            Type   valueType  = ContainerHelper.GetTypeFromNameForContainer(returnType, graph);
                            returnVars[i] = ContainerHelper.NewList(valueType);
                            paramBindings.ReturnVars[i].SetVariableValue(returnVars[i], this);
                        }
                        else
                        {
                            returnVars[i].Clear();
                        }
                    }
                }
                for (int curRetElemNum = 0; curRetElemNum < retElemsList.Count; ++curRetElemNum)
                {
                    object[] retElems = retElemsList[curRetElemNum];
                    for (int i = 0; i < paramBindings.ReturnVars.Length; i++)
                    {
                        returnVars[i].Add(retElems[i]);
                    }
                }
            }
            else
            {
                object[] retElems = retElemsList[0];
                for (int i = 0; i < paramBindings.ReturnVars.Length; i++)
                {
                    paramBindings.ReturnVars[i].SetVariableValue(retElems[i], this);
                }
            }
#if DEBUGACTIONS || MATCHREWRITEDETAIL
            PerformanceInfo.StopRewrite();
#endif
            Finished(matches, special);

            if (paramBindings.Subgraph != null)
            {
                ReturnFromSubgraph();
            }
            return(matches.Count);
        }