Exemplo n.º 1
0
    /**
     * Returns the selection array or vector.
     *
     * <p>The selSizes array contains sizes of the selectors (number of elements that will be
     * returned by it). The idx array contains the indices returned by the selectors (that is the
     * indices used to compute the source offset).
     *
     * <p>The selIdx array contains the position in the selector (when this is equal to the selector
     * size the selector has overflown).
     */
    public Object execute(RArray source, boolean drop, int exact) throws UnexpectedResultException {
      int[] sourceDim = source.dimensions();
      boolean mayHaveNA = Selector.initialize(offsets, selectorVals, sourceDim, selSizes, ast);
      int[] destDim = Selector.calculateDestinationDimensions(selSizes, !subset || drop);
      int destSize = Selector.calculateSizeFromSelectorSizes(selSizes);

      RArray dest = Utils.createArray(source, destSize, destDim, null, null); // drop attributes
      if (destSize == 0) {
        return dest;
      }
      int offset = 0;
      for (; ; ) {
        int sourceOffset = offsets[0];
        if (sourceOffset == RInt.NA) {
          Utils.setNA(dest, offset);
        } else {
          dest.set(offset, source.getRef(sourceOffset));
        }
        offset++;
        if (offset < destSize) {
          if (!mayHaveNA) {
            Selector.advanceNoNA(offsets, sourceDim, selectorVals, ast);
          } else {
            Selector.advance(offsets, sourceDim, selectorVals, ast);
          }
        } else {
          break;
        }
      }
      return dest;
    }
Exemplo n.º 2
0
 /**
  * It checks if the complex covers a given example
  *
  * @param m The example
  * @return boolean True if it covers the example. False in other case
  */
 public boolean covered(Instance m) {
   boolean cubierto = true;
   double[] ejemplo = m.getMuest();
   for (int i = 0; i < this.size() && cubierto; i++) {
     Selector s = this.getSelector(i);
     switch (s.getOperator()) {
       case 0: // equal operator
         double[] valor = s.getValues();
         cubierto = false;
         for (int j = 0; (j < valor.length) && (!cubierto); j++) {
           cubierto = (ejemplo[s.getAttribute()] == valor[j]);
         }
         break;
       case 1: // distinct operator
         cubierto = ejemplo[s.getAttribute()] != s.getValue();
         break;
       case 2: // lower or equal
         cubierto = ejemplo[s.getAttribute()] <= s.getValue();
         break;
       case 3: // higher
         cubierto = ejemplo[s.getAttribute()] > s.getValue();
         break;
     }
   }
   return cubierto;
 }
Exemplo n.º 3
0
  /**
   * Serves the incoming connections.
   *
   * @throws IOException
   * @throws DirectoryException
   */
  private void serveIncomingConnections() throws IOException, DirectoryException {
    int selectorState = selector.select();

    // We can't rely on return value of select to determine if any keys
    // are ready.
    // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4850373
    for (Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
        iterator.hasNext(); ) {
      SelectionKey key = iterator.next();
      iterator.remove();
      if (key.isAcceptable()) {
        // Accept the new client connection.
        ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
        SocketChannel clientChannel = serverChannel.accept();
        if (clientChannel != null) {
          acceptConnection(clientChannel);
        }
      }

      if (selectorState == 0 && enabled && !shutdownRequested && logger.isTraceEnabled()) {
        // Selected keys was non empty but select() returned 0.
        // Log warning and hope it blocks on the next select() call.
        logger.trace(
            "Selector.select() returned 0. "
                + "Selected Keys: %d, Interest Ops: %d, Ready Ops: %d ",
            selector.selectedKeys().size(), key.interestOps(), key.readyOps());
      }
    }
  }
