-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
969 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
pmml-model/src/main/java/org/jpmml/model/MisplacedElementListException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
pmml-model/src/main/java/org/jpmml/model/PMMLOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,6 @@ | |
public @interface Added { | ||
|
||
Version value(); | ||
|
||
boolean removable() default false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
pmml-model/src/main/java/org/jpmml/model/visitors/VersionDowngrader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.