-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added support for model inheritance in code generators * added support for datatypes
- Loading branch information
Showing
11 changed files
with
366 additions
and
5 deletions.
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
...del/org.eclipse.vorto.core/src/org/eclipse/vorto/core/api/model/ModelConversionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/** | ||
* Copyright (c) 2015-2018 Bosch Software Innovations GmbH and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* The Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* Contributors: | ||
* Bosch Software Innovations GmbH - Please refer to git log | ||
*/ | ||
package org.eclipse.vorto.core.api.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.emf.common.util.TreeIterator; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.util.EcoreUtil; | ||
import org.eclipse.vorto.core.api.model.datatype.Entity; | ||
import org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType; | ||
import org.eclipse.vorto.core.api.model.datatype.Property; | ||
import org.eclipse.vorto.core.api.model.functionblock.Configuration; | ||
import org.eclipse.vorto.core.api.model.functionblock.FunctionBlock; | ||
import org.eclipse.vorto.core.api.model.functionblock.FunctionblockFactory; | ||
import org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel; | ||
import org.eclipse.vorto.core.api.model.functionblock.Operation; | ||
import org.eclipse.vorto.core.api.model.functionblock.Status; | ||
import org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty; | ||
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel; | ||
import org.eclipse.vorto.core.api.model.model.ModelFactory; | ||
import org.eclipse.vorto.core.api.model.model.ModelIdFactory; | ||
import org.eclipse.vorto.core.api.model.model.ModelReference; | ||
|
||
public class ModelConversionUtils { | ||
|
||
public static FunctionblockModel convertToFlatHierarchy(FunctionblockModel fbm) { | ||
FunctionBlock fb = fbm.getFunctionblock(); | ||
|
||
// Consolidate all properties | ||
List<Property> properties = getFlatProperties(fbm); | ||
|
||
// remove super type reference | ||
if (fbm.getSuperType() != null) { | ||
removeSuperTypeModelReference(fbm); | ||
} | ||
properties.stream().filter(p -> p.getType() instanceof ObjectPropertyType).forEach(p -> createReference(fbm, (ObjectPropertyType)p.getType())); | ||
|
||
Status status = FunctionblockFactory.eINSTANCE.createStatus(); | ||
status.getProperties().addAll(properties.stream().filter(p -> p.eContainer() instanceof Status).collect(Collectors.toList())); | ||
|
||
fb.setStatus(status); | ||
|
||
Configuration configuration = FunctionblockFactory.eINSTANCE.createConfiguration(); | ||
configuration.getProperties().addAll(properties.stream().filter(p -> p.eContainer() instanceof Configuration).collect(Collectors.toList())); | ||
fb.setConfiguration(configuration); | ||
|
||
|
||
// Consolidate all operations | ||
List<Operation> operations = getFlatOperations(fbm); | ||
fb.getOperations().clear(); | ||
fb.getOperations().addAll(operations); | ||
|
||
|
||
return fbm; | ||
} | ||
|
||
private static void removeSuperTypeModelReference(FunctionblockModel fbm) { | ||
Iterator<ModelReference> iter = fbm.getReferences().iterator(); | ||
while (iter.hasNext()) { | ||
ModelReference reference = iter.next(); | ||
ModelReference superTypeReference = ModelIdFactory.newInstance(fbm.getSuperType()).asModelReference(); | ||
if (EcoreUtil.equals(superTypeReference, reference)) { | ||
iter.remove(); | ||
} | ||
} | ||
|
||
} | ||
|
||
private static void createReference(FunctionblockModel fbm, ObjectPropertyType type) { | ||
ModelReference reference = ModelFactory.eINSTANCE.createModelReference(); | ||
reference.setImportedNamespace(type.getType().getNamespace()+"."+type.getType().getName()); | ||
reference.setVersion(type.getType().getVersion()); | ||
fbm.getReferences().add(reference); | ||
} | ||
|
||
private static List<Operation> getFlatOperations(FunctionblockModel fbm) { | ||
List<Operation> operations = new ArrayList<Operation>(); | ||
TreeIterator<EObject> iter = fbm.eAllContents(); | ||
while (iter.hasNext()) { | ||
EObject obj = iter.next(); | ||
if (obj instanceof Operation) { | ||
operations.add((Operation)obj); | ||
} | ||
} | ||
if (fbm.getSuperType() != null) { | ||
operations.addAll(getFlatOperations(fbm.getSuperType())); | ||
} | ||
return operations; | ||
} | ||
|
||
public static InformationModel convertToFlatHierarchy(InformationModel infomodel) { | ||
for (FunctionblockProperty fbProperty : infomodel.getProperties()) { | ||
FunctionblockModel fbm = fbProperty.getType(); | ||
fbProperty.setType(convertToFlatHierarchy(fbm)); | ||
|
||
// merge any extended properties from information model to the FB properties | ||
if (fbProperty.getExtendedFunctionBlock() != null && fbProperty.getExtendedFunctionBlock().getStatus() != null) { | ||
for (Property extendedProperty : fbProperty.getExtendedFunctionBlock().getStatus().getProperties()) { | ||
Optional<Property> baseProperty = fbm.getFunctionblock().getStatus().getProperties().stream().filter(p -> p.getName().equals(extendedProperty.getName())).findFirst(); | ||
if (baseProperty.isPresent()) { | ||
baseProperty.get().setConstraintRule(extendedProperty.getConstraintRule()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return infomodel; | ||
} | ||
|
||
private static List<Property> getFlatProperties(FunctionblockModel fbm) { | ||
List<Property> properties = new ArrayList<Property>(); | ||
TreeIterator<EObject> iter = fbm.eAllContents(); | ||
while (iter.hasNext()) { | ||
EObject obj = iter.next(); | ||
if (obj instanceof Property) { | ||
Property property = (Property)obj; | ||
properties.add(property); | ||
|
||
if (property.getType() instanceof ObjectPropertyType) { | ||
ObjectPropertyType objectType = (ObjectPropertyType)property.getType(); | ||
if (objectType.getType() instanceof Entity) { // only flatten entities | ||
Entity entity = (Entity)((ObjectPropertyType)property.getType()).getType(); | ||
List<Property> entityProperties = getFlatProperties(entity); | ||
entity.getProperties().addAll(entityProperties); | ||
if (entity.getSuperType() != null) { | ||
removeSuperTypeModelReference(entity); | ||
} | ||
entity.getProperties().stream().filter(p -> p.getType() instanceof ObjectPropertyType).forEach(p -> createReference(entity,(ObjectPropertyType)p.getType())); | ||
|
||
} | ||
} | ||
} | ||
} | ||
if (fbm.getSuperType() != null) { | ||
properties.addAll(getFlatProperties(fbm.getSuperType())); | ||
} | ||
return properties; | ||
} | ||
|
||
private static void removeSuperTypeModelReference(Entity entity) { | ||
Iterator<ModelReference> iter = entity.getReferences().iterator(); | ||
while (iter.hasNext()) { | ||
ModelReference reference = iter.next(); | ||
ModelReference superTypeReference = ModelIdFactory.newInstance(entity.getSuperType()).asModelReference(); | ||
if (EcoreUtil.equals(superTypeReference, reference)) { | ||
iter.remove(); | ||
} | ||
} | ||
|
||
} | ||
|
||
private static void createReference(Entity entity, ObjectPropertyType type) { | ||
ModelReference reference = ModelFactory.eINSTANCE.createModelReference(); | ||
reference.setImportedNamespace(type.getType().getNamespace()+"."+type.getType().getName()); | ||
reference.setVersion(type.getType().getVersion()); | ||
entity.getReferences().add(reference); | ||
} | ||
|
||
private static List<Property> getFlatProperties(Entity entity) { | ||
List<Property> properties = new ArrayList<Property>(); | ||
TreeIterator<EObject> iter = entity.eAllContents(); | ||
while (iter.hasNext()) { | ||
EObject obj = iter.next(); | ||
if (obj instanceof Property) { | ||
Property property = (Property)obj; | ||
properties.add(property); | ||
} | ||
} | ||
if (entity.getSuperType() != null) { | ||
properties.addAll(getFlatProperties(entity.getSuperType())); | ||
} | ||
return properties; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace iot | ||
version 0.0.1 | ||
|
||
enum Brightness { | ||
HIGH,LOW | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace iot | ||
version 0.0.1 | ||
|
||
using iot.Light;0.0.1 | ||
|
||
entity ColorLight extends Light { | ||
mandatory color as string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace iot | ||
version 0.0.1 | ||
|
||
using iot.Brightness;0.0.1 | ||
|
||
entity Light { | ||
mandatory brightness as Brightness | ||
} |
18 changes: 18 additions & 0 deletions
18
utilities/dsl-reader/src/test/resources/dsls/SomeFb.fbmodel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace iot | ||
version 0.0.1 | ||
displayname "SomeFb" | ||
description "" | ||
|
||
using iot.SuperFb;0.0.1 | ||
|
||
functionblock SomeFb extends SuperFb { | ||
|
||
configuration { | ||
mandatory configProp as string | ||
} | ||
|
||
status { | ||
mandatory statusProp as string | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
utilities/dsl-reader/src/test/resources/dsls/SuperFb.fbmodel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace iot | ||
version 0.0.1 | ||
displayname "SuperFb" | ||
description "" | ||
|
||
using iot.SuperSuperFb;0.0.1 | ||
|
||
functionblock SuperFb extends SuperSuperFb { | ||
|
||
configuration { | ||
mandatory superConfigProp as string | ||
} | ||
|
||
status { | ||
mandatory superStatusProp as string | ||
} | ||
|
||
operations { | ||
superOp() | ||
} | ||
|
||
} |
Oops, something went wrong.