aboutsummaryrefslogtreecommitdiff
path: root/src/batchtools/rsTester/src/main/java/com/galois/fiveui/RuleSet.java
blob: ed9bc992e365aefc8df2e7538dd5b697330e8c08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * Module : RuleSet.java Copyright : (c) 2011-2012, Galois, Inc.
 * 
 * Maintainer : Stability : Provisional Portability: Portable
 * 
 * Licensed 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 com.galois.fiveui;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class RuleSet {

	public static RuleSet parseFile(String rsFileName) throws JsonSyntaxException, IOException {
		String descDir = new File(rsFileName).getParent();
		
        Gson gson = new Gson();
        RuleSet rs = gson.fromJson(Utils.readFile(rsFileName), RuleSet.class);
        
        rs.setDirectory(descDir);
        return rs;
	}

    public void setDirectory(String descDir) {
    	this.descDir = descDir;
	}

	private final String name;
    private final String description;
    private final List<String> rules;
    
    private transient ImmutableMap<String, Rule> _evaledRules = null;

	private transient String descDir = ".";
	
    public RuleSet(String name, String description, List<String> ruleFiles) {
        this.name = name;
        this.description = description;
        this.rules = ruleFiles;
    }
    
    private void parseRules() {
        ImmutableMap.Builder<String, Rule> builder = ImmutableMap.builder();
        
        // Parse all the rules from files:
        for (String r : rules) {
        	String adjustedPath = descDir + File.separator + r;
        	try {
				Rule evRule = Rule.parse(
						Utils.readFile(adjustedPath));
			    
				builder.put(evRule.getName(), evRule);
			} catch (IOException e) {
				System.err.println("Could not load rule from file: "+adjustedPath);
				System.err.println("  error: "+e);
			}
        }
        
        _evaledRules = builder.build();
    }
	
    /**
     * Construct a new empty rule set.
     * 
     * @return a new empty RuleSet object
     */
    public static RuleSet empty() {
    	List<String> rules = Lists.newArrayList();
    	return new RuleSet("", "", ImmutableList.copyOf(rules));
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public ImmutableCollection<Rule> getRules() {
    	if (null == _evaledRules) {
    		parseRules();
    	}
    	
        return _evaledRules.values();
    }

	public Rule getRule(String ruleName) {
		return _evaledRules.get(ruleName);
	}
    
    @Override
    public String toString() {
    	Gson gson = new Gson();
    	return gson.toJson(this);
    }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((rules == null) ? 0 : rules.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RuleSet other = (RuleSet) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (rules == null) {
			if (other.rules != null)
				return false;
		} else if (!rules.equals(other.rules))
			return false;
		return true;
	}
}