/** * 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); }
// Updates indexes, deleting informations about the last removed clause public void unregister(ClauseInfo ci) { Term clause = ci.getHead(); if (clause instanceof Struct) { Struct g = (Struct) clause.getTerm(); if (g.getArity() == 0) { return; } Term t = g.getArg(0).getTerm(); if (t instanceof Var) { numCompClausesIndex.removeShared(ci); constantCompClausesIndex.removeShared(ci); structCompClausesIndex.removeShared(ci); listCompClausesList.remove(ci); } else if (t.isAtomic()) { if (t instanceof Number) { numCompClausesIndex.delete((Number) t, ci); } else if (t instanceof Struct) { constantCompClausesIndex.delete(((Struct) t).getName(), ci); } } else if (t instanceof Struct) { if (t.isList()) { listCompClausesList.remove(ci); } else { structCompClausesIndex.delete(((Struct) t).getPredicateIndicator(), ci); } } } }
// Updates indexes, storing informations about the last added clause private void register(ClauseInfo ci, boolean first) { // See FamilyClausesList.get(Term): same concept Term clause = ci.getHead(); if (clause instanceof Struct) { Struct g = (Struct) clause.getTerm(); if (g.getArity() == 0) { return; } Term t = g.getArg(0).getTerm(); if (t instanceof Var) { numCompClausesIndex.insertAsShared(ci, first); constantCompClausesIndex.insertAsShared(ci, first); structCompClausesIndex.insertAsShared(ci, first); if (first) { listCompClausesList.addFirst(ci); } else { listCompClausesList.addLast(ci); } } else if (t.isAtomic()) { if (t instanceof Number) { numCompClausesIndex.insert((Number) t, ci, first); } else if (t instanceof Struct) { constantCompClausesIndex.insert(((Struct) t).getName(), ci, first); } } else if (t instanceof Struct) { if (isAList((Struct) t)) { if (first) { listCompClausesList.addFirst(ci); } else { listCompClausesList.addLast(ci); } } else { structCompClausesIndex.insert(((Struct) t).getPredicateIndicator(), ci, first); } } } }