コード例 #1
0
 protected void setAnnotation(String annoName, IValueList annoList) {
   IList annos = (IList) annoList.asList();
   if (this.ownValue == null) {
     return;
   }
   if (annoList != null
       && this.ownValue.getType().declaresAnnotation(this.typeStore, annoName)
       && !annos.isEmpty()) {
     this.ownValue = ((IConstructor) this.ownValue).asAnnotatable().setAnnotation(annoName, annos);
   }
 }
コード例 #2
0
  public static IList projectByFieldNames(IList rel1, String... fields) {
    int[] indexes = new int[fields.length];
    int i = 0;

    if (rel1.getType().getFieldTypes().hasFieldNames()) {
      for (String field : fields) {
        indexes[i++] = rel1.getType().getFieldTypes().getFieldIndex(field);
      }

      return project(rel1, indexes);
    }

    throw new IllegalOperationException("select with field names", rel1.getType());
  }
コード例 #3
0
  public static IList compose(IList rel1, IList rel2) {

    Type otherTupleType = rel2.getType().getFieldTypes();

    if (rel1.getElementType() == voidType) return rel1;
    if (otherTupleType == voidType) return rel2;

    if (rel1.getElementType().getArity() != 2 || otherTupleType.getArity() != 2)
      throw new IllegalOperationException("compose", rel1.getElementType(), otherTupleType);

    // Relaxed type constraint:
    if (!rel1.getElementType().getFieldType(1).comparable(otherTupleType.getFieldType(0)))
      throw new IllegalOperationException("compose", rel1.getElementType(), otherTupleType);

    Type[] newTupleFieldTypes =
        new Type[] {rel1.getElementType().getFieldType(0), otherTupleType.getFieldType(1)};
    Type tupleType = typeFactory.tupleType(newTupleFieldTypes);

    IListWriter w = new ListWriter(tupleType);

    for (IValue v1 : rel1) {
      ITuple tuple1 = (ITuple) v1;
      for (IValue t2 : rel2) {
        ITuple tuple2 = (ITuple) t2;

        if (tuple1.get(1).isEqual(tuple2.get(0))) {
          w.append(Tuple.newTuple(tuple1.get(0), tuple2.get(1)));
        }
      }
    }
    return w.done();
  }
コード例 #4
0
  private static Result<IValue> call(ICallableValue function, IList args) {
    try {
      int nrOfArgs = args.length();
      Type[] types = new Type[nrOfArgs];
      IValue[] actuals = new IValue[nrOfArgs];

      for (int i = nrOfArgs - 1; i >= 0; --i) {
        IValue arg = args.get(i);
        types[i] = RascalTypeFactory.getInstance().nonTerminalType((IConstructor) arg);
        actuals[i] = arg;
      }

      return function.call(types, actuals, null);
    } catch (MatchFailed e) {
      return null;
    } catch (Failure f) {
      return null;
    }
  }
コード例 #5
0
 private boolean checkIndent(IList o) {
   if (indent && o.length() > 1) {
     for (IValue x : o) {
       Type type = x.getType();
       if (indented(type)) {
         return true;
       }
     }
   }
   return false;
 }
コード例 #6
0
  public static IList closureStar(IList rel1) {
    Type resultType = rel1.getType().closure();
    // an exception will have been thrown if the type is not acceptable

    IListWriter reflex = List.createListWriter(resultType.getElementType());

    for (IValue e : carrier(rel1)) {
      reflex.insert(Tuple.newTuple(new IValue[] {e, e}));
    }

    return closure(rel1).concat(reflex.done());
  }
コード例 #7
0
  public static IList domain(IList rel1) {
    Type lrelType = rel1.getType();
    IListWriter w = List.createListWriter(lrelType.getFieldType(0));
    HashSet<IValue> cache = new HashSet<>();

    for (IValue elem : rel1) {
      ITuple tuple = (ITuple) elem;
      IValue e = tuple.get(0);
      if (!cache.contains(e)) {
        cache.add(e);
        w.append(e);
      }
    }
    return w.done();
  }
コード例 #8
0
  public static IList carrier(IList rel1) {
    Type newType = rel1.getType().carrier();
    IListWriter w = List.createListWriter(newType.getElementType());
    HashSet<IValue> cache = new HashSet<>();

    for (IValue v : rel1) {
      ITuple t = (ITuple) v;
      for (IValue e : t) {
        if (!cache.contains(e)) {
          cache.add(e);
          w.append(e);
        }
      }
    }

    return w.done();
  }
