public bool IsTrue(Parser.Binding query) { foreach (Stream walkStream in this.Query(query)) { return(true); } return(false); }
public string[] GetValues(Parser.Binding binding) { foreach (Stream stream in this.Query(binding)) { return(this.WalkTokens(binding.tokens, stream)); } return(null); }
private IEnumerable <Stream> Query(Parser.Binding binding) { Relation relation = this.relations[binding.key]; Pair[] vars = TokenToPairs(binding.tokens); Stream stream = new Stream(); foreach (Stream walkStream in relation.Query(vars).Walk(stream)) { yield return(walkStream); } }
private Parser.Binding GetBindingWithStream(string line, Stream stream) { Parser.Binding initial = Parser.GetBinding(line); Parser.Token[] tokens = new Parser.Token[initial.tokens.Length]; string[] walked = this.WalkTokens(initial.tokens, stream); for (int i = 0; i < tokens.Length; i++) { tokens[i] = new Parser.Token(); tokens[i].type = walked[i] == null ? Parser.TokenType.Variable : Parser.TokenType.String; tokens[i].value = walked[i]; } initial.tokens = tokens; return(initial); }
public void Claim(Parser.Binding binding) { Relation relation = this.GetRelation(binding.key); if (this.exclusions.ContainsKey(binding.key)) { foreach (Exclusion exclusion in this.exclusions[binding.key]) { this.RemoveExclusions(exclusion, binding); } } Pair[] values = StateMachine.TokenToPairs(binding.tokens); relation.AddFact(values); }
private void RemoveExclusions(Exclusion exclusion, Parser.Binding binding) { // Remove any claims that would conflict with the input claim (given by binding) // via the given exclusion. Relation relation = this.GetRelation(exclusion.relationKey); Pair[] vars = new Pair[binding.tokens.Length]; for (int i = 0; i < vars.Length; i++) { if (exclusion.boundVars[i]) { vars[i] = Pair.Value(binding.tokens[i].value); } else { vars[i] = Pair.Fresh(); } } if (!exclusion.rule.IsTrue(vars)) { return; } List <string[]> varsToRemove = new List <string[]>(); foreach (Stream stream in relation.Query(vars, false).Walk(new Stream())) { string[] v = new string[vars.Length]; for (int i = 0; i < v.Length; i++) { v[i] = stream.Walk(vars[i]).key; } varsToRemove.Add(v); } foreach (string[] factToRemove in varsToRemove) { relation.RemoveFact(factToRemove); } }
public void PerformAction(Parser.Binding binding) { Action action = this.GetAction(binding); Pair[] vars = TokenToPairs(binding.tokens); Pair outKey = Pair.Fresh(); foreach (Stream stream in action.GetGoal(vars, outKey).Walk(new Stream())) { string payloadKey = stream.Walk(outKey).key; Parser.Line[] payload = this.actionPayloads[payloadKey]; foreach (Parser.Line line in payload) { if (line.type == Parser.LineType.Callback) { string name = Regex.Match(line.content, "^([a-zA-Z0-9]+):").Groups[1].Value; string restOfLine = Regex.Replace(line.content, "^[a-zA-Z0-9]+: ?", ""); restOfLine = restOfLine.Trim(); if (name == "claim") { Parser.Binding reified = this.GetBindingWithStream(restOfLine, stream); this.Claim(reified); } else if (name == "action") { Parser.Binding reified = this.GetBindingWithStream(restOfLine, stream); this.PerformAction(reified); } else { string[] variables = this.WalkTokens(line.binding.tokens, stream); this.DoCallback(name, variables); } } } return; } }
private Relation GetRelation(Parser.Binding binding) { return(this.GetRelation(binding.key)); }
public string[] GetValues(string query) { Parser.Binding binding = Parser.GetBinding(query); return(this.GetValues(binding)); }
public bool IsTrue(string query) { Parser.Binding binding = Parser.GetBinding(query); return(this.IsTrue(binding)); }
private IEnumerable <Stream> Query(string query) { Parser.Binding binding = Parser.GetBinding(query); return(this.Query(binding)); }
public void PerformAction(string query) { Parser.Binding binding = Parser.GetBinding(query); this.PerformAction(binding); }
public void Claim(string claim) { Parser.Binding binding = Parser.GetBinding(claim); this.Claim(binding); }
private Action GetAction(Parser.Binding binding) { return(this.GetAction(binding.key)); }