/// <summary> /// Initialize the source from a valid <see cref="ModelPath"/> /// </summary> /// <param name="instancePath"></param> void InitializeFromModelPath(ModelPath instancePath, string path, out ModelProperty sourceProperty) { this.Path = path; this.IsStatic = false; this.RootType = instancePath.RootType.Name; var tokens = tokenizer.Matches(path); this.steps = new SourceStep[tokens.Count]; var rootType = instancePath.RootType; int i = 0; sourceProperty = null; foreach (Match token in tokens) { sourceProperty = rootType.Properties[token.Groups["Property"].Value]; if (sourceProperty == null) { throw new ArgumentException(String.Format("Property {0} is not valid for type {1} in path {2}.", token.Groups["Property"].Value, rootType.Name, path)); } int index; if (!Int32.TryParse(token.Groups["Index"].Value, out index)) { index = -1; } steps[i] = new SourceStep() { Property = sourceProperty.Name, Index = index, DeclaringType = sourceProperty.DeclaringType, IsReferenceProperty = sourceProperty is ModelReferenceProperty }; rootType = sourceProperty is ModelReferenceProperty ? ((ModelReferenceProperty)sourceProperty).PropertyType : null; i++; } this.SourceProperty = sourceProperty.Name; this.SourceType = sourceProperty.DeclaringType.Name; }
/// <summary> /// Initialize the source from a valid <see cref="ModelPath"/> /// </summary> /// <param name="instancePath"></param> void InitializeFromModelPath(ModelPath instancePath, string path, out ModelProperty sourceProperty) { this.Path = path; this.IsStatic = false; this.RootType = instancePath.RootType.Name; var tokens = tokenizer.Matches(path); this.steps = new SourceStep[tokens.Count]; var rootType = instancePath.RootType; int i = 0; sourceProperty = null; foreach(Match token in tokens) { sourceProperty = rootType.Properties[token.Groups["Property"].Value]; if (sourceProperty == null) throw new ArgumentException(String.Format("Property {0} is not valid for type {1} in path {2}.", token.Groups["Property"].Value, rootType.Name, path)); int index; if (!Int32.TryParse(token.Groups["Index"].Value, out index)) index = -1; steps[i] = new SourceStep() { Property = sourceProperty.Name, Index = index, DeclaringType = sourceProperty.DeclaringType.Name, IsReferenceProperty = sourceProperty is ModelReferenceProperty }; rootType = sourceProperty is ModelReferenceProperty ? ((ModelReferenceProperty)sourceProperty).PropertyType : null; i++; } this.SourceProperty = sourceProperty.Name; this.SourceType = sourceProperty.DeclaringType.Name; }
public static RecipeGraph CreateFactory(RecipeGraph recipe) { if (recipe == null) { throw new ArgumentNullException("recipe"); } var inputs = new List <SourceStep>(); var outputs = new List <SinkStep>(); var wastes = new List <SinkStep>(); var resources = new List <FlowStep>(); var transforms = new List <TransformStep>(); foreach (var resource in recipe.Resources) { resources.Add(new FlowStep(resource.Item) { Parent = resource }); } foreach (var input in recipe.InputNodes) { var r = new FlowStep(input.Item) { Parent = input }; var s = new SourceStep(input.Item) { Parent = input }; r.Previous.Add(s); resources.Add(r); inputs.Add(s); } foreach (var output in recipe.OutputNodes) { var r = new FlowStep(output.Item) { Parent = output }; var o = new SinkStep(output.Item) { Parent = output }; o.Previous.Add(r); resources.Add(r); outputs.Add(o); } foreach (var waste in recipe.WasteNodes) { var w = new SinkStep(waste.Item) { Parent = waste }; wastes.Add(w); w.Previous.Add(resources.Where((r) => r.Item.Item == waste.Item.Item).First()); } foreach (var transform in recipe.Transformations) { var amount = transform.Amount; var transformRecipe = transform.Recipe; var building = FirstMatchingBuilding(transformRecipe.Buildings); var modTime = building.MaxProductionFor(transformRecipe); var nrOfFactories = Math.Ceiling(amount / modTime); for (int i = 0; i < nrOfFactories; i++) { var p = new ProductionStep(transform.Recipe, transform.Amount / nrOfFactories, building) { Parent = transform }; foreach (var input in transformRecipe.Ingredients) { p.Previous.Add(resources.Where((r) => r.Item.Item == input.Item).First()); } foreach (var output in transformRecipe.Results) { resources.Where((r) => r.Item.Item == output.Item).First().Previous.Add(p); } transforms.Add(p); } } return(new RecipeGraph(wastes, inputs, outputs, resources, transforms)); }