private ProvaLiteral[] rebuildNewGoals(final ProvaDerivationNode node) { if (target.getBody() == null || target.getBody().length == 0) return new ProvaLiteral[0]; boolean allGround = true; for (ProvaVariable var : targetVariables) { if (!var.getRecursivelyAssigned().isGround()) { allGround = false; break; } } final ProvaLiteral[] body = target.getGuardedBody(source.getBody()[0]); final int bodyLength = body == null ? 0 : body.length; final ProvaLiteral[] goals = new ProvaLiteralImpl[bodyLength]; for (int i = 0; i < bodyLength; i++) { if ("cut".equals(body[i].getPredicate().getSymbol())) { final ProvaVariablePtr any = (ProvaVariablePtr) body[i].getTerms().getFixed()[0]; final ProvaConstantImpl cutnode = ProvaConstantImpl.create(node); if (any.getRuleId() == source.getRuleId()) sourceVariables.get(any.getIndex()).setAssigned(cutnode); else targetVariables.get(any.getIndex()).setAssigned(cutnode); goals[i] = new ProvaLiteralImpl( body[i].getPredicate(), ProvaListImpl.create(new ProvaObject[] {cutnode})); continue; } goals[i] = body[i].rebuild(this); goals[i].setLine(body[i].getLine()); if (allGround) goals[i].setGround(true); } return goals; }
private boolean matchMetadata(final ProvaLiteral sourceLiteral, final ProvaRule target) { Map<String, List<Object>> sourceMetadata = sourceLiteral.getMetadata(); if (sourceMetadata == null || sourceMetadata.size() == 0) // No source metadata or only line number return true; Map<String, List<Object>> targetMetadata = target.getMetadata(); if (targetMetadata == null) return false; // All requested metadata must be found in the target for (Entry<String, List<Object>> s : sourceMetadata.entrySet()) { List<Object> value = targetMetadata.get(s.getKey()); List<Object> sValue = s.getValue(); if (value == null) return false; boolean matched = false; // Either of values in the source must be present in the list of values in the target for (Object vo : value) { if (!(vo instanceof String)) continue; String v = (String) vo; for (Object sVo : sValue) { if (!(sVo instanceof String)) continue; String sV = (String) sVo; if (sV.length() != 0 && Character.isUpperCase(sV.charAt(0))) { if (meta == null) // Should not normally happen return false; for (ProvaList m : meta) { ProvaObject[] mo = m.getFixed(); String varName = (String) ((ProvaConstant) mo[0]).getObject(); ProvaObject var = mo[1]; if (varName.equals(sV)) { if (mo[1] instanceof ProvaVariablePtr) { ProvaVariablePtr varPtr = (ProvaVariablePtr) var; var = sourceVariables.get(varPtr.getIndex()).getRecursivelyAssigned(); } if (var instanceof ProvaVariable) { ((ProvaVariable) var).setAssigned(ProvaConstantImpl.create(v)); matched = true; break; } else if (var instanceof ProvaConstant) { // This allows for dynamic instantiation of metadata values from bound variables sV = (String) ((ProvaConstant) var).getObject(); break; } } } } if (matched) break; if (v.equals(sV)) { matched = true; break; } } if (matched) break; } if (!matched) return false; } return true; }