Skip to content

Commit

Permalink
refactor(codegen): cleanup type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 23, 2022
1 parent 8d36f18 commit 9ac5c0c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
package org.apache.plc4x.plugins.codegenerator.language.mspec;

import org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition;
import org.apache.plc4x.plugins.codegenerator.types.definitions.TypeDefinition;

import java.util.function.Consumer;

public interface LazyTypeDefinitionConsumer {

void setOrScheduleTypeDefinitionConsumer(String typeRefName, Consumer<ComplexTypeDefinition> setComplexTypeDefinition);
void setOrScheduleTypeDefinitionConsumer(String typeRefName, Consumer<TypeDefinition> setTypeDefinition);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,38 @@ public class DefaultComplexTypeDefinition extends DefaultTypeDefinition implemen

private final boolean isAbstract;
private final List<Field> fields;
protected ComplexTypeDefinition parentType;

public DefaultComplexTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, boolean isAbstract, List<Field> fields) {
super(name, attributes, parserArguments);
this.isAbstract = isAbstract;
this.fields = Objects.requireNonNull(fields);
}

public Optional<ComplexTypeDefinition> getParentType() {
return Optional.ofNullable(parentType);
}

public void setParentType(ComplexTypeDefinition parentType) {
this.parentType = parentType;
}

public Optional<List<Argument>> getAllParserArguments() {
List<Argument> allArguments = new ArrayList<>();
getParentType()
.map(ComplexTypeDefinition::getParserArguments)
.map(arguments -> arguments.orElse(Collections.emptyList()))
.map(allArguments::addAll);
if (parserArguments != null) {
allArguments.addAll(parserArguments);
}
if (allArguments.isEmpty()) {
return Optional.empty();
}
return Optional.of(allArguments);
}