Exemplo n.º 4
0
 /** It prints the complex content */
 public void print() {
   for (int x = 0; x < compl.size(); x++) {
     Selector s = (Selector) compl.get(x);
     System.out.print("(" + nombreAtributos[s.getAttribute()] + " ");
     switch (s.getOperator()) {
       case 0:
         System.out.print("=");
         break;
       case 1:
         System.out.print("<>");
         break;
       case 2:
         System.out.print("<=");
         break;
       default:
         System.out.print(">");
     }
     double[] valores = s.getValues();
     if (valores.length > 1) {
       System.out.print(" " + valores[0]);
       for (int i = 1; i < valores.length - 1; i++) {
         System.out.print(" ^ " + valores[i]);
       }
       System.out.print(" ^ " + valores[valores.length - 1] + ")");
     } else {
       System.out.print(" " + valores[0] + ")");
     }
     if (x < compl.size() - 1) {
       System.out.print(" AND ");
     }
   }
 }
Exemplo n.º 5
0
 /**
  * It prints on a string the content of the complex
  *
  * @return String a string with the content of the complex
  */
 public String printString() {
   String cad = "";
   for (int x = 0; x < compl.size(); x++) {
     Selector s = (Selector) compl.get(x);
     cad += nombreAtributos[s.getAttribute()] + " ";
     switch (s.getOperator()) {
       case 0:
         cad += "=";
         break;
       case 1:
         cad += "<>";
         break;
       case 2:
         cad += "<=";
         break;
       case 3:
         cad += ">";
         break;
     }
     double[] valores = s.getValues();
     if (valores.length > 1) {
       cad += " " + valores[0];
       for (int i = 1; i < valores.length - 1; i++) {
         cad += " ^ " + valores[i];
       }
       cad += " ^ " + valores[valores.length - 1] + "";
     } else {
       cad += " " + valores[0] + "";
     }
     if (x < compl.size() - 1) {
       cad += " AND ";
     }
   }
   return cad;
 }
Exemplo n.º 6
0
  public DatasetScanFilter(Selector selector) {
    if (selector == null) {
      this.selectorGroup = Collections.emptyList();
      this.containsAtomicIncluders = false;
      this.containsAtomicExcluders = false;
      this.containsCollectionIncluders = false;
      this.containsCollectionExcluders = false;
    } else {
      boolean anyAtomicIncluders = false;
      boolean anyAtomicExcluders = false;
      boolean anyCollectionIncluders = false;
      boolean anyCollectionExcluders = false;

      if (selector.isIncluder()) {
        if (selector.isApplyToAtomicDataset()) anyAtomicIncluders = true;
        if (selector.isApplyToCollectionDataset()) anyCollectionIncluders = true;
      } else { // curSelector.isExcluder()
        if (selector.isApplyToAtomicDataset()) anyAtomicExcluders = true;
        if (selector.isApplyToCollectionDataset()) anyCollectionExcluders = true;
      }

      this.selectorGroup = Collections.singletonList(selector);
      this.containsAtomicIncluders = anyAtomicIncluders;
      this.containsAtomicExcluders = anyAtomicExcluders;
      this.containsCollectionIncluders = anyCollectionIncluders;
      this.containsCollectionExcluders = anyCollectionExcluders;
    }
  }
 private List<Object> buildBboxWhereParameters(List<Selector.Polygon> bboxSelectors) {
   List<Object> obj = new LinkedList<Object>();
   for (Selector selector : bboxSelectors) {
     obj.addAll(selector.getWhereParam());
   }
   return obj;
 }
Exemplo n.º 8
0
  /** Selects on sockets and informs their Communicator when there is something to do. */
  public void communicate(int timeout) {

    try {
      selector.select(timeout);
    } catch (IOException e) {
      // Not really sure why/when this happens yet
      return;
    }

    Iterator<SelectionKey> keys = selector.selectedKeys().iterator();

    while (keys.hasNext()) {
      SelectionKey key = keys.next();
      keys.remove();
      if (!key.isValid()) continue; // WHY
      Communicator communicator = (Communicator) key.attachment();

      if (key.isReadable()) communicator.onReadable();
      if (key.isWritable()) communicator.onWritable();
      if (key.isAcceptable()) communicator.onAcceptable();
    }

    // Go through the queue and handle each communicator
    while (!queue.isEmpty()) {
      Communicator c = queue.poll();
      c.onMemo();
    }
  }
 private List<Object> buildSelectorWhereParameters(List<? extends Selector> tagSelectors) {
   List<Object> obj = new LinkedList<Object>();
   for (Selector selector : tagSelectors) {
     obj.addAll(selector.getWhereParam());
   }
   return obj;
 }
