Skip to content

Commit

Permalink
Add fragments of common-3 and scripting-3 schemas
Browse files Browse the repository at this point in the history
Also:
- attempted to factor out Prism concepts from Axiom language
- minor fixes regarding @NotNull/@nullable
- fixed Java 8 compilability, making checkstyle happy
  • Loading branch information
mederly committed May 13, 2020
1 parent 272745a commit 463abf0
Show file tree
Hide file tree
Showing 13 changed files with 1,532 additions and 68 deletions.
Expand Up @@ -35,12 +35,8 @@ public static class Item implements AxiomItemDefinition {
public static final AxiomItemDefinition TYPE_DEFINITION = new Item("type", Type.TYPE_DEFINITION, false);
public static final AxiomItemDefinition SUPERTYPE_REFERENCE = new Item("extends", Type.TYPE_REFERENCE, false);
public static final AxiomItemDefinition ROOT_DEFINITION = new Item("root", Type.ITEM_DEFINITION, false);
public static final AxiomItemDefinition OBJECT_DEFINITION = new Item("object", Type.OBJECT_DEFINITION, false);
public static final AxiomItemDefinition REFERENCE_DEFINITION = new Item("reference", Type.ITEM_DEFINITION, false);
public static final AxiomItemDefinition ITEM_DEFINITION = new Item("item", Type.ITEM_DEFINITION, false);
public static final AxiomItemDefinition OBJECT_REFERENCE_DEFINITION = new Item("objectReference", Type.OBJECT_REFERENCE_DEFINITION, false);
public static final AxiomItemDefinition MODEL_DEFINITION = new Item("model", Type.MODEL, false);
public static final AxiomItemDefinition ITEM_NAME = new Item("itemName", Type.IDENTIFIER, false);
public static final AxiomItemDefinition MIN_OCCURS = new Item("minOccurs", Type.STRING, false);
public static final AxiomItemDefinition MAX_OCCURS = new Item("maxOccurs", Type.STRING, false);
public static final AxiomItemDefinition TARGET_TYPE = new Item("targetType", Type.IDENTIFIER, true);
Expand Down Expand Up @@ -99,34 +95,20 @@ public static class Type implements AxiomTypeDefinition {
Item.NAMESPACE,
Item.VERSION,
Item.TYPE_DEFINITION,
Item.OBJECT_DEFINITION,
Item.ROOT_DEFINITION
));
public static final Type TYPE_DEFINITION =
new Type("AxiomTypeDefinition", BASE_DEFINITION, () -> itemDefs(
Item.ARGUMENT,
Item.SUPERTYPE_REFERENCE,
Item.ITEM_DEFINITION,
Item.OBJECT_REFERENCE_DEFINITION
Item.ITEM_DEFINITION
));
public static final Type ITEM_DEFINITION =
new Type("AxiomItemDefinition", BASE_DEFINITION, () -> itemDefs(
Item.TYPE_REFERENCE,
Item.MIN_OCCURS,
Item.MAX_OCCURS
));
public static final Type REFERENCE_DEFINITION =
new Type("AxiomReferenceDefinition", ITEM_DEFINITION, () -> itemDefs(

));
public static final Type OBJECT_DEFINITION =
new Type("AxiomObjectDefinition", TYPE_DEFINITION, () -> itemDefs(
Item.ITEM_NAME
));
public static final Type OBJECT_REFERENCE_DEFINITION =
new Type("AxiomObjectReferenceDefinition", ITEM_DEFINITION, () -> itemDefs(
Item.TARGET_TYPE
));