public boolean isAbstract() {
return isAbstract;
}
Expand Down Expand Up @@ -104,19 +129,16 @@ public List<VirtualField> getVirtualFields() {
@Override
public List<PropertyField> getAllPropertyFields() {
List<PropertyField> fields = new LinkedList<>();
if (getParentType() != null) {
fields.addAll(((ComplexTypeDefinition) getParentType()).getAllPropertyFields());
}
getParentType()
.map(ComplexTypeDefinition::getAllPropertyFields)
.map(fields::addAll);
fields.addAll(getPropertyFields());
return fields;
}

@Override
public List<PropertyField> getParentPropertyFields() {
if (getParentType() == null) {
return Collections.emptyList();
}
return ((ComplexTypeDefinition) getParentType()).getAllPropertyFields();
return getParentType().map(ComplexTypeDefinition::getAllPropertyFields).orElse(Collections.emptyList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class DefaultDiscriminatedComplexTypeDefinition extends DefaultComplexTypeDefinition implements DiscriminatedComplexTypeDefinition {

Expand All @@ -37,14 +38,15 @@ public DefaultDiscriminatedComplexTypeDefinition(String name, Map<String, Term>
this.discriminatorValueTerms = Objects.requireNonNull(discriminatorValueTerms);
}

public DiscriminatorField getDiscriminatorField() {
public Optional<DiscriminatorField> getDiscriminatorField() {
// For a discriminated type, the discriminator is always defined in the parent type,
// which is always a DefaultComplexTypeDefinition instance.
return ((DefaultComplexTypeDefinition) getParentType()).getFields().stream()
.filter(field -> field instanceof DiscriminatorField)
.map(field -> (DiscriminatorField) field)
.findFirst()
.orElse(null);
return getParentType()
.flatMap(parentType -> parentType.getFields().stream()
.filter(field -> field instanceof DiscriminatorField)
.map(DiscriminatorField.class::cast)
.findFirst()
);
}

public List<Term> getDiscriminatorValueTerms() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@


import org.apache.plc4x.plugins.codegenerator.types.definitions.Argument;
import org.apache.plc4x.plugins.codegenerator.types.definitions.TypeDefinition;
import org.apache.plc4x.plugins.codegenerator.types.terms.Term;

import java.util.*;
Expand All @@ -30,13 +29,11 @@ public abstract class DefaultTypeDefinition {
protected final String name;
private final Map<String, Term> attributes;
protected final List<Argument> parserArguments;
protected TypeDefinition parentType;

public DefaultTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments) {
this.name = Objects.requireNonNull(name);
this.attributes = attributes;
this.parserArguments = parserArguments;
this.parentType = null;
}

public String getName() {
Expand All @@ -56,31 +53,21 @@ public Optional<List<Argument>> getParserArguments() {

public Optional<List<Argument>> getAllParserArguments() {
List<Argument> allArguments = new ArrayList<>();
if (getParentType() != null) {
final TypeDefinition parentType = getParentType();
allArguments.addAll(parentType.getParserArguments().orElse(Collections.emptyList()));
}
if (parserArguments != null) {
allArguments.addAll(parserArguments);
}
if (allArguments.isEmpty()) {
return Optional.empty();
}
return Optional.of(allArguments);
}

public TypeDefinition getParentType() {
return parentType;
}

public void setParentType(TypeDefinition parentType) {
this.parentType = parentType;
}

@Override
public String toString() {
return "DefaultTypeDefinition{" +
"name='" + name + '\'' +
", attributes=" + attributes +
", parserArguments=" + parserArguments +
", parentType=" + parentType +
'}';
}

Expand All @@ -89,11 +76,11 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DefaultTypeDefinition that = (DefaultTypeDefinition) o;
return name.equals(that.name) && Objects.equals(attributes, that.attributes) && Objects.equals(parserArguments, that.parserArguments) && Objects.equals(parentType, that.parentType);
return name.equals(that.name) && Objects.equals(attributes, that.attributes) && Objects.equals(parserArguments, that.parserArguments);
}

@Override
public int hashCode() {
return Objects.hash(name, attributes, parserArguments, parentType);
return Objects.hash(name, attributes, parserArguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType

protected Map<String, TypeDefinition> types;

protected Map<String, List<Consumer<ComplexTypeDefinition>>> typeDefinitionConsumers = new HashMap<>();
protected Map<String, List<Consumer<TypeDefinition>>> typeDefinitionConsumers = new HashMap<>();

private Stack<Map<String, Term>> batchSetAttributes = new Stack<>();

Expand Down Expand Up @@ -127,14 +127,6 @@ public void exitComplexType(MSpecParser.ComplexTypeContext ctx) {
DefaultDataIoTypeDefinition type = new DefaultDataIoTypeDefinition(typeName, attributes, parserArguments, switchField);
dispatchType(typeName, type);

// Set the parent type for all sub-types.
if (switchField != null) {
for (DiscriminatedComplexTypeDefinition subtype : switchField.getCases()) {
if (subtype instanceof DefaultDiscriminatedComplexTypeDefinition) {
((DefaultDiscriminatedComplexTypeDefinition) subtype).setParentType(type);
}
}
}
parserContexts.pop();
} else { // Handle all other types.
// If the type has sub-types it's an abstract type.
Expand Down Expand Up @@ -236,7 +228,7 @@ public void enterDiscriminatorField(MSpecParser.DiscriminatorFieldContext ctx) {
public void enterEnumField(MSpecParser.EnumFieldContext ctx) {
String typeRefName = ctx.type.complexTypeReference.getText();
DefaultComplexTypeReference type = new DefaultComplexTypeReference(typeRefName, null);
setOrScheduleTypeDefinitionConsumer(typeRefName, type::setComplexTypeDefinition);
setOrScheduleTypeDefinitionConsumer(typeRefName, type::setTypeDefinition);
String name = getIdString(ctx.name);
String fieldName = null;
if (ctx.fieldName != null) {
Expand Down Expand Up @@ -542,7 +534,7 @@ private TypeReference getTypeReference(MSpecParser.TypeReferenceContext ctx) {
} else {
String typeRefName = ctx.complexTypeReference.getText();
DefaultComplexTypeReference type = new DefaultComplexTypeReference(typeRefName, getParams(ctx.params));
setOrScheduleTypeDefinitionConsumer(typeRefName, type::setComplexTypeDefinition);
setOrScheduleTypeDefinitionConsumer(typeRefName, type::setTypeDefinition);
return type;
}
}
Expand Down Expand Up @@ -664,11 +656,11 @@ private String getExprString(MSpecParser.ExpressionContext ctx) {

public void dispatchType(String typeName, TypeDefinition type) {
LOGGER.debug("dispatching {}:{}", typeName, type);
List<Consumer<ComplexTypeDefinition>> waitingConsumers = typeDefinitionConsumers.getOrDefault(typeName, new LinkedList<>());
List<Consumer<TypeDefinition>> waitingConsumers = typeDefinitionConsumers.getOrDefault(typeName, new LinkedList<>());
LOGGER.debug("{} waiting for {}", waitingConsumers.size(), typeName);
Iterator<Consumer<ComplexTypeDefinition>> consumerIterator = waitingConsumers.iterator();
Iterator<Consumer<TypeDefinition>> consumerIterator = waitingConsumers.iterator();
while (consumerIterator.hasNext()) {
Consumer<ComplexTypeDefinition> setter = consumerIterator.next();
Consumer<TypeDefinition> setter = consumerIterator.next();
LOGGER.debug("setting {} for {}", typeName, setter);
setter.accept(type);
consumerIterator.remove();
Expand All @@ -678,20 +670,20 @@ public void dispatchType(String typeName, TypeDefinition type) {
}

@Override
public void setOrScheduleTypeDefinitionConsumer(String typeRefName, Consumer<ComplexTypeDefinition> setComplexTypeDefinition) {
public void setOrScheduleTypeDefinitionConsumer(String typeRefName, Consumer<TypeDefinition> setTypeDefinition) {
LOGGER.debug("set or schedule {}", typeRefName);
TypeDefinition typeDefinition = types.get(typeRefName);
if (typeDefinition != null) {
LOGGER.debug("{} present so setting for {}", typeRefName, setComplexTypeDefinition);
LOGGER.debug("{} present so setting for {}", typeRefName, setTypeDefinition);
// TODO: This should actually only be a complex-type, right?
setComplexTypeDefinition.accept((ComplexTypeDefinition) typeDefinition);
setTypeDefinition.accept((ComplexTypeDefinition) typeDefinition);
} else {
// put up order
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("{} already waiting for {}", typeDefinitionConsumers.getOrDefault(typeRefName, new LinkedList<>()).size(), typeRefName);
}
typeDefinitionConsumers.putIfAbsent(typeRefName, new LinkedList<>());
typeDefinitionConsumers.get(typeRefName).add(setComplexTypeDefinition);
typeDefinitionConsumers.get(typeRefName).add(setTypeDefinition);
}
}

Expand Down
2 changes: 1 addition & 1 deletion plc4j/drivers/opcua/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ appender.out.type=Console
appender.out.name=out
appender.out.layout.type=PatternLayout
appender.out.layout.pattern=[%30.30t] %-30.30c{1} %-5p %m%n
rootLogger.level=TRACE
rootLogger.level=ERROR
rootLogger.appenderRef.out.ref=out

0 comments on commit 9ac5c0c

Please sign in to comment.