use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class PropertyFunctionScriptPage method addActions.
@Override
protected void addActions(ToolBar toolbar, CompilingSourceViewer<GroovyAST> viewer) {
super.addActions(toolbar, viewer);
PageHelp.createToolItem(toolbar, this);
TypeStructureTray.createToolItem(toolbar, this, SchemaSpaceID.SOURCE, new TypeProvider() {
@Override
public Collection<? extends TypeDefinition> getTypes() {
// create a dummy type with the variables as children
DefaultTypeDefinition dummy = new DefaultTypeDefinition(TypeStructureTray.VARIABLES_TYPE_NAME);
DefaultCustomPropertyFunction cf = getWizard().getUnfinishedFunction();
int index = 0;
for (EntityDefinition variable : getVariables()) {
DefaultCustomPropertyFunctionEntity source = cf.getSources().get(index);
if (variable.getDefinition() instanceof PropertyDefinition) {
PropertyDefinition prop = (PropertyDefinition) variable.getDefinition();
TypeDefinition propertyType;
boolean useInstanceValue = CustomGroovyTransformation.useInstanceVariableForSource(source);
if (useInstanceValue) {
// use instance type
propertyType = prop.getPropertyType();
} else {
// use dummy type with only the
// binding/HasValueFlag copied
DefaultTypeDefinition crippledType = new DefaultTypeDefinition(prop.getPropertyType().getName());
crippledType.setConstraint(prop.getPropertyType().getConstraint(Binding.class));
crippledType.setConstraint(prop.getPropertyType().getConstraint(HasValueFlag.class));
propertyType = crippledType;
}
DefaultPropertyDefinition dummyProp = new DefaultPropertyDefinition(new QName(source.getName()), dummy, propertyType);
// number of times
if (source.isEager())
dummyProp.setConstraint(Cardinality.CC_ANY_NUMBER);
}
index++;
}
return Collections.singleton(dummy);
}
});
TypeStructureTray.createToolItem(toolbar, this, SchemaSpaceID.TARGET, new TypeProvider() {
@Override
public Collection<? extends TypeDefinition> getTypes() {
DefaultCustomPropertyFunctionEntity target = getWizard().getUnfinishedFunction().getTarget();
if (target != null) {
return Collections.singleton(createDummyType(target));
}
return Collections.emptyList();
}
});
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class GroovyTransformationPage method addActions.
@Override
protected void addActions(ToolBar toolbar, CompilingSourceViewer<GroovyAST> viewer) {
super.addActions(toolbar, viewer);
PageHelp.createToolItem(toolbar, this);
TypeStructureTray.createToolItem(toolbar, this, SchemaSpaceID.SOURCE, new TypeProvider() {
@Override
public Collection<? extends TypeDefinition> getTypes() {
// create a dummy type with the variables as children
DefaultTypeDefinition dummy = new DefaultTypeDefinition(TypeStructureTray.VARIABLES_TYPE_NAME);
Cell cell = getWizard().getUnfinishedCell();
boolean useInstanceValues = CellUtil.getOptionalParameter(cell, GroovyTransformation.PARAM_INSTANCE_VARIABLES, Value.of(false)).as(Boolean.class);
for (EntityDefinition variable : getVariables()) {
if (variable.getDefinition() instanceof PropertyDefinition) {
PropertyDefinition prop = (PropertyDefinition) variable.getDefinition();
TypeDefinition propertyType;
if (useInstanceValues) {
// use instance type
propertyType = prop.getPropertyType();
} else {
// use dummy type with only the
// binding/HasValueFlag copied
DefaultTypeDefinition crippledType = new DefaultTypeDefinition(prop.getPropertyType().getName());
crippledType.setConstraint(prop.getPropertyType().getConstraint(Binding.class));
crippledType.setConstraint(prop.getPropertyType().getConstraint(HasValueFlag.class));
propertyType = crippledType;
}
DefaultPropertyDefinition dummyProp = new DefaultPropertyDefinition(new QName(getVariableName(variable)), dummy, propertyType);
// number of times
if (cell.getTransformationIdentifier().equals(GroovyGreedyTransformation.ID))
dummyProp.setConstraint(Cardinality.CC_ANY_NUMBER);
}
}
return Collections.singleton(dummy);
}
});
TypeStructureTray.createToolItem(toolbar, this, SchemaSpaceID.TARGET, new TypeProvider() {
@Override
public Collection<? extends TypeDefinition> getTypes() {
Property targetProperty = (Property) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
if (targetProperty != null) {
return Collections.singleton(targetProperty.getDefinition().getDefinition().getPropertyType());
}
return Collections.emptyList();
}
});
PageFunctions.createToolItem(toolbar, this);
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class InstanceBuilderCode method appendBuildProperties.
/**
* Append example code to build the properties identified by the given path
* tree.
*
* @param example the example code to append to
* @param baseIndent the base indent to use
* @param tree the path tree representing a specific segment
* @param parent the parent of the segment
* @param useBrackets if brackets should be used in the generated code
* @param startWithIndent if at the beginning the indent should be added
* @param endWithNewline if at the end a new line break should be added
* @param useExampleValues if example values should be used
* @return the relative offset where editing should be continued
*/
public static int appendBuildProperties(StringBuilder example, String baseIndent, PathTree tree, DefinitionGroup parent, final boolean useBrackets, final boolean startWithIndent, final boolean endWithNewline, final boolean useExampleValues) {
Definition<?> def = (Definition<?>) tree.getSegment();
final String indent = baseIndent;
int cursor = 0;
boolean opened = false;
if (def instanceof PropertyDefinition) {
// property name
if (startWithIndent) {
example.append(indent);
}
// TODO test if property must be accessed explicitly through
// builder?
example.append(def.getName().getLocalPart());
// test if uniquely accessible from parent
boolean useNamespace = true;
if (parent instanceof Definition<?>) {
try {
new DefinitionAccessor((Definition<?>) parent).findChildren(def.getName().getLocalPart()).eval();
useNamespace = false;
} catch (IllegalStateException e) {
// ignore - namespace needed
}
}
boolean needComma = false;
// add namespace if necessary
if (useNamespace) {
if (useBrackets && !needComma) {
example.append('(');
}
example.append(" namespace: '");
example.append(def.getName().getNamespaceURI());
example.append('\'');
needComma = true;
}
TypeDefinition propertyType = ((PropertyDefinition) def).getPropertyType();
boolean hasValue = propertyType.getConstraint(HasValueFlag.class).isEnabled();
if (hasValue) {
// add an example value
if (useBrackets && !needComma) {
example.append('(');
}
if (needComma) {
example.append(',');
}
example.append(' ');
if (useExampleValues) {
switch(Classification.getClassification(def)) {
case NUMERIC_PROPERTY:
example.append("42");
break;
case STRING_PROPERTY:
example.append("'some value'");
break;
default:
example.append("some_value");
}
}
needComma = true;
}
if (DefinitionUtil.hasChildren(propertyType) && (!tree.getChildren().isEmpty() || !needComma || !hasValue)) {
if (needComma) {
if (useBrackets) {
example.append(" )");
} else {
example.append(',');
}
}
example.append(" {");
example.append('\n');
opened = true;
} else {
cursor = example.length();
if (useBrackets && needComma) {
example.append(" )");
}
if (endWithNewline) {
example.append('\n');
}
}
} else {
// groups are ignored
}
if (opened) {
// set the new parent
parent = DefinitionUtil.getDefinitionGroup(def);
// create child properties
String newIndent = indent + createIndent(1);
if (tree.getChildren().isEmpty()) {
example.append(newIndent);
cursor = example.length();
example.append('\n');
} else {
for (PathTree child : tree.getChildren()) {
cursor = appendBuildProperties(example, newIndent, child, parent, useBrackets, true, true, useExampleValues);
}
}
// close bracket
example.append(indent);
example.append('}');
if (endWithNewline) {
example.append('\n');
}
}
return cursor;
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class TypeStructureTray method createSourceSample.
/**
* Create sample code for a single tree path specifying a source property.
*
* @param path the tree path
* @param types the types serving as input
* @return the sample code or <code>null</code>
*/
private String createSourceSample(TreePath path, Collection<? extends TypeDefinition> types) {
DefinitionGroup parent;
int startIndex = 0;
boolean hasPropDef = false;
StringBuilder access = new StringBuilder();
// determine parent type
if (path.getFirstSegment() instanceof TypeDefinition) {
// types are the top level elements
parent = (DefinitionGroup) path.getFirstSegment();
startIndex = 1;
access.append(GroovyConstants.BINDING_SOURCE);
} else {
// types are not in the tree, single type must be root
TypeDefinition type = types.iterator().next();
parent = type;
if (VARIABLES_TYPE_NAME.equals(type.getName())) {
// Groovy property transformation
// first segment is variable name
Definition<?> def = (Definition<?>) path.getFirstSegment();
startIndex++;
access.append(def.getName().getLocalPart());
// XXX add namespace if necessary
// XXX currently groovy transformation does not support multiple
// variables with the same name
parent = DefinitionUtil.getDefinitionGroup(def);
} else {
// assuming Retype/Merge
access.append(GroovyConstants.BINDING_SOURCE);
}
}
// is a property or list of inputs referenced -> use accessor
boolean propertyOrList = path.getSegmentCount() > startIndex || (path.getLastSegment() instanceof ChildDefinition<?> && canOccureMultipleTimes((ChildDefinition<?>) path.getLastSegment()));
if (!propertyOrList) {
if (parent instanceof TypeDefinition && canHaveValue((TypeDefinition) parent)) {
if (!DefinitionUtil.hasChildren((Definition<?>) path.getLastSegment())) {
// variable w/o children referenced
return "// access variable\ndef value = " + access;
} else {
// variable w/ children referenced
return "// access instance variable value\ndef value = " + access + ".value";
}
} else {
// return null;
}
}
if (path.getFirstSegment() instanceof TypeDefinition) {
access.append(".links." + ((TypeDefinition) path.getFirstSegment()).getDisplayName());
} else {
access.append(".p");
// check for encountering property definition, so that there should
// not be 2 'p's appended.
hasPropDef = true;
}
for (int i = startIndex; i < path.getSegmentCount(); i++) {
Definition<?> def = (Definition<?>) path.getSegment(i);
if (def instanceof PropertyDefinition) {
for (int j = i - 1; j >= 0; j--) {
Definition<?> def1 = (Definition<?>) path.getSegment(j);
if (def1 instanceof TypeDefinition) {
// do nothing
} else {
hasPropDef = true;
}
}
if (!hasPropDef) {
access.append(".p");
}
// property name
access.append('.');
access.append(def.getName().getLocalPart());
// test if uniquely accessible from parent
boolean useNamespace = true;
if (parent instanceof Definition<?>) {
useNamespace = namespaceNeeded((Definition<?>) parent, def);
}
// add namespace if necessary
if (useNamespace) {
access.append("('");
access.append(def.getName().getNamespaceURI());
access.append("')");
}
} else if (def instanceof TypeDefinition) {
access.append("." + ((TypeDefinition) def).getDisplayName());
}
// set the new parent
parent = DefinitionUtil.getDefinitionGroup(def);
}
if (parent instanceof TypeDefinition) {
// only properties at the end of the path are supported
TypeDefinition propertyType = (TypeDefinition) parent;
StringBuilder example = new StringBuilder();
boolean canOccurMultipleTimes = false;
if (path.getFirstSegment() instanceof TypeDefinition) {
canOccurMultipleTimes = true;
}
/*
* Instances/values may occur multiple times if any element in the
* path may occur multiple times.
*/
for (int i = path.getSegmentCount() - 1; i >= 0 && !canOccurMultipleTimes; i--) {
if (path.getSegment(i) instanceof ChildDefinition<?>) {
canOccurMultipleTimes = canOccureMultipleTimes((ChildDefinition<?>) path.getSegment(i));
}
}
if (canHaveValue(propertyType)) {
// single value
if (canOccurMultipleTimes) {
example.append("// access first value\n");
} else {
example.append("// access value\n");
}
example.append("def value = ");
example.append(access);
example.append(".value()\n\n");
// multiple values
if (canOccurMultipleTimes) {
example.append("// access all values as list\n");
example.append("def valueList = ");
example.append(access);
example.append(".values()\n\n");
}
}
if (DefinitionUtil.hasChildren(propertyType)) {
// single instance
if (canOccurMultipleTimes) {
example.append("// access first instance\n");
} else {
example.append("// access instance\n");
}
example.append("def instance = ");
example.append(access);
example.append(".first()\n\n");
if (canOccurMultipleTimes) {
// multiple values
example.append("// access all instances as list\n");
example.append("def instanceList = ");
example.append(access);
example.append(".list()\n\n");
// iterate over instances
example.append("// iterate over instances\n");
example.append(access);
example.append(".each {\n");
example.append("\tinstance ->\n");
example.append("}\n\n");
}
} else if (canOccurMultipleTimes && propertyType.getConstraint(HasValueFlag.class).isEnabled()) {
// iterate over values
example.append("// iterate over values\n");
example.append(access);
example.append(".each {\n");
example.append("\tvalue ->\n");
example.append("}\n\n");
}
return example.toString();
}
return null;
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class GeographicalNamePage method createSpellingGroup.
private void createSpellingGroup(Composite parent, PropertyFunctionDefinition function) {
// define Spelling Group composite
Group configurationGroup = new Group(parent, SWT.NONE);
configurationGroup.setText(SPELLING_GROUP_TEXT);
configurationGroup.setLayout(GridLayoutFactory.fillDefaults().create());
GridData configurationAreaGD = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
configurationAreaGD.grabExcessHorizontalSpace = true;
configurationAreaGD.grabExcessVerticalSpace = true;
configurationGroup.setLayoutData(configurationAreaGD);
configurationGroup.setSize(configurationGroup.computeSize(SWT.DEFAULT, SWT.DEFAULT));
configurationGroup.setFont(parent.getFont());
final Composite configurationComposite = new Composite(configurationGroup, SWT.NONE);
GridData configurationLayoutData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
configurationLayoutData.grabExcessHorizontalSpace = true;
configurationComposite.setLayoutData(configurationLayoutData);
GridLayout spellingLayout = new GridLayout();
spellingLayout.numColumns = 2;
spellingLayout.makeColumnsEqualWidth = false;
spellingLayout.marginWidth = 0;
spellingLayout.marginHeight = 0;
spellingLayout.horizontalSpacing = 8;
configurationComposite.setLayout(spellingLayout);
// or get the known information about the cell to be edited
if (getSpellings() == null || getSpellings().size() == 0) {
spellings = new ArrayList<SpellingType>();
ListMultimap<String, ? extends Entity> source = getWizard().getUnfinishedCell().getSource();
if (source != null) {
for (Entity item : source.values()) {
int i = 0;
Definition<?> entity = item.getDefinition().getDefinition();
if (entity instanceof PropertyDefinition) {
SpellingType sp = new SpellingType((PropertyDefinition) entity);
// set the same script value if you had a value before
if (scripts != null && i < scripts.size()) {
sp.setScript(scripts.get(i));
} else {
// else set the default value
sp.setScript(ISO_CODE_ENG);
}
// before
if (trans != null && i < trans.size()) {
sp.setTransliteration(trans.get(i));
} else {
// else set the default value
sp.setTransliteration("");
}
spellings.add(sp);
}
i++;
}
}
} else {
// after initialization of the spellings
ArrayList<PropertyDefinition> temp = new ArrayList<PropertyDefinition>();
ArrayList<SpellingType> templist = getSpellings();
// we have to create a new spellings list because a live
// modification of the combo box input would occur an error
spellings = new ArrayList<SpellingType>();
for (int i = 0; i < templist.size(); i++) {
temp.add(templist.get(i).getProperty());
if (scripts != null && trans != null && i < scripts.size() && scripts.get(i) != null && i < trans.size() && trans.get(i) != null) {
templist.get(i).setScript(scripts.get(i));
templist.get(i).setTransliteration(trans.get(i));
}
}
for (Entity item : getWizard().getUnfinishedCell().getSource().values()) {
Definition<?> entity = item.getDefinition().getDefinition();
if (entity instanceof PropertyDefinition) {
PropertyDefinition propDef = (PropertyDefinition) entity;
for (SpellingType st : templist) {
// transliteration) to the new spellings list
if (propDef.equals(st.getProperty())) {
spellings.add(st);
}
}
// with default values
if (!temp.contains(propDef)) {
SpellingType sp = new SpellingType(propDef);
sp.setScript(ISO_CODE_ENG);
sp.setTransliteration("");
spellings.add(sp);
}
}
}
}
// Text
final Label nameSpellingTextLabel = new Label(configurationComposite, SWT.NONE);
nameSpellingTextLabel.setText(SPELLING_TEXT_LABEL_TEXT);
this.nameSpellingText = new ComboViewer(configurationComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
this.nameSpellingText.getControl().setLayoutData(configurationLayoutData);
this.nameSpellingText.setContentProvider(ArrayContentProvider.getInstance());
this.nameSpellingText.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof SpellingType) {
return ((SpellingType) element).getProperty().getName().getLocalPart();
}
return super.getText(element);
}
});
this.nameSpellingText.setInput(spellings);
// default set selection to the first element on the list
if (!spellings.isEmpty()) {
this.activeSpelling = spellings.iterator().next();
this.nameSpellingText.setSelection(new StructuredSelection(activeSpelling));
}
// set active spelling
nameSpellingText.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
if (!event.getSelection().isEmpty() && event.getSelection() instanceof IStructuredSelection) {
SpellingType selected = (SpellingType) ((IStructuredSelection) event.getSelection()).getFirstElement();
String script = ISO_CODE_ENG;
// $NON-NLS-1$
String transliteration = "";
activeSpelling = selected;
if (activeSpelling.getScript() != null && // $NON-NLS-1$
!activeSpelling.getScript().equals(""))
script = activeSpelling.getScript();
if (activeSpelling.getTransliteration() != null && // $NON-NLS-1$
!activeSpelling.getTransliteration().equals(""))
transliteration = activeSpelling.getTransliteration();
nameSpellingScript.setText(script);
nameSpellingTransliteration.setText(transliteration);
}
}
});
// Script
final Label nameSpellingScriptLabel = new Label(configurationComposite, SWT.NONE);
nameSpellingScriptLabel.setText(SCRIPT_LABEL_TEXT);
configureParameterLabel(nameSpellingScriptLabel, PROPERTY_SCRIPT, function);
this.nameSpellingScript = new Text(configurationComposite, SWT.BORDER | SWT.SINGLE);
this.nameSpellingScript.setLayoutData(configurationLayoutData);
this.nameSpellingScript.setEnabled(true);
this.nameSpellingScript.setTabs(0);
// $NON-NLS-1$
String script = "eng";
// read script from the active spelling
if (activeSpelling != null && activeSpelling.getScript() != null)
script = activeSpelling.getScript();
// set default value for script
this.nameSpellingScript.setText(script);
this.nameSpellingScript.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
activeSpelling.setScript(nameSpellingScript.getText());
}
});
// Transliteration
final Label nameSpellingTransliterationLabel = new Label(configurationComposite, SWT.NONE);
nameSpellingTransliterationLabel.setText(TRANSLITERATION_LABEL_TEXT);
configureParameterLabel(nameSpellingTransliterationLabel, PROPERTY_TRANSLITERATION, function);
this.nameSpellingTransliteration = new Text(configurationComposite, SWT.BORDER | SWT.SINGLE);
this.nameSpellingTransliteration.setLayoutData(configurationLayoutData);
this.nameSpellingTransliteration.setEnabled(true);
this.nameSpellingTransliteration.setTabs(0);
// read script from the active spelling
// $NON-NLS-1$
String transliteration = "";
if (activeSpelling != null && activeSpelling.getTransliteration() != null)
transliteration = activeSpelling.getTransliteration();
// set default value for transliteration
this.nameSpellingTransliteration.setText(transliteration);
this.nameSpellingTransliteration.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
activeSpelling.setTransliteration(nameSpellingTransliteration.getText());
}
});
}
Aggregations