/// <summary> /// Add a cell to the list of constrained cells. /// </summary> /// <remarks> /// <para> /// Note that this function expects that the cell is making use of /// "CONSTRAIN" function, and will reject a cell that does not. /// </para> /// </remarks> /// <param name="key">The CreamCheese address of the cell to be /// added.</param> /// <param name="formula">The cell's formula.</param> public void Add(string key, string formula) { if(formula.StartsWith("CONSTRAIN")) { if(_cells.ContainsKey(key)) { _cells[key].Formula = formula; } else { _cells.Add(key, new Cell(key, formula)); } _lastSolution = null; } }
Change(string key, string formula) { if (_cells.ContainsKey(key)) { _cells[key].Formula = formula; if (!formula.StartsWith("CONSTRAIN")) { _cells.Remove(key); } _lastSolution = null; } }
Add(string key, string formula) { if (formula.StartsWith("CONSTRAIN")) { if (_cells.ContainsKey(key)) { _cells[key].Formula = formula; } else { _cells.Add(key, new Cell(key, formula)); } _lastSolution = null; } }
CalculateValues() { Cream.Network network = new Cream.Network(); foreach (KeyValuePair <string, Cell> kvp in _cells) { kvp.Value.Variable = new Cream.IntVariable(network, kvp.Key); } foreach (KeyValuePair <string, Cell> kvp in _cells) { CP.Semantics semantics = new CP.Semantics(kvp.Value, _cells, network, _spreadSheet); FP.Parser fp = new FP.Parser(kvp.Value.Formula, semantics); fp.Parse(); } Cream.Solver solver = new Cream.DefaultSolver(network); _lastSolution = solver.FindFirst(); }
CreamCheese(SpreadSheet spreadSheet) { _spreadSheet = spreadSheet; _lastSolution = null; _cells = new Dictionary <string, Cell>(); }
/// <summary> /// To be called when a cell is changed. /// </summary> /// <param name="key">The CreamCheese address of the cell that has /// changed.</param> /// <param name="formula">The cell's formula.</param> public void Change(string key, string formula) { if(_cells.ContainsKey(key)) { _cells[key].Formula = formula; if(!formula.StartsWith("CONSTRAIN")) { _cells.Remove(key); } _lastSolution = null; } }
/// <summary> /// Constructs and initializes a CreamCheese instance. /// </summary> /// <param name="spreadSheet">An instance of /// <see cref="SpreadSheet"/>.</param> public CreamCheese(SpreadSheet spreadSheet) { _spreadSheet = spreadSheet; _lastSolution = null; _cells = new Dictionary<string, Cell>(); }
/// <summary> /// Converts the constrained cells to a CSP, and gets the solution /// from the solver. /// </summary> private void CalculateValues() { Cream.Network network = new Cream.Network(); foreach(KeyValuePair<string, Cell> kvp in _cells) { kvp.Value.Variable = new Cream.IntVariable(network, kvp.Key); } foreach(KeyValuePair<string, Cell> kvp in _cells) { CP.Semantics semantics = new CP.Semantics(kvp.Value, _cells, network, _spreadSheet); FP.Parser fp = new FP.Parser(kvp.Value.Formula, semantics); fp.Parse(); } Cream.Solver solver = new Cream.DefaultSolver(network); _lastSolution = solver.FindFirst(); }