Skip to content

Commit

Permalink
API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lhstrh committed Oct 15, 2023
1 parent de53b8f commit f1f1735
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 72 deletions.
8 changes: 5 additions & 3 deletions core/src/main/java/org/lflang/AbstractTargetProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,18 @@ protected void update(TargetConfig config, T value) {
config.set(this, value);
}

public void update(TargetConfig config, Element node, MessageReporter reporter) {
public final void update(TargetConfig config, Element node, MessageReporter reporter) {
this.update(config, fromAst(node, reporter));
}

public void update(TargetConfig config, String value, MessageReporter reporter) {
public final void update(TargetConfig config, String value, MessageReporter reporter) {
this.update(config, fromString(value, reporter));
}

/**
* Return true if the given object is an instance of a class with the same name.
* Return true if the given object is an instance of a class with the same name. FIXME: make this
* a singleton class and remove this override https://www.baeldung.com/kotlin/singleton-classes
* https://stackoverflow.com/questions/24214148/java-getinstance-vs-static
*
* @param obj The object to compare this instance to.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
import org.lflang.InferredType;
Expand Down Expand Up @@ -180,7 +181,8 @@ public static void handleCompileDefinitions(
int numOfFederates,
RtiConfig rtiConfig,
MessageReporter messageReporter) {
var definitions = federate.targetConfig.get(new CompileDefinitionsProperty());

var definitions = new HashMap<String, String>();
definitions.put("FEDERATED", "");
definitions.put(
String.format(
Expand All @@ -192,7 +194,8 @@ public static void handleCompileDefinitions(
}
definitions.put("NUMBER_OF_FEDERATES", String.valueOf(numOfFederates));
definitions.put("EXECUTABLE_PREAMBLE", "");
federate.targetConfig.markSet(new CompileDefinitionsProperty());

new CompileDefinitionsProperty().update(federate.targetConfig, definitions);

handleAdvanceMessageInterval(federate);

Expand Down Expand Up @@ -300,8 +303,9 @@ public static void generateCMakeInclude(FederateInstance federate, FedFileConfig
}

new CmakeIncludeProperty()
.add(
federate.targetConfig, fileConfig.getSrcPath().relativize(cmakeIncludePath).toString());
.update(
federate.targetConfig,
List.of(fileConfig.getSrcPath().relativize(cmakeIncludePath).toString()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public void update(
if (p.isPresent()) {
var value = pair.getValue();
if (pair.getName().equals("files")) {
// FIXME: this logic doesn't really belong here.
// Also: what about other target properties that have paths?
var array = LfFactory.eINSTANCE.createArray();
ASTUtils.elementToListOfStrings(pair.getValue()).stream()
.map(relativePath::resolve) // assume all paths are relative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@ public List<String> initialValue() {
}

@Override
public void update(TargetConfig config, Element node, MessageReporter reporter) {
var files = fromAst(node, reporter);
public void update(TargetConfig config, List<String> value) {
var files = new ArrayList<>(value);
var existing = config.get(this);
if (config.isSet(this)) {
files.stream()
.forEach(
f -> {
if (!existing.contains(f)) {
existing.add(f);
}
});

} else {
config.get(this).addAll(files);
config.markSet(this);
existing.forEach(
f -> {
if (!files.contains(f)) {
files.add(f);
}
});
}
config.set(this, files.stream().sorted(String::compareTo).toList());
}

@Override
public List<String> fromAst(Element node, MessageReporter reporter) {
protected List<String> fromAst(Element node, MessageReporter reporter) {
return ASTUtils.elementToListOfStrings(node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ public AbstractStringListProperty() {
super(UnionType.STRING_OR_STRING_ARRAY);
}

public void add(TargetConfig config, String entry) {
config.markSet(this);
config.get(this).add(entry);
}

@Override
public List<String> initialValue() {
return new ArrayList<>();
}

@Override
public void update(TargetConfig config, Element node, MessageReporter reporter) {
public void update(TargetConfig config, List<String> value) {
var files = new ArrayList<>(value);
var existing = config.get(this);
if (config.isSet(this)) {
config.get(this).addAll(fromAst(node, reporter));
existing.forEach(
f -> {
if (!files.contains(f)) {
files.add(f);
}
});
}
config.set(this, files.stream().sorted(String::compareTo).toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,19 @@
package org.lflang.target.property;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.lflang.AbstractTargetProperty;
import org.lflang.MessageReporter;
import org.lflang.Target;
import org.lflang.ast.ASTUtils;
import org.lflang.lf.Element;
import org.lflang.target.TargetConfig;
import org.lflang.target.property.type.UnionType;

/**
* Directive to specify a cmake to be included by the generated build systems.
*
* <p>This gives full control over the C/C++ build as any cmake parameters can be adjusted in the
* included file.
*/
public class CmakeIncludeProperty extends AbstractTargetProperty<List<String>, UnionType> {

public CmakeIncludeProperty() {
super(UnionType.FILE_OR_FILE_ARRAY);
}

public void add(TargetConfig config, String entry) {
config.markSet(this);
config.get(this).add(entry);
}

@Override
public List<String> initialValue() {
return new ArrayList<>();
}

@Override
public void update(TargetConfig config, Element node, MessageReporter reporter) {
var files = fromAst(node, reporter);
var existing = config.get(this);
if (config.isSet(this)) {
files.stream()
.forEach(
f -> {
if (!existing.contains(f)) {
existing.add(f);
}
});

} else {
config.get(this).addAll(files);
config.markSet(this);
}
}
public class CmakeIncludeProperty extends AbstractFileListProperty {

@Override
protected List<String> fromAst(Element node, MessageReporter reporter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.lflang.target.property;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.lflang.AbstractTargetProperty;
Expand All @@ -25,14 +24,12 @@ public CompileDefinitionsProperty() {
super(StringDictionaryType.COMPILE_DEFINITION);
}

public void put(TargetConfig config, String k, String v) {
config.markSet(this);
config.get(this).put(k, v);
}
@Override
public void update(TargetConfig config, Map<String, String> value) {}

@Override
public Map<String, String> initialValue() {
return new HashMap<>();
return Map.of();
}

@Override
Expand Down

0 comments on commit f1f1735

Please sign in to comment.