Skip to content

Commit

Permalink
module definitions from api #123
Browse files Browse the repository at this point in the history
  • Loading branch information
isuru89 committed Jul 31, 2022
1 parent 88e6504 commit 5789bf3
Show file tree
Hide file tree
Showing 64 changed files with 1,083 additions and 175 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/io/github/oasis/core/ID.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class ID {
public static final String CACHE_ELEMENTS_META = "oasis:cache:elementmeta";
public static final String CACHE_ELEMENTS_BY_TYPE_META = "oasis:cache:elementtypemeta";
public static final String CACHE_ELEMENTS = "oasis:cache:elements";
public static final String CACHE_MODULES = "oasis:cache:modules";
public static final String CACHE_RANKS = "oasis:cache:ranks";
public static final String ALL_TEAMS_INDEX = "oasis:teams:index";
public static final String ALL_USERS_INDEX = "oasis:users:index";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

String description() default "";

String type() default "";
boolean required() default true;

String[] possibleTypes() default {};

String[] valueSet() default {};

Class<?> parameterizedType() default Object.class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one
* * or more contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. The ASF licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/

package io.github.oasis.core.elements;

import io.github.oasis.core.elements.spec.BaseSpecification;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class AcceptedDefinition {
private Class<? extends AbstractDef> definitionClz;
private Class<? extends BaseSpecification> specificationClz;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one
* * or more contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. The ASF licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/

package io.github.oasis.core.elements;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

@Data
public class AcceptedDefinitions {

private final List<DefinitionEntry> definitions = new ArrayList<>();

public AcceptedDefinitions addAcceptingDefinition(String key, AcceptedDefinition supportingDefinition) {
definitions.add(new DefinitionEntry().setKey(key).setAcceptedDefinitions(supportingDefinition));
return this;
}

@Data
@Accessors(chain = true)
public static class DefinitionEntry {
private String key;
private AcceptedDefinition acceptedDefinitions;
}
}
26 changes: 14 additions & 12 deletions core/src/main/java/io/github/oasis/core/elements/ElementModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.github.oasis.core.external.Db;

import java.io.Serializable;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;

Expand All @@ -48,18 +49,6 @@ public void init(RuntimeContextSupport context) throws OasisException {
*/
public abstract String getId();

public List<Class<? extends AbstractDef>> getSupportedDefinitions() {
return List.of();
}

/**
* Returns list of accepted keys eligible for parsing definitions using this module.
* These keys are case-insensitive.
*
* @return list of keys supported
*/
public abstract List<String> getSupportedDefinitionKeys();

public ElementParser getParser() {
return null;
}
Expand All @@ -74,6 +63,19 @@ public Map<String, Class<? extends Serializable>> getFeedDefinitions() {
return Map.of();
}

/**
* Customize the already parsed and derived module definition from the information given on this same class.
*
* This method could be used to modify any module specific data. If nothing needs to be changed,
* just return the same provided instance as return value.
*
* @param alreadyParsedDefinition definition already derived using existing provided information.
* @return module definition instance to be used.
*/
public ModuleDefinition customizeModuleDef(ModuleDefinition alreadyParsedDefinition) {
return alreadyParsedDefinition;
}

/**
* Loads scripts package under the given class.
* @param db db instance to refer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ default AbstractRule parseToRule(EngineMessage dto) {

AbstractRule convert(AbstractDef<? extends BaseSpecification> definition);

AcceptedDefinitions getAcceptingDefinitions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,36 @@
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
public class ModuleDefinition implements Serializable {

private String id;
private Specs specs;

@Data
public static class Specs {
private Map<String, Object> feeds;
private Map<String, Object> rules;

public void addRuleSpec(String ruleId, Object spec) {
if (rules == null) {
rules = new HashMap<>();
}
rules.put(ruleId, spec);
}

public void addFeedSpec(String id, Object spec) {
if (feeds == null) {
feeds = new HashMap<>();
}
feeds.put(id, spec);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one
* * or more contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. The ASF licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
*
*/

package io.github.oasis.core.elements;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

@Data
@Accessors(chain = true)
public class SpecAttributeSchema implements Serializable {
private String name;
private String type;
private String valueType;
private String description;
private List<String> valueSet;
private List<SpecAttributeSchema> children;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.github.oasis.core.elements.spec;

import io.github.oasis.core.annotations.DefinitionDetails;
import io.github.oasis.core.elements.Validator;
import io.github.oasis.core.exception.OasisParseException;
import io.github.oasis.core.utils.Utils;
Expand All @@ -33,8 +34,14 @@
@Data
public class AcceptsWithinDef implements Validator, Serializable {

@DefinitionDetails(
description = "Filters an event if any of time range matched",
parameterizedType = TimeRangeDef.class)
private List<TimeRangeDef> anyOf;

@DefinitionDetails(
description = "Filters an event only when all of the given time ranges matched",
parameterizedType = TimeRangeDef.class)
private List<TimeRangeDef> allOf;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.github.oasis.core.elements.spec;

import io.github.oasis.core.annotations.DefinitionDetails;
import io.github.oasis.core.elements.Validator;
import io.github.oasis.core.exception.OasisParseException;
import lombok.Data;
Expand All @@ -31,6 +32,7 @@
@Data
public class AwardDef implements Validator, Serializable {

@DefinitionDetails(description = "Reward with points")
private PointAwardDef points;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.github.oasis.core.elements.spec;

import io.github.oasis.core.annotations.DefinitionDetails;
import io.github.oasis.core.elements.Validator;
import io.github.oasis.core.exception.OasisParseException;
import lombok.Data;
Expand All @@ -32,8 +33,11 @@
@Data
public class BaseSpecification implements Validator, Serializable {

@DefinitionDetails(description = "Selection criteria")
private SelectorDef selector;

@DefinitionDetails(parameterizedType = String.class,
description = "Feature flags for each element.")
private Set<String> flags;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.github.oasis.core.elements.spec;

import io.github.oasis.core.annotations.DefinitionDetails;
import io.github.oasis.core.elements.Validator;
import io.github.oasis.core.exception.OasisParseException;
import io.github.oasis.core.utils.Texts;
Expand All @@ -32,6 +33,7 @@
@Data
public class EventFilterDef implements Validator, Serializable {

@DefinitionDetails(description = "Expression as a script.")
private String expression;

private String className;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.github.oasis.core.elements.spec;

import io.github.oasis.core.annotations.DefinitionDetails;
import io.github.oasis.core.elements.Validator;
import io.github.oasis.core.exception.OasisParseException;
import io.github.oasis.core.utils.Utils;
Expand All @@ -33,8 +34,12 @@
@Data
public class MatchEventsDef implements Validator, Serializable {

@DefinitionDetails(parameterizedType = String.class,
description = "List of event ids to match exactly as given. If at least one matches, event gets matched.")
private List<String> anyOf;

@DefinitionDetails(parameterizedType = String.class,
description = "List of patterns to match by event ids. If at least one matches, event gets matched.")
private List<String> patterns;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.github.oasis.core.elements.spec;

import io.github.oasis.core.annotations.DefinitionDetails;
import io.github.oasis.core.elements.Validator;
import io.github.oasis.core.exception.OasisParseException;
import io.github.oasis.core.utils.Texts;
Expand All @@ -36,22 +37,15 @@
@NoArgsConstructor
public class PointAwardDef implements Validator, Serializable {

@DefinitionDetails(description = "Referenced point id")
private String id;

@DefinitionDetails(description = "Amount of points to reward")
private BigDecimal amount;

@DefinitionDetails(description = "Scripted expression to derive rewarding points based on event data")
private String expression;

public PointAwardDef(String id, BigDecimal amount) {
this.id = id;
this.amount = amount;
}

public PointAwardDef(String id, String expression) {
this.id = id;
this.expression = expression;
}

@Override
public void validate() throws OasisParseException {
Validate.notEmpty(id, "Mandatory field 'id' is missing!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.github.oasis.core.elements.spec;

import io.github.oasis.core.annotations.DefinitionDetails;
import io.github.oasis.core.elements.Validator;
import io.github.oasis.core.exception.OasisParseException;
import io.github.oasis.core.utils.Texts;
Expand All @@ -33,16 +34,21 @@
@Data
public class SelectorDef implements Validator, Serializable {

@DefinitionDetails(description = "Event id to match a single event")
private String matchEvent;
@DefinitionDetails(description = "Criteria to match multiple events by event ids")
private MatchEventsDef matchEvents;

/**
* Accepted point ids. These are same as event ids in game engine.
*/
@DefinitionDetails(description = "Criteria to match multiple events by point ids")
private MatchEventsDef matchPointIds;

@DefinitionDetails(description = "Filter to match an event by its content.")
private EventFilterDef filter;

@DefinitionDetails(description = "Criteria to match events by time or date range")
private AcceptsWithinDef acceptsWithin;

public static SelectorDef singleEvent(String matchEvent) {
Expand Down
Loading

0 comments on commit 5789bf3

Please sign in to comment.