You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2022/06/04 11:29:18 UTC

[plc4x] branch feature/plc4rs updated: Started to work on code generation... enums already work (yay!)

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch feature/plc4rs
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/plc4rs by this push:
     new 89195750dd Started to work on code generation... enums already work (yay!)
89195750dd is described below

commit 89195750dd368683b8485a5ba3d41a4e3904435f
Author: julian <j....@pragmaticminds.de>
AuthorDate: Sat Jun 4 13:29:09 2022 +0200

    Started to work on code generation... enums already work (yay!)
---
 .../language/rust/RustLanguageTemplateHelper.java  |  45 +++--
 .../resources/templates/rust/enum-template.rs.ftlh | 125 +------------
 plc4rust/modbus/pom.xml                            | 201 +++++++++++++++++++++
 plc4rust/pom.xml                                   |   4 +
 4 files changed, 242 insertions(+), 133 deletions(-)

diff --git a/code-generation/language-rust/src/main/java/org/apache/plc4x/language/rust/RustLanguageTemplateHelper.java b/code-generation/language-rust/src/main/java/org/apache/plc4x/language/rust/RustLanguageTemplateHelper.java
index a159a10fb3..ce677c733b 100644
--- a/code-generation/language-rust/src/main/java/org/apache/plc4x/language/rust/RustLanguageTemplateHelper.java
+++ b/code-generation/language-rust/src/main/java/org/apache/plc4x/language/rust/RustLanguageTemplateHelper.java
@@ -89,6 +89,25 @@ public class RustLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHe
         return getLanguageTypeNameForTypeReference(typeReference, false);
     }
 
