[New] added StrolchRootElement.addRelation()

This commit is contained in:
Robert von Burg 2021-07-15 09:54:41 +02:00
parent 3aa02aeb77
commit 0ec6e2e634
2 changed files with 70 additions and 16 deletions

View File

@ -2,11 +2,14 @@ package li.strolch.model;
import static java.util.Collections.emptyList;
import static li.strolch.model.StrolchModelConstants.*;
import static li.strolch.model.builder.BuilderHelper.buildParamId;
import static li.strolch.model.builder.BuilderHelper.buildParamName;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
@ -26,27 +29,47 @@ public abstract class AbstractStrolchRootElement extends GroupedParameterizedEle
if (relationP == null) {
String name = buildParamName(param);
relationP = new StringParameter(param, name, "");
switch (element.getObjectType()) {
case Tags.RESOURCE:
relationP.setInterpretation(INTERPRETATION_RESOURCE_REF);
break;
case Tags.ORDER:
relationP.setInterpretation(INTERPRETATION_ORDER_REF);
break;
case Tags.ACTIVITY:
relationP.setInterpretation(INTERPRETATION_ACTIVITY_REF);
break;
}
relationP.setUom(element.getType());
setInterpretationAndUom(element, relationP);
relationsBag().addParameter(relationP);
}
relationP.setValue(element.getId());
}
@Override
public void addRelation(StrolchRootElement element) {
addRelation(buildParamId(element.getType()), element);
}
@Override
public void addRelation(String param, StrolchRootElement element) {
StringListParameter relationsP = relationsBag().getParameter(param);
if (relationsP == null) {
String name = buildParamName(param);
relationsP = new StringListParameter(param, name, Collections.emptyList());
setInterpretationAndUom(element, relationsP);
relationsBag().addParameter(relationsP);
}
relationsP.addValueIfNotContains(element.getId());
}
private void setInterpretationAndUom(StrolchRootElement element, Parameter<?> relationP) {
switch (element.getObjectType()) {
case Tags.RESOURCE:
relationP.setInterpretation(INTERPRETATION_RESOURCE_REF);
break;
case Tags.ORDER:
relationP.setInterpretation(INTERPRETATION_ORDER_REF);
break;
case Tags.ACTIVITY:
relationP.setInterpretation(INTERPRETATION_ACTIVITY_REF);
break;
}
relationP.setUom(element.getType());
}
@Override
public void setRelations(String param, List<? extends StrolchRootElement> elements) {

View File

@ -25,6 +25,8 @@ import com.google.gson.JsonObject;
import li.strolch.model.activity.Activity;
import li.strolch.model.json.StrolchRootElementToJsonVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.visitor.SetParameterValueVisitor;
import li.strolch.model.visitor.StrolchElementVisitor;
import li.strolch.model.visitor.StrolchRootElementVisitor;
@ -82,7 +84,16 @@ public interface StrolchRootElement extends StrolchElement, PolicyContainer, Par
void setVersion(Version version) throws IllegalArgumentException;
/**
* Set a relation to the given element by using the given param. Should the parameter not exist, then it will be
* Set a relation to the given element by using the type of the given element as the parameter ID, but lower-casing
* the first letter. Should the {@link StringParameter} not exist, then it will be created
*
* @param element
* the element for which to set the relation to
*/
void setRelation(StrolchRootElement element);
/**
* Set a relation to the given element by using the given param. Should the {@link StringParameter} not exist, then it will be
* created
*
* @param param
@ -103,6 +114,26 @@ public interface StrolchRootElement extends StrolchElement, PolicyContainer, Par
*/
void setRelations(String param, List<? extends StrolchRootElement> elements);
/**
* Set a relation to the given element by using the type of the given element as the parameter ID, but lower-casing
* the first letter. Should the {@link StringListParameter} not exist, then it will be created
*
* @param element
* the element for which to set the relation to
*/
void addRelation(StrolchRootElement element);
/**
* Adds a relation to the given element by using the given parameter ID. Should the {@link StringListParameter} not exist,
* then it will be created
*
* @param param
* the parameter ID on which to set the relations
* @param element
* the element for which to set the relation to
*/
void addRelation(String param, StrolchRootElement element);
/**
* Return a clone of this {@link StrolchElement}
*