Ejemplo n.º 1
0
        public void StreamRead()
        {
            var rf = new DelegateStreamReadFunction(Sread);

            PlEngine.SetStreamFunctionRead(PlStreamType.Input, rf);
            // NOTE: read/1 needs a dot ('.') at the end
            PlQuery.PlCall("assert( (test_read(A) :- read(A)) )");
            PlTerm t = PlQuery.PlCallQuery("test_read(A)");

            Assert.AreEqual(ValidationStringRead, t.ToString() + ".");
        }
Ejemplo n.º 2
0
#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
            }
        }