Skip to content

Commit

Permalink
Merged version 1.6.7
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Nov 20, 2024
2 parents 7902cf4 + b51ac0e commit b38c044
Show file tree
Hide file tree
Showing 22 changed files with 969 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public InvalidAttributeException(PMMLObject object, Enum<?> value){
}

public InvalidAttributeException(PMMLObject object, Field field, Object value){
super(formatMessage(XPathUtil.formatAttribute(field, value)), object);
super(formatMessage(XPathUtil.formatAttribute(object.getClass(), field, value)), object);
}

static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

public class InvalidElementListException extends InvalidMarkupException {

public InvalidElementListException(String message){
super(message);
}

public InvalidElementListException(String message, PMMLObject object){
super(message, object);
}

public InvalidElementListException(List<? extends PMMLObject> objects){
super("List of elements " + XPathUtil.formatElement((objects.get(0)).getClass()) + " is not valid", objects.get(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public MisplacedAttributeException(PMMLObject object, Enum<?> value){
}

public MisplacedAttributeException(PMMLObject object, Field field, Object value){
super(formatMessage(XPathUtil.formatAttribute(field, value)), object);
super(formatMessage(XPathUtil.formatAttribute(object.getClass(), field, value)), object);
}

static
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*/
package org.jpmml.model;

import java.util.List;

import org.dmg.pmml.PMMLObject;

public class MisplacedElementListException extends InvalidElementListException {

public MisplacedElementListException(List<? extends PMMLObject> objects){
super(formatMessage(XPathUtil.formatElement((objects.get(0)).getClass())), objects.get(0));
}

static
public String formatMessage(String xPath){
return "List of elements " + xPath + " is not permitted in this location";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MissingAttributeException(String message, PMMLObject context){
}

public MissingAttributeException(PMMLObject object, Field field){
super(formatMessage(XPathUtil.formatElementOrAttribute(field)), object);
super(formatMessage(XPathUtil.formatElementOrAttribute(object.getClass(), field)), object);
}

static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MissingElementException(String message, PMMLObject context){
}

public MissingElementException(PMMLObject object, Field field){
super(formatMessage(XPathUtil.formatElementOrAttribute(field)), object);
super(formatMessage(XPathUtil.formatElementOrAttribute(object.getClass(), field)), object);
}

static
Expand Down
103 changes: 103 additions & 0 deletions pmml-model/src/main/java/org/jpmml/model/PMMLOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*/
package org.jpmml.model;

import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

import org.dmg.pmml.Version;

public class PMMLOutputStream extends FilterOutputStream {

private Version version = null;

private ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);


public PMMLOutputStream(OutputStream os, Version version){
super(os);

this.version = Objects.requireNonNull(version);

if(!version.isStandard()){
throw new IllegalArgumentException();
}
}

@Override
public void write(byte[] bytes) throws IOException {
this.write(bytes, 0, bytes.length);
}

@Override
public void write(byte[] bytes, int offset, int length) throws IOException {

if(this.buffer != null){

for(int i = offset, max = offset + length; i < max; i++){
this.write(bytes[i]);
}
} else

{
super.out.write(bytes, offset, length);
}
}

@Override
public void write(int b) throws IOException {

if(this.buffer != null){
this.buffer.write(b);

if(b == '>'){
String string = this.buffer.toString("UTF-8");

if(string.endsWith("?>")){
super.out.write(string.getBytes("UTF-8"));

this.buffer.reset();
} else

if(string.endsWith(">")){
String updatedString = string.replace(Version.PMML_4_4.getNamespaceURI(), this.version.getNamespaceURI());

if(Objects.equals(string, updatedString)){
throw new IllegalStateException();
}

super.out.write(updatedString.getBytes("UTF-8"));

this.buffer = null;
} else

{
throw new IllegalStateException();
}
}
} else

{
super.out.write(b);
}
}

@Override
public void flush() throws IOException {

if(this.buffer != null){
throw new IllegalStateException();
}

super.flush();
}

@Override
public void close() throws IOException {
super.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public UnsupportedAttributeException(PMMLObject object, Enum<?> value){
}

public UnsupportedAttributeException(PMMLObject object, Field field, Object value){
super("Attribute with value " + XPathUtil.formatAttribute(field, value) + " is not supported", object);
super("Attribute with value " + XPathUtil.formatAttribute(object.getClass(), field, value) + " is not supported", object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
public @interface Added {

Version value();

boolean removable() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import org.jpmml.model.MarkupException;
import org.jpmml.model.MisplacedAttributeException;
import org.jpmml.model.MisplacedElementException;
import org.jpmml.model.MisplacedElementListException;
import org.jpmml.model.MissingAttributeException;
import org.jpmml.model.MissingElementException;
import org.jpmml.model.ReflectionUtil;
import org.jpmml.model.UnsupportedAttributeException;
import org.jpmml.model.UnsupportedElementException;
import org.jpmml.model.UnsupportedElementListException;
import org.jpmml.model.annotations.Added;
import org.jpmml.model.annotations.Optional;
import org.jpmml.model.annotations.Removed;
Expand Down Expand Up @@ -60,7 +62,14 @@ public void handleAdded(PMMLObject object, AnnotatedElement element, Added added
} else

if(isElement(field)){
report(new UnsupportedElementException((PMMLObject)value));

if(value instanceof List){
report(new UnsupportedElementListException((List<? extends PMMLObject>)value));
} else

{
report(new UnsupportedElementException((PMMLObject)value));
}
} else

{
Expand Down Expand Up @@ -90,7 +99,14 @@ public void handleRemoved(PMMLObject object, AnnotatedElement element, Removed r
} else

if(isElement(field)){
report(new MisplacedElementException((PMMLObject)value));

if(value instanceof List){
report(new MisplacedElementListException((List<? extends PMMLObject>)value));
} else

{
report(new MisplacedElementException((PMMLObject)value));
}
} else

{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*/
package org.jpmml.model.visitors;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.util.Objects;

import org.dmg.pmml.Apply;
import org.dmg.pmml.MiningField;
import org.dmg.pmml.PMML;
import org.dmg.pmml.PMMLAttributes;
import org.dmg.pmml.PMMLObject;
import org.dmg.pmml.TargetValue;
import org.dmg.pmml.Version;
import org.dmg.pmml.VisitorAction;
import org.dmg.pmml.time_series.TrendExpoSmooth;
import org.jpmml.model.ReflectionUtil;
import org.jpmml.model.UnsupportedAttributeException;
import org.jpmml.model.UnsupportedElementException;
import org.jpmml.model.annotations.Added;
import org.jpmml.model.annotations.Optional;
import org.jpmml.model.annotations.Removed;
import org.jpmml.model.annotations.Required;
import org.jpmml.model.filters.ExportFilter;

/**
* <p>
* A Visitor that downgrades a class model object from the latest PMML schema version to some earlier one.
* </p>
*
* @see ExportFilter
*/
public class VersionDowngrader extends VersionInspector {

private Version version = null;


public VersionDowngrader(Version version){
this.version = Objects.requireNonNull(version);

if(!version.isStandard()){
throw new IllegalArgumentException();
}
}

@Override
public void handleAdded(PMMLObject object, AnnotatedElement element, Added added){
Version version = added.value();

if(version.isStandard() && version.compareTo(this.version) > 0){

if(element instanceof Class){
// Ignored
} else

if(element instanceof Field){
Field field = (Field)element;

if(added.removable()){
ReflectionUtil.setFieldValue(field, object, null);
}
} else

{
throw new IllegalArgumentException();
}
}
}

@Override
public void handleRemoved(PMMLObject object, AnnotatedElement element, Removed removed){
}

@Override
public void handleOptional(PMMLObject object, AnnotatedElement element, Optional optional){
}

@Override
public void handleRequired(PMMLObject object, AnnotatedElement element, Required required){
}

@Override
public VisitorAction visit(Apply apply){
Object defaultValue = apply.getDefaultValue();

if(defaultValue != null){

if(this.version.compareTo(Version.PMML_4_1) == 0){
Object mapMissingTo = apply.getMapMissingTo();

if(mapMissingTo != null){
throw new UnsupportedAttributeException(apply, PMMLAttributes.APPLY_DEFAULTVALUE, defaultValue);
}

apply
.setDefaultValue(null)
.setMapMissingTo(defaultValue);
}
}

return super.visit(apply);
}

@Override
public VisitorAction visit(MiningField miningField){
MiningField.UsageType usageType = miningField.getUsageType();

switch(usageType){
case TARGET:
if(this.version.compareTo(Version.PMML_4_2) < 0){
miningField.setUsageType(MiningField.UsageType.PREDICTED);
}
break;
default:
break;
}

return super.visit(miningField);
}

@Override
public VisitorAction visit(PMML pmml){
pmml.setVersion(this.version.getVersion());

return super.visit(pmml);
}

@Override
public VisitorAction visit(TargetValue targetValue){
String displayValue = targetValue.getDisplayValue();

if(displayValue != null){

if(this.version.compareTo(Version.PMML_3_2) <= 0){
throw new UnsupportedAttributeException(targetValue, PMMLAttributes.TARGETVALUE_DISPLAYVALUE, displayValue);
}
}

return super.visit(targetValue);
}

@Override
public VisitorAction visit(TrendExpoSmooth trendExpoSmooth){

if(this.version.compareTo(Version.PMML_4_0) == 0){
throw new UnsupportedElementException(trendExpoSmooth);
}

return super.visit(trendExpoSmooth);
}
}
Loading

0 comments on commit b38c044

Please sign in to comment.