com.datatorrent.api.Operator.OutputPort Java Examples

The following examples show how to use com.datatorrent.api.Operator.OutputPort. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlanModifier.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
/**
 * Add stream to logical plan. If a stream with same name and source already
 * exists, the new downstream operator will be attached to it.
 *
 * @param streamName
 * @param sourceOperName
 * @param sourcePortName
 * @param targetOperName
 * @param targetPortName
 */
public void addStream(String streamName, String sourceOperName, String sourcePortName, String targetOperName, String targetPortName)
{
  OperatorMeta om = logicalPlan.getOperatorMeta(sourceOperName);
  if (om == null) {
    throw new ValidationException("Invalid operator name " + sourceOperName);
  }

  Operators.PortMappingDescriptor portMap = new Operators.PortMappingDescriptor();
  Operators.describe(om.getOperator(), portMap);
  PortContextPair<OutputPort<?>> sourcePort = portMap.outputPorts.get(sourcePortName);
  if (sourcePort == null) {
    throw new AssertionError(String.format("Invalid port %s (%s)", sourcePortName, om));
  }
  addStream(streamName, sourcePort.component, getInputPort(targetOperName, targetPortName));
}
 
Example #2
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
public Operator.Unifier<?> getUnifier()
{
  for (Map.Entry<OutputPort<?>, OutputPortMeta> e : operatorMeta.getPortMapping().outPortMap.entrySet()) {
    if (e.getValue() == this) {
      Unifier<?> unifier = e.getKey().getUnifier();
      if (unifier == null) {
        break;
      }
      LOG.debug("User supplied unifier is {}", unifier);
      return unifier;
    }
  }

  LOG.debug("Using default unifier for {}", this);
  return new DefaultUnifier();
}
 
Example #3
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@Override
public StreamMeta setSource(Operator.OutputPort<?> port)
{
  if (port instanceof ProxyOutputPort) {
    proxySource = port;
    return this;
  }
  OutputPortMeta portMeta = assertGetPortMeta(port);
  OperatorMeta om = portMeta.getOperatorMeta();
  if (om.outputStreams.containsKey(portMeta)) {
    String msg = String.format("Operator %s already connected to %s", om.name, om.outputStreams.get(portMeta).id);
    throw new IllegalArgumentException(msg);
  }
  this.source = portMeta;
  om.outputStreams.put(portMeta, this);
  return this;
}
 
Example #4
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
/**
 * Go over each Proxy port and find out the actual port connected to the ProxyPort
 * and update StreamMeta.
 */
private void resolvePorts()
{
  if (proxySource != null && proxySource instanceof ProxyOutputPort) {
    OutputPort<?> outputPort = proxySource;
    while (outputPort instanceof ProxyOutputPort) {
      outputPort = ((ProxyOutputPort<?>)outputPort).get();
    }
    setSource(outputPort);
  }

  for (InputPort<?> inputPort : proxySinks) {
    while (inputPort instanceof ProxyInputPort) {
      inputPort = ((ProxyInputPort<?>)inputPort).get();
    }
    addSink(inputPort);
  }

  proxySource = null;
  proxySinks.clear();
}
 
Example #5
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@Override
public void addOutputPort(OutputPort<?> portObject, Field field, OutputPortFieldAnnotation portAnnotation, AppData.ResultPort adrAnnotation)
{
  if (!OperatorMeta.this.outputStreams.isEmpty()) {
    for (Map.Entry<LogicalPlan.OutputPortMeta, LogicalPlan.StreamMeta> e : OperatorMeta.this.outputStreams.entrySet()) {
      LogicalPlan.OutputPortMeta pm = e.getKey();
      if (pm.operatorMeta == OperatorMeta.this && pm.fieldName.equals(field.getName())) {
        //LOG.debug("Found existing port meta for: " + field);
        outPortMap.put(portObject, pm);
        markOutputPortIfHidden(pm.getPortName(), pm, field.getDeclaringClass());
        return;
      }
    }
  }
  OutputPortMeta metaPort = new OutputPortMeta();
  metaPort.operatorMeta = OperatorMeta.this;
  metaPort.fieldName = field.getName();
  metaPort.portAnnotation = portAnnotation;
  metaPort.adrAnnotation = adrAnnotation;
  outPortMap.put(portObject, metaPort);
  markOutputPortIfHidden(metaPort.getPortName(), metaPort, field.getDeclaringClass());
}
 
Example #6
Source File: Operators.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public void addOutputPort(Operator.OutputPort<?> port, Field field, OutputPortFieldAnnotation portAnnotation, AppData.ResultPort adrAnnotation)
{
  if (!outputPorts.containsKey(field.getName())) {
    outputPorts.put(field.getName(), new PortContextPair<OutputPort<?>>(port));
  }
}
 