+    public boolean isUnsigned(TypeReference typeReference) {
+        if (typeReference instanceof SimpleTypeReference) {
+            return ((SimpleTypeReference) typeReference).getBaseType() == SimpleTypeReference.SimpleBaseType.UINT;
+        }
+        return false;
+    }
+
+    public boolean isExactBitLength(Optional<TypeReference> optionalTypeReference) {
+        TypeReference typeReference = optionalTypeReference.get();
+        if (isUnsigned(typeReference)) {
+            return Arrays.asList(8, 16, 32, 64, 128).contains(((SimpleTypeReference) typeReference).getSizeInBits());
+        }
+        return false;
+    }
+
+    public int getExactBitLength(Optional<TypeReference> optionalTypeReference) {
+        return ((SimpleTypeReference) optionalTypeReference.get()).getSizeInBits();
+    }
+
     public String getLanguageTypeNameForTypeReference(TypeReference typeReference, boolean allowPrimitive) {
         Objects.requireNonNull(typeReference);
         if (typeReference instanceof ArrayTypeReference) {
@@ -111,37 +130,37 @@ public class RustLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHe
             case BIT:
                 return allowPrimitive ? boolean.class.getSimpleName() : Boolean.class.getSimpleName();
             case BYTE:
-                return allowPrimitive ? byte.class.getSimpleName() : Byte.class.getSimpleName();
+                return "u8";
             case UINT:
                 IntegerTypeReference unsignedIntegerTypeReference = (IntegerTypeReference) simpleTypeReference;
-                if (unsignedIntegerTypeReference.getSizeInBits() <= 4) {
-                    return allowPrimitive ? byte.class.getSimpleName() : Byte.class.getSimpleName();
-                }
                 if (unsignedIntegerTypeReference.getSizeInBits() <= 8) {
-                    return allowPrimitive ? short.class.getSimpleName() : Short.class.getSimpleName();
+                    return "u8";
                 }
                 if (unsignedIntegerTypeReference.getSizeInBits() <= 16) {
-                    return allowPrimitive ? int.class.getSimpleName() : Integer.class.getSimpleName();
+                    return "u16";
                 }
                 if (unsignedIntegerTypeReference.getSizeInBits() <= 32) {
-                    return allowPrimitive ? long.class.getSimpleName() : Long.class.getSimpleName();
+                    return "u32";
+                }
+                if (unsignedIntegerTypeReference.getSizeInBits() <= 64) {
+                    return "u64";
                 }
-                return BigInteger.class.getSimpleName();
+                return "u128";
             case INT:
                 IntegerTypeReference integerTypeReference = (IntegerTypeReference) simpleTypeReference;
                 if (integerTypeReference.getSizeInBits() <= 8) {
-                    return allowPrimitive ? byte.class.getSimpleName() : Byte.class.getSimpleName();
+                    return "i8";
                 }
                 if (integerTypeReference.getSizeInBits() <= 16) {
-                    return allowPrimitive ? short.class.getSimpleName() : Short.class.getSimpleName();
+                    return "i16";
                 }
                 if (integerTypeReference.getSizeInBits() <= 32) {
-                    return allowPrimitive ? int.class.getSimpleName() : Integer.class.getSimpleName();
+                    return "i32";
                 }
                 if (integerTypeReference.getSizeInBits() <= 64) {
-                    return allowPrimitive ? long.class.getSimpleName() : Long.class.getSimpleName();
+                    return "i64";
                 }
-                return BigInteger.class.getSimpleName();
+                return "i128";
             case FLOAT:
             case UFLOAT:
                 FloatTypeReference floatTypeReference = (FloatTypeReference) simpleTypeReference;
diff --git a/code-generation/language-rust/src/main/resources/templates/rust/enum-template.rs.ftlh b/code-generation/language-rust/src/main/resources/templates/rust/enum-template.rs.ftlh
index 9db458cf9e..2c194148e5 100644
--- a/code-generation/language-rust/src/main/resources/templates/rust/enum-template.rs.ftlh
+++ b/code-generation/language-rust/src/main/resources/templates/rust/enum-template.rs.ftlh
@@ -45,127 +45,12 @@ ${helper.packageName(protocolName, languageName, outputFlavor)?replace(".", "/")
  * specific language governing permissions and limitations
  * under the License.
  */
-package ${helper.packageName(protocolName, languageName, outputFlavor)};
+// package ${helper.packageName(protocolName, languageName, outputFlavor)};
 
-import org.apache.plc4x.java.spi.generation.Message;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-// Code generated by code-generation. DO NOT EDIT.
-
-public enum ${type.name} {
-
-<@compress single_line=true>
+plc4x_enum!
+[enum <#if helper.isExactBitLength(type.type)>${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)}<#else>${helper.getExactBitLength(type.type)} => ${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)}</#if> : ${type.name}
     <#list type.enumValues as enumValue>
-        ${enumValue.name}(
-            <#if type.type.isPresent()>
-                (${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)})
-                <#if type.type.orElseThrow().isNonSimpleTypeReference()>
-                    <#if type.type.orElseThrow().isEnumTypeReference()>
-                        ${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)}.${enumValue.value}
-                    <#else>
-                        ${enumValue.value}
-                    </#if>
-                <#else>
-                    ${enumValue.value}
-                </#if>
-            </#if>
-            <#if type.constantNames?has_content>
-                <#if type.type?has_content>, </#if>
-                <#list type.constantNames as constantName>
-                    <#if type.getConstantType(constantName).isNonSimpleTypeReference()>
-                        <#if helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElse(null)) == 'null'>
-                            null
-                        <#elseif type.getConstantType(constantName).isEnumTypeReference()>
-                            ${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)}.${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow())}<#else>(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)}) ${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName).orElseThrow())}</#if><#else>(${helper.getLanguageTypeNameForTypeReference(type [...]
-                        </#if>
-                    <#sep>, </#sep>
-                </#list>
-            </#if>)
-        <#sep>, </#sep>
-    </#list>;
-</...@compress>
-
-<#if type.type.isPresent()>
-    private static final Logger logger = LoggerFactory.getLogger(${type.name}.class);
-
-    private static final Map<${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), false)}, ${type.name}> map;
-    static {
-        map = new HashMap<>();
-        for (${type.name} value : ${type.name}.values()) {
-            map.put((${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)}) value.getValue(), value);
-        }
-    }
-
-    private ${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value;
-</#if>
-<#if type.constantNames?has_content>
-    <#list type.constantNames as constantName>
-        private ${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} ${constantName};
-    </#list>
-</#if>
-
-    ${type.name}(<#if type.type.isPresent()>${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value</#if><#if type.constantNames?has_content><#if type.type?has_content>, </#if><#list type.constantNames as constantName>${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} ${constantName}<#sep>, </#sep></#list></#if>) {
-<#if type.type.isPresent()>        this.value = value;</#if>
-<#if type.constantNames?has_content>
-    <#list type.constantNames as constantName>
-        this.${constantName} = ${constantName};
+        [${enumValue.value} => ${enumValue.name}]<#sep>, </#sep>
     </#list>
-</#if>
-    }
-
-<#if type.type.isPresent()>
-    public ${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} getValue() {
-        return value;
-    }
-</#if>
-
-<#if type.constantNames?has_content>
-    <#list type.constantNames as constantName>
-    public ${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} get${constantName?cap_first}() {
-        return ${constantName};
-    }
-
-    public static ${type.name} firstEnumForField${constantName?cap_first}(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} fieldValue) {
-        for (${type.name} _val : ${type.name}.values()) {
-            if(_val.get${constantName?cap_first}() == fieldValue) {
-                return _val;
-            }
-        }
-        return null;
-    }
-
-    public static List<${type.name}> enumsForField${constantName?cap_first}(${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName), true)} fieldValue) {
-        List<${type.name}> _values = new ArrayList();
-        for (${type.name} _val : ${type.name}.values()) {
-            if(_val.get${constantName?cap_first}() == fieldValue) {
-                _values.add(_val);
-            }
-        }
-        return _values;
-    }
-
-    </#list>
-</#if>
-<#if type.type.isPresent()>
-    public static ${type.name} enumForValue(${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value) {
-        if (!map.containsKey(value)) {
-            logger.error("No ${type.name} for value {}", value);
-        }
-        return map.get(value);
-    }
-</#if>
-
-<#if type.type.isPresent()>
-    public static Boolean isDefined(${helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow(), true)} value) {
-        return map.containsKey(value);
-    }
-</#if>
-
-}
+];
 </#outputformat>
