예제 #1
0
 @Override
 public ProvaList getAssignedWithOffset() {
   if (offset == 0) return assigned;
   ProvaObject[] newFixed =
       Arrays.copyOfRange(assigned.getFixed(), offset, assigned.getFixed().length);
   return ProvaListImpl.create(newFixed);
 }
예제 #2
0
 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;
 }
예제 #3
0
 public String toString() {
   StringBuilder sb = new StringBuilder("[");
   for (int i = offset; i < assigned.getFixed().length; i++) {
     if (i != offset) sb.append(',');
     sb.append(assigned.getFixed()[i]);
   }
   if (assigned.getTail() != null) {
     sb.append('|');
     sb.append(assigned.getTail());
   }
   sb.append(']');
   return sb.toString();
 }
예제 #4
0
 @Override
 // TODO: recursive expressions as operands
 public boolean process(
     ProvaReagent prova,
     ProvaDerivationNode node,
     ProvaGoal goal,
     List<ProvaLiteral> newLiterals,
     ProvaRule query) {
   ProvaLiteral literal = goal.getGoal();
   List<ProvaVariable> variables = query.getVariables();
   ProvaList terms = (ProvaList) literal.getTerms().cloneWithVariables(variables);
   ProvaObject[] data = terms.getFixed();
   if (data.length != 3) return false;
   ProvaObject lt = data[0];
   if (lt instanceof ProvaVariablePtr) {
     ProvaVariablePtr varPtr = (ProvaVariablePtr) lt;
     lt = variables.get(varPtr.getIndex()).getRecursivelyAssigned();
   }
   if (!((lt instanceof ProvaVariable) || (lt instanceof ProvaConstant))) return false;
   ProvaObject a1 = data[1];
   if (a1 instanceof ProvaVariablePtr) {
     ProvaVariablePtr varPtr = (ProvaVariablePtr) a1;
     a1 = variables.get(varPtr.getIndex()).getRecursivelyAssigned();
   }
   if (!(a1 instanceof ProvaConstant)) return false;
   Object oa1 = ((ProvaConstant) a1).getObject();
   if (!(oa1 instanceof Number)) return false;
   ProvaObject a2 = data[2];
   if (a2 instanceof ProvaVariablePtr) {
     ProvaVariablePtr varPtr = (ProvaVariablePtr) a2;
     a2 = variables.get(varPtr.getIndex()).getRecursivelyAssigned();
   }
   if (!(a2 instanceof ProvaConstant)) return false;
   Object oa2 = ((ProvaConstant) a2).getObject();
   if (!(oa2 instanceof Number)) return false;
   Number na1 = (Number) oa1;
   Number na2 = (Number) oa2;
   Number result;
   if (na1 instanceof Double || na2 instanceof Double)
     result = na1.doubleValue() * na2.doubleValue();
   else if (na1 instanceof Float || na2 instanceof Float)
     result = na1.floatValue() * na2.floatValue();
   else if (na1 instanceof Long || na2 instanceof Long) result = na1.longValue() * na2.longValue();
   else if (na1 instanceof Integer || na2 instanceof Integer)
     result = na1.intValue() * na2.intValue();
   else result = na1.byteValue() * na2.byteValue();
   if (lt instanceof ProvaConstant) return ((ProvaConstant) lt).getObject() == result;
   ((ProvaVariable) lt).setAssigned(ProvaConstantImpl.create(result));
   return true;
 }
예제 #5
0
 @Override
 public ProvaObject cloneWithBoundVariables(
     final List<ProvaVariable> variables, final List<Boolean> isConstant) {
   final ProvaObject[] fixed = assigned.getFixed();
   final ProvaObject[] newFixed = new ProvaObject[fixed.length - offset];
   System.arraycopy(fixed, offset, newFixed, 0, newFixed.length);
   return ProvaListImpl.create(newFixed).cloneWithBoundVariables(variables, isConstant);
 }
예제 #6
0
 @Override
 public boolean process(
     ProvaReagent prova,
     ProvaDerivationNode node,
     ProvaGoal goal,
     List<ProvaLiteral> newLiterals,
     ProvaRule query) {
   ProvaLiteral literal = goal.getGoal();
   List<ProvaVariable> variables = query.getVariables();
   ProvaList terms = (ProvaList) literal.getTerms().cloneWithVariables(variables);
   ProvaObject[] data = terms.getFixed();
   if (data.length != 1 || !(data[0] instanceof ProvaList)) return false;
   String symbol = ((ProvaConstant) ((ProvaList) data[0]).getFixed()[0]).getObject().toString();
   ProvaLiteral lit = kb.generateLiteral(symbol, ((ProvaList) data[0]).getFixed(), 1);
   // This automatically adds the rule to the respective predicate in the knowledge base
   kb.generateRuleA(lit, new ProvaLiteral[] {});
   return true;
 }
예제 #7
0
 @Override
 public ProvaObject rebuildSource(final ProvaUnification unification) {
   return assigned.rebuildSource(unification, offset);
 }
예제 #8
0
 @Override
 public boolean unify(final ProvaObject target, final ProvaUnification unification) {
   return assigned.unify(offset, target, unification);
 }
예제 #9
0
 @Override
 public int computeSize() {
   return assigned.computeSize(offset);
 }
예제 #10
0
 @Override
 public int collectVariables(final long ruleId, final List<ProvaVariable> variables) {
   return assigned.collectVariables(ruleId, variables);
 }
예제 #11
0
 @Override
 public boolean updateGround(final List<ProvaVariable> variables) {
   return assigned.updateGround(variables);
 }
예제 #12
0
 @Override
 public boolean isGround() {
   return assigned.isGround();
 }
예제 #13
0
 @Override
 public void substituteVariables(final ProvaVariablePtr[] varsMap) {
   assigned.substituteVariables(varsMap);
 }