#pragma warning disable 1573 /// <inheritdoc cref="PlQuery(string)" /> /// <summary>locating the predicate in the named module.</summary> /// <param name="module">locating the predicate in the named module.</param> public PlQuery(string module, string goal) { if (string.IsNullOrEmpty(goal)) { throw new ArgumentNullException("goal"); } _module = module; var queryString = goal; try { // call read_term(Term_of_query_string, [variable_names(VN)]). // read_term_from_atom('noun(ş,C)', T, [variable_names(Vars)]). // befor 2014 with redirected IO-Streams (PlQuery_Old_Kill_unused) var atom = new PlTerm("'" + goal.Replace("'", @"\'") + "'"); PlTerm term = PlTerm.PlVar(); PlTerm options = PlTerm.PlVar(); PlTerm variablenames = PlTerm.PlVar(); PlTerm l = PlTerm.PlTail(options); l.Append(PlTerm.PlCompound("variable_names", variablenames)); l.Close(); var args = new PlTermV(atom, term, options); if (!PlCall("read_term_from_atom", args)) { throw new PlLibException("PlCall read_term_from_atom/3 fails! goal:" + queryString); } // set list of variables and variable_names into _queryVariables foreach (PlTerm t in variablenames.ToList()) { // t[0]='=' , t[1]='VN', t[2]=_G123 _queryVariables.Add(new PlQueryVar(t[1].ToString(), t[2])); } // Build the query _name = term.Name; // is ok e.g. for listing/0. // Check.Require(term.Arity > 0, "PlQuery(PlTerm t): t.Arity must be greater than 0."); _av = new PlTermV(term.Arity); for (int index = 0; index < term.Arity; index++) { if (0 == LibPl.PL_get_arg(index + 1, term.TermRef, _av[index].TermRef)) { throw new PlException("PL_get_arg in PlQuery " + term.ToString()); } } } #if _DEBUG catch (Exception ex) { System.Diagnostics.Debug.Print(ex.Message); Console.WriteLine(ex.Message); } #endif finally { // NBT } }
#pragma warning disable 1573 /// <inheritdoc cref="PlQuery(string)" /> /// <summary>locating the predicate in the named module.</summary> /// <param name="module">locating the predicate in the named module.</param> public PlQuery(string module, string goal) { _module = module; _query_string = goal; //_query_string = "(" + _query_string + ")"; if (!_query_string.EndsWith(".", StringComparison.Ordinal)) { _query_string += "."; } _query_string += Environment.NewLine; // redirect read stream DelegateStreamReadFunction old_read_function = PlEngine._function_read; DelegateStreamReadFunction rf = new DelegateStreamReadFunction(Sread); if (PrologCLR.RedirectStreams) { PlEngine.SetStreamFunctionRead(PlStreamType.Input, rf); } try { // call read_term(Term_of_query_string, [variable_names(VN)]). PlTerm term = PlTerm.PlVar(); PlTerm option_list = PlTerm.PlVar(); PlTerm variablenames_list = PlTerm.PlVar(); PlTerm l = PlTerm.PlTail(option_list); l.Append(PlTerm.PlCompound("variable_names", variablenames_list)); l.Close(); PlTermV args = new PlTermV(term, option_list); // NOTE: read/1 needs a dot ('.') at the end if (!PlQuery.PlCall("read_term", args)) { throw new PlLibException("PlCall read_term fails! goal:" + _query_string); } // restore stream function if (PrologCLR.RedirectStreams) { PlEngine.SetStreamFunctionRead(PlStreamType.Input, old_read_function); } // set list of variables and variable_names into _queryVariables foreach (PlTerm t in variablenames_list.ToList()) { // t[0]='=' , t[1]='VN', t[2]=_G123 _queryVariables.Add(new PlQueryVar(t[1].ToString(), t[2])); } // Build the query _name = term.Name; // is ok e.g. for listing/0. // Check.Require(term.Arity > 0, "PlQuery(PlTerm t): t.Arity must be greater than 0."); _av = new PlTermV(term.Arity); for (int index = 0; index < term.Arity; index++) { if (0 == libpl.PL_get_arg(index + 1, term.TermRef, _av[index].TermRef)) { throw new PlException("PL_get_arg in PlQuery " + term.ToString()); } } } #if _DEBUG catch (Exception ex) { PrologCLR.ConsoleTrace(ex.Message); } #endif finally { // NBT } }