Deprecated. Used to be used only in the context of Term.textToTerm() but a better solution has been implemented there and hence does not require this difficult method anymore. Left it for reference. This is called with query atom_to_term(Atom, Term, Bindings) https://www.swi-prolog.org/pldoc/doc_f
()
| 541 | * @return a Term with the variables with names |
| 542 | */ |
| 543 | @Deprecated |
| 544 | public final Map<String, Term> getSolutionWithVarNames() { |
| 545 | if (!open) { |
| 546 | throw new JPLException("Query is not open"); |
| 547 | } else { |
| 548 | // We will operate on the solution of query of the form atom_to_term('hello(a, X, Y)', T, B) |
| 549 | // The query has been executed and a solution is there, B and T have bindings |
| 550 | // T will be the atom converted into a term, in our case the term hello(a, _1, _2) |
| 551 | // B will map the name of the variables (as atoms) to the Prolog Vars _ thus providing access to |
| 552 | // the Prolog _ variables via the names in the query (X and Y here) |
| 553 | // In our example T = hello(1, _1, _2) and |
| 554 | // B = ['X' = _1, 'Y' = _2] |
| 555 | // |
| 556 | // |
| 557 | |
| 558 | // FIRST, retrieve the binding of B as a JPL Term (a Compound list) and name (a String) |
| 559 | // BindingVarTerm = the JPL Variable instance of the binding variable "B" in the atom_to_term/3 query |
| 560 | // BindingVarName = the name "B" as a String |
| 561 | Term[] args = goal_.args(); // for slight convenience below |
| 562 | Term BindingVarTerm = args[args.length - 1]; // the Query's last arg: a variable binding list; type Variable |
| 563 | String BindingVarName = ((Variable) BindingVarTerm).name; // its textual name ("Bindings" in example) |
| 564 | |
| 565 | // Term TermVar = args[args.length - 2]; // the Query's last arg: a variable binding list; type Variable |
| 566 | // String TermVarName = ((Variable) TermVar).name; // its textual name ("Bindings" in example) |
| 567 | |
| 568 | // SECOND, we store what B was bound to as JPL Compound list instance BindingVarValue |
| 569 | // If B was bound to ['X' = _1, 'Y' = _2] in the current solution |
| 570 | // then BindingVarValue will be the JPL Compound instance storing that value of B |
| 571 | // BindingVarValue will be a LIST |
| 572 | // 'X', 'Y' will be JPL Atoms |
| 573 | // _1, _2 will be JPL Variables |
| 574 | // |
| 575 | // (Note: the second argument vars_to_Vars1 will contain the map between the variables and Prolg term |
| 576 | // representation, but will NOT be used.) |
| 577 | // |
| 578 | // Notation: vars = refers to the Prolog term for variables (term_t type) |
| 579 | // Vars = refers to JPL Variable type (a subclass of Term) |
| 580 | // varnames = refers to the textual name of the variable (e.g., "X") |
| 581 | Map<String, Term> varNames_to_Terms1 = new HashMap<String, Term>(); |
| 582 | Map<term_t, Variable> aux_vars_to_Vars1 = new HashMap<term_t, Variable>(); |
| 583 | BindingVarTerm.getSubst(varNames_to_Terms1, aux_vars_to_Vars1); |
| 584 | Term BindingVarValue = varNames_to_Terms1.get(BindingVarName); // The binding as a JPL Compound list |
| 585 | |
| 586 | |
| 587 | // THIRD, build the mapping we will return |
| 588 | // Associate the Prolog term for the _ vars to a new JPL Variable with the name as per B |
| 589 | // For example Prolog term_t@930 instance representing JPL Variable _1 will map to Variable@933 for "X" |
| 590 | // So here we do the link from _ vars to named Variables |
| 591 | Map<String, Term> varnames_to_Terms2 = new HashMap<String, Term>(); |
| 592 | Map<term_t, Variable> vars_to_Vars2 = Util.namevarsToMap(BindingVarValue); |
| 593 | for (int i = 0; i < args.length - 1; ++i) { |
| 594 | args[i].getSubst(varnames_to_Terms2, vars_to_Vars2); |
| 595 | } |
| 596 | return varnames_to_Terms2; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | /** |
no test coverage detected