Exemplo n.º 10
0
 boolean initiateConnection(Connection conn_, Peer peer) {
   TCPConnection conn = (TCPConnection) conn_;
   try {
     SocketChannel channel = SocketChannel.open();
     InetSocketAddress localAddress = new InetSocketAddress(conn.host_id, 0);
     channel.socket().bind(localAddress);
     channel.configureBlocking(false);
     try {
       InetSocketAddress remoteAddress = new InetSocketAddress(peer.host(), peer.port());
       if (channel.connect(remoteAddress)) {
         // This only happens on Solaris when connecting locally
         logger.log(Level.FINEST, "Connected!");
         conn.state = Connection.State.connected_out;
         conn.channel = channel;
         selector.wakeup();
         channel.register(selector, SelectionKey.OP_READ, conn);
         initiateCER(conn);
         return true;
       }
     } catch (java.nio.channels.UnresolvedAddressException ex) {
       channel.close();
       return false;
     }
     conn.state = Connection.State.connecting;
     conn.channel = channel;
     selector.wakeup();
     channel.register(selector, SelectionKey.OP_CONNECT, conn);
   } catch (java.io.IOException ex) {
     logger.log(
         Level.WARNING,
         "java.io.IOException caught while initiating connection to '" + peer.host() + "'.",
         ex);
   }
   return true;
 }
Exemplo n.º 11
0
  /** Creates the total selector's set for get all the possible rules */
  private Complejo hazSelectores(Dataset train) {

    Complejo almacenSelectores;
    int nClases = train.getnclases();
    almacenSelectores =
        new Complejo(nClases); // Aqui voy a almacenar los selectores (numVariable,operador,valor)
    Attribute[] atributos = null;
    int num_atributos, type;
    Vector nominalValues;
    atributos = Attributes.getAttributes();
    num_atributos = Attributes.getNumAttributes();
    Selector s;

    for (int i = 0; i < train.getnentradas(); i++) {
      type = atributos[i].getType();
      switch (type) {
        case 0: // NOMINAL
          nominalValues = atributos[i].getNominalValuesList();
          // System.out.print("{");
          for (int j = 0; j < nominalValues.size(); j++) {
            // System.out.print ((String)nominalValues.elementAt(j)+"  ");
            s = new Selector(i, 0, (String) nominalValues.elementAt(j), true); // [atr,op,valor]
            // incluimos tb los valores en double para facilitar algunas funciones
            s.setValor((double) j);
            almacenSelectores.addSelector(s);
            // s.print();
          }
          // System.out.println("}");
          break;
      }
      // System.out.println(num_atributos);
    }
    return almacenSelectores;
  }
Exemplo n.º 12
0
  public static void main(String[] argv) throws Exception {
    Pipe[] pipes = new Pipe[PIPES_COUNT];
    Pipe pipe = Pipe.open();
    Pipe.SinkChannel sink = pipe.sink();
    Pipe.SourceChannel source = pipe.source();
    Selector sel = Selector.open();
    source.configureBlocking(false);
    source.register(sel, SelectionKey.OP_READ);

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i] = Pipe.open();
      Pipe.SourceChannel sc = pipes[i].source();
      sc.configureBlocking(false);
      sc.register(sel, SelectionKey.OP_READ);
      Pipe.SinkChannel sc2 = pipes[i].sink();
      sc2.configureBlocking(false);
      sc2.register(sel, SelectionKey.OP_WRITE);
    }

    for (int i = 0; i < LOOPS; i++) {
      sink.write(ByteBuffer.allocate(BUF_SIZE));
      int x = sel.selectNow();
      sel.selectedKeys().clear();
      source.read(ByteBuffer.allocate(BUF_SIZE));
    }

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i].sink().close();
      pipes[i].source().close();
    }
    pipe.sink().close();
    pipe.source().close();
    sel.close();
  }
