Esempio n. 1
0
        /// <summary>
        /// Creates a new LGSPGraph or LGSPNamedGraph instance from the specified specification file.
        /// If the according dll does not exist or is out of date, the needed processing steps are performed automatically.
        /// </summary>
        /// <param name="gmFilename">Filename of the model specification file (.gm).</param>
        /// <param name="graphName">Name of the new graph.</param>
        /// <param name="statisticsPath">Optional path to a file containing the graph statistics to be used for building the matchers.</param>
        /// <param name="flags">Specifies how the specification is to be processed; only KeepGeneratedFiles and CompileWithDebug are taken care of!</param>
        /// <param name="externalAssemblies">List of external assemblies to reference.</param>
        /// <param name="named">Returns a named graph if true otherwise a non-named graph. You must cast the LGSPGraph returned to the inherited LGSPNamedGraph if named=true.</param>
        /// <param name="capacity">The initial capacity for the name maps (performance optimization, use 0 if unsure).</param>
        /// <exception cref="System.IO.FileNotFoundException">Thrown, when a needed specification file does not exist.</exception>
        /// <exception cref="System.Exception">Thrown, when something goes wrong.</exception>
        /// <returns>The new LGSPGraph or LGSPNamedGraph instance.</returns>
        public LGSPGraph CreateFromSpec(String gmFilename, String graphName, String statisticsPath,
                                        ProcessSpecFlags flags, List <String> externalAssemblies, bool named, int capacity)
        {
            if (!File.Exists(gmFilename))
            {
                throw new FileNotFoundException("The model specification file \"" + gmFilename + "\" does not exist!", gmFilename);
            }

            String modelFilename;

            if (MustGenerate(gmFilename, statisticsPath, flags, out modelFilename))
            {
                LGSPGrGen.ProcessSpecification(gmFilename, statisticsPath, flags, externalAssemblies.ToArray());
            }

            return(CreateGraph(modelFilename, graphName, named, "capacity=" + capacity.ToString()));
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new LGSPGraph or LGSPNamedGraph and LGSPActions instance from the specified specification file.
        /// If the according dlls do not exist or are out of date, the needed processing steps are performed automatically.
        /// </summary>
        /// <param name="grgFilename">Filename of the rule specification file (.grg).</param>
        /// <param name="graphName">Name of the new graph.</param>
        /// <param name="statisticsPath">Optional path to a file containing the graph statistics to be used for building the matchers.</param>
        /// <param name="flags">Specifies how the specification is to be processed; only KeepGeneratedFiles and CompileWithDebug are taken care of!</param>
        /// <param name="externalAssemblies">List of external assemblies to reference.</param>
        /// <param name="named">Returns a named graph if true otherwise a non-named graph. You must cast the LGSPGraph returned to the inherited LGSPNamedGraph if named=true.</param>
        /// <param name="capacity">The initial capacity for the name maps, only used if named (performance optimization, use 0 if unsure).</param>
        /// <param name="newGraph">Returns the new graph.</param>
        /// <param name="newActions">Returns the new BaseActions object.</param>
        /// <exception cref="System.IO.FileNotFoundException">Thrown, when a needed specification file does not exist.</exception>
        /// <exception cref="System.Exception">Thrown, when something goes wrong.</exception>
        public void CreateFromSpec(String grgFilename, String graphName, String statisticsPath,
                                   ProcessSpecFlags flags, List <String> externalAssemblies, bool named, int capacity,
                                   out LGSPGraph newGraph, out LGSPActions newActions)
        {
            if (!File.Exists(grgFilename))
            {
                throw new FileNotFoundException("The rule specification file \"" + grgFilename + "\" does not exist!", grgFilename);
            }

            String actionsFilename;
            String modelFilename;

            if (MustGenerate(grgFilename, statisticsPath, flags, out actionsFilename, out modelFilename))
            {
                LGSPGrGen.ProcessSpecification(grgFilename, statisticsPath, flags, externalAssemblies.ToArray());
            }

            newGraph   = named ? (LGSPNamedGraph)CreateNamedGraph(modelFilename, graphName, "capacity=" + capacity.ToString()) : (LGSPGraph)CreateGraph(modelFilename, graphName);
            newActions = LGSPActions.LoadActions(actionsFilename, newGraph);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new LGSPGraph or LGSPNamedGraph backend instance with the graph model provided by the graph model file and a name.
        /// </summary>
        /// <param name="modelFilename">Filename of a graph model file.</param>
        /// <param name="graphName">Name of the graph.</param>
        /// <param name="named">Returns a named graph if true otherwise a non-named graph. You must cast the LGSPGraph returned to the inherited LGSPNamedGraph if named=true.</param>
        /// <param name="parameters">Backend specific parameters.</param>
        /// <returns>The new IGraph backend instance.</returns>
        public LGSPGraph CreateGraph(String modelFilename, String graphName, bool named, params String[] parameters)
        {
            Assembly assembly;

            String extension = Path.GetExtension(modelFilename);

            if (extension.Equals(".cs", StringComparison.OrdinalIgnoreCase))
            {
                CSharpCodeProvider compiler   = new CSharpCodeProvider();
                CompilerParameters compParams = new CompilerParameters();
                compParams.ReferencedAssemblies.Add("System.dll");
                compParams.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(IBackend)).Location);

                //                compParams.GenerateInMemory = true;
                compParams.CompilerOptions = "/optimize";
                //                compParams.IncludeDebugInformation = true;
                compParams.OutputAssembly = String.Format("lgspmodelassembly-{0:00000}.dll", modelID++);

                CompilerResults compResults = compiler.CompileAssemblyFromFile(compParams, modelFilename);
                if (compResults.Errors.HasErrors)
                {
                    String errorMsg = compResults.Errors.Count + " Errors:";
                    foreach (CompilerError error in compResults.Errors)
                    {
                        errorMsg += String.Format("\r\nLine: {0} - {1}", error.Line, error.ErrorText);
                    }
                    throw new ArgumentException("Illegal model C# source code: " + errorMsg);
                }

                assembly = compResults.CompiledAssembly;
            }
            else if (extension.Equals(".dll", StringComparison.OrdinalIgnoreCase))
            {
                assembly = Assembly.LoadFrom(modelFilename);
                AddAssembly(assembly);
            }
            else if (extension.Equals(".gm", StringComparison.OrdinalIgnoreCase))
            {
                String modelDllFilename;
                if (MustGenerate(modelFilename, null, ProcessSpecFlags.UseNoExistingFiles, out modelDllFilename))
                {
                    LGSPGrGen.ProcessSpecification(modelFilename, null, ProcessSpecFlags.UseNoExistingFiles, new String[0]);
                }
                return(named ? (LGSPNamedGraph)CreateNamedGraph(modelDllFilename, graphName, parameters) : (LGSPGraph)CreateGraph(modelDllFilename, graphName, parameters));
            }
            else
            {
                throw new ArgumentException("The model filename must be either a .cs or a .dll filename!");
            }

            Type modelType = null;

            try
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (!type.IsClass || type.IsNotPublic)
                    {
                        continue;
                    }
                    if (type.GetInterface("IGraphModel") != null && type.GetInterface("IGraph") == null)
                    {
                        if (modelType != null)
                        {
                            throw new ArgumentException(
                                      "The given model contains more than one IGraphModel implementation!");
                        }
                        modelType = type;
                    }
                }
            }
            catch (ReflectionTypeLoadException e)
            {
                String errorMsg = "";
                foreach (Exception ex in e.LoaderExceptions)
                {
                    errorMsg += "- " + ex.Message + Environment.NewLine;
                }
                if (errorMsg.Length == 0)
                {
                    errorMsg = e.Message;
                }
                throw new ArgumentException(errorMsg);
            }
            if (modelType == null)
            {
                throw new ArgumentException("The given model does not contain an IGraphModel implementation!");
            }

            IGraphModel graphModel = (IGraphModel)Activator.CreateInstance(modelType);
            int         capacity   = 0;

            if (parameters != null)
            {
                foreach (String parameter in parameters)
                {
                    if (parameter.StartsWith("capacity="))
                    {
                        capacity = int.Parse(parameter.Substring("capacity=".Length));
                    }
                }
            }
            return(named ? new LGSPNamedGraph(graphModel, graphName, capacity) : new LGSPGraph(graphModel, graphName));
        }