Example #7
Source File: Operators.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public static void describe(GenericOperator operator, OperatorDescriptor descriptor)
{
  for (Class<?> c = operator.getClass(); c != Object.class; c = c.getSuperclass()) {
    Field[] fields = c.getDeclaredFields();
    for (Field field : fields) {
      field.setAccessible(true);
      InputPortFieldAnnotation inputAnnotation = field.getAnnotation(InputPortFieldAnnotation.class);
      OutputPortFieldAnnotation outputAnnotation = field.getAnnotation(OutputPortFieldAnnotation.class);
      AppData.QueryPort adqAnnotation = field.getAnnotation(AppData.QueryPort.class);
      AppData.ResultPort adrAnnotation = field.getAnnotation(AppData.ResultPort.class);

      try {
        Object portObject = field.get(operator);

        if (portObject instanceof InputPort) {
          descriptor.addInputPort((Operator.InputPort<?>)portObject, field, inputAnnotation, adqAnnotation);
        } else {
          if (inputAnnotation != null) {
            throw new IllegalArgumentException("port is not of type " + InputPort.class.getName() + ": " + field);
          }
        }

        if (portObject instanceof OutputPort) {
          descriptor.addOutputPort((Operator.OutputPort<?>)portObject, field, outputAnnotation, adrAnnotation);
        } else {
          if (outputAnnotation != null) {
            throw new IllegalArgumentException("port is not of type " + OutputPort.class.getName() + ": " + field);
          }
        }
      } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
      }
    }
  }
}
 
Example #8
Source File: PlanModifier.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public <T> StreamMeta addStream(String id, Operator.OutputPort<? extends T> source, Operator.InputPort<?>... sinks)
{
  StreamMeta sm = logicalPlan.getStream(id);
  if (sm != null) {
    if (sm.getSource().getOperatorMeta().getMeta(source) != sm.getSource()) {
      throw new AssertionError(String.format("Stream %s already connected to %s", sm, sm.getSource()));
    }
  } else {
    // fails on duplicate stream name
    @SuppressWarnings("unchecked")
    StreamMeta newStream = logicalPlan.addStream(id, source);
    sm = newStream;
  }
  return addSinks(id, sinks);
}
 
Example #9
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
private OutputPortMeta assertGetPortMeta(Operator.OutputPort<?> port)
{
  for (OperatorMeta o : getAllOperators()) {
    OutputPortMeta opm = o.getPortMapping().outPortMap.get(port);
    if (opm != null) {
      return opm;
    }
  }
  throw new IllegalArgumentException("Port is not associated to any operator in the DAG: " + port);
}
 
Example #10
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> StreamMeta addStream(String id, Operator.OutputPort<? extends T> source, Operator.InputPort<? super T> sink1, Operator.InputPort<? super T> sink2)
{
  @SuppressWarnings("rawtypes")
  InputPort[] ports = new Operator.InputPort[] {sink1, sink2};
  return addStream(id, source, ports);
}
 
Example #11
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> StreamMeta addStream(String id, Operator.OutputPort<? extends T> source, Operator.InputPort<? super T> sink1)
{
  @SuppressWarnings("rawtypes")
  InputPort[] ports = new Operator.InputPort[]{sink1};
  return addStream(id, source, ports);
}
 
Example #12
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> StreamMeta addStream(@Nonnull String id, Operator.OutputPort<? extends T> source, Operator.InputPort<? super T>... sinks)
{
  StreamMeta s = addStream(id);
  s.setSource(source);
  for (Operator.InputPort<?> sink : sinks) {
    s.addSink(sink);
  }
  return s;
}
 
Example #13
Source File: TwitterTopCounterApplication.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
public static void consoleOutput(DAG dag, String operatorName, OutputPort<List<Map<String, Object>>> topCount, String schemaFile, String alias)
{
  if (PubSubHelper.isGatewayConfigured(dag)) {
    URI uri = PubSubHelper.getURI(dag);

    AppDataSnapshotServerMap snapshotServer = dag.addOperator("SnapshotServer", new AppDataSnapshotServerMap());

    Map<String, String> conversionMap = Maps.newHashMap();
    conversionMap.put(alias, WindowedTopCounter.FIELD_TYPE);
    String snapshotServerJSON = SchemaUtils.jarResourceFileToString(schemaFile);

    snapshotServer.setSnapshotSchemaJSON(snapshotServerJSON);
    snapshotServer.setTableFieldToMapField(conversionMap);

    PubSubWebSocketAppDataQuery wsQuery = new PubSubWebSocketAppDataQuery();
    wsQuery.setUri(uri);
    snapshotServer.setEmbeddableQueryInfoProvider(wsQuery);

    PubSubWebSocketAppDataResult wsResult = dag.addOperator("QueryResult", new PubSubWebSocketAppDataResult());
    wsResult.setUri(uri);
    Operator.InputPort<String> queryResultPort = wsResult.input;

    dag.addStream("MapProvider", topCount, snapshotServer.input);
    dag.addStream("Result", snapshotServer.queryResult, queryResultPort);
  } else {
    ConsoleOutputOperator operator = dag.addOperator(operatorName, new ConsoleOutputOperator());
    operator.setStringFormat(operatorName + ": %s");

    dag.addStream("MapProvider", topCount, operator.input);
  }
}
 
