use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.
the class BroadcastingArchetypeRepository method invalidateAndRecompileArchetypes.
public void invalidateAndRecompileArchetypes(Archetype newArchetype) {
// now invalidate all related archetypes
Set<String> archetypesToInvalidate = new HashSet<>();
archetypesToInvalidate.add(newArchetype.getArchetypeId().toString());
for (Archetype result : getAllArchetypes()) {
if (isDescendant(result, newArchetype.getArchetypeId().getFullId())) {
archetypesToInvalidate.add(result.getArchetypeId().toString());
}
// this assumes templates cannot be further specialized
if (result instanceof Template) {
Template template = (Template) result;
for (TemplateOverlay overlay : template.getTemplateOverlays()) {
// TODO: not only direct descendants, but also those far below should be checked with (a variant of?) isDescendant
if (archetypesToInvalidate.contains(overlay.getParentArchetypeId())) {
archetypesToInvalidate.add(result.getArchetypeId().getFullId());
}
}
}
}
for (String archetypeId : archetypesToInvalidate) {
removeValidationResult(archetypeId);
}
// TODO: for operational templates we need to scan way more, all archetype roots as well. future addition
if (compile) {
for (String archetypeId : archetypesToInvalidate) {
Archetype archetype = getArchetype(archetypeId);
if (archetype != null && getValidationResult(archetypeId) == null) {
validator.validate(archetype, this);
}
}
resolveDocumentLinks();
}
}
use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.
the class AQLStorage method extractHoverInfo.
private void extractHoverInfo(AQLDocument document) {
HoverInfo hoverInfo = new HoverInfo("aql");
MetaModels metaModels = BuiltinReferenceModels.getMetaModels();
for (ArchetypePathReference reference : document.getArchetypePathReferences()) {
if (reference.getArchetypeId() == null) {
continue;
}
try {
// TODO: get operational template here. I think that should be cached?
ValidationResult validationResult = archetypeRepository.getValidationResult(reference.getArchetypeId());
if (validationResult != null) {
Archetype flattened = validationResult.getFlattened();
metaModels.selectModel(flattened);
if (flattened != null) {
PartialAOMPathQuery aomPathQuery = new PartialAOMPathQuery(reference.getPath());
PartialAOMPathQuery.PartialMatch partial = aomPathQuery.findLSPPartial(flattened.getDefinition());
if (partial.getMatches().size() > 0) {
ArchetypeModelObject archetypeModelObject = partial.getMatches().get(0);
String content = null;
String description = null;
String typeName = "";
if (archetypeModelObject instanceof CAttribute) {
CAttribute attribute = (CAttribute) archetypeModelObject;
content = findNearestText((CAttribute) archetypeModelObject);
description = findNearestDescription((CAttribute) archetypeModelObject);
CObject parent = attribute.getParent();
if (partial.getRemainingQuery().isEmpty()) {
// TODO: proper path lookup here
BmmClass classDefinition = metaModels.getSelectedBmmModel().getClassDefinition(BmmDefinitions.typeNameToClassKey(parent.getRmTypeName()));
if (classDefinition != null) {
BmmProperty bmmProperty = classDefinition.getFlatProperties().get(attribute.getRmAttributeName());
if (bmmProperty != null) {
bmmProperty.getType().toDisplayString();
}
}
}
} else if (archetypeModelObject instanceof CObject) {
content = findNearestText((CObject) archetypeModelObject);
description = findNearestDescription((CObject) archetypeModelObject);
if (partial.getRemainingQuery().isEmpty()) {
// TODO: proper path lookup here.
typeName = ((CObject) archetypeModelObject).getRmTypeName();
}
}
String text = content + partial.getRemainingQuery().stream().map(PathSegment::toString).collect(Collectors.joining("/"));
text += "\n\n" + typeName;
text += "\n\n" + description;
text += "\n\nIn Archetype " + flattened.getDefinition().getTerm().getText() + " (" + reference.getArchetypeId() + ")";
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, text)));
} else {
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "path " + reference.getPath() + " not found")));
}
} else {
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "flattened archetype not found")));
}
} else {
hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "archetype not found")));
}
} catch (Exception e) {
// ok... report as diagnostics or log
e.printStackTrace();
}
}
document.setHoverInfo(hoverInfo);
}
use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.
the class AQLStorage method getCodeLens.
public List<? extends CodeLens> getCodeLens(CodeLensParams params) {
AQLDocument document = this.aqlDocumentsByUri.get(params.getTextDocument().getUri());
if (document == null) {
return new ArrayList<>();
}
List<CodeLens> result = new ArrayList<>();
for (ArchetypePathReference reference : document.getArchetypePathReferences()) {
if (reference.getArchetypeId() == null) {
continue;
}
try {
// TODO: get operational template here. I think that should be cached?
ValidationResult validationResult = archetypeRepository.getValidationResult(reference.getArchetypeId());
if (validationResult != null) {
Archetype flattened = validationResult.getFlattened();
if (flattened != null) {
PartialAOMPathQuery aomPathQuery = new PartialAOMPathQuery(reference.getPath());
PartialAOMPathQuery.PartialMatch partial = aomPathQuery.findLSPPartial(flattened.getDefinition());
if (partial.getMatches().size() > 0) {
ArchetypeModelObject archetypeModelObject = partial.getMatches().get(0);
String content = null;
String description = null;
if (archetypeModelObject instanceof CAttribute) {
content = findNearestText((CAttribute) archetypeModelObject);
description = findNearestDescription((CAttribute) archetypeModelObject);
} else if (archetypeModelObject instanceof CObject) {
content = findNearestText((CObject) archetypeModelObject);
description = findNearestDescription((CObject) archetypeModelObject);
}
String text = content + partial.getRemainingQuery().stream().map(PathSegment::toString).collect(Collectors.joining("/"));
String extraText = description;
extraText += "\n\nIn Archetype " + flattened.getDefinition().getTerm().getText() + " (" + reference.getArchetypeId() + ")";
// result.add(new CodeLens(reference.getRange(), new Command(text, ADL2TextDocumentService.SHOW_INFO_COMMAND, Lists.newArrayList(extraText)), null));
result.add(new CodeLens(reference.getRange(), new Command(text, ADL2TextDocumentService.SHOW_INFO_COMMAND, Lists.newArrayList(extraText)), null));
} else {
// hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "path " + reference.getPath() + " not found")));
}
} else {
// hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "flattened archetype not found")));
}
} else {
// hoverInfo.getHoverRanges().addRange(reference.getRange(), new Hover(new MarkupContent(HoverInfo.MARKDOWN, "archetype not found")));
}
} catch (Exception e) {
// ok... report as diagnostics or log
e.printStackTrace();
}
}
return result;
}
use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.
the class GenerateExampleCommand method apply.
public void apply() {
String documentUri = ((JsonPrimitive) params.getArguments().get(0)).getAsString();
String format = ((JsonPrimitive) params.getArguments().get(1)).getAsString();
String serializedExample = null;
Flattener flattener = new Flattener(storage, BuiltinReferenceModels.getMetaModels(), FlattenerConfiguration.forOperationalTemplate());
DocumentInformation documentInformation = storage.getDocumentInformation(documentUri);
Archetype archetype = storage.getArchetype(documentInformation.getArchetypeId());
OperationalTemplate opt = (OperationalTemplate) flattener.flatten(archetype);
ExampleJsonInstanceGenerator exampleJsonInstanceGenerator = new ExampleJsonInstanceGenerator(BuiltinReferenceModels.getMetaModels(), archetype.getOriginalLanguage().getCodeString());
Map<String, Object> exampleMap = exampleJsonInstanceGenerator.generate(opt);
String extension;
ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
String jsonRmObject = null;
OpenEHRBase example;
try {
jsonRmObject = objectMapper.writeValueAsString(exampleMap);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
switch(format) {
case "json":
extension = "_example.json";
serializedExample = jsonRmObject;
break;
case "flat_json":
extension = "_flat_example.json";
FlatJsonGenerator flatJsonGenerator = new FlatJsonGenerator(ArchieRMInfoLookup.getInstance(), FlatJsonFormatConfiguration.nedapInternalFormat());
try {
example = objectMapper.readValue(jsonRmObject, OpenEHRBase.class);
Map<String, Object> flatExample = flatJsonGenerator.buildPathsAndValues(example);
serializedExample = JacksonUtil.getObjectMapper().writeValueAsString(flatExample);
} catch (JsonProcessingException | DuplicateKeyException e) {
throw new RuntimeException(e);
}
break;
case "xml":
{
extension = "_example.xml";
try {
example = objectMapper.readValue(jsonRmObject, OpenEHRBase.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
StringWriter sw = new StringWriter();
try {
Marshaller marshaller = JAXBUtil.getArchieJAXBContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(example, sw);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
serializedExample = sw.toString();
break;
}
//
default:
throw new UnsupportedOperationException("unsupported format: " + format);
}
String uriToWrite = documentUri.substring(0, documentUri.lastIndexOf("/")) + "/example/" + opt.getArchetypeId() + extension;
textDocumentService.writeFile(uriToWrite, "opt in " + format, serializedExample);
}
use of com.nedap.archie.aom.Archetype in project archetype-languageserver by nedap.
the class ADL14ConvertingStorage method getAllToConvertIncludingParents.
private List<Archetype> getAllToConvertIncludingParents(Archetype archetype) {
List<Archetype> toConvert = new ArrayList<>();
toConvert.add(archetype);
Stack<Archetype> toAdd = new Stack();
toAdd.push(archetype);
while (!toAdd.isEmpty()) {
Archetype a = toAdd.pop();
// user can override using the convert all adl 1.4 archetypes
if (a.getParentArchetypeId() != null && repository.getArchetype(a.getParentArchetypeId()) == null) {
for (Archetype possibleParent : this.adl14Files.values()) {
// ADL 1.4, so simple string comparison is enough
if (a.getParentArchetypeId().equalsIgnoreCase(possibleParent.getArchetypeId().toString())) {
toConvert.add(possibleParent);
toAdd.push(possibleParent);
}
}
}
}
return toConvert;
}
Aggregations