Skip to content

Commit

Permalink
Add some rules and conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
yazan committed Nov 13, 2017
1 parent 19e9188 commit c300b7b
Show file tree
Hide file tree
Showing 33 changed files with 426 additions and 239 deletions.
41 changes: 36 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<modelVersion>4.0.0</modelVersion>

<groupId>edu.iris.dmc</groupId>
<artifactId>stationXml-Validator</artifactId>
<artifactId>station-xml-validator</artifactId>
<packaging>jar</packaging>
<version>1.5.8</version>
<version>1.5.8.3</version>
<name>FDSN StationXML Validator</name>


Expand All @@ -26,15 +26,42 @@
</configuration>

</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>

<configuration>
<archive>
<manifest>
<mainClass>edu.iris.dmc.Application</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>

<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>package-src</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>
<dependencies>

<dependency>
<groupId>edu.iris.fdsn.station</groupId>
<groupId>edu.iris.dmc</groupId>
<artifactId>fdsn-stationxml-model</artifactId>
<version>1.4.2</version>
<version>1.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-digester3 -->
<dependency>
Expand All @@ -47,7 +74,11 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.3</version>
</dependency>

<!-- http://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
Expand Down
54 changes: 44 additions & 10 deletions src/main/java/edu/iris/dmc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

import com.beust.jcommander.JCommander;