Exemplo n.º 13
0
    @Override
    public Object execute(Frame frame) {

      RAny lhsVal = (RAny) lhs.execute(frame);
      RAny rowVal = (RAny) rowExpr.execute(frame);
      boolean dropVal =
          dropExpr.executeLogical(frame)
              != RLogical.FALSE; // FIXME: what is the correct execution order of these args?
      int exactVal = exactExpr.executeLogical(frame);

      if (!(lhsVal instanceof RArray)) {
        throw RError.getObjectNotSubsettable(ast, lhsVal.typeOf());
      }
      RArray array = (RArray) lhsVal;
      int[] dim = array.dimensions();
      if (dim == null || dim.length != 2) {
        throw RError.getIncorrectDimensions(getAST());
      }
      int m = dim[0];
      int n = dim[1];

      try {
        int row;
        if (rowVal instanceof ScalarIntImpl) {
          row = ((ScalarIntImpl) rowVal).getInt();
        } else if (rowVal instanceof ScalarDoubleImpl) {
          row = Convert.double2int(((ScalarDoubleImpl) rowVal).getDouble());
        } else {
          throw new UnexpectedResultException(null);
        }
        if (row > n || row <= 0) {
          throw new UnexpectedResultException(null);
        }

        int[] ndim;
        if (dropVal) {
          ndim = null;
        } else {
          ndim = new int[] {1, n};
        }

        // note: also could be lazy here
        RArray res = Utils.createArray(array, n, ndim, null, null); // drop attributes
        int offset = row - 1;
        for (int i = 0; i < n; i++) {
          res.set(i, array.getRef(offset));
          offset += m;
        }
        return res;
      } catch (UnexpectedResultException e) {
        SelectorNode selIExpr = Selector.createSelectorNode(ast, true, rowExpr);
        SelectorNode selJExpr = Selector.createSelectorNode(ast, true, null);
        MatrixRead nn = new MatrixRead(ast, true, lhs, selIExpr, selJExpr, dropExpr, exactExpr);
        replace(nn, "install MatrixRead from MatrixRowSubset");
        Selector selI = selIExpr.executeSelector(rowVal);
        Selector selJ = selJExpr.executeSelector(frame);
        return nn.executeLoop(array, selI, selJ, dropVal, exactVal);
      }
    }
Exemplo n.º 14
0
 /**
  * Vorbedingung: variante und selectors sind ungleich null. selectors enthaelt keine Elemente
  * gleich null
  *
  * @param variante
  * @return true, wenn alle Selektoren die Variante selektieren, false anderenfalls.
  */
 private boolean select(Variante variante, List<Selector<Variante>> selectors) {
   for (Selector<Variante> selector : selectors) {
     if (!selector.select(variante)) {
       return false;
     }
   }
   return true;
 }
 @Test
 public void testGetSelectors() {
   List<Selector> expResult = new ArrayList<Selector>();
   expResult.add(Selector.createSelector("One.two#three"));
   expResult.add(Selector.createSelector("Four.five#six"));
   Rule instance = new Rule(expResult, Collections.EMPTY_LIST);
   List result = instance.getSelectors();
   assertEquals(expResult, result);
 }
Exemplo n.º 16
0
 private void dispatch() throws IOException {
   sel.select();
   for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) {
     SelectionKey sk = (SelectionKey) i.next();
     i.remove();
     Handler h = (Handler) sk.attachment();
     h.handle(sk);
   }
 }
Exemplo n.º 17
0
 /** Sending a request to a node not listening on that port should result in disconnection */
 @Test
 public void testConnectionRefused() throws Exception {
   String connectionId =
       selector.connect(
           new InetSocketAddress("localhost", 6668), BUFFER_SIZE, BUFFER_SIZE, PortType.SSL);
   while (selector.disconnected().contains(connectionId)) {
     selector.poll(1000L);
   }
 }
 public static void main(String[] args) {
   Sequence sequence = new Sequence(10);
   for (int i = 0; i < 10; i++) sequence.add(new StringHolder(Integer.toString(i)));
   Selector selector = sequence.selector();
   while (!selector.end()) {
     System.out.print(selector.current() + " ");
     selector.next();
   }
 }