private final AxiomIdentifier identifier;
private final AxiomTypeDefinition superType;
Expand Down
Expand Up @@ -43,7 +43,7 @@ public AxiomAntlrVisitor(String name, AxiomIdentifierResolver statements, AxiomS
private AxiomIdentifier statementIdentifier(IdentifierContext identifier) {
String prefix = nullableText(identifier.prefix());
String localName = identifier.localIdentifier().getText();
return statements.resolveStatementIdentifier(prefix,localName);
return statements.resolveStatementIdentifier(prefix, localName);
}

private String nullableText(ParserRuleContext prefix) {
Expand Down
Expand Up @@ -10,13 +10,13 @@

import com.evolveum.axiom.api.AxiomIdentifier;

import org.jetbrains.annotations.Nullable;

public interface AxiomIdentifierResolver {

final AxiomIdentifierResolver AXIOM_DEFAULT_NAMESPACE = defaultNamespace(AxiomIdentifier.AXIOM_NAMESPACE);

AxiomIdentifier resolveStatementIdentifier(@NotNull String prefix, @NotNull String localName);


AxiomIdentifier resolveStatementIdentifier(@Nullable String prefix, @NotNull String localName);

static AxiomIdentifierResolver defaultNamespace(String namespace) {
return (prefix, localName) -> prefix == null ? AxiomIdentifier.from(namespace, localName) : null;
Expand Down
Expand Up @@ -17,7 +17,8 @@ public class AxiomItemDefinitionImpl extends AbstractAxiomBaseDefinition impleme
public AxiomItemDefinitionImpl(AxiomIdentifier keyword, AxiomIdentifier value, List<AxiomStatement<?>> children,
Multimap<AxiomIdentifier, AxiomStatement<?>> keywordMap) {
super(keyword, value, children, keywordMap);
type = first(AxiomBuiltIn.Item.TYPE_DEFINITION.name(), AxiomTypeDefinition.class).get();;
type = first(AxiomBuiltIn.Item.TYPE_DEFINITION.name(), AxiomTypeDefinition.class)
.orElseThrow(() -> new IllegalStateException("No 'type' declaration in " + super.toString()));
}

@Override
Expand Down
Expand Up @@ -74,7 +74,7 @@ protected void putAll(Builder<AxiomIdentifier, AxiomItemDefinition> builder,
Collection<AxiomItemDefinition> children) {
for (AxiomItemDefinition definition : children) {
builder.put(definition.name(), definition);
};
}
}

public static <V, T extends AxiomStatement<V>> Factory<V, T> factory() {
Expand Down
Expand Up @@ -38,7 +38,7 @@ public AxiomTypeDefinitionImpl(AxiomIdentifier keyword, AxiomIdentifier value, L

@Override
public Optional<AxiomItemDefinition> argument() {
if(argument.isEmpty() && superType().isPresent()) {
if (!argument.isPresent() && superType().isPresent()) {
return superType().get().argument();
}
return argument;
Expand Down
Expand Up @@ -28,6 +28,8 @@
import com.evolveum.axiom.lang.impl.AxiomStatementImpl.Factory;
import com.google.common.collect.ImmutableMap;

import org.jetbrains.annotations.Nullable;

public class ModelReactorContext implements AxiomIdentifierResolver {


Expand Down Expand Up @@ -202,7 +204,7 @@ public void loadModelFromSource(AxiomStatementSource statementSource) {
}

@Override
public AxiomIdentifier resolveStatementIdentifier(@NotNull String prefix, @NotNull String localName) {
public AxiomIdentifier resolveStatementIdentifier(@Nullable String prefix, @NotNull String localName) {
return AxiomIdentifier.axiom(localName);
}

Expand Down
91 changes: 73 additions & 18 deletions infra/axiom/src/main/resources/axiom-lang.axiom
Expand Up @@ -9,39 +9,58 @@ model axiom-lang {

type AxiomModel {
extends AxiomBaseDefinition;

item root {
type AxiomItemDefinition;
}

item type {
documentation """
Type Declaration
""";
type AxiomTypeDefinition;
}

item object {
type AxiomObjectDefinition;
}

item namespace {
type string;
}

item version {
type string;
}

// TODO move to prism schema; consider renaming to objectType?
item object {
type PrismObjectDefinition;
}

// TODO move to prism schema; consider renaming to containerType?
item container {
type PrismContainerDefinition;
}

// TODO move to prism schema; consider renaming to referenceType?
item reference {
type PrismReferenceDefinition;
}

// TODO move to prism schema
item item {
type PrismItemDefinition;
}
}

type AxiomBaseDefinition {
argument name;

item name {
type AxiomIdentifier;
}
}

item identifier {
type AxiomIdentifier;
}

item documentation {
type string;
}
Expand All @@ -53,30 +72,61 @@ model axiom-lang {
item argument {
type AxiomIdentifier;
}

item extends { // Should be reference
type AxiomIdentifier; // Should be AxiomTypeIdentifier in future
}

item identifier {
type AxiomIdentifier;
}

// TODO move to prism schema
item object {
type boolean;
}

// TODO move to prism schema
item container {
type boolean;
}

// TODO move to prism schema
item objectReference {
type boolean;
}

// TODO reconsider this - strictly speaking this is part of "global type+item definition combo"
item itemName {
type AxiomIdentifier;
}

item item {
type AxiomItemDefinition;
}
}

type AxiomObjectDefinition {
// TODO move to prism schema; probably should be prism:ObjectDefinition
type PrismObjectDefinition {
extends AxiomTypeDefinition;
item itemName {
type AxiomIdentifier; // This should be probably at dif
}
}

type AxiomIdentifier {

// TODO move to prism schema; probably should be prism:ContainerDefinition
type PrismContainerDefinition {
extends AxiomTypeDefinition;
}

// TODO move to prism schema; probably should be prism:ReferenceDefinition
type PrismReferenceDefinition {
extends AxiomTypeDefinition;
}

type string {

// TODO move to prism schema; probably should be prism:ItemDefinition
type PrismItemDefinition {
extends AxiomItemDefinition;
}

type AxiomIdentifier {
}

type AxiomItemDefinition {
Expand All @@ -99,4 +149,9 @@ model axiom-lang {
}

type AxiomTypeReference;
}

// "Type library" (temporary)

type string;
type boolean;
}
Expand Up @@ -14,8 +14,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.testng.annotations.Test;
Expand All @@ -28,24 +26,22 @@
import com.evolveum.axiom.lang.api.AxiomTypeDefinition;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Item;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Type;
import com.evolveum.axiom.lang.api.stmt.AxiomStatement;
import com.evolveum.axiom.lang.impl.AxiomItemDefinitionImpl;

import com.evolveum.axiom.lang.impl.AxiomStatementSource;

import com.evolveum.axiom.lang.impl.AxiomSyntaxException;
import com.evolveum.axiom.lang.impl.AxiomTypeDefinitionImpl;
import com.evolveum.axiom.lang.impl.BasicStatementRule;
import com.evolveum.axiom.lang.impl.ModelReactorContext;
import com.evolveum.midpoint.tools.testng.AbstractUnitTest;
import com.google.common.collect.Iterables;

public class TestAxiomParser extends AbstractUnitTest {

private static final String COMMON_DIR_PATH = "src/test/resources/";
private static final String NAME = "base-example.axiom";
private static final String AXIOM_LANG = "/axiom-lang.axiom";

private static final String BASE_EXAMPLE = "base-example.axiom";
private static final String COMMON_CORE = "common-core.axiom";
private static final String SCRIPTING = "scripting.axiom";

@Test
public void axiomSelfDescribingTest() throws IOException, AxiomSyntaxException {

Expand Down Expand Up @@ -76,16 +72,23 @@ private void assertTypedefBasetype(Optional<AxiomTypeDefinition> optional) {

private void assertInstanceOf(Class<?> clz, Object value) {
assertTrue(clz.isInstance(value));

}


@Test
public void moduleHeaderTest() throws IOException, AxiomSyntaxException {
AxiomSchemaContext context = parseFile(NAME);
AxiomSchemaContext context = parseFile(BASE_EXAMPLE);
assertNotNull(context.getType(AxiomIdentifier.axiom("Example")).get());
}

@Test
public void commonCoreTest() throws IOException, AxiomSyntaxException {
AxiomSchemaContext context = parseFile(COMMON_CORE);
}

@Test
public void scriptingTest() throws IOException, AxiomSyntaxException {
AxiomSchemaContext context = parseFile(SCRIPTING);
}

private AxiomSchemaContext parseFile(String name) throws AxiomSyntaxException, FileNotFoundException, IOException {
return parseInputStream(name, new FileInputStream(COMMON_DIR_PATH + name));
Expand Down

0 comments on commit 463abf0

Please sign in to comment.