import edu.iris.dmc.fdsn.station.model.BaseNodeType.LEVEL;
import edu.iris.dmc.fdsn.station.model.FDSNStationXML;
import edu.iris.dmc.station.RuleEngineService;
import edu.iris.dmc.station.actions.Action;
Expand All @@ -44,7 +49,7 @@ public class Application {
* @throws Exception
*/
public static void main(String[] argv) throws Exception {

//argv = new String[] { "/Users/Suleiman/test.xml", "--format", "csv" };
JCommander.newBuilder().addObject(args).build().parse(argv);
if (args.version) {
System.out.println(Application.getVersion());
Expand Down Expand Up @@ -112,6 +117,8 @@ public void run() throws Exception {
if (out != null) {
out.close();
}


System.exit(EXIT);
}

Expand All @@ -124,32 +131,50 @@ private void run(RuleContext context, List<String> input, String format, OutputS
InputStream is = null;
Bool bool = new Bool();
for (String uri : input) {
ps.printRow(uri);
if (uri.startsWith("http://")) {
is = new URL(uri).openStream();
} else {
if (!uri.endsWith(".xml")) {
continue;
}
File file = new File(uri);
if (!file.exists()) {
System.out.println("File does not exist. File is required!");
System.err.println("File does not exist. File is required!");
help();
System.exit(1);
}
is = new FileInputStream(new File(uri));
}

FDSNStationXML document = (FDSNStationXML) theMarshaller().unmarshal(new StreamSource(is));

ruleEngineService.executeAllRules(document, context, new Action() {
@Override
public void update(RuleContext context, Result result) {
ps.print(result);
ps.flush();
try {
bool.value = false;
ps.print(uri, result);
ps.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
if (bool.value) {
ps.printRow("PASSED");
ps.printMessage("PASSED");
}
ps.printFooter();
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand All @@ -169,10 +194,18 @@ private RuleResultPrintStream getOutputStream(String format, OutputStream output
}
}

private Unmarshaller theMarshaller() throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return jaxbUnmarshaller;
private Unmarshaller theMarshaller() throws IOException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class);
Unmarshaller u = jaxbContext.createUnmarshaller();
StreamSource stream = new StreamSource(Application.class.getResourceAsStream("fdsn-station-1.0.xsd"));
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(stream);
u.setSchema(schema);
return u;
} catch (JAXBException | SAXException e) {
throw new IOException(e);
}

}

Expand Down Expand Up @@ -232,6 +265,7 @@ private static void help() throws IOException {
System.out.println(" --ignore-rules : comma seperated numbers of validation rules");
System.out.println(" --print-rules : print a list of validation rules");
System.out.println(" --print-units : print a list of units used to validate");
System.out.println(" --format : csv|html|xml");
System.out.println(" --summary : print summary only report for errors if any");
System.out.println(" --debug :");
System.out.println(" --help : print this message");
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/edu/iris/dmc/DocumentMarshaller.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package edu.iris.dmc;

import java.io.InputStream;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import edu.iris.dmc.fdsn.station.model.FDSNStationXML;

public class DocumentMarshaller {

public static FDSNStationXML unmarshal(InputStream is) throws JAXBException {
public static FDSNStationXML unmarshal(InputStream inputStream) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (FDSNStationXML) jaxbUnmarshaller.unmarshal(new StreamSource(is));
return (FDSNStationXML) jaxbUnmarshaller.unmarshal(inputStream);
}

public static FDSNStationXML unmarshalString(String inputStream) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (FDSNStationXML) jaxbUnmarshaller.unmarshal(new StringReader(inputStream));
}
}
69 changes: 21 additions & 48 deletions src/main/java/edu/iris/dmc/station/RuleEngineRegistry.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package edu.iris.dmc.station;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.iris.dmc.LEVEL;
import edu.iris.dmc.fdsn.station.model.Channel;
import edu.iris.dmc.fdsn.station.model.Network;
import edu.iris.dmc.fdsn.station.model.Response;
import edu.iris.dmc.fdsn.station.model.Station;
import edu.iris.dmc.station.actions.Action;
import edu.iris.dmc.station.conditions.AzimuthCondition;
import edu.iris.dmc.station.conditions.CalibrationUnitCondition;
import edu.iris.dmc.station.conditions.CodeCondition;
Expand All @@ -23,20 +22,19 @@
import edu.iris.dmc.station.conditions.DistanceCondition;
import edu.iris.dmc.station.conditions.EpochOverlapCondition;
import edu.iris.dmc.station.conditions.EpochRangeCondition;
import edu.iris.dmc.station.conditions.GeoLocationCondition;
import edu.iris.dmc.station.conditions.LatitudeCondition;
import edu.iris.dmc.station.conditions.LocationCodeCondition;
import edu.iris.dmc.station.conditions.LongitudeCondition;
import edu.iris.dmc.station.conditions.MissingDecimationCondition;
import edu.iris.dmc.station.conditions.OrientationCondition;
import edu.iris.dmc.station.conditions.SampleRateCondition;
import edu.iris.dmc.station.conditions.SampleRateDecimationCondition;
import edu.iris.dmc.station.conditions.SensorCondition;
import edu.iris.dmc.station.conditions.StageSequenceCondition;
import edu.iris.dmc.station.conditions.StageUnitCondition;
import edu.iris.dmc.station.conditions.StartTimeCondition;
import edu.iris.dmc.station.conditions.StationElevationCondition;
import edu.iris.dmc.station.rules.Rule;
import edu.iris.dmc.station.rules.RuleContext;

public class RuleEngineRegistry {

Expand Down Expand Up @@ -82,8 +80,6 @@ private void defaultStationRules() {
add(210, new StationElevationCondition(true, "Station elevation must be equal to or above Channel elevation"),
Station.class);

add(211, new GeoLocationCondition(true, "longitude,latitude cannot be zero"), Station.class);

add(251, new DistanceCondition(true, "Distance from station to channel must not exceed 1 km", 1),
Station.class);
add(252, new EpochOverlapCondition(true, "Channel epochs cannot overlap in time"), Station.class);
Expand All @@ -94,7 +90,7 @@ private void defaultChannelRules() {
add(301, new CodeCondition(true, codeRegex,
"Channel attribute 'code' cannot be null, must consist of a three-character string"), Channel.class);
add(302, new LocationCodeCondition(true, "([A-Za-z0-9\\*\\?\\-\\ ]{1,2})?",
"Channel attribute 'location' cannot be null, must consist of a three-character string"),
"Channel attribute 'location' cannot be null, must consist of a two-character string"),
Channel.class);
add(303, new StartTimeCondition(true, "'startDate' is required"), Channel.class);
add(305, new EpochRangeCondition(true, "Channel startDate must be before endDate when endDate available"),
Expand All @@ -104,13 +100,13 @@ private void defaultChannelRules() {
add(308, new DepthCondition(true, "depth is required"), Channel.class);

add(309, new AzimuthCondition(true, "Invalid azimuth", 0, 90), Channel.class);

add(310, new SampleRateCondition(false, "If Channel sample rate = 0, no Response should be included."),
Channel.class);

add(311, new SensorCondition(true, "Invalid azimuth"), Channel.class);
add(311, new SensorCondition(true, "Invalid sensor"), Channel.class);
add(312, new CalibrationUnitCondition(true, "Calibration unit is invalid"), Channel.class);

add(313, new GeoLocationCondition(true, "longitude,latitude cannot be zero"), Channel.class);
add(314, new DipCondition(true, "Invalid dip", -90, 90), Channel.class);
add(315, new OrientationCondition(true, "Invalid channel orientation"), Channel.class);
}
Expand All @@ -120,6 +116,10 @@ private void defaultResponseRules() {
add(402, new StageUnitCondition(true,
"The input unit of a stage must match the output unit of the preceding stage, except for stages 0 or 1."),
Response.class);

add(408, new SampleRateDecimationCondition(true,
"The value of Channel::SampleRate must be equal to the value of Decimation::InputSampleRate divided by Decimation::Factor of the final response stage."),
Response.class);
add(409, new MissingDecimationCondition(true,
"Response stages having Coefficient, FIR ResponseList or a PolesZeros with with transfer function type Digital, must include a Decimation element."),
Response.class);
Expand Down Expand Up @@ -194,48 +194,21 @@ public List<Rule> getRules() {
list.addAll(this.responseRules.values());
return list;
}

public void executeAllRules(Network network, RuleContext context, Action action) {
if (network != null) {
for (Rule rule : networkRules.values()) {
rule.execute(network, context, action);
}
}

if (network.getStations() != null) {
for (Station station : network.getStations()) {
executeAllRules(network, station, context, action);
}
}

public Collection<Rule> getNetworkRules(){
return this.networkRules.values();
}

public void executeAllRules(Network network, Station station, RuleContext context, Action action) {
if (station != null) {
for (Rule rule : stationRules.values()) {
rule.execute(network, station, context, action);
}
}

public Collection<Rule> getStationRules(){
return this.stationRules.values();
}

public void executeAllRules(Network network, Station station, Channel channel, RuleContext context, Action action) {
if (channel != null) {
for (Rule rule : channelRules.values()) {
rule.execute(network, station, channel, context, action);

if (context.getLevel().getValue() > LEVEL.CHANNEL.getValue()) {
executeAllRules(network, station, channel, channel.getResponse(), context, action);
}
}
}

public Collection<Rule> getChannelRules(){
return this.channelRules.values();
}

private void executeAllRules(Network network, Station station, Channel channel, Response response,
RuleContext context, Action action) {
if (response != null) {
for (Rule rule : responseRules.values()) {
rule.execute(network, station, channel, response, context, action);
}
}

public Collection<Rule> getResponseRules(){
return this.responseRules.values();
}

}
Loading

0 comments on commit c300b7b

Please sign in to comment.