Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REST] New REST APIs to generate DSL syntax for items and things #4569

Merged
merged 10 commits into from
Mar 2, 2025

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.core.model.core;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
Expand All @@ -26,6 +27,7 @@
* come from.
*
* @author Kai Kreuzer - Initial contribution
* @author Laurent Garnier - Added method generateSyntaxFromModel
*/
@NonNullByDefault
public interface ModelRepository {
Expand Down Expand Up @@ -92,4 +94,14 @@ public interface ModelRepository {
* @param listener the listener to remove
*/
void removeModelRepositoryChangeListener(ModelRepositoryChangeListener listener);

/**
* Generate the syntax from a provided model content.
*
* @param out the output stream to write the generated syntax to
* @param modelType the model type
* @param modelContent the content of the model
* @return the corresponding syntax
*/
void generateSyntaxFromModel(OutputStream out, String modelType, EObject modelContent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,6 +51,7 @@
* @author Kai Kreuzer - Initial contribution
* @author Oliver Libutzki - Added reloadAllModelsOfType method
* @author Simon Kaufmann - added validation of models before loading them
* @author Laurent Garnier - Added method generateSyntaxFromModel
*/
@Component(immediate = true)
@NonNullByDefault
Expand All @@ -64,6 +66,8 @@ public class ModelRepositoryImpl implements ModelRepository {

private final SafeEMF safeEmf;

private int counter;

@Activate
public ModelRepositoryImpl(final @Reference SafeEMF safeEmf) {
this.safeEmf = safeEmf;
Expand Down Expand Up @@ -171,7 +175,8 @@ public Iterable<String> getAllModelNamesOfType(final String modelType) {

return resourceListCopy.stream()
.filter(input -> input.getURI().lastSegment().contains(".") && input.isLoaded()
&& modelType.equalsIgnoreCase(input.getURI().fileExtension()))
&& modelType.equalsIgnoreCase(input.getURI().fileExtension())
&& !input.getURI().lastSegment().startsWith("tmp_"))
.map(from -> from.getURI().path()).toList();
}
}
Expand Down Expand Up @@ -227,6 +232,23 @@ public void removeModelRepositoryChangeListener(ModelRepositoryChangeListener li
listeners.remove(listener);
}

@Override
public void generateSyntaxFromModel(OutputStream out, String modelType, EObject modelContent) {
String result = "";
synchronized (resourceSet) {
String name = "tmp_generated_syntax_%d.%s".formatted(++counter, modelType);
Resource resource = resourceSet.createResource(URI.createURI(name));
try {
resource.getContents().add(modelContent);
resource.save(out, Map.of(XtextResource.OPTION_ENCODING, StandardCharsets.UTF_8.name()));
} catch (IOException e) {
logger.warn("Exception when saving the model {}", resource.getURI().lastSegment());
} finally {
resourceSet.getResources().remove(resource);
}
}
}

private @Nullable Resource getResource(String name) {
return resourceSet.getResource(URI.createURI(name), false);
}
Expand Down
1 change: 1 addition & 0 deletions bundles/org.openhab.core.model.item/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Import-Package: javax.measure,\
org.openhab.core.i18n,\
org.openhab.core.items,\
org.openhab.core.items.dto,\
org.openhab.core.items.fileconverter,\
org.openhab.core.library.items,\
org.openhab.core.library.types,\
org.openhab.core.thing.util,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ org.openhab.core.model

import com.google.inject.Binder
import com.google.inject.name.Names
import org.openhab.core.model.formatting.ItemsFormatter
import org.openhab.core.model.internal.valueconverter.ItemValueConverters
import org.eclipse.xtext.conversion.IValueConverterService
import org.eclipse.xtext.formatting.IFormatter
import org.eclipse.xtext.linking.lazy.LazyURIEncoder

/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
class ItemsRuntimeModule extends AbstractItemsRuntimeModule {

override Class<? extends IValueConverterService> bindIValueConverterService() {
return ItemValueConverters
}


override Class<? extends IFormatter> bindIFormatter() {
return ItemsFormatter
}

override void configureUseIndexFragmentsForLazyLinking(Binder binder) {
binder.bind(Boolean.TYPE).annotatedWith(Names.named(LazyURIEncoder.USE_INDEXED_FRAGMENTS_BINDING)).toInstance(
Boolean.FALSE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ class ItemsFormatter extends AbstractDeclarativeFormatter {

override protected void configureFormatting(FormattingConfig c) {
c.setLinewrap(1, 1, 2).before(modelGroupItemRule)
c.setLinewrap(1, 1, 2).before(modelItemTypeRule)
c.setLinewrap(1, 1, 2).before(modelNormalItemRule)

c.setNoSpace().withinKeywordPairs("<", ">")
c.setNoSpace().withinKeywordPairs("(", ")")
c.setNoSpace().withinKeywordPairs("[", "]")

c.setIndentationIncrement.after(modelItemTypeRule)
c.setIndentationDecrement.before(modelItemTypeRule)
c.setIndentationIncrement.after(modelGroupItemRule)
c.setIndentationDecrement.before(modelGroupItemRule)
c.setNoSpace().around(":", "=")
c.setNoSpace().before(",")

c.autoLinewrap = 400

c.autoLinewrap = 160
c.setLinewrap(0, 1, 2).before(SL_COMMENTRule)
c.setLinewrap(0, 1, 2).before(ML_COMMENTRule)
c.setLinewrap(0, 1, 1).after(ML_COMMENTRule)
Expand All @@ -48,4 +48,16 @@ class ItemsFormatter extends AbstractDeclarativeFormatter {
locator.before(pair.second)
}
}

def around(FormattingConfig.ElementLocator locator, String ... listKW) {
for (keyword : findKeywords(listKW)) {
locator.around(keyword)
}
}

def before(FormattingConfig.ElementLocator locator, String ... listKW) {
for (keyword : findKeywords(listKW)) {
locator.before(keyword)
}
}
}
Loading