Exemplo n.º 19
0
 @Override
 public String select(String text) {
   for (Selector selector : selectors) {
     if (text == null) {
       return null;
     }
     text = selector.select(text);
   }
   return text;
 }
Exemplo n.º 20
0
  @Override
  public List<Object> getWhereParam() {
    List<Object> ret = new LinkedList<Object>();

    for (Selector selector : selectors) {
      ret.addAll(selector.getWhereParam());
    }

    return ret;
  }
Exemplo n.º 21
0
 private String blockingRequest(String connectionId, String s) throws Exception {
   selector.poll(1000L, asList(SelectorTest.createSend(connectionId, s)));
   while (true) {
     selector.poll(1000L);
     for (NetworkReceive receive : selector.completedReceives()) {
       if (receive.getConnectionId() == connectionId) {
         return SelectorTest.asString(receive);
       }
     }
   }
 }
Exemplo n.º 22
0
  public boolean accept(MFile dataset) {
    if (dataset == null) return false;

    // If no Selectors, accept all datasets.
    if (this.selectorGroup.isEmpty()) return true;

    if (dataset.isDirectory()) {
      // If no collection selectors, accept all collection datasets.
      if (!this.containsCollectionIncluders && !this.containsCollectionExcluders) return true;
    } else {
      // If no atomic selectors, accept all atomic datasets.
      if (!this.containsAtomicIncluders && !this.containsAtomicExcluders) return true;
    }

    boolean include = false;
    boolean exclude = false;

    for (Selector curSelector : this.selectorGroup) {
      if (curSelector.isApplicable(dataset)) {
        if (curSelector.match(dataset)) {
          if (curSelector.isIncluder()) include = true;
          else exclude = true;
        }
      }
    }

    // Deal with atomic datasets
    if (!dataset.isDirectory()) {
      // If have only inclusion Selectors, accept any dataset that is explicitly included.
      if (this.containsAtomicIncluders && !this.containsAtomicExcluders) return include;

      // If have only exclusion Selectors, accept any dataset not explicitly excluded.
      if (this.containsAtomicExcluders && !this.containsAtomicIncluders) return !exclude;

      // If have both inclusion and exclusion Selectors, accept datasets that are
      // explicitly included but not explicitly excluded.
      if (this.containsAtomicIncluders && this.containsAtomicExcluders && include) return !exclude;
      // Deal with collection datasets
    } else {
      // If have only inclusion Selectors, accept any dataset that is explicitly included.
      if (this.containsCollectionIncluders && !this.containsCollectionExcluders) return include;

      // If have only exclusion Selectors, accept any dataset not explicitly excluded.
      if (this.containsCollectionExcluders && !this.containsCollectionIncluders) return !exclude;

      // If have both inclusion and exclusion Selectors, accept datasets that are
      // explicitly included but not explicitly excluded.
      if (this.containsCollectionIncluders && this.containsCollectionExcluders && include)
        return !exclude;
    }

    // Otherwise, don't accept.
    return false;
  }
Exemplo n.º 23
0
  /**
   * Prints on the standard output the content of this complex object. (List -> Attribute operator
   * value)
   */
  public void print(int nominal) {
    for (int x = 0; x < compl.size(); x++) {
      Selector s = (Selector) compl.get(x);

      double[] values = s.getValues();
      String[] nValues = s.getNValues();

      // System.out.print("(Atr" + s.getAtributo() + " ");
      System.out.print("(" + attributeNames[s.getAttribute()] + " ");

      switch (s.getOperator()) {
        case 0:
          if (values.length > 1 /*|| valoresN.length>1*/) System.out.print("in");
          else System.out.print("=");
          break;
        case 1:
          System.out.print("<>");
          break;
        case 2:
          System.out.print("<=");
          break;
        default:
          System.out.print(">");
      }

      if (nominal == 0) { // el atributo es nominal
        if (nValues.length > 1) {
          System.out.print(" " + nValues[0]);
          for (int i = 1; i < nValues.length - 1; i++) {
            System.out.print(" " + nValues[i]);
          }
          System.out.print(" " + nValues[nValues.length - 1] + ")");
        } else {
          System.out.print(" " + nValues[0] + ")");
        }
      } else {
        if (values.length > 1) {
          System.out.print(" [" + values[0]);
          for (int i = 1; i < values.length - 1; i++) {
            System.out.print(" " + values[i]);
          }
          System.out.print(" " + values[values.length - 1] + "])");
        } else {
          System.out.print(" " + values[0] + ")");
        }
      }

      if (x < compl.size() - 1) {
        System.out.print(" AND ");
      }
      // System.out.print(" -- "+heuristica+" -- ");
      System.out.println();
    }
  }
  private List<Object> buildTagSelectorWhereParameters(List<? extends Selector> tagSelectors) {
    List<Object> obj = new LinkedList<Object>();
    for (Selector selector : tagSelectors) {
      if (selector instanceof Selector.Polygon) {

      } else {
        obj.addAll(selector.getWhereParam());
      }
    }
    return obj;
  }
