[
https://issues.apache.org/jira/browse/APEXMALHAR-2010?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15203815#comment-15203815
]
ASF GitHub Bot commented on APEXMALHAR-2010:
--------------------------------------------
Github user chinmaykolhatkar commented on a diff in the pull request:
https://github.com/apache/incubator-apex-malhar/pull/209#discussion_r56785970
--- Diff:
library/src/main/java/com/datatorrent/lib/transform/TransformOperator.java ---
@@ -0,0 +1,172 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.datatorrent.lib.transform;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang3.ClassUtils;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DefaultInputPort;
+import com.datatorrent.api.DefaultOutputPort;
+import com.datatorrent.api.Operator;
+import com.datatorrent.api.annotation.InputPortFieldAnnotation;
+import com.datatorrent.api.annotation.OutputPortFieldAnnotation;
+import com.datatorrent.common.util.BaseOperator;
+import com.datatorrent.lib.expression.Expression;
+import com.datatorrent.lib.util.PojoUtils;
+
+/**
+ * This operator can transform given POJO using provided expressions and
+ * return a final POJO as a return of transformation process.
+ *
+ * Following are the mandatory fields that needs to be set for
TransformOperator to work:
+ * <ul>
+ * <li><b>expressionMap</b> : Set how the transformation should
happen</li>
+ * <li><b>inputPort.attr.TUPLE_CLASS</b>: Set class type at input
port</li>
+ * <li><b>outputPort.attr.TUPLE_CLASS</b> : Set class type at output
port</li>
+ * </ul>
+ *
+ * The operator uses interaction via {@link Expression} and {@link
PojoUtils} to transform given POJO.
+ */
+public class TransformOperator extends BaseOperator implements
Operator.ActivationListener
+{
+ private Map<String, String> expressionMap = new HashMap<>();
+ private List<String> expressionImports = new LinkedList<>();
+
+ private transient Map<PojoUtils.Setter, Expression> transformationMap =
new HashMap<>();
+ private Class<?> inputClass;
+ private Class<?> outputClass;
+
+ public TransformOperator()
+ {
+ expressionImports.add("java.lang.Math.*");
+ expressionImports.add("org.apache.commons.lang3.StringUtils.*");
+ }
+
+ @InputPortFieldAnnotation(schemaRequired = true) public final transient
DefaultInputPort<Object> input = new DefaultInputPort<Object>()
+ {
+ @Override public void setup(Context.PortContext context)
+ {
+ inputClass = context.getValue(Context.PortContext.TUPLE_CLASS);
+ }
+
+ @Override public void process(Object o)
+ {
+ processTuple(o);
+ }
+ };
+
+ @OutputPortFieldAnnotation(schemaRequired = true) public final transient
DefaultOutputPort<Object> output = new DefaultOutputPort<Object>()
+ {
+ @Override public void setup(Context.PortContext context)
+ {
+ outputClass = context.getValue(Context.PortContext.TUPLE_CLASS);
+ }
+ };
+
+ private void processTuple(Object in)
+ {
+ if (!inputClass.isAssignableFrom(in.getClass())) {
+ throw new RuntimeException(
+ "Unexpected tuple received. Received class: " + in.getClass() +
". Expected class: " + inputClass.getClass());
+ }
+
+ Object out;
+ try {
+ out = outputClass.newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new RuntimeException("Failed to create new object", e);
+ }
+
+ for (Map.Entry<PojoUtils.Setter, Expression> entry :
transformationMap.entrySet()) {
+ PojoUtils.Setter set = entry.getKey();
+ Expression expr = entry.getValue();
+ set.set(out, expr.execute(in));
+ }
--- End diff --
Added a variable which can toggle this functionality. Default is true.
Added a unit test for the same.
> Add transform operator
> ----------------------
>
> Key: APEXMALHAR-2010
> URL: https://issues.apache.org/jira/browse/APEXMALHAR-2010
> Project: Apache Apex Malhar
> Issue Type: New Feature
> Components: algorithms
> Affects Versions: 3.4.0
> Reporter: Chinmay Kolhatkar
> Assignee: Chinmay Kolhatkar
>
> Add transform operator
> Details to be added soon once the mailing thread reaching conclusion:
> http://mail-archives.apache.org/mod_mbox/incubator-apex-dev/201603.mbox/%3CCABAipVZUdRGx5dS35bYPN46koLQU5gSLgbCeRH8aM_y9ANRU2w%40mail.gmail.com%3E
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)