Exemplo n.º 1
0
  /**
   * Retrieves a sublist of all the clauses of the same family as the goal and which, in all
   * probability, could match with the given goal
   *
   * @param goal The goal to be resolved
   * @return The list of goal-compatible predicates
   */
  public List<ClauseInfo> get(Term goal) {
    // Gets the correct list and encapsulates it in ReadOnlyLinkedList
    if (goal instanceof Struct) {
      Struct g = (Struct) goal.getTerm();

      /*
       * If no arguments no optimization can be applied
       * (and probably no optimization is needed)
       */
      if (g.getArity() == 0) {
        return new ReadOnlyLinkedList<>(this);
      }

      /* Retrieves first argument and checks type */
      Term t = g.getArg(0).getTerm();
      if (t instanceof Var) {
        /*
         * if first argument is an unbounded variable,
         * no reasoning is possible, all family must be returned
         */
        return new ReadOnlyLinkedList<>(this);
      } else if (t.isAtomic()) {
        if (t instanceof Number) {
          /* retrieves clauses whose first argument is numeric (or Var)
           * and same as goal's first argument, if no clauses
           * are retrieved, all clauses with a variable
           * as first argument
           */
          return new ReadOnlyLinkedList<>(numCompClausesIndex.get((Number) t));
        } else if (t instanceof Struct) {
          /* retrieves clauses whose first argument is a constant (or Var)
           * and same as goal's first argument, if no clauses
           * are retrieved, all clauses with a variable
           * as first argument
           */
          return new ReadOnlyLinkedList<>(constantCompClausesIndex.get(((Struct) t).getName()));
        }
      } else if (t instanceof Struct) {
        if (isAList((Struct) t)) {
          /* retrieves clauses which has a list  (or Var) as first argument */
          return new ReadOnlyLinkedList<>(listCompClausesList);
        } else {
          /* retrieves clauses whose first argument is a struct (or Var)
           * and same as goal's first argument, if no clauses
           * are retrieved, all clauses with a variable
           * as first argument
           */
          return new ReadOnlyLinkedList<>(
              structCompClausesIndex.get(((Struct) t).getPredicateIndicator()));
        }
      }
    }

    /* Default behaviour: no optimization done */
    return new ReadOnlyLinkedList<>(this);
  }