Exemplo n.º 25
0
  public Individual mutate(Selector selector) {
    Individual child = selector.best().clone();
    Individual mutationSource = selector.secondBest().clone();

    CrossoverSelector xOverSel = new CrossoverSelector(mutationSource);
    xOverSel.visit();

    MutationApplier xOverApp = new MutationApplier(child, xOverSel.getCrossover());
    xOverApp.visit();

    return child;
  }
Exemplo n.º 26
0
 /** Validate that the client can intentionally disconnect and reconnect */
 @Test
 public void testClientDisconnect() throws Exception {
   String connectionId = blockingSSLConnect();
   selector.disconnect(connectionId);
   selector.poll(10, asList(SelectorTest.createSend(connectionId, "hello1")));
   assertEquals("Request should not have succeeded", 0, selector.completedSends().size());
   assertEquals("There should be a disconnect", 1, selector.disconnected().size());
   assertTrue(
       "The disconnect should be from our node", selector.disconnected().contains(connectionId));
   connectionId = blockingSSLConnect();
   assertEquals("hello2", blockingRequest(connectionId, "hello2"));
 }
Exemplo n.º 27
0
 /* connect and wait for the connection to complete */
 private String blockingSSLConnect() throws IOException {
   String connectionId =
       selector.connect(
           new InetSocketAddress("localhost", server.port),
           BUFFER_SIZE,
           BUFFER_SIZE,
           PortType.SSL);
   while (!selector.connected().contains(connectionId)) {
     selector.poll(10000L);
   }
   return connectionId;
 }
Exemplo n.º 28
0
 /**
  * Remove the selectors from the list of the selectors that have the parameter attribute o
  *
  * @param attribute the attribute
  */
 public void removeSelectorAttribute(int attribute) {
   Selector s;
   int atrib;
   for (int i = 0; i < compl.size(); i++) {
     s = (Selector) compl.get(i);
     atrib = s.getAttribute();
     if (atrib == attribute) {
       compl.remove(s);
       /*CUIDADO!!!hay q volver al indice anterior pq se ha eliminado un elemento!!*/
       i = i - 1;
     }
   }
 }
Exemplo n.º 29
0
 @Test
 public void testCloseAfterConnectCall() throws IOException {
   String connectionId =
       selector.connect(
           new InetSocketAddress("localhost", server.port),
           BUFFER_SIZE,
           BUFFER_SIZE,
           PortType.SSL);
   selector.close(connectionId);
   selector.poll(0);
   Assert.assertTrue(
       "Channel should have been added to disconnected list",
       selector.disconnected().contains(connectionId));
 }
Exemplo n.º 30
0
 @Test
 public void testSSLConnect() throws IOException {
   String connectionId =
       selector.connect(
           new InetSocketAddress("localhost", server.port),
           BUFFER_SIZE,
           BUFFER_SIZE,
           PortType.SSL);
   while (!selector.connected().contains(connectionId)) {
     selector.poll(10000L);
   }
   Assert.assertTrue(
       "Channel should have been ready by now ", selector.isChannelReady(connectionId));
 }