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

Rules element with Rules and Group by #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions yard-api/src/main/java/org/kie/yard/api/model/Accumulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 org.kie.yard.api.model;

public class Accumulator {
private String function;

private String parameter;

private String as;

public String getFunction() {
return function;
}

public void setFunction(String function) {
this.function = function;
}

public String getParameter() {
return parameter;
}

public void setParameter(String parameter) {
this.parameter = parameter;
}

public String getAs() {
return as;
}

public void setAs(String as) {
this.as = as;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
key = "type",
value = {
@YamlSubtype(alias = "DecisionTable", type = DecisionTable.class),
@YamlSubtype(alias = "LiteralExpression", type = LiteralExpression.class)
@YamlSubtype(alias = "LiteralExpression", type = LiteralExpression.class),
@YamlSubtype(alias = "Rules", type = RuleExpression.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly this is putting a whole Drools like syntax dialect inside the current AST of YaRD, which I'm not necessarily against but it seems very different to what we have now.

Currently there is no documentation nor specification of what YaRD should be so this seems like a risky change

})
@JsonbTypeInfo(
key = "type",
Expand Down
51 changes: 51 additions & 0 deletions yard-api/src/main/java/org/kie/yard/api/model/Given.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 org.kie.yard.api.model;

import java.util.List;

public class Given implements Pattern {
private String given;
private String from;
private List<String> having;

public String getGiven() {
return given;
}

public void setGiven(String given) {
this.given = given;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public List<String> getHaving() {
return having;
}

public void setHaving(List<String> having) {
this.having = having;
}
}
51 changes: 51 additions & 0 deletions yard-api/src/main/java/org/kie/yard/api/model/GroupBy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 org.kie.yard.api.model;

public class GroupBy implements Pattern {
private Given given;

private Grouping grouping;

private Accumulator accumulator;

public Given getGiven() {
return given;
}

public void setGiven(Given given) {
this.given = given;
}

public Grouping getGrouping() {
return grouping;
}

public void setGrouping(Grouping grouping) {
this.grouping = grouping;
}

public Accumulator getAccumulators() {
return accumulator;
}

public void setAccumulators(Accumulator accumulator) {
this.accumulator = accumulator;
}
}
39 changes: 39 additions & 0 deletions yard-api/src/main/java/org/kie/yard/api/model/Grouping.java
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 org.kie.yard.api.model;
public class Grouping {
private String by;
private String as;

public String getBy() {
return by;
}

public void setBy(String by) {
this.by = by;
}

public String getAs() {
return as;
}

public void setAs(String as) {
this.as = as;
}
}
31 changes: 31 additions & 0 deletions yard-api/src/main/java/org/kie/yard/api/model/ListResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 org.kie.yard.api.model;

public class ListResult {
private String add;

public String getAdd() {
return add;
}

public void setAdd(String add) {
this.add = add;
}
}
22 changes: 22 additions & 0 deletions yard-api/src/main/java/org/kie/yard/api/model/Pattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 org.kie.yard.api.model;

public interface Pattern {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 org.kie.yard.api.model;

import org.kie.j2cl.tools.yaml.mapper.api.YAMLDeserializer;
import org.kie.j2cl.tools.yaml.mapper.api.exception.YAMLDeserializationException;
import org.kie.j2cl.tools.yaml.mapper.api.internal.deser.YAMLDeserializationContext;
import org.kie.j2cl.tools.yaml.mapper.api.node.NodeType;
import org.kie.j2cl.tools.yaml.mapper.api.node.YamlMapping;
import org.kie.j2cl.tools.yaml.mapper.api.node.YamlNode;

import java.util.ArrayList;
import java.util.Objects;

public class Pattern_YamlDeserializerImpl
implements YAMLDeserializer<Pattern> {

@Override
public Pattern deserialize(YamlMapping yamlMapping, String s, YAMLDeserializationContext yamlDeserializationContext) throws YAMLDeserializationException {
return deserialize(yamlMapping.getNode(s), yamlDeserializationContext);
}

@Override
public Pattern deserialize(YamlNode yamlNode, YAMLDeserializationContext yamlDeserializationContext) {
final YamlMapping mapping = yamlNode.asMapping();
if (mapping.keys().contains("given")) {
return createGiven(mapping);
} else if (mapping.keys().contains("groupBy")) {
final GroupBy groupBy = new GroupBy();
groupBy.setGiven(createGiven(mapping.getNode("groupBy").asMapping()));
groupBy.setGrouping(createGrouping(mapping.getNode("grouping").asMapping()));
groupBy.setAccumulators(createAccumulator(mapping.getNode("accumulator").asMapping()));
return groupBy;
}
throw new IllegalStateException("Unknown element, should be given or groupBy");
}

private Accumulator createAccumulator(final YamlMapping groupBy) {
final Accumulator accumulator = new Accumulator();
accumulator.setFunction((String) groupBy.getNode("function").asScalar().value());
accumulator.setAs((String) groupBy.getNode("as").asScalar().value());
if (groupBy.keys().contains("parameter")) {
accumulator.setParameter((String) groupBy.getNode("parameter").asScalar().value());
}
// TODO All are cast to Strings, check what happens with numbers
return accumulator;
}

private static Given createGiven(YamlMapping mapping) {
final Given given = new Given();
given.setGiven((String) mapping.getNode("given").asScalar().value());
given.setFrom((String) mapping.getNode("from").asScalar().value());
given.setHaving(new ArrayList<>());
if (mapping.keys().contains("having")
&& Objects.equals(mapping.getNode("having").type(), NodeType.SEQUENCE)) {
for (YamlNode having : mapping.getNode("having").asSequence()) {
given.getHaving().add((String) having.asScalar().value());
}
}
return given;
}

private Grouping createGrouping(YamlMapping groupingNode) {
final Grouping grouping = new Grouping();
grouping.setBy((String) groupingNode.getNode("by").asScalar().value());
grouping.setAs((String) groupingNode.getNode("as").asScalar().value());
return grouping;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 org.kie.yard.api.model;

import org.kie.j2cl.tools.yaml.mapper.api.YAMLSerializer;
import org.kie.j2cl.tools.yaml.mapper.api.internal.ser.YAMLSerializationContext;
import org.kie.j2cl.tools.yaml.mapper.api.node.YamlMapping;
import org.kie.j2cl.tools.yaml.mapper.api.node.YamlSequence;

public class Pattern_YamlSerializerImpl
implements YAMLSerializer<Pattern> {
@Override
public void serialize(YamlMapping yamlMapping, String s, Pattern when, YAMLSerializationContext yamlSerializationContext) {

}

@Override
public void serialize(YamlSequence yamlSequence, Pattern when, YAMLSerializationContext yamlSerializationContext) {

}
}
Loading
Loading