コード例 #9
0
ファイル: NonTerminalType.java プロジェクト: stil4m/rascal
  @Override
  public boolean isSubtypeOfNonTerminal(RascalType other) {
    if (other == this) {
      return true;
    }
    IConstructor otherSym = ((NonTerminalType) other).symbol;

    if ((SymbolAdapter.isIterPlus(symbol)
            && (SymbolAdapter.isIterStar(otherSym) || SymbolAdapter.isIterPlus(otherSym)))
        || (SymbolAdapter.isIterStar(symbol) && SymbolAdapter.isIterStar(otherSym))) {
      RascalType nt1 = (RascalType) RTF.nonTerminalType(SymbolAdapter.getSymbol(symbol));
      RascalType nt2 = (RascalType) RTF.nonTerminalType(SymbolAdapter.getSymbol(otherSym));
      return nt1.isSubtypeOfNonTerminal(nt2);
    } else if ((SymbolAdapter.isIterPlusSeps(symbol)
            && (SymbolAdapter.isIterStarSeps(otherSym) || SymbolAdapter.isIterPlusSeps(otherSym)))
        || (SymbolAdapter.isIterStarSeps(symbol) && SymbolAdapter.isIterStarSeps(otherSym))) {
      RascalType nt1 = (RascalType) RTF.nonTerminalType(SymbolAdapter.getSymbol(symbol));
      RascalType nt2 = (RascalType) RTF.nonTerminalType(SymbolAdapter.getSymbol(otherSym));

      if (nt1.isSubtypeOfNonTerminal(nt2)) {
        IList seps1 = SymbolAdapter.getSeparators(symbol);
        IList seps2 = SymbolAdapter.getSeparators(otherSym);

        // this works around broken regular prods in the RVM which have the wrong or missing layout
        // symbols:
        int sep1index = seps1.length() == 3 ? 1 : 0;
        int sep2index = seps2.length() == 3 ? 1 : 0;

        nt1 = (RascalType) RTF.nonTerminalType((IConstructor) seps1.get(sep1index));
        nt2 = (RascalType) RTF.nonTerminalType((IConstructor) seps2.get(sep2index));
        return nt1.isSubtypeOfNonTerminal(nt2);
      }

      return false;
    } else if (SymbolAdapter.isOpt(symbol) && SymbolAdapter.isOpt(otherSym)) {
      RascalType nt1 = (RascalType) RTF.nonTerminalType(SymbolAdapter.getSymbol(symbol));
      RascalType nt2 = (RascalType) RTF.nonTerminalType(SymbolAdapter.getSymbol(otherSym));
      return nt1.isSubtypeOfNonTerminal(nt2);
    }
    //	  else if (SymbolAdapter.isSequence(symbol) && SymbolAdapter.isSeq(otherSym)) {
    // TODO pairwise issubtype
    //	  }

    if (SymbolAdapter.isParameter(otherSym)) {
      RascalType bound = (RascalType) RTF.nonTerminalType((IConstructor) otherSym.get("bound"));
      return isSubtypeOf(bound);
    }

    // TODO co-variance for the other structured symbols (sequence, opt, list)
    return SymbolAdapter.isEqual(otherSym, symbol);
  }
コード例 #10
0
    public IValue visitList(IList o) throws IOException {
      append('[');

      boolean indent = checkIndent(o);
      Iterator<IValue> listIterator = o.iterator();
      tab();
      indent(indent);
      if (listIterator.hasNext()) {
        listIterator.next().accept(this);

        while (listIterator.hasNext()) {
          append(',');
          if (indent) indent();
          listIterator.next().accept(this);
        }
      }
      untab();
      indent(indent);
      append(']');

      return o;
    }
コード例 #11
0
  public static IList closure(IList rel1) {
    Type resultType = rel1.getType().closure(); // will throw exception if not binary and reflexive
    IList tmp = rel1;

    int prevCount = 0;

    ShareableValuesHashSet addedTuples = new ShareableValuesHashSet();
    while (prevCount != tmp.length()) {
      prevCount = tmp.length();
      IList tcomp = compose(tmp, tmp);
      IListWriter w = List.createListWriter(resultType.getElementType());
      for (IValue t1 : tcomp) {
        if (!tmp.contains(t1)) {
          if (!addedTuples.contains(t1)) {
            addedTuples.add(t1);
            w.append(t1);
          }
        }
      }
      tmp = tmp.concat(w.done());
      addedTuples.clear();
    }
    return tmp;
  }
コード例 #12
0
 public static int arity(IList rel) {
   return rel.getElementType().getArity();
 }