Skip to content

Commit

Permalink
Reworked how Metapath expressions are compiled to ensure the static c…
Browse files Browse the repository at this point in the history
…ontext from their source is used in compilation.

Also improved node item, definition, and instance creation for easier use in unit tests.
  • Loading branch information
david-waltermire committed Dec 19, 2024
1 parent 6a9f9d1 commit 28e6ebd
Show file tree
Hide file tree
Showing 81 changed files with 993 additions and 580 deletions.
2 changes: 1 addition & 1 deletion core/src/main/antlr4/Metapath10.g4
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ parenthesizeditemtype : OP itemtype CP ;


// Error in the spec. EQName also includes acceptable keywords.
eqname : NCName | QName | URIQualifiedName
eqname : URIQualifiedName | NCName | QName
| KW_ANCESTOR
| KW_ANCESTOR_OR_SELF
| KW_AND
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/antlr4/Metapath10Lexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ DecimalLiteral : '.' FragDigits | FragDigits '.' [0-9]*;
DoubleLiteral : ('.' FragDigits | FragDigits ('.' [0-9]*)?) [eE] [+-]? FragDigits;
StringLiteral : '"' (~["] | FragEscapeQuot)* '"' | '\'' (~['] | FragEscapeApos)* '\'';
URIQualifiedName : BracedURILiteral NCName;
BracedURILiteral : 'Q' '{' [^{}]* '}';
BracedURILiteral : 'Q' '{' ~[{}]* '}';
// Error in spec: EscapeQuot and EscapeApos are not terminals!
fragment FragEscapeQuot : '""';
fragment FragEscapeApos : '\'\'';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

package gov.nist.secauto.metaschema.core.mdm;

import gov.nist.secauto.metaschema.core.mdm.impl.DefinitionAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.mdm.impl.IDMModelNodeItem;
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
import gov.nist.secauto.metaschema.core.model.IAssemblyInstance;
import gov.nist.secauto.metaschema.core.model.IFieldInstance;
import gov.nist.secauto.metaschema.core.model.IResourceLocation;

import edu.umd.cs.findbugs.annotations.NonNull;

Expand All @@ -21,36 +22,44 @@
*/
public interface IDMAssemblyNodeItem
extends IAssemblyNodeItem, IDMModelNodeItem<IAssemblyDefinition, IAssemblyInstance> {
/**
* Create new assembly node item that is detached from a parent node item.
*
* @param definition
* the Metaschema field definition describing the assembly
* @param staticContext
* the atomic field value
* @return the new field node item
*/
@NonNull
static IDMAssemblyNodeItem newInstance(
@NonNull IAssemblyDefinition definition,
@NonNull StaticContext staticContext) {
return new DefinitionAssemblyNodeItem(definition, staticContext);
}

/**
* Create and add a new field to the underlying data model.
*
* @param instance
* the Metaschema field instance describing the field
* @param resourceLocation
* information about the location of the field within the containing
* resource
* @param value
* the atomic field value
* @return the new field node item
*/
@NonNull
IDMFieldNodeItem newField(
@NonNull IFieldInstance instance,
@NonNull IResourceLocation resourceLocation,
@NonNull IAnyAtomicItem value);

/**
* Create and add a new assembly to the underlying data model.
*
* @param instance
* the Metaschema assembly instance describing the assembly
* @param resourceLocation
* information about the location of the assembly within the containing
* resource
* @return the new assembly node item
*/
@NonNull
IDMAssemblyNodeItem newAssembly(
@NonNull IAssemblyInstance instance,
@NonNull IResourceLocation resourceLocation);
@NonNull IAssemblyInstance instance);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.mdm;

import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem;
import gov.nist.secauto.metaschema.core.model.IDefinition;
import gov.nist.secauto.metaschema.core.model.INamedInstance;

public interface IDMDefinitionNodeItem<D extends IDefinition, I extends INamedInstance>
extends IDMNodeItem, IDefinitionNodeItem<D, I> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import gov.nist.secauto.metaschema.core.mdm.impl.DocumentImpl;
import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
import gov.nist.secauto.metaschema.core.model.IResourceLocation;

import java.net.URI;

Expand All @@ -28,21 +27,14 @@ public interface IDMDocumentNodeItem
*
* @param resource
* the base URI of the document resource
* @param resourceLocation
* information about the (intended) location of the document resource
* @param rootAssembly
* the assembly that is at the root of the node tree for this document
* @param rootAssemblyLocation
* information about the (intended) location of the root assembly
* resource
* @return the document node item
*/
@NonNull
static IDMDocumentNodeItem newInstance(
@NonNull URI resource,
@NonNull IResourceLocation resourceLocation,
@NonNull IAssemblyDefinition rootAssembly,
@NonNull IResourceLocation rootAssemblyLocation) {
return new DocumentImpl(resource, resourceLocation, rootAssembly, rootAssemblyLocation);
@NonNull IAssemblyDefinition rootAssembly) {
return new DocumentImpl(resource, rootAssembly);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,40 @@

package gov.nist.secauto.metaschema.core.mdm;

import gov.nist.secauto.metaschema.core.mdm.impl.DefinitionFieldNodeItem;
import gov.nist.secauto.metaschema.core.mdm.impl.IDMAtomicValuedNodeItem;
import gov.nist.secauto.metaschema.core.mdm.impl.IDMModelNodeItem;
import gov.nist.secauto.metaschema.core.metapath.StaticContext;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IFieldNodeItem;
import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
import gov.nist.secauto.metaschema.core.model.IFieldInstance;

import edu.umd.cs.findbugs.annotations.NonNull;

/**
* A field node item implementation that is backed by a simple Metaschema
* module-based data model.
*/
public interface IDMFieldNodeItem
extends IFieldNodeItem, IDMModelNodeItem<IFieldDefinition, IFieldInstance> {
// no additional methods
extends IFieldNodeItem, IDMModelNodeItem<IFieldDefinition, IFieldInstance>, IDMAtomicValuedNodeItem {
/**
* Create new field node item that is detached from a parent node item.
*
* @param definition
* the Metaschema field definition describing the field
* @param value
* the field's initial value
* @param staticContext
* the atomic field value
* @return the new field node item
*/
@NonNull
static IDMFieldNodeItem newInstance(
@NonNull IFieldDefinition definition,
@NonNull IAnyAtomicItem value,
@NonNull StaticContext staticContext) {
return new DefinitionFieldNodeItem(definition, value, staticContext);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.mdm;

import gov.nist.secauto.metaschema.core.mdm.impl.IDMAtomicValuedNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem;

public interface IDMFlagNodeItem extends IFlagNodeItem, IDMNodeItem, IDMAtomicValuedNodeItem {
// no additional methods
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.mdm;

import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.model.IResourceLocation;

import edu.umd.cs.findbugs.annotations.NonNull;

public interface IDMNodeItem extends INodeItem {
/**
*
* @param location
* information about the location of the node within the containing
* resource
*/
void setLocation(@NonNull IResourceLocation location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import gov.nist.secauto.metaschema.core.mdm.IDMAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.mdm.IDMFieldNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.AbstractNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem;
import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
import gov.nist.secauto.metaschema.core.model.IAssemblyInstance;
import gov.nist.secauto.metaschema.core.model.IFieldInstance;
import gov.nist.secauto.metaschema.core.model.IFlagInstance;
import gov.nist.secauto.metaschema.core.model.IResourceLocation;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
Expand All @@ -28,21 +25,14 @@
import edu.umd.cs.findbugs.annotations.NonNull;

public abstract class AbstractDMAssemblyNodeItem
extends AbstractNodeItem
extends AbstractDMModelNodeItem<IAssemblyDefinition, IAssemblyInstance>
implements IDMAssemblyNodeItem {
@NonNull
private final Map<IEnhancedQName, IFlagNodeItem> flags = new ConcurrentHashMap<>();
@NonNull
private final Map<IEnhancedQName, List<IDMModelNodeItem<?, ?>>> modelItems
= new ConcurrentHashMap<>();

protected AbstractDMAssemblyNodeItem() {
// nothing to do
}

@Override
public Object getValue() {
return this;
// only allow extending classes to create instances
}

@Override
Expand All @@ -55,26 +45,6 @@ protected String getValueSignature() {
return "";
}

@Override
public Collection<? extends IFlagNodeItem> getFlags() {
return ObjectUtils.notNull(flags.values());
}

@Override
public IFlagNodeItem getFlagByName(IEnhancedQName name) {
return flags.get(name);
}

@Override
public IFlagNodeItem newFlag(
@NonNull IFlagInstance instance,
@NonNull IResourceLocation resourceLocation,
@NonNull IAnyAtomicItem value) {
IFlagNodeItem flag = new FlagImpl(instance, this, resourceLocation, value);
flags.put(instance.getQName(), flag);
return flag;
}

@Override
public Collection<List<IDMModelNodeItem<?, ?>>> getModelItems() {
return ObjectUtils.notNull(modelItems.values());
Expand All @@ -87,21 +57,21 @@ public IFlagNodeItem newFlag(
}

@Override
public IDMFieldNodeItem newField(IFieldInstance instance, IResourceLocation resourceLocation, IAnyAtomicItem value) {
public IDMFieldNodeItem newField(IFieldInstance instance, IAnyAtomicItem value) {
List<IDMModelNodeItem<?, ?>> result = modelItems.computeIfAbsent(
instance.getQName(),
name -> Collections.synchronizedList(new LinkedList<IDMModelNodeItem<?, ?>>()));
IDMFieldNodeItem field = new FieldImpl(instance, this, resourceLocation, value);
IDMFieldNodeItem field = new ChildFieldNodeItem(instance, this, value);
result.add(field);
return field;
}

@Override
public IDMAssemblyNodeItem newAssembly(IAssemblyInstance instance, IResourceLocation resourceLocation) {
public IDMAssemblyNodeItem newAssembly(IAssemblyInstance instance) {
List<IDMModelNodeItem<?, ?>> result = modelItems.computeIfAbsent(
instance.getQName(),
name -> Collections.synchronizedList(new LinkedList<IDMModelNodeItem<?, ?>>()));
IDMAssemblyNodeItem assembly = new AssemblyImpl(instance, this, resourceLocation);
IDMAssemblyNodeItem assembly = new ChildAssemblyNodeItem(instance, this);
result.add(assembly);
return assembly;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,24 @@

import gov.nist.secauto.metaschema.core.mdm.IDMFieldNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem;
import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
import gov.nist.secauto.metaschema.core.model.IFieldInstance;
import gov.nist.secauto.metaschema.core.model.IFlagInstance;
import gov.nist.secauto.metaschema.core.model.IResourceLocation;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import edu.umd.cs.findbugs.annotations.NonNull;

public class FieldImpl
extends AbstractDMInstanceNodeItem<IFieldDefinition, IFieldInstance, IAssemblyNodeItem>
public abstract class AbstractDMFieldNodeItem
extends AbstractDMModelNodeItem<IFieldDefinition, IFieldInstance>
implements IDMFieldNodeItem {
@NonNull
private IAnyAtomicItem value;
@NonNull
private final Map<IEnhancedQName, IFlagNodeItem> flags = new ConcurrentHashMap<>();

public FieldImpl(
@NonNull IFieldInstance instance,
@NonNull IAssemblyNodeItem parent,
@NonNull IResourceLocation resourceLocation,
@NonNull IAnyAtomicItem value) {
super(instance, parent, resourceLocation);
protected AbstractDMFieldNodeItem(@NonNull IAnyAtomicItem value) {
this.value = value;
}

Expand All @@ -55,11 +41,6 @@ public void setValue(@NonNull Object value) {
this.value = getValueItemType().newItem(value);
}

@Override
public Object getValue() {
return toAtomicItem().getValue();
}

@Override
public String stringValue() {
return toAtomicItem().asString();
Expand All @@ -70,21 +51,6 @@ protected String getValueSignature() {
return toAtomicItem().toSignature();
}

@Override
public int getPosition() {
return getParentNodeItem().getModelItemsByName(getQName()).indexOf(this);
}

@Override
public Collection<? extends IFlagNodeItem> getFlags() {
return ObjectUtils.notNull(flags.values());
}

@Override
public IFlagNodeItem getFlagByName(IEnhancedQName name) {
return flags.get(name);
}

@Override
public Collection<? extends List<? extends IModelNodeItem<?, ?>>> getModelItems() {
// no model items
Expand All @@ -97,13 +63,4 @@ public IFlagNodeItem getFlagByName(IEnhancedQName name) {
return CollectionUtil.emptyList();
}

@Override
public IFlagNodeItem newFlag(
@NonNull IFlagInstance instance,
@NonNull IResourceLocation resourceLocation,
@NonNull IAnyAtomicItem value) {
IFlagNodeItem flag = new FlagImpl(instance, this, resourceLocation, value);
flags.put(instance.getQName(), flag);
return flag;
}
}
Loading

0 comments on commit 28e6ebd

Please sign in to comment.