public static IsVariable ( |
||
literal | ||
return | bool |
public SimpleName(string description) { this.literals = new List <Literal>(); this.isGrounded = true; description = description.RemoveWhiteSpace(); var n = Name.BuildName(description); //Substituir if (n.IsComposed) { char[] literalLimiterChars = { ' ', ',', '(', ')' }; string[] stringLiterals = description.Split(literalLimiterChars, StringSplitOptions.RemoveEmptyEntries); var lPos = 0; var depth = 0; for (int i = 0; i < description.Length; i++) { //add literal Literal lit = null; switch (description[i]) { case '(': this.literals.Add(new Literal(stringLiterals[lPos], LiteralType.Root, depth)); lPos++; depth++; break; case ',': if (description[i - 1] != ')') { this.literals.Add(new Literal(stringLiterals[lPos], LiteralType.Param, depth)); lPos++; } break; case ')': if (description[i - 1] != ')') { this.literals.Add(new Literal(stringLiterals[lPos], LiteralType.Param, depth)); lPos++; } depth--; break; } } } else //if n is non composed { this.literals.Add(new Literal(description, LiteralType.Param, 0)); } //check if grounded foreach (var l in this.literals) { if (SimpleWFN.IsVariable(l)) { this.isGrounded = false; } } }
public SimpleName(IEnumerable <Literal> literalList) { this.literals = new List <Literal>(); this.isGrounded = true; // used to reset the depth of the first literal to 0 var delta = literalList.FirstOrDefault().depth; foreach (var l in literalList) { this.literals.Add(new Literal(l.description, l.type, l.depth - delta)); if (SimpleWFN.IsVariable(l)) { this.isGrounded = false; } } }
private static IEnumerable <Substitution> FindSubst(SimpleName n1, SimpleName n2) { // SubstitutionSet bindings = new SubstitutionSet(); Dictionary <string, Substitution> bindings = new Dictionary <string, Substitution>(); var idx1 = 0; var idx2 = 0; do { var l1 = n1.literals[idx1]; var l2 = n2.literals[idx2]; //neither literal is a variable if (!SimpleWFN.IsVariable(l1) && !SimpleWFN.IsVariable(l2)) { if (l1.description.EqualsIgnoreCase(l2.description)) { idx1++; idx2++; continue; } else { return(null); } } //both literals are a variable if (SimpleWFN.IsVariable(l1) && SimpleWFN.IsVariable(l2)) { if (!l1.description.EqualsIgnoreCase(l2.description)) { if (bindings.ContainsKey(l1.description)) { return(null); } bindings[l1.description] = new Substitution(l1.description, l2.description); } idx1++; idx2++; continue; } //only l1 is a variable if (SimpleWFN.IsVariable(l1) && !SimpleWFN.IsVariable(l2)) { var res = FindSubsAux(l1, l2, n2, bindings); if (res == -1) { return(null); } else { idx1++; idx2 += res; continue; } } //only l2 is a variable if (!SimpleWFN.IsVariable(l1) && SimpleWFN.IsVariable(l2)) { var res = FindSubsAux(l2, l1, n1, bindings); if (res == -1) { return(null); } else { idx1 += res; idx2++; continue; } } throw new Exception("Unexpected Situation"); } while (idx1 < n1.literals.Count && idx2 < n2.literals.Count); if (idx1 == n1.literals.Count && idx2 == n2.literals.Count) { return(bindings.Values); // full match } else { return(null); // only partial match }; }