Esempio n. 4
0
 /// <summary>
 /// Processes the given rule specification file and generates a model and actions library in the same directory as the specification file.
 /// </summary>
 /// <param name="specPath">The path to the rule specification file (.grg).</param>
 /// <exception cref="System.Exception">Thrown, when an error occurred.</exception>
 public void ProcessSpecification(string specPath)
 {
     LGSPGrGen.ProcessSpecification(specPath, null, ProcessSpecFlags.UseNoExistingFiles, new String[0]);
 }
Esempio n. 5
0
 /// <summary>
 /// Processes the given rule specification file and generates a model and actions library.
 /// </summary>
 /// <param name="specPath">The path to the rule specification file (.grg).</param>
 /// <param name="destDir">The directory, where the generated libraries are to be placed.</param>
 /// <param name="intermediateDir">A directory, where intermediate files can be placed.</param>
 /// <param name="statisticsPath">Optional path to a file containing the graph statistics to be used for building the matchers.</param>
 /// <param name="flags">Specifies how the specification is to be processed.</param>
 /// <param name="externalAssemblies">External assemblies to reference</param>
 /// <exception cref="System.Exception">Thrown, when an error occurred.</exception>
 public void ProcessSpecification(string specPath, string destDir, string intermediateDir, String statisticsPath, ProcessSpecFlags flags, params String[] externalAssemblies)
 {
     LGSPGrGen.ProcessSpecification(specPath, destDir, intermediateDir, statisticsPath, flags, externalAssemblies);
 }