Skip to content

Commit

Permalink
Order validator output. Add condition 320 azimuth dip condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
timronan committed Apr 9, 2020
1 parent 0d1cd23 commit 14d82eb
Show file tree
Hide file tree
Showing 9 changed files with 1,394 additions and 132 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<dependency>
<groupId>edu.iris.dmc</groupId>
<artifactId>stationxml-seed-converter</artifactId>
<version>2.0.11-SNAPSHOT</version>
<version>2.1.0-RC-2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
47 changes: 39 additions & 8 deletions src/main/java/edu/iris/dmc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ private void run(List<Path> input, String format, OutputStream outputStream, int
if (document == null) {
continue;
}

// This is where the output is printed.
print(ps, ruleEngineService.executeAllRules(document));
}
Expand All @@ -193,11 +192,11 @@ private FDSNStationXML read(Path path) throws Exception {
throw new IOException(String.format("File %s does not exist. File is required!", file.getAbsoluteFile()));
}
InputStream is = new FileInputStream(file);
LOGGER.info("Input file: " + path.toString());

// This is where stationxml vs seed is decided.
// This is where stationxml vs seed is decided.
if(isStationXml(file)) {
try {
LOGGER.info("Input file: " + path.toString());
return DocumentMarshaller.unmarshal(is);
} catch (StationxmlException | IOException | RuntimeException e){
exitWithError(e);
Expand All @@ -207,7 +206,8 @@ private FDSNStationXML read(Path path) throws Exception {
}
} else {
try {
LOGGER.info("Input file: " + path.toString());
//LOGGER.setLevel(Level.WARNING);
//LOGGER.info("Input file: " + path.toString());
Volume volume = IrisUtil.readSeed(file);
return SeedToXmlDocumentConverter.getInstance().convert(volume);
}catch(RuntimeException e){
Expand All @@ -220,17 +220,23 @@ private FDSNStationXML read(Path path) throws Exception {
}

private void print(RuleResultPrintStream ps, Map<Integer, Set<Message>> map) throws IOException {

List<Message> lsorter = new ArrayList<>();
if (map != null && !map.isEmpty()) {
SortedSet<Integer> keys = new TreeSet<>(map.keySet());
for (Integer key : keys) {
Set<Message> l = map.get(key);
for (Message m : l) {
ps.print(m);
ps.flush();
lsorter.add(m);
//ps.print(m);
//ps.flush();
}
ps.printMessage("\n");
}
lsorter.sort(comparatorMessage);
for (Message m : lsorter) {
ps.print(m);
ps.flush();
}
ps.printMessage("\n");
} else {
ps.printMessage("PASSED");
ps.printMessage("\n");
Expand Down Expand Up @@ -344,6 +350,31 @@ public int compare(Rule c1, Rule c2) {
}
};

private static Comparator<Message> comparatorMessage = new Comparator<Message>() {
private Comparator<Rule> comparatorOne;
private Comparator<String> comparatorTwo;

public void ComplexComparator(Comparator<Rule> c1,
Comparator<String> c2) {
this.comparatorOne = c1;
this.comparatorTwo = c2;
}

@Override
public int compare(Message c1, Message c2) {
int r = Integer.compare(c1.getRule().getId(), c2.getRule().getId());
// check if it was 0 (items equal in that attribute)
if (r == 0) {
// if yes, return the result of the next comparison
return c1.getDescription().compareTo(c2.getDescription());
} else {
// otherwise return the result of the first comparison
return r;
}
}
};


public static void printUnits() {
System.out.println("===============================================================");
System.out.println("|" + center("Table of Acceptable Units", 62, " ") + "|");
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/edu/iris/dmc/station/RuleEngineRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.conditions.AzimuthDipCondition;
import edu.iris.dmc.station.conditions.CalibrationUnitCondition;
import edu.iris.dmc.station.conditions.CodeCondition;
import edu.iris.dmc.station.conditions.Condition;
Expand Down Expand Up @@ -159,7 +160,11 @@ private void defaultChannelRules(Set<Integer> set) {
"Channel:startDate must be included and must occur before Channel:endDate if included."),
Channel.class);
}

if (!set.contains(320)) {
add(320, new AzimuthDipCondition(true,
"IF Channel:Code[2]==(H | L | M | N) THEN Channel:Azimuth and Channel:Dip must be included."),
Channel.class);
}
if (!set.contains(332)) {
add(332, new OrientationCondition(true,
"If Channel:Code[LAST]==N then Channel:Azimuth must be assigned (>=355.0 or <=5.0) or (>=175.0 and <=185.0) and Channel:Dip must be assigned (>=-5 AND <=5.0).",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import edu.iris.dmc.fdsn.station.model.Azimuth;
import edu.iris.dmc.fdsn.station.model.Channel;
import edu.iris.dmc.fdsn.station.model.Dip;
import edu.iris.dmc.fdsn.station.model.Network;
import edu.iris.dmc.fdsn.station.model.Station;
import edu.iris.dmc.station.rules.Message;
import edu.iris.dmc.station.rules.Result;

public class AzimuthCondition extends AbstractCondition {
public class AzimuthDipCondition extends AbstractCondition {

private double min;
private double max;

public AzimuthCondition(boolean required, String description, double min, double max) {
public AzimuthDipCondition(boolean required, String description) {
super(required, description);
this.min = min;
this.max = max;

}

@Override
Expand All @@ -31,12 +28,24 @@ public Message evaluate(Station station) {
@Override
public Message evaluate(Channel channel) {
Azimuth azimuth = channel.getAzimuth();
if (azimuth == null) {
if (required) {
return Result.error("Expected an Azimuth value between " + min + " and " + max + " but received null.");
Dip dip = channel.getDip();
String code = channel.getCode();
try {
if("HLMN".indexOf(code.charAt(1)) >=0 | "hlmn".indexOf(code.charAt(1)) >=0) {
if (azimuth == null) {
return Result.error("Azimuth must be included for channels with " + code.charAt(1) + " instrument values");

}
return Result.success();
if (dip == null) {
return Result.error("Dip must be included for channels with " + code.charAt(1) + " instrument values");

}

}
return Result.success();
}
}catch(Exception e) {

}
return Result.success();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package edu.iris.dmc.station.conditions;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.InputStream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import edu.iris.dmc.DocumentMarshaller;
import edu.iris.dmc.fdsn.station.model.Channel;
import edu.iris.dmc.fdsn.station.model.FDSNStationXML;
import edu.iris.dmc.fdsn.station.model.Network;
import edu.iris.dmc.fdsn.station.model.Station;
import edu.iris.dmc.station.RuleEngineServiceTest;
import edu.iris.dmc.station.conditions.EpochRangeCondition;
import edu.iris.dmc.station.conditions.StartTimeCondition;
import edu.iris.dmc.station.rules.Message;

public class Condition320Test {

private FDSNStationXML theDocument;

@BeforeEach
public void init() throws Exception {

}

@Test
public void azimuthfail() throws Exception {
try (InputStream is = RuleEngineServiceTest.class.getClassLoader().getResourceAsStream("F1_320.xml")) {
theDocument = DocumentMarshaller.unmarshal(is);

Network n = theDocument.getNetwork().get(0);
Station s = n.getStations().get(0);
Channel c = s.getChannels().get(0);

AzimuthDipCondition condition = new AzimuthDipCondition(true, "");
Message result = condition.evaluate(c);
assertTrue(result instanceof edu.iris.dmc.station.rules.Error);
}

}

@Test
public void dipfail() throws Exception {
try (InputStream is = RuleEngineServiceTest.class.getClassLoader().getResourceAsStream("F2_320.xml")) {
theDocument = DocumentMarshaller.unmarshal(is);

Network n = theDocument.getNetwork().get(0);
Station s = n.getStations().get(0);
Channel c = s.getChannels().get(0);

AzimuthDipCondition condition = new AzimuthDipCondition(true, "");
Message result = condition.evaluate(c);
assertTrue(result instanceof edu.iris.dmc.station.rules.Error);
}

}

@Test
public void pass() throws Exception {
try (InputStream is = RuleEngineServiceTest.class.getClassLoader().getResourceAsStream("pass.xml")) {
theDocument = DocumentMarshaller.unmarshal(is);

Network n = theDocument.getNetwork().get(0);
Station s = n.getStations().get(0);

EpochRangeCondition condition = new EpochRangeCondition(true, "");

Message result = condition.evaluate(s);
assertTrue(result instanceof edu.iris.dmc.station.rules.Success);
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class StageGainNonZeroCondition413Test {

private FDSNStationXML theDocument;

//@Test
@Test
public void fail() throws Exception {
try (InputStream is = RuleEngineServiceTest.class.getClassLoader().getResourceAsStream("F1_413.xml")) {
theDocument = DocumentMarshaller.unmarshal(is);
Expand All @@ -44,27 +44,10 @@ public void fail() throws Exception {

}

@Test
public void pass2() throws Exception {
try (InputStream is = RuleEngineServiceTest.class.getClassLoader().getResourceAsStream("P1_413.xml")) {
theDocument = DocumentMarshaller.unmarshal(is);

Network iu = theDocument.getNetwork().get(0);
Channel bhz00 = iu.getStations().get(0).getChannels().get(0);

Restriction[] restrictions = new Restriction[] { new ChannelCodeRestriction(),
new ChannelTypeRestriction() ,new ResponsePolynomialRestriction()};

StageGainNonZeroCondition condition = new StageGainNonZeroCondition(true, "", restrictions);

Response response = bhz00.getResponse();
Message result = condition.evaluate(bhz00, response);
assertTrue(result instanceof edu.iris.dmc.station.rules.Success);
}

}


//@Test
@Test
public void pass1() throws Exception {
try (InputStream is = RuleEngineServiceTest.class.getClassLoader().getResourceAsStream("pass.xml")) {
theDocument = DocumentMarshaller.unmarshal(is);
Expand Down
Loading

0 comments on commit 14d82eb

Please sign in to comment.