Example #14
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public OutputPort<?> getPort()
{
  for (Map.Entry<OutputPort<?>, OutputPortMeta> e : operatorMeta.getPortMapping().outPortMap.entrySet()) {
    if (e.getValue() == this) {
      return e.getKey();
    }
  }
  throw new AssertionError("Cannot find the port object for " + this);
}
 
Example #15
Source File: Node.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public void removeSinks(Map<String, Sink<Object>> sinks)
{
  boolean changes = false;
  for (Entry<String, Sink<Object>> e : sinks.entrySet()) {
    /* make sure that we ignore all the input ports */
    PortContextPair<OutputPort<?>> pcpair = descriptor.outputPorts.get(e.getKey());
    if (pcpair == null) {
      continue;
    }

    Sink<Object> ics = outputs.get(e.getKey());
    if (ics == e.getValue()) {
      pcpair.component.setSink(null);
      outputs.remove(e.getKey());
      changes = true;
    } else if (ics instanceof MuxSink) {
      MuxSink ms = (MuxSink)ics;
      ms.remove(e.getValue());
      Sink<Object>[] sinks1 = ms.getSinks();
      if (sinks1.length == 0) {
        pcpair.component.setSink(null);
        outputs.remove(e.getKey());
        changes = true;
      } else if (sinks1.length == 1) {
        pcpair.component.setSink(sinks1[0]);
        outputs.put(e.getKey(), sinks1[0]);
        changes = true;
      }
    }
  }

  if (changes) {
    activateSinks();
  }
}
 
Example #16
Source File: Node.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
public void addSinks(Map<String, Sink<Object>> sinks)
{
  boolean changes = false;
  for (Entry<String, Sink<Object>> e : sinks.entrySet()) {
    /* make sure that we ignore all the input ports */
    PortContextPair<OutputPort<?>> pcpair = descriptor.outputPorts.get(e.getKey());
    if (pcpair == null) {
      continue;
    }
    changes = true;

    Sink<Object> ics = outputs.get(e.getKey());
    if (ics == null) {
      pcpair.component.setSink(e.getValue());
      outputs.put(e.getKey(), e.getValue());
      changes = true;
    } else if (ics instanceof MuxSink) {
      ((MuxSink)ics).add(e.getValue());
    } else {
      MuxSink muxSink = new MuxSink(ics, e.getValue());
      pcpair.component.setSink(muxSink);
      outputs.put(e.getKey(), muxSink);
      changes = true;
    }
  }

  if (changes) {
    activateSinks();
  }
}
 
Example #17
Source File: Node.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public void connectOutputPort(String port, final Sink<Object> sink)
{
  PortContextPair<OutputPort<?>> outputPort = descriptor.outputPorts.get(port);
  if (outputPort != null) {
    if (sink == null) {
      outputPort.component.setSink(null);
      outputs.remove(port);
    } else {
      outputPort.component.setSink(sink);
      outputs.put(port, sink);
    }
  }
}
 
Example #18
Source File: Module.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
public void set(OutputPort<T> port)
{
  outputPort = port;
}
 
Example #19
Source File: TestUtils.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
public static <S extends Sink, T> S setSink(OutputPort<T> port, S sink)
{
  port.setSink(sink);
  return sink;
}
 
Example #20
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void setUnifierAttribute(Operator.OutputPort<?> port, Attribute<T> key, T value)
{
  assertGetPortMeta(port).getUnifierMeta().attributes.put(key, value);
}
 
Example #21
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void setOutputPortAttribute(Operator.OutputPort<?> port, Attribute<T> key, T value)
{
  assertGetPortMeta(port).attributes.put(key, value);
}
 
Example #22
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
@Override
public OutputPortMeta getMeta(Operator.OutputPort<?> port)
{
  return getPortMapping().outPortMap.get(port);
}
 
Example #23
Source File: Module.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
public OutputPort<T> get()
{
  return outputPort;
}
 
Example #24
Source File: Operators.java    From attic-apex-core with Apache License 2.0 votes vote down vote up
void addOutputPort(Operator.OutputPort<?> port, Field field, OutputPortFieldAnnotation portAnnotation, AppData.ResultPort adrAnnotation);