diff --git a/plc4rust/modbus/pom.xml b/plc4rust/modbus/pom.xml
new file mode 100644
index 0000000000..1b2c215342
--- /dev/null
+++ b/plc4rust/modbus/pom.xml
@@ -0,0 +1,201 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.plc4x</groupId>
+    <artifactId>plc4rust</artifactId>
+    <version>0.10.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>plc4rust-driver-modbus</artifactId>
+  <name>PLC4J: Driver: Modbus</name>
+  <description>Implementation of a PLC4X driver for the Modbus protocol.</description>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.plc4x.plugins</groupId>
+        <artifactId>plc4x-maven-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>generate-driver</id>
+            <phase>generate-sources</phase>
+            <goals>
+              <goal>generate-driver</goal>
+            </goals>
+            <configuration>
+              <protocolName>modbus</protocolName>
+              <languageName>rust</languageName>
+              <outputFlavor>read-write</outputFlavor>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.karaf.tooling</groupId>
+        <artifactId>karaf-maven-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>generate-feature-xml</id>
+            <phase>compile</phase>
+            <goals>
+              <!-- Generate the feature.xml -->
+              <goal>features-generate-descriptor</goal>
+              <!-- Check the feature.xml -->
+              <goal>verify</goal>
+            </goals>
+            <configuration>
+              <enableGeneration>true</enableGeneration>
+              <aggregateFeatures>true</aggregateFeatures>
+            </configuration>
+          </execution>
+          <execution>
+            <id>build-kar</id>
+            <phase>package</phase>
+            <goals>
+              <!--
+                Build a kar archive (Jar containing the feature.xml
+                as well as the module content and it's dependencies.
+              -->
+              <goal>kar</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
+            <Bundle-Activator>org.apache.plc4x.java.osgi.DriverActivator</Bundle-Activator>
+            <Export-Service>org.apache.plc4x.java.api.PlcDriver,org.apache.plc4x.java.modbus.tcp.ModbusTcpDriver</Export-Service>
+            <Import-Package>
+              com.fasterxml.jackson.annotation;resolution:=optional,
+              *
+            </Import-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <configuration>
+          <usedDependencies combine.children="append">
+            <usedDependency>org.apache.plc4x:plc4j-transport-serial</usedDependency>
+            <usedDependency>org.apache.plc4x:plc4j-transport-raw-socket</usedDependency>
+            <usedDependency>org.apache.plc4x:plc4x-code-generation-language-java</usedDependency>
+            <usedDependency>org.apache.plc4x:plc4x-protocols-modbus</usedDependency>
+          </usedDependencies>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-api</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-spi</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-transport-tcp</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-transport-serial</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-transport-raw-socket</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-utils-raw-sockets</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+    </dependency>
+
+    <dependency>
+      <groupId>io.netty</groupId>
+      <artifactId>netty-buffer</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-annotations</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>commons-codec</groupId>
+      <artifactId>commons-codec</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.pcap4j</groupId>
+      <artifactId>pcap4j-core</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-utils-test-utils</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4x-code-generation-language-rust</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+      <!-- Scope is 'provided' as this way it's not shipped with the driver -->
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4x-protocols-modbus</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+      <!-- Scope is 'provided' as this way it's not shipped with the driver -->
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4x-protocols-modbus</artifactId>
+      <version>0.10.0-SNAPSHOT</version>
+      <classifier>tests</classifier>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>
diff --git a/plc4rust/pom.xml b/plc4rust/pom.xml
index a71e9ef2b7..03c113357f 100644
--- a/plc4rust/pom.xml
+++ b/plc4rust/pom.xml
@@ -33,6 +33,10 @@
   <name>PLC4Rust</name>
   <description>Implementation of the protocol adapters for usage as Rust module.</description>
 
+  <modules>
+    <module>modbus</module>
+  </modules>
+
   <!-- Disabled for now as C# support is currently not installed in Apache SonarQube -->
   <!--properties>
     <sonar.language>c#</sonar.language>