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

RMPathQuery matchSpecialisedNodes #448

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions aom/src/main/java/com/nedap/archie/aom/Archetype.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,9 @@
import javax.annotation.Nullable;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.Stack;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -391,4 +379,4 @@ public RmOverlay getRmOverlay() {
public void setRmOverlay(RmOverlay rmOverlay) {
this.rmOverlay = rmOverlay;
}
}
}
6 changes: 3 additions & 3 deletions aom/src/main/java/com/nedap/archie/query/AOMPathQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private AOMPathQuery(List<PathSegment> pathSegments, boolean findThroughCComplex
}

public <T extends ArchetypeModelObject> T find(CComplexObject root) {
List<T> list = findList(root);
List<T> list = findList(root);
if(list.isEmpty()) {
return null;
} else if (list.size() == 1) {
Expand Down Expand Up @@ -227,7 +227,7 @@ public List<PathSegment> getPathSegments() {
* Useful mainly when flattening, probably does not have many other uses
*/
public CComplexObjectProxy findAnyInternalReference(CComplexObject root) {
return (CComplexObjectProxy) findMatchingPredicate(root, (o) -> o instanceof CComplexObjectProxy);
return (CComplexObjectProxy) findMatchingPredicate(root, (o) -> o instanceof CComplexObjectProxy);
}

/**
Expand Down Expand Up @@ -305,4 +305,4 @@ public PartialMatch findPartial(CComplexObject root) {


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public Object itemAtPath(String s) {
return new RMPathQuery(s).find(ArchieRMInfoLookup.getInstance(), this);
}

public Object itemAtPathMatchSpecialisedNodes(String s) {
return new RMPathQuery(s, true).find(ArchieRMInfoLookup.getInstance(), this);
}

public List<Object> itemsAtPath(String s) {
List<RMObjectWithPath> objects = new RMPathQuery(s).findList(ArchieRMInfoLookup.getInstance(), this);
List<Object> result = new ArrayList<>();
Expand All @@ -59,6 +63,15 @@ public List<Object> itemsAtPath(String s) {
return result;
}

public List<Object> itemsAtPathMatchSpecialisedNodes(String s) {
List<RMObjectWithPath> objects = new RMPathQuery(s, true).findList(ArchieRMInfoLookup.getInstance(), this);
List<Object> result = new ArrayList<>();
for (RMObjectWithPath object : objects) {
result.add(object.getObject());
}
return result;
}

@JsonIgnore
public Pathable getParent() {
return parent;
Expand Down
30 changes: 24 additions & 6 deletions path-queries/src/main/java/com/nedap/archie/query/RMPathQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.google.common.collect.Lists;
import com.nedap.archie.aom.utils.AOMUtils;
import com.nedap.archie.definitions.AdlCodeDefinitions;
import com.nedap.archie.paths.PathSegment;
import com.nedap.archie.rminfo.ModelInfoLookup;
Expand All @@ -27,10 +28,15 @@
public class RMPathQuery {

private List<PathSegment> pathSegments = new ArrayList<>();
private final boolean matchSpecialisedNodes;

public RMPathQuery(String query) {
pathSegments = new APathQuery(query).getPathSegments();
this(query, false);
}

public RMPathQuery(String query, boolean matchSpecialisedNodes) {
pathSegments = new APathQuery(query).getPathSegments();
this.matchSpecialisedNodes = matchSpecialisedNodes;
}


Expand Down Expand Up @@ -256,9 +262,16 @@ private Collection<RMObjectWithPath> findRMObjectsWithPathCollection(ModelInfoLo
String archetypeNodeId = lookup.getArchetypeNodeIdFromRMObject(object);

if (segment.hasIdCode()) {
if (segment.getNodeId().equals(archetypeNodeId)) {
result.add(new RMObjectWithPath(object, path + buildPathConstraint(i, archetypeNodeId)));
if (matchSpecialisedNodes) {
if (AOMUtils.codesConformant(archetypeNodeId, segment.getNodeId())) {
result.add(new RMObjectWithPath(object, path + buildPathConstraint(i, archetypeNodeId)));
}
} else {
if (segment.getNodeId().equals(archetypeNodeId)) {
result.add(new RMObjectWithPath(object, path + buildPathConstraint(i, archetypeNodeId)));
}
}

} else if (segment.hasArchetypeRef()) {
//operational templates in RM Objects have their archetype node ID set to an archetype ref. That
//we support. Other things not so much
Expand Down Expand Up @@ -289,10 +302,15 @@ private Object findRMObject(ModelInfoLookup lookup, PathSegment segment, Collect
}
for(Object o:collection) {
String archetypeNodeId = lookup.getArchetypeNodeIdFromRMObject(o);

if (segment.hasIdCode()) {
if (segment.getNodeId().equals(archetypeNodeId)) {
return o;
if (matchSpecialisedNodes) {
if (AOMUtils.codesConformant(archetypeNodeId, segment.getNodeId())) {
return o;
}
} else {
if (segment.getNodeId().equals(archetypeNodeId)) {
return o;
}
}
} else if (segment.hasArchetypeRef()) {
//operational templates in RM Objects have their archetype node ID set to an archetype ref. That
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class RMPathQueryTest {

private Cluster cluster;
private Element elementId2;
private Element elementId3_1;
private Element elementId3_2;
private Element elementId3_3;
private Element elementId4Specialised;
private Element elementNoId_1;
private Element elementNoId_2;
private Element elementArchetypeId;
Expand All @@ -31,6 +32,10 @@ public void setup() {
elementId3_1.setArchetypeNodeId("id3.1");
elementId3_2 = new Element();
elementId3_2.setArchetypeNodeId("id3.1");
elementId3_3 = new Element();
elementId3_3.setArchetypeNodeId("id3.1.1");
elementId4Specialised = new Element();
elementId4Specialised.setArchetypeNodeId("id4.1");
elementNoId_1 = new Element();
elementNoId_2 = new Element();
elementArchetypeId = new Element();
Expand All @@ -40,6 +45,8 @@ public void setup() {
cluster.addItem(elementId2);
cluster.addItem(elementId3_1);
cluster.addItem(elementId3_2);
cluster.addItem(elementId3_3);
cluster.addItem(elementId4Specialised);
cluster.addItem(elementNoId_2);
cluster.addItem(elementArchetypeId);
}
Expand All @@ -48,18 +55,29 @@ public void setup() {
public void adl2NodeIdMatch() {
assertEquals(elementId2, cluster.itemAtPath("/items[id2]"));
assertEquals(elementId3_1, cluster.itemAtPath("/items[id3.1]"));
assertEquals(elementId4Specialised, cluster.itemAtPathMatchSpecialisedNodes("/items[id4]"));
assertEquals(elementNoId_1, cluster.itemAtPath("/items[1]"));
assertEquals(elementArchetypeId, cluster.itemAtPath("/items[openEHR-EHR-ELEMENT.element.v1.0.0]"));
}

@Test
public void adl2NodeIdMatchSpecialisedNodes() {
assertEquals(elementId4Specialised, cluster.itemAtPathMatchSpecialisedNodes("/items[id4]"));
}

@Test
public void adl2NodeIdMatchList() {
assertEquals(Collections.singletonList(elementId2), cluster.itemsAtPath("/items[id2]"));
assertEquals(Arrays.asList(elementId3_1, elementId3_2), cluster.itemsAtPath("/items[id3.1]"));
assertEquals(Arrays.asList(elementNoId_1, elementId2, elementId3_1, elementId3_2, elementNoId_2, elementArchetypeId), cluster.itemsAtPath("/items"));
assertEquals(Arrays.asList(elementNoId_1, elementId2, elementId3_1, elementId3_2, elementId3_3, elementId4Specialised, elementNoId_2, elementArchetypeId), cluster.itemsAtPath("/items"));
assertEquals(Collections.singletonList(elementArchetypeId), cluster.itemsAtPath("/items[openEHR-EHR-ELEMENT.element.v1.0.0]"));
}

@Test
public void adl2NodeIdMatchSpecialisedNodesList() {
assertEquals(Arrays.asList(elementId3_1, elementId3_2, elementId3_3), cluster.itemsAtPathMatchSpecialisedNodes("/items[id3]"));
}

@Test
public void adl14NodeIdMatch() {
Cluster cluster = new Cluster();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
*/
public class APathQueryCache {

private HashMap<String, RMPathQuery> queryCache = new HashMap<>();
private final boolean matchSpecialisedNodes;
private final HashMap<String, RMPathQuery> queryCache = new HashMap<>();

public APathQueryCache() {
this(false);
}

public APathQueryCache(boolean matchSpecialisedNodes) {
this.matchSpecialisedNodes = matchSpecialisedNodes;
}

public RMPathQuery getApathQuery(String query) {
RMPathQuery result = queryCache.get(query);
if (result == null) {
result = new RMPathQuery(query);
result = new RMPathQuery(query, matchSpecialisedNodes);
queryCache.put(query, result);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package com.nedap.archie.rules.evaluation;

import com.google.common.collect.Lists;
import com.nedap.archie.aom.Archetype;
import com.nedap.archie.aom.ArchetypeModelObject;
import com.nedap.archie.aom.CAttribute;
import com.nedap.archie.aom.CComplexObject;
import com.nedap.archie.aom.CObject;
import com.nedap.archie.aom.*;
import com.nedap.archie.creation.RMObjectCreator;
import com.nedap.archie.rminfo.ModelInfoLookup;
import com.nedap.archie.rminfo.RMAttributeInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.xpath.XPathExpressionException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.nedap.archie.aom.Archetype;
import com.nedap.archie.creation.RMObjectCreator;
import com.nedap.archie.query.RMObjectWithPath;
import com.nedap.archie.query.RMPathQuery;
import com.nedap.archie.query.RMQueryContext;
import com.nedap.archie.rminfo.ModelInfoLookup;
import com.nedap.archie.rmobjectvalidator.APathQueryCache;
Expand All @@ -21,7 +20,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.stream.Collectors;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import com.nedap.archie.adlparser.modelconstraints.RMConstraintImposer;
import com.nedap.archie.aom.Archetype;
import com.nedap.archie.query.RMPathQuery;
import com.nedap.archie.aom.OperationalTemplate;
import com.nedap.archie.flattener.Flattener;
import com.nedap.archie.flattener.FlattenerConfiguration;
import com.nedap.archie.flattener.InMemoryFullArchetypeRepository;
import com.nedap.archie.query.RMObjectWithPath;
import com.nedap.archie.query.RMPathQuery;
import com.nedap.archie.rm.archetyped.Pathable;
import com.nedap.archie.rm.composition.Composition;
import com.nedap.archie.rm.datastructures.Element;
import com.nedap.archie.rm.datastructures.ItemTree;
import com.nedap.archie.rm.datavalues.DvText;
import com.nedap.archie.rminfo.ArchieRMInfoLookup;
import com.nedap.archie.rminfo.ModelInfoLookup;
import com.nedap.archie.testutil.TestUtil;
import org.junit.Before;
import org.junit.Test;
import org.openehr.referencemodels.BuiltinReferenceModels;

import java.util.List;

Expand Down Expand Up @@ -109,5 +115,57 @@ public void multipleItems() {
}
}

@Test
public void findMatchSpecialisedNodes() throws Exception {
InMemoryFullArchetypeRepository inMemoryFullArchetypeRepository = new InMemoryFullArchetypeRepository();
inMemoryFullArchetypeRepository.addArchetype(archetype);
Archetype archetype_specialised = TestUtil.parseFailOnErrors("/basic_specialised.adls");
inMemoryFullArchetypeRepository.addArchetype(archetype_specialised);
Flattener flattener = new Flattener(inMemoryFullArchetypeRepository, BuiltinReferenceModels.getMetaModels(), FlattenerConfiguration.forOperationalTemplate());
OperationalTemplate opt = (OperationalTemplate) flattener.flatten(archetype_specialised);
root = (Pathable) testUtil.constructEmptyRMObject(opt.getDefinition());

Element element = new RMPathQuery("/context/other_context[id2]/items[id3]/items[id5]", true).find(ArchieRMInfoLookup.getInstance(), root);
assertNotNull(element);
assertEquals("/context/other_context[id2]/items[id3]/items[id5.1]", element.getPath());
}

@Test
public void findListMatchSpecialisedNodes() throws Exception {
InMemoryFullArchetypeRepository inMemoryFullArchetypeRepository = new InMemoryFullArchetypeRepository();
inMemoryFullArchetypeRepository.addArchetype(archetype);
Archetype archetype_specialised = TestUtil.parseFailOnErrors("/basic_specialised.adls");
inMemoryFullArchetypeRepository.addArchetype(archetype_specialised);
Flattener flattener = new Flattener(inMemoryFullArchetypeRepository, BuiltinReferenceModels.getMetaModels(), FlattenerConfiguration.forOperationalTemplate());
OperationalTemplate opt = (OperationalTemplate) flattener.flatten(archetype_specialised);
root = (Pathable) testUtil.constructEmptyRMObject(opt.getDefinition());

List<RMObjectWithPath> list = new RMPathQuery("/context/other_context[id2]/items[id3]/items[id5]", true).findList(ArchieRMInfoLookup.getInstance(), root);
assertEquals(1, list.size());
assertEquals("/context/other_context[id2]/items[id3]/items[id5.1]", ((Element) list.get(0).getObject()).getPath());
}

@Test
public void findListMatchTwiceSpecialisedNodes() throws Exception {
InMemoryFullArchetypeRepository inMemoryFullArchetypeRepository = new InMemoryFullArchetypeRepository();
inMemoryFullArchetypeRepository.addArchetype(archetype);
Archetype archetype_specialised = TestUtil.parseFailOnErrors("/basic_specialised.adls");
Archetype archetype_specialised_twice = TestUtil.parseFailOnErrors("/basic_specialised2.adls");
inMemoryFullArchetypeRepository.addArchetype(archetype_specialised);
inMemoryFullArchetypeRepository.addArchetype(archetype_specialised_twice);
Flattener flattener = new Flattener(inMemoryFullArchetypeRepository, BuiltinReferenceModels.getMetaModels(), FlattenerConfiguration.forOperationalTemplate());
OperationalTemplate opt = (OperationalTemplate) flattener.flatten(archetype_specialised_twice);
root = (Pathable) testUtil.constructEmptyRMObject(opt.getDefinition());

List<RMObjectWithPath> listId5 = new RMPathQuery("/context/other_context[id2]/items[id3]/items[id5]", true).findList(ArchieRMInfoLookup.getInstance(), root);
assertEquals(2, listId5.size());
assertEquals("/context/other_context[id2]/items[id3]/items[id5.1]", ((Element) listId5.get(0).getObject()).getPath());
assertEquals("/context/other_context[id2]/items[id3]/items[id5.1.1]", ((Element) listId5.get(1).getObject()).getPath());

List<RMObjectWithPath> listId6 = new RMPathQuery("/context/other_context[id2]/items[id3]/items[id6]", true).findList(ArchieRMInfoLookup.getInstance(), root);
assertEquals(2, listId6.size());
assertEquals("/context/other_context[id2]/items[id3]/items[id6]", ((Element) listId6.get(0).getObject()).getPath());
assertEquals("/context/other_context[id2]/items[id3]/items[id6.0.1]", ((Element) listId6.get(1).getObject()).getPath());